2017-10-07 06:48:14 +02:00
|
|
|
//! Basic http responses
|
|
|
|
#![allow(non_upper_case_globals)]
|
|
|
|
use std::rc::Rc;
|
|
|
|
use http::StatusCode;
|
|
|
|
|
2017-10-24 08:25:32 +02:00
|
|
|
use body::Body;
|
2017-10-07 06:48:14 +02:00
|
|
|
use task::Task;
|
2017-10-09 05:16:48 +02:00
|
|
|
use route::RouteHandler;
|
2017-10-15 07:52:38 +02:00
|
|
|
use httprequest::HttpRequest;
|
2017-10-24 08:25:32 +02:00
|
|
|
use httpresponse::{HttpResponse, HttpResponseBuilder};
|
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);
|
2017-10-16 00:52:52 +02:00
|
|
|
|
2017-10-24 07:02:42 +02:00
|
|
|
pub const HTTPMultipleChoices: StaticResponse = StaticResponse(StatusCode::MULTIPLE_CHOICES);
|
|
|
|
pub const HTTPMovedPermanenty: StaticResponse = StaticResponse(StatusCode::MOVED_PERMANENTLY);
|
2017-10-21 08:12:36 +02:00
|
|
|
pub const HTTPFound: StaticResponse = StaticResponse(StatusCode::FOUND);
|
2017-10-24 07:02:42 +02:00
|
|
|
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);
|
2017-10-21 08:12:36 +02:00
|
|
|
|
2017-10-07 06:48:14 +02:00
|
|
|
pub const HTTPBadRequest: StaticResponse = StaticResponse(StatusCode::BAD_REQUEST);
|
|
|
|
pub const HTTPNotFound: StaticResponse = StaticResponse(StatusCode::NOT_FOUND);
|
2017-10-16 00:52:52 +02:00
|
|
|
pub const HTTPUnauthorized: StaticResponse = StaticResponse(StatusCode::UNAUTHORIZED);
|
|
|
|
pub const HTTPPaymentRequired: StaticResponse = StaticResponse(StatusCode::PAYMENT_REQUIRED);
|
2017-10-15 07:52:38 +02:00
|
|
|
pub const HTTPForbidden: StaticResponse = StaticResponse(StatusCode::FORBIDDEN);
|
2017-10-16 00:52:52 +02:00
|
|
|
|
|
|
|
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 HTTPExpectationFailed: StaticResponse =
|
|
|
|
StaticResponse(StatusCode::EXPECTATION_FAILED);
|
|
|
|
|
2017-10-11 01:03:32 +02:00
|
|
|
pub const HTTPInternalServerError: StaticResponse =
|
|
|
|
StaticResponse(StatusCode::INTERNAL_SERVER_ERROR);
|
2017-10-07 06:48:14 +02:00
|
|
|
|
|
|
|
|
2017-10-08 06:48:00 +02:00
|
|
|
pub struct StaticResponse(StatusCode);
|
|
|
|
|
|
|
|
impl StaticResponse {
|
2017-10-15 23:17:41 +02:00
|
|
|
pub fn builder(&self) -> HttpResponseBuilder {
|
2017-10-11 01:03:32 +02:00
|
|
|
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 06:48:00 +02:00
|
|
|
}
|
2017-10-24 08:25:32 +02:00
|
|
|
pub fn with_body<B: Into<Body>>(self, body: B) -> HttpResponse {
|
|
|
|
HttpResponse::new(self.0, body.into())
|
2017-10-16 00:52:52 +02:00
|
|
|
}
|
2017-10-08 06:48:00 +02:00
|
|
|
}
|
|
|
|
|
2017-10-07 06:48:14 +02:00
|
|
|
impl<S> RouteHandler<S> for StaticResponse {
|
2017-11-27 04:00:57 +01:00
|
|
|
fn handle(&self, _: &mut HttpRequest, _: Rc<S>) -> Task {
|
2017-10-13 23:43:17 +02:00
|
|
|
Task::reply(HttpResponse::new(self.0, Body::Empty))
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-11 01:03:32 +02:00
|
|
|
impl From<StaticResponse> for HttpResponse {
|
|
|
|
fn from(st: StaticResponse) -> Self {
|
|
|
|
st.response()
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-14 19:01:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use http::StatusCode;
|
|
|
|
use super::{HTTPOk, HTTPBadRequest, Body, HttpResponse};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_builder() {
|
|
|
|
let resp = HTTPOk.builder().body(Body::Empty).unwrap();
|
|
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_response() {
|
|
|
|
let resp = HTTPOk.response();
|
|
|
|
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() {
|
|
|
|
let resp = HTTPOk.response();
|
|
|
|
assert_eq!(resp.reason(), "");
|
|
|
|
|
|
|
|
let resp = HTTPBadRequest.with_reason("test");
|
|
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
|
|
assert_eq!(resp.reason(), "test");
|
|
|
|
}
|
|
|
|
}
|