2017-12-04 05:47:15 +01:00
|
|
|
//! Default response headers
|
|
|
|
use http::{HeaderMap, HttpTryFrom};
|
2017-12-14 20:20:45 +01:00
|
|
|
use http::header::{HeaderName, HeaderValue, CONTENT_TYPE};
|
2017-12-04 05:47:15 +01:00
|
|
|
|
2018-01-10 07:48:35 +01:00
|
|
|
use error::Result;
|
2017-12-04 05:47:15 +01:00
|
|
|
use httprequest::HttpRequest;
|
|
|
|
use httpresponse::HttpResponse;
|
2017-12-27 04:59:41 +01:00
|
|
|
use middleware::{Response, Middleware};
|
2017-12-04 05:47:15 +01:00
|
|
|
|
|
|
|
/// `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;
|
2018-03-31 09:16:55 +02:00
|
|
|
/// use actix_web::{http, middleware, App, HttpResponse};
|
2017-12-04 05:47:15 +01:00
|
|
|
///
|
|
|
|
/// fn main() {
|
2018-03-31 09:16:55 +02:00
|
|
|
/// let app = App::new()
|
2017-12-04 05:47:15 +01:00
|
|
|
/// .middleware(
|
2017-12-27 04:59:41 +01:00
|
|
|
/// middleware::DefaultHeaders::build()
|
2017-12-04 05:47:15 +01:00
|
|
|
/// .header("X-Version", "0.2")
|
|
|
|
/// .finish())
|
|
|
|
/// .resource("/test", |r| {
|
2018-03-31 08:07:33 +02:00
|
|
|
/// r.method(http::Method::GET).f(|_| HttpResponse::Ok());
|
|
|
|
/// r.method(http::Method::HEAD).f(|_| HttpResponse::MethodNotAllowed());
|
2017-12-04 05:47:15 +01:00
|
|
|
/// })
|
|
|
|
/// .finish();
|
|
|
|
/// }
|
|
|
|
/// ```
|
2017-12-14 20:20:45 +01:00
|
|
|
pub struct DefaultHeaders{
|
|
|
|
ct: bool,
|
|
|
|
headers: HeaderMap,
|
|
|
|
}
|
2017-12-04 05:47:15 +01:00
|
|
|
|
|
|
|
impl DefaultHeaders {
|
|
|
|
pub fn build() -> DefaultHeadersBuilder {
|
2017-12-14 20:20:45 +01:00
|
|
|
DefaultHeadersBuilder{ct: false, headers: Some(HeaderMap::new())}
|
2017-12-04 05:47:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-09 13:33:40 +01:00
|
|
|
impl<S> Middleware<S> for DefaultHeaders {
|
2017-12-04 05:47:15 +01:00
|
|
|
|
2018-01-10 07:48:35 +01:00
|
|
|
fn response(&self, _: &mut HttpRequest<S>, mut resp: HttpResponse) -> Result<Response> {
|
2017-12-14 20:20:45 +01:00
|
|
|
for (key, value) in self.headers.iter() {
|
2017-12-04 05:47:15 +01:00
|
|
|
if !resp.headers().contains_key(key) {
|
|
|
|
resp.headers_mut().insert(key, value.clone());
|
|
|
|
}
|
|
|
|
}
|
2017-12-14 20:20:45 +01:00
|
|
|
// default content-type
|
|
|
|
if self.ct && !resp.headers().contains_key(CONTENT_TYPE) {
|
|
|
|
resp.headers_mut().insert(
|
|
|
|
CONTENT_TYPE, HeaderValue::from_static("application/octet-stream"));
|
|
|
|
}
|
2018-01-10 07:48:35 +01:00
|
|
|
Ok(Response::Done(resp))
|
2017-12-04 05:47:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Structure that follows the builder pattern for building `DefaultHeaders` middleware.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct DefaultHeadersBuilder {
|
2017-12-14 20:20:45 +01:00
|
|
|
ct: bool,
|
2017-12-04 05:47:15 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-12-14 20:20:45 +01:00
|
|
|
/// Set *CONTENT-TYPE* header if response does not contain this header.
|
|
|
|
pub fn content_type(&mut self) -> &mut Self {
|
|
|
|
self.ct = true;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-12-04 05:47:15 +01:00
|
|
|
/// Finishes building and returns the built `DefaultHeaders` middleware.
|
|
|
|
pub fn finish(&mut self) -> DefaultHeaders {
|
|
|
|
let headers = self.headers.take().expect("cannot reuse middleware builder");
|
2018-02-26 23:33:56 +01:00
|
|
|
DefaultHeaders{ ct: self.ct, headers }
|
2017-12-04 05:47:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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();
|
|
|
|
|
2018-03-31 08:07:33 +02:00
|
|
|
let resp = HttpResponse::Ok().finish();
|
2017-12-16 03:49:11 +01:00
|
|
|
let resp = match mw.response(&mut req, resp) {
|
2018-01-10 07:48:35 +01:00
|
|
|
Ok(Response::Done(resp)) => resp,
|
2017-12-04 05:47:15 +01:00
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0001");
|
|
|
|
|
2018-03-31 08:07:33 +02:00
|
|
|
let resp = HttpResponse::Ok().header(CONTENT_TYPE, "0002").finish();
|
2017-12-16 03:49:11 +01:00
|
|
|
let resp = match mw.response(&mut req, resp) {
|
2018-01-10 07:48:35 +01:00
|
|
|
Ok(Response::Done(resp)) => resp,
|
2017-12-04 05:47:15 +01:00
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0002");
|
|
|
|
}
|
|
|
|
}
|