From 932e7512408e1535b2df41c74b67b388220d7a77 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Tue, 28 Nov 2017 14:23:42 -0800 Subject: [PATCH] add status code helper method for http response --- examples/basic.rs | 6 +++--- guide/src/qs_2.md | 4 ++-- src/httpcodes.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ src/httpresponse.rs | 6 ++++++ 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/examples/basic.rs b/examples/basic.rs index 2b6076fd6..dd33014b6 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -28,7 +28,7 @@ fn index(mut req: HttpRequest) -> Result { req.session().set("counter", 1)?; } - Ok(httpcodes::HTTPOk.into()) + Ok(HttpResponse::Ok().into()) } /// async handler @@ -36,7 +36,7 @@ fn index_async(req: HttpRequest) -> Once { println!("{:?}", req); - once(Ok(HttpResponse::build(StatusCode::OK) + once(Ok(HttpResponse::Ok() .content_type("text/html") .body(format!("Hello {}!", req.match_info().get("name").unwrap())) .unwrap() @@ -48,7 +48,7 @@ fn with_param(req: HttpRequest) -> Result { println!("{:?}", req); - Ok(HttpResponse::build(StatusCode::OK) + Ok(HttpResponse::Ok() .content_type("test/plain") .body(format!("Hello {}!", req.match_info().get("name").unwrap()))?) } diff --git a/guide/src/qs_2.md b/guide/src/qs_2.md index ada042bb2..44bb84e36 100644 --- a/guide/src/qs_2.md +++ b/guide/src/qs_2.md @@ -33,8 +33,8 @@ and returns a `HttpResponse` instance or actor that uses `HttpContext` as an act extern crate actix_web; use actix_web::prelude::*; -fn index(req: HttpRequest) -> Result { - Ok(httpcodes::HTTPOk.with_body("Hello world!")) +fn index(req: HttpRequest) -> &'static str { + "Hello world!" } ``` diff --git a/src/httpcodes.rs b/src/httpcodes.rs index 84ebb7cd3..5ff5cdbe9 100644 --- a/src/httpcodes.rs +++ b/src/httpcodes.rs @@ -80,6 +80,49 @@ impl From for HttpResponse { } } +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); +} #[cfg(test)] mod tests { diff --git a/src/httpresponse.rs b/src/httpresponse.rs index d8d0cf6fe..115776718 100644 --- a/src/httpresponse.rs +++ b/src/httpresponse.rs @@ -441,6 +441,12 @@ impl HttpResponseBuilder { } } +impl From for HttpResponse { + fn from(mut builder: HttpResponseBuilder) -> Self { + builder.finish().into() + } +} + fn parts<'a>(parts: &'a mut Option, err: &Option) -> Option<&'a mut Parts> { if err.is_some() {