2017-10-07 06:48:14 +02:00
|
|
|
//! Basic http responses
|
|
|
|
#![allow(non_upper_case_globals)]
|
|
|
|
use std::rc::Rc;
|
|
|
|
use http::StatusCode;
|
|
|
|
|
|
|
|
use task::Task;
|
2017-10-09 05:16:48 +02:00
|
|
|
use route::RouteHandler;
|
|
|
|
use payload::Payload;
|
2017-10-07 08:14:13 +02:00
|
|
|
use httpmessage::{Body, HttpRequest, HttpResponse, IntoHttpResponse};
|
2017-10-07 06:48:14 +02: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-08 06:48:00 +02:00
|
|
|
pub struct StaticResponse(StatusCode);
|
|
|
|
|
|
|
|
impl StaticResponse {
|
|
|
|
pub fn with_reason(self, req: HttpRequest, reason: &'static str) -> HttpResponse {
|
|
|
|
HttpResponse::new(req, self.0, Body::Empty)
|
|
|
|
.set_reason(reason)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 06:48:14 +02:00
|
|
|
impl<S> RouteHandler<S> for StaticResponse {
|
2017-10-09 05:16:48 +02:00
|
|
|
fn handle(&self, req: HttpRequest, _: Payload, _: Rc<S>) -> Task {
|
2017-10-07 08:36:36 +02:00
|
|
|
Task::reply(HttpResponse::new(req, self.0, Body::Empty))
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 08:14:13 +02:00
|
|
|
impl IntoHttpResponse for StaticResponse {
|
2017-10-08 06:48:00 +02:00
|
|
|
fn response(self, req: HttpRequest) -> HttpResponse {
|
2017-10-07 08:14:13 +02:00
|
|
|
HttpResponse::new(req, self.0, Body::Empty)
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|