2017-12-04 05:47:15 +01:00
|
|
|
//! Default response headers
|
|
|
|
use http::{HeaderMap, HttpTryFrom};
|
|
|
|
use http::header::{HeaderName, HeaderValue};
|
|
|
|
|
|
|
|
use httprequest::HttpRequest;
|
|
|
|
use httpresponse::HttpResponse;
|
|
|
|
use middlewares::{Response, Middleware};
|
|
|
|
|
|
|
|
/// `Middleware` for setting default response headers.
|
|
|
|
///
|
|
|
|
/// This middleware does not set header if response headers already contains it.
|
|
|
|
///
|
|
|
|
/// ```rust
|
2017-12-06 20:00:39 +01:00
|
|
|
/// # extern crate actix_web;
|
2017-12-04 05:47:15 +01:00
|
|
|
/// use actix_web::*;
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2017-12-11 23:16:29 +01:00
|
|
|
/// let app = Application::new()
|
2017-12-04 05:47:15 +01:00
|
|
|
/// .middleware(
|
|
|
|
/// middlewares::DefaultHeaders::build()
|
|
|
|
/// .header("X-Version", "0.2")
|
|
|
|
/// .finish())
|
|
|
|
/// .resource("/test", |r| {
|
2017-12-04 23:07:53 +01:00
|
|
|
/// r.method(Method::GET).f(|_| httpcodes::HTTPOk);
|
|
|
|
/// r.method(Method::HEAD).f(|_| httpcodes::HTTPMethodNotAllowed);
|
2017-12-04 05:47:15 +01:00
|
|
|
/// })
|
|
|
|
/// .finish();
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub struct DefaultHeaders(HeaderMap);
|
|
|
|
|
|
|
|
impl DefaultHeaders {
|
|
|
|
pub fn build() -> DefaultHeadersBuilder {
|
|
|
|
DefaultHeadersBuilder{headers: Some(HeaderMap::new())}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-09 13:33:40 +01:00
|
|
|
impl<S> Middleware<S> for DefaultHeaders {
|
2017-12-04 05:47:15 +01:00
|
|
|
|
2017-12-13 20:10:03 +01:00
|
|
|
fn response(&self, _: &mut HttpRequest<S>, mut resp: Box<HttpResponse>) -> Response {
|
2017-12-04 05:47:15 +01:00
|
|
|
for (key, value) in self.0.iter() {
|
|
|
|
if !resp.headers().contains_key(key) {
|
|
|
|
resp.headers_mut().insert(key, value.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Response::Done(resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Structure that follows the builder pattern for building `DefaultHeaders` middleware.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct DefaultHeadersBuilder {
|
|
|
|
headers: Option<HeaderMap>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DefaultHeadersBuilder {
|
|
|
|
|
|
|
|
/// Set a header.
|
|
|
|
#[inline]
|
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(match_wild_err_arm))]
|
|
|
|
pub fn header<K, V>(&mut self, key: K, value: V) -> &mut Self
|
|
|
|
where HeaderName: HttpTryFrom<K>,
|
|
|
|
HeaderValue: HttpTryFrom<V>
|
|
|
|
{
|
|
|
|
if let Some(ref mut headers) = self.headers {
|
|
|
|
match HeaderName::try_from(key) {
|
|
|
|
Ok(key) => {
|
|
|
|
match HeaderValue::try_from(value) {
|
|
|
|
Ok(value) => { headers.append(key, value); }
|
|
|
|
Err(_) => panic!("Can not create header value"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(_) => panic!("Can not create header name"),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Finishes building and returns the built `DefaultHeaders` middleware.
|
|
|
|
pub fn finish(&mut self) -> DefaultHeaders {
|
|
|
|
let headers = self.headers.take().expect("cannot reuse middleware builder");
|
|
|
|
DefaultHeaders(headers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use http::header::CONTENT_TYPE;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_default_headers() {
|
|
|
|
let mw = DefaultHeaders::build()
|
|
|
|
.header(CONTENT_TYPE, "0001")
|
|
|
|
.finish();
|
|
|
|
|
|
|
|
let mut req = HttpRequest::default();
|
|
|
|
|
|
|
|
let resp = HttpResponse::Ok().finish().unwrap();
|
2017-12-13 20:10:03 +01:00
|
|
|
let resp = match mw.response(&mut req, Box::new(resp)) {
|
2017-12-04 05:47:15 +01:00
|
|
|
Response::Done(resp) => resp,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0001");
|
|
|
|
|
|
|
|
let resp = HttpResponse::Ok().header(CONTENT_TYPE, "0002").finish().unwrap();
|
2017-12-13 20:10:03 +01:00
|
|
|
let resp = match mw.response(&mut req, Box::new(resp)) {
|
2017-12-04 05:47:15 +01:00
|
|
|
Response::Done(resp) => resp,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0002");
|
|
|
|
}
|
|
|
|
}
|