1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-18 15:41:17 +00:00
actix-web/src/httpcodes.rs

311 lines
13 KiB
Rust
Raw Normal View History

2017-10-07 04:48:14 +00:00
//! Basic http responses
#![allow(non_upper_case_globals)]
use http::StatusCode;
2017-10-07 04:48:14 +00:00
2017-10-24 06:25:32 +00:00
use body::Body;
use error::Error;
2017-12-14 17:43:42 +00:00
use handler::{Reply, Handler, RouteHandler, Responder};
use httprequest::HttpRequest;
2017-10-24 06:25:32 +00:00
use httpresponse::{HttpResponse, HttpResponseBuilder};
2017-10-07 04:48:14 +00:00
2018-03-02 03:12:59 +00:00
pub const HttpOk: StaticResponse = StaticResponse(StatusCode::OK);
pub const HttpCreated: StaticResponse = StaticResponse(StatusCode::CREATED);
pub const HttpAccepted: StaticResponse = StaticResponse(StatusCode::ACCEPTED);
pub const HttpNonAuthoritativeInformation: StaticResponse =
StaticResponse(StatusCode::NON_AUTHORITATIVE_INFORMATION);
pub const HttpNoContent: StaticResponse = StaticResponse(StatusCode::NO_CONTENT);
pub const HttpResetContent: StaticResponse = StaticResponse(StatusCode::RESET_CONTENT);
pub const HttpPartialContent: StaticResponse = StaticResponse(StatusCode::PARTIAL_CONTENT);
pub const HttpMultiStatus: StaticResponse = StaticResponse(StatusCode::MULTI_STATUS);
pub const HttpAlreadyReported: StaticResponse = StaticResponse(StatusCode::ALREADY_REPORTED);
pub const HttpMultipleChoices: StaticResponse = StaticResponse(StatusCode::MULTIPLE_CHOICES);
2018-03-07 20:54:25 +00:00
pub const HttpMovedPermanently: StaticResponse = StaticResponse(StatusCode::MOVED_PERMANENTLY);
2018-03-02 03:12:59 +00:00
pub const HttpFound: StaticResponse = StaticResponse(StatusCode::FOUND);
pub const HttpSeeOther: StaticResponse = StaticResponse(StatusCode::SEE_OTHER);
pub const HttpNotModified: StaticResponse = StaticResponse(StatusCode::NOT_MODIFIED);
pub const HttpUseProxy: StaticResponse = StaticResponse(StatusCode::USE_PROXY);
pub const HttpTemporaryRedirect: StaticResponse =
StaticResponse(StatusCode::TEMPORARY_REDIRECT);
pub const HttpPermanentRedirect: StaticResponse =
StaticResponse(StatusCode::PERMANENT_REDIRECT);
pub const HttpBadRequest: StaticResponse = StaticResponse(StatusCode::BAD_REQUEST);
pub const HttpUnauthorized: StaticResponse = StaticResponse(StatusCode::UNAUTHORIZED);
pub const HttpPaymentRequired: StaticResponse = StaticResponse(StatusCode::PAYMENT_REQUIRED);
pub const HttpForbidden: StaticResponse = StaticResponse(StatusCode::FORBIDDEN);
pub const HttpNotFound: StaticResponse = StaticResponse(StatusCode::NOT_FOUND);
pub const HttpMethodNotAllowed: StaticResponse =
StaticResponse(StatusCode::METHOD_NOT_ALLOWED);
pub const HttpNotAcceptable: StaticResponse = StaticResponse(StatusCode::NOT_ACCEPTABLE);
pub const HttpProxyAuthenticationRequired: StaticResponse =
StaticResponse(StatusCode::PROXY_AUTHENTICATION_REQUIRED);
pub const HttpRequestTimeout: StaticResponse = StaticResponse(StatusCode::REQUEST_TIMEOUT);
pub const HttpConflict: StaticResponse = StaticResponse(StatusCode::CONFLICT);
pub const HttpGone: StaticResponse = StaticResponse(StatusCode::GONE);
pub const HttpLengthRequired: StaticResponse = StaticResponse(StatusCode::LENGTH_REQUIRED);
pub const HttpPreconditionFailed: StaticResponse =
StaticResponse(StatusCode::PRECONDITION_FAILED);
pub const HttpPayloadTooLarge: StaticResponse = StaticResponse(StatusCode::PAYLOAD_TOO_LARGE);
pub const HttpUriTooLong: StaticResponse = StaticResponse(StatusCode::URI_TOO_LONG);
pub const HttpUnsupportedMediaType: StaticResponse =
StaticResponse(StatusCode::UNSUPPORTED_MEDIA_TYPE);
pub const HttpRangeNotSatisfiable: StaticResponse =
StaticResponse(StatusCode::RANGE_NOT_SATISFIABLE);
pub const HttpExpectationFailed: StaticResponse =
StaticResponse(StatusCode::EXPECTATION_FAILED);
pub const HttpInternalServerError: StaticResponse =
StaticResponse(StatusCode::INTERNAL_SERVER_ERROR);
pub const HttpNotImplemented: StaticResponse = StaticResponse(StatusCode::NOT_IMPLEMENTED);
pub const HttpBadGateway: StaticResponse = StaticResponse(StatusCode::BAD_GATEWAY);
pub const HttpServiceUnavailable: StaticResponse =
StaticResponse(StatusCode::SERVICE_UNAVAILABLE);
pub const HttpGatewayTimeout: StaticResponse =
StaticResponse(StatusCode::GATEWAY_TIMEOUT);
pub const HttpVersionNotSupported: StaticResponse =
StaticResponse(StatusCode::HTTP_VERSION_NOT_SUPPORTED);
pub const HttpVariantAlsoNegotiates: StaticResponse =
StaticResponse(StatusCode::VARIANT_ALSO_NEGOTIATES);
pub const HttpInsufficientStorage: StaticResponse =
StaticResponse(StatusCode::INSUFFICIENT_STORAGE);
pub const HttpLoopDetected: StaticResponse = StaticResponse(StatusCode::LOOP_DETECTED);
#[doc(hidden)]
2017-10-07 04:48:14 +00:00
pub const HTTPOk: StaticResponse = StaticResponse(StatusCode::OK);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2017-10-07 04:48:14 +00:00
pub const HTTPCreated: StaticResponse = StaticResponse(StatusCode::CREATED);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2018-01-31 20:34:58 +00:00
pub const HTTPAccepted: StaticResponse = StaticResponse(StatusCode::ACCEPTED);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2018-01-31 20:34:58 +00:00
pub const HTTPNonAuthoritativeInformation: StaticResponse =
StaticResponse(StatusCode::NON_AUTHORITATIVE_INFORMATION);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2017-10-07 04:48:14 +00:00
pub const HTTPNoContent: StaticResponse = StaticResponse(StatusCode::NO_CONTENT);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2018-01-31 20:34:58 +00:00
pub const HTTPResetContent: StaticResponse = StaticResponse(StatusCode::RESET_CONTENT);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2018-01-31 20:34:58 +00:00
pub const HTTPPartialContent: StaticResponse = StaticResponse(StatusCode::PARTIAL_CONTENT);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2018-01-31 20:34:58 +00:00
pub const HTTPMultiStatus: StaticResponse = StaticResponse(StatusCode::MULTI_STATUS);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2018-01-31 20:34:58 +00:00
pub const HTTPAlreadyReported: StaticResponse = StaticResponse(StatusCode::ALREADY_REPORTED);
2017-10-15 22:52:52 +00:00
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
pub const HTTPMultipleChoices: StaticResponse = StaticResponse(StatusCode::MULTIPLE_CHOICES);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
pub const HTTPMovedPermanenty: StaticResponse = StaticResponse(StatusCode::MOVED_PERMANENTLY);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2017-10-21 06:12:36 +00:00
pub const HTTPFound: StaticResponse = StaticResponse(StatusCode::FOUND);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
pub const HTTPSeeOther: StaticResponse = StaticResponse(StatusCode::SEE_OTHER);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
pub const HTTPNotModified: StaticResponse = StaticResponse(StatusCode::NOT_MODIFIED);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
pub const HTTPUseProxy: StaticResponse = StaticResponse(StatusCode::USE_PROXY);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
pub const HTTPTemporaryRedirect: StaticResponse =
StaticResponse(StatusCode::TEMPORARY_REDIRECT);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
pub const HTTPPermanentRedirect: StaticResponse =
StaticResponse(StatusCode::PERMANENT_REDIRECT);
2017-10-21 06:12:36 +00:00
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2017-10-07 04:48:14 +00:00
pub const HTTPBadRequest: StaticResponse = StaticResponse(StatusCode::BAD_REQUEST);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2017-10-15 22:52:52 +00:00
pub const HTTPUnauthorized: StaticResponse = StaticResponse(StatusCode::UNAUTHORIZED);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2017-10-15 22:52:52 +00:00
pub const HTTPPaymentRequired: StaticResponse = StaticResponse(StatusCode::PAYMENT_REQUIRED);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
pub const HTTPForbidden: StaticResponse = StaticResponse(StatusCode::FORBIDDEN);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2018-01-31 20:34:58 +00:00
pub const HTTPNotFound: StaticResponse = StaticResponse(StatusCode::NOT_FOUND);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2017-10-15 22:52:52 +00:00
pub const HTTPMethodNotAllowed: StaticResponse =
StaticResponse(StatusCode::METHOD_NOT_ALLOWED);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2017-10-15 22:52:52 +00:00
pub const HTTPNotAcceptable: StaticResponse = StaticResponse(StatusCode::NOT_ACCEPTABLE);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2017-10-15 22:52:52 +00:00
pub const HTTPProxyAuthenticationRequired: StaticResponse =
StaticResponse(StatusCode::PROXY_AUTHENTICATION_REQUIRED);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2017-10-15 22:52:52 +00:00
pub const HTTPRequestTimeout: StaticResponse = StaticResponse(StatusCode::REQUEST_TIMEOUT);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2017-10-15 22:52:52 +00:00
pub const HTTPConflict: StaticResponse = StaticResponse(StatusCode::CONFLICT);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2017-10-15 22:52:52 +00:00
pub const HTTPGone: StaticResponse = StaticResponse(StatusCode::GONE);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2017-10-15 22:52:52 +00:00
pub const HTTPLengthRequired: StaticResponse = StaticResponse(StatusCode::LENGTH_REQUIRED);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2017-10-15 22:52:52 +00:00
pub const HTTPPreconditionFailed: StaticResponse =
StaticResponse(StatusCode::PRECONDITION_FAILED);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2017-10-15 22:52:52 +00:00
pub const HTTPPayloadTooLarge: StaticResponse = StaticResponse(StatusCode::PAYLOAD_TOO_LARGE);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2017-10-15 22:52:52 +00:00
pub const HTTPUriTooLong: StaticResponse = StaticResponse(StatusCode::URI_TOO_LONG);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2018-01-31 20:34:58 +00:00
pub const HTTPUnsupportedMediaType: StaticResponse =
StaticResponse(StatusCode::UNSUPPORTED_MEDIA_TYPE);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2018-01-31 20:34:58 +00:00
pub const HTTPRangeNotSatisfiable: StaticResponse =
StaticResponse(StatusCode::RANGE_NOT_SATISFIABLE);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2017-10-15 22:52:52 +00:00
pub const HTTPExpectationFailed: StaticResponse =
StaticResponse(StatusCode::EXPECTATION_FAILED);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2017-10-10 23:03:32 +00:00
pub const HTTPInternalServerError: StaticResponse =
StaticResponse(StatusCode::INTERNAL_SERVER_ERROR);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2018-01-31 20:34:58 +00:00
pub const HTTPNotImplemented: StaticResponse = StaticResponse(StatusCode::NOT_IMPLEMENTED);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2018-01-31 20:34:58 +00:00
pub const HTTPBadGateway: StaticResponse = StaticResponse(StatusCode::BAD_GATEWAY);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2018-01-31 20:34:58 +00:00
pub const HTTPServiceUnavailable: StaticResponse =
StaticResponse(StatusCode::SERVICE_UNAVAILABLE);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2018-01-31 20:34:58 +00:00
pub const HTTPGatewayTimeout: StaticResponse =
StaticResponse(StatusCode::GATEWAY_TIMEOUT);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2018-01-31 20:34:58 +00:00
pub const HTTPVersionNotSupported: StaticResponse =
StaticResponse(StatusCode::HTTP_VERSION_NOT_SUPPORTED);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2018-01-31 20:34:58 +00:00
pub const HTTPVariantAlsoNegotiates: StaticResponse =
StaticResponse(StatusCode::VARIANT_ALSO_NEGOTIATES);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2018-01-31 20:34:58 +00:00
pub const HTTPInsufficientStorage: StaticResponse =
StaticResponse(StatusCode::INSUFFICIENT_STORAGE);
2018-03-02 03:12:59 +00:00
#[doc(hidden)]
2018-01-31 20:34:58 +00:00
pub const HTTPLoopDetected: StaticResponse = StaticResponse(StatusCode::LOOP_DETECTED);
2017-10-07 04:48:14 +00:00
2018-01-11 19:14:18 +00:00
#[derive(Copy, Clone, Debug)]
2017-10-08 04:48:00 +00:00
pub struct StaticResponse(StatusCode);
impl StaticResponse {
2017-11-27 06:31:29 +00:00
pub fn build(&self) -> HttpResponseBuilder {
HttpResponse::build(self.0)
2017-10-10 23:03:32 +00:00
}
pub fn with_reason(self, reason: &'static str) -> HttpResponse {
let mut resp = HttpResponse::new(self.0, Body::Empty);
resp.set_reason(reason);
resp
2017-10-08 04:48:00 +00:00
}
2017-10-24 06:25:32 +00:00
pub fn with_body<B: Into<Body>>(self, body: B) -> HttpResponse {
HttpResponse::new(self.0, body.into())
2017-10-15 22:52:52 +00:00
}
2017-10-08 04:48:00 +00:00
}
impl<S> Handler<S> for StaticResponse {
type Result = HttpResponse;
2017-12-26 17:00:45 +00:00
fn handle(&mut self, _: HttpRequest<S>) -> HttpResponse {
HttpResponse::new(self.0, Body::Empty)
}
}
2017-10-07 04:48:14 +00:00
impl<S> RouteHandler<S> for StaticResponse {
2017-12-26 17:00:45 +00:00
fn handle(&mut self, _: HttpRequest<S>) -> Reply {
2017-11-30 23:13:56 +00:00
Reply::response(HttpResponse::new(self.0, Body::Empty))
2017-10-07 04:48:14 +00:00
}
}
2017-12-14 17:43:42 +00:00
impl Responder for StaticResponse {
type Item = HttpResponse;
type Error = Error;
fn respond_to(self, _: HttpRequest) -> Result<HttpResponse, Error> {
self.build().body(Body::Empty)
}
}
2017-10-10 23:03:32 +00:00
impl From<StaticResponse> for HttpResponse {
fn from(st: StaticResponse) -> Self {
2018-01-01 01:26:32 +00:00
HttpResponse::new(st.0, Body::Empty)
2017-10-07 04:48:14 +00:00
}
}
2017-10-14 17:01:53 +00:00
impl From<StaticResponse> for Reply {
fn from(st: StaticResponse) -> Self {
HttpResponse::new(st.0, Body::Empty).into()
}
}
macro_rules! STATIC_RESP {
($name:ident, $status:expr) => {
#[allow(non_snake_case)]
pub fn $name() -> HttpResponseBuilder {
HttpResponse::build($status)
}
}
}
impl HttpResponse {
STATIC_RESP!(Ok, StatusCode::OK);
STATIC_RESP!(Created, StatusCode::CREATED);
STATIC_RESP!(NoContent, StatusCode::NO_CONTENT);
STATIC_RESP!(MultipleChoices, StatusCode::MULTIPLE_CHOICES);
STATIC_RESP!(MovedPermanenty, StatusCode::MOVED_PERMANENTLY);
STATIC_RESP!(Found, StatusCode::FOUND);
STATIC_RESP!(SeeOther, StatusCode::SEE_OTHER);
STATIC_RESP!(NotModified, StatusCode::NOT_MODIFIED);
STATIC_RESP!(UseProxy, StatusCode::USE_PROXY);
STATIC_RESP!(TemporaryRedirect, StatusCode::TEMPORARY_REDIRECT);
STATIC_RESP!(PermanentRedirect, StatusCode::PERMANENT_REDIRECT);
STATIC_RESP!(BadRequest, StatusCode::BAD_REQUEST);
STATIC_RESP!(NotFound, StatusCode::NOT_FOUND);
STATIC_RESP!(Unauthorized, StatusCode::UNAUTHORIZED);
STATIC_RESP!(PaymentRequired, StatusCode::PAYMENT_REQUIRED);
STATIC_RESP!(Forbidden, StatusCode::FORBIDDEN);
STATIC_RESP!(MethodNotAllowed, StatusCode::METHOD_NOT_ALLOWED);
STATIC_RESP!(NotAcceptable, StatusCode::NOT_ACCEPTABLE);
STATIC_RESP!(ProxyAuthenticationRequired, StatusCode::PROXY_AUTHENTICATION_REQUIRED);
STATIC_RESP!(RequestTimeout, StatusCode::REQUEST_TIMEOUT);
STATIC_RESP!(Conflict, StatusCode::CONFLICT);
STATIC_RESP!(Gone, StatusCode::GONE);
STATIC_RESP!(LengthRequired, StatusCode::LENGTH_REQUIRED);
STATIC_RESP!(PreconditionFailed, StatusCode::PRECONDITION_FAILED);
STATIC_RESP!(PayloadTooLarge, StatusCode::PAYLOAD_TOO_LARGE);
STATIC_RESP!(UriTooLong, StatusCode::URI_TOO_LONG);
STATIC_RESP!(ExpectationFailed, StatusCode::EXPECTATION_FAILED);
STATIC_RESP!(InternalServerError, StatusCode::INTERNAL_SERVER_ERROR);
}
2017-10-14 17:01:53 +00:00
#[cfg(test)]
mod tests {
use http::StatusCode;
use super::{HTTPOk, HTTPBadRequest, Body, HttpResponse};
#[test]
2017-11-27 06:31:29 +00:00
fn test_build() {
let resp = HTTPOk.build().body(Body::Empty).unwrap();
2017-10-14 17:01:53 +00:00
assert_eq!(resp.status(), StatusCode::OK);
}
#[test]
fn test_response() {
2018-01-01 01:26:32 +00:00
let resp: HttpResponse = HTTPOk.into();
2017-10-14 17:01:53 +00:00
assert_eq!(resp.status(), StatusCode::OK);
}
#[test]
fn test_from() {
let resp: HttpResponse = HTTPOk.into();
assert_eq!(resp.status(), StatusCode::OK);
}
#[test]
fn test_with_reason() {
2018-01-01 01:26:32 +00:00
let resp: HttpResponse = HTTPOk.into();
2017-12-13 19:10:03 +00:00
assert_eq!(resp.reason(), "OK");
2017-10-14 17:01:53 +00:00
let resp = HTTPBadRequest.with_reason("test");
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
assert_eq!(resp.reason(), "test");
}
}