1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
//! Helpers for HTTP response generation
use hyper::header::{CONTENT_TYPE, LOCATION};
use hyper::{Body, Method, Response, StatusCode};
use mime::Mime;
use std::borrow::Cow;
use crate::helpers::http::header::X_REQUEST_ID;
use crate::state::{request_id, FromState, State};
/// Creates a `Response` object and populates it with a set of default headers that help to improve
/// security and conformance to best practice.
///
/// `create_response` utilises `extend_response`, which delegates to `set_headers` for setting
/// security headers. See `set_headers` for information about the headers which are populated.
///
/// # Examples
///
/// ```rust
/// # extern crate gotham;
/// # extern crate hyper;
/// # extern crate mime;
/// #
/// # use hyper::{Body, Response, StatusCode};
/// # use hyper::header::{CONTENT_LENGTH, CONTENT_TYPE};
/// # use gotham::state::State;
/// # use gotham::helpers::http::header::X_REQUEST_ID;
/// # use gotham::helpers::http::response::create_response;
/// # use gotham::test::TestServer;
/// #
/// static BODY: &'static [u8] = b"Hello, world!";
///
/// fn handler(state: State) -> (State, Response<Body>) {
/// let response = create_response(&state, StatusCode::OK, mime::TEXT_PLAIN, BODY);
///
/// (state, response)
/// }
/// #
/// # fn main() {
/// # let test_server = TestServer::new(|| Ok(handler)).unwrap();
/// # let response = test_server
/// # .client()
/// # .get("http://example.com/")
/// # .perform()
/// # .unwrap();
/// #
/// # assert_eq!(response.status(), StatusCode::OK);
/// # assert!(response.headers().get(X_REQUEST_ID).is_some());
/// #
/// # assert_eq!(
/// # *response.headers().get(CONTENT_TYPE).unwrap(),
/// # mime::TEXT_PLAIN.to_string()
/// # );
/// #
/// # assert_eq!(
/// # *response.headers().get(CONTENT_LENGTH).unwrap(),
/// # format!("{}", BODY.len() as u64)
/// # );
/// # }
/// ```
pub fn create_response<B>(state: &State, status: StatusCode, mime: Mime, body: B) -> Response<Body>
where
B: Into<Body>,
{
// use the basic empty response as a base
let mut res = create_empty_response(state, status);
// insert the content type header
res.headers_mut()
.insert(CONTENT_TYPE, mime.as_ref().parse().unwrap());
// add the body on non-HEAD requests
if Method::borrow_from(state) != Method::HEAD {
*res.body_mut() = body.into();
}
res
}
/// Produces a simple empty `Response` with a provided status.
///
/// # Examples
///
/// ```rust
/// # extern crate gotham;
/// # extern crate hyper;
/// #
/// # use hyper::{Body, Response, StatusCode};
/// # use gotham::state::State;
/// # use gotham::helpers::http::response::create_empty_response;
/// # use gotham::test::TestServer;
/// fn handler(state: State) -> (State, Response<Body>) {
/// let resp = create_empty_response(&state, StatusCode::NO_CONTENT);
///
/// (state, resp)
/// }
/// # fn main() {
/// # let test_server = TestServer::new(|| Ok(handler)).unwrap();
/// # let response = test_server
/// # .client()
/// # .get("http://example.com/")
/// # .perform()
/// # .unwrap();
/// #
/// # assert_eq!(response.status(), StatusCode::NO_CONTENT);
/// # }
/// ```
pub fn create_empty_response(state: &State, status: StatusCode) -> Response<Body> {
// new builder for the response
let built = Response::builder()
// always add status and req-id
.status(status)
.header(X_REQUEST_ID, request_id(state))
// attach an empty body by default
.body(Body::empty());
// this expect should be safe due to generic bounds
built.expect("Response built from a compatible type")
}
/// Produces a simple empty `Response` with a `Location` header and a 308
/// status.
///
/// # Examples
///
/// ```rust
/// # extern crate gotham;
/// # extern crate hyper;
/// #
/// # use hyper::{Body, Response, StatusCode};
/// # use gotham::state::State;
/// # use gotham::helpers::http::response::create_permanent_redirect;
/// # use gotham::test::TestServer;
/// # use hyper::header::LOCATION;
/// fn handler(state: State) -> (State, Response<Body>) {
/// let resp = create_permanent_redirect(&state, "/over-there");
///
/// (state, resp)
/// }
/// # fn main() {
/// # let test_server = TestServer::new(|| Ok(handler)).unwrap();
/// # let response = test_server
/// # .client()
/// # .get("http://example.com/")
/// # .perform()
/// # .unwrap();
/// #
/// # assert_eq!(response.status(), StatusCode::PERMANENT_REDIRECT);
/// # assert_eq!(
/// # response.headers().get(LOCATION).unwrap(),
/// # "/over-there"
/// # );
/// # }
/// ```
pub fn create_permanent_redirect<L: Into<Cow<'static, str>>>(
state: &State,
location: L,
) -> Response<Body> {
let mut res = create_empty_response(state, StatusCode::PERMANENT_REDIRECT);
res.headers_mut()
.insert(LOCATION, location.into().to_string().parse().unwrap());
res
}
/// Produces a simple empty `Response` with a `Location` header and a 307
/// status.
///
/// # Examples
///
/// ```rust
/// # extern crate gotham;
/// # extern crate hyper;
/// #
/// # use hyper::{Body, Response, StatusCode};
/// # use gotham::state::State;
/// # use gotham::helpers::http::response::create_temporary_redirect;
/// # use gotham::test::TestServer;
/// # use hyper::header::LOCATION;
/// fn handler(state: State) -> (State, Response<Body>) {
/// let resp = create_temporary_redirect(&state, "/quick-detour");
///
/// (state, resp)
/// }
/// # fn main() {
/// # let test_server = TestServer::new(|| Ok(handler)).unwrap();
/// # let response = test_server
/// # .client()
/// # .get("http://example.com/")
/// # .perform()
/// # .unwrap();
/// #
/// # assert_eq!(response.status(), StatusCode::TEMPORARY_REDIRECT);
/// # assert_eq!(
/// # response.headers().get(LOCATION).unwrap(),
/// # "/quick-detour"
/// # );
/// # }
/// ```
pub fn create_temporary_redirect<L: Into<Cow<'static, str>>>(
state: &State,
location: L,
) -> Response<Body> {
let mut res = create_empty_response(state, StatusCode::TEMPORARY_REDIRECT);
res.headers_mut()
.insert(LOCATION, location.into().to_string().parse().unwrap());
res
}