mirror of
https://github.com/actix/actix-web.git
synced 2024-12-23 00:26:34 +00:00
add status code helper method for http response
This commit is contained in:
parent
706e2a07de
commit
932e751240
4 changed files with 54 additions and 5 deletions
|
@ -28,7 +28,7 @@ fn index(mut req: HttpRequest) -> Result<HttpResponse> {
|
|||
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<actix_web::Frame, Error>
|
|||
{
|
||||
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<HttpResponse>
|
|||
{
|
||||
println!("{:?}", req);
|
||||
|
||||
Ok(HttpResponse::build(StatusCode::OK)
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("test/plain")
|
||||
.body(format!("Hello {}!", req.match_info().get("name").unwrap()))?)
|
||||
}
|
||||
|
|
|
@ -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<HttpResponse> {
|
||||
Ok(httpcodes::HTTPOk.with_body("Hello world!"))
|
||||
fn index(req: HttpRequest) -> &'static str {
|
||||
"Hello world!"
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
@ -80,6 +80,49 @@ impl From<StaticResponse> 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 {
|
||||
|
|
|
@ -441,6 +441,12 @@ impl HttpResponseBuilder {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<HttpResponseBuilder> for HttpResponse {
|
||||
fn from(mut builder: HttpResponseBuilder) -> Self {
|
||||
builder.finish().into()
|
||||
}
|
||||
}
|
||||
|
||||
fn parts<'a>(parts: &'a mut Option<Parts>, err: &Option<HttpError>) -> Option<&'a mut Parts>
|
||||
{
|
||||
if err.is_some() {
|
||||
|
|
Loading…
Reference in a new issue