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

48 lines
1.5 KiB
Rust
Raw Normal View History

2017-10-07 04:48:14 +00:00
//! Basic http responses
#![allow(non_upper_case_globals)]
use std::rc::Rc;
use http::StatusCode;
use task::Task;
2017-10-09 03:16:48 +00:00
use route::RouteHandler;
use payload::Payload;
2017-10-10 23:03:32 +00:00
use httpmessage::{Body, Builder, HttpRequest, HttpResponse};
2017-10-07 04:48:14 +00:00
pub const HTTPOk: StaticResponse = StaticResponse(StatusCode::OK);
pub const HTTPCreated: StaticResponse = StaticResponse(StatusCode::CREATED);
pub const HTTPNoContent: StaticResponse = StaticResponse(StatusCode::NO_CONTENT);
pub const HTTPBadRequest: StaticResponse = StaticResponse(StatusCode::BAD_REQUEST);
pub const HTTPNotFound: StaticResponse = StaticResponse(StatusCode::NOT_FOUND);
pub const HTTPMethodNotAllowed: StaticResponse = StaticResponse(StatusCode::METHOD_NOT_ALLOWED);
2017-10-10 23:03:32 +00:00
pub const HTTPInternalServerError: StaticResponse =
StaticResponse(StatusCode::INTERNAL_SERVER_ERROR);
2017-10-07 04:48:14 +00:00
2017-10-08 04:48:00 +00:00
pub struct StaticResponse(StatusCode);
impl StaticResponse {
2017-10-10 23:03:32 +00:00
pub fn builder(&self) -> Builder {
HttpResponse::builder(self.0)
}
pub fn response(&self) -> HttpResponse {
HttpResponse::new(self.0, Body::Empty)
}
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-07 04:48:14 +00:00
impl<S> RouteHandler<S> for StaticResponse {
fn handle(&self, _: HttpRequest, _: Payload, _: Rc<S>) -> Task {
Task::reply(HttpResponse::new(self.0, Body::Empty))
2017-10-07 04:48:14 +00:00
}
}
2017-10-10 23:03:32 +00:00
impl From<StaticResponse> for HttpResponse {
fn from(st: StaticResponse) -> Self {
st.response()
2017-10-07 04:48:14 +00:00
}
}