1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-12-01 02:44:37 +01:00
actix-extras/src/middleware/defaultheaders.rs

121 lines
3.4 KiB
Rust
Raw Normal View History

2017-12-04 05:47:15 +01:00
//! Default response headers
use http::header::{HeaderName, HeaderValue, CONTENT_TYPE};
2018-04-14 01:02:01 +02:00
use http::{HeaderMap, HttpTryFrom};
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;
2018-04-14 01:02:01 +02:00
use middleware::{Middleware, Response};
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()
2018-06-01 18:37:14 +02:00
/// .middleware(middleware::DefaultHeaders::new().header("X-Version", "0.2"))
2017-12-04 05:47:15 +01:00
/// .resource("/test", |r| {
2018-06-01 18:37:14 +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();
/// }
/// ```
2018-04-14 01:02:01 +02:00
pub struct DefaultHeaders {
ct: bool,
headers: HeaderMap,
}
2017-12-04 05:47:15 +01:00
2018-04-03 06:43:50 +02:00
impl Default for DefaultHeaders {
fn default() -> Self {
2018-04-14 01:02:01 +02:00
DefaultHeaders {
ct: false,
headers: HeaderMap::new(),
}
2017-12-04 05:47:15 +01:00
}
}
2018-04-03 06:43:50 +02:00
impl DefaultHeaders {
/// Construct `DefaultHeaders` middleware.
pub fn new() -> DefaultHeaders {
DefaultHeaders::default()
2017-12-04 05:47:15 +01:00
}
/// Set a header.
#[inline]
2018-10-02 06:16:56 +02:00
#[cfg_attr(feature = "cargo-clippy", allow(clippy::match_wild_err_arm))]
2018-04-03 06:43:50 +02:00
pub fn header<K, V>(mut self, key: K, value: V) -> Self
2018-04-14 01:02:01 +02:00
where
HeaderName: HttpTryFrom<K>,
HeaderValue: HttpTryFrom<V>,
2017-12-04 05:47:15 +01:00
{
2018-04-03 06:43:50 +02:00
match HeaderName::try_from(key) {
2018-04-14 01:02:01 +02:00
Ok(key) => match HeaderValue::try_from(value) {
Ok(value) => {
self.headers.append(key, value);
2018-04-03 06:43:50 +02:00
}
2018-04-14 01:02:01 +02:00
Err(_) => panic!("Can not create header value"),
2018-04-03 06:43:50 +02:00
},
Err(_) => panic!("Can not create header name"),
2017-12-04 05:47:15 +01:00
}
self
}
/// Set *CONTENT-TYPE* header if response does not contain this header.
2018-04-03 06:43:50 +02:00
pub fn content_type(mut self) -> Self {
self.ct = true;
self
}
2018-04-03 06:43:50 +02:00
}
2018-04-03 06:43:50 +02:00
impl<S> Middleware<S> for DefaultHeaders {
2018-06-25 06:58:04 +02:00
fn response(&self, _: &HttpRequest<S>, mut resp: HttpResponse) -> Result<Response> {
2018-04-03 06:43:50 +02:00
for (key, value) in self.headers.iter() {
if !resp.headers().contains_key(key) {
resp.headers_mut().insert(key, value.clone());
}
}
// default content-type
if self.ct && !resp.headers().contains_key(CONTENT_TYPE) {
resp.headers_mut().insert(
2018-04-14 01:02:01 +02:00
CONTENT_TYPE,
HeaderValue::from_static("application/octet-stream"),
);
2018-04-03 06:43:50 +02:00
}
Ok(Response::Done(resp))
2017-12-04 05:47:15 +01:00
}
}
#[cfg(test)]
mod tests {
use super::*;
use http::header::CONTENT_TYPE;
2018-06-25 06:58:04 +02:00
use test::TestRequest;
2017-12-04 05:47:15 +01:00
#[test]
fn test_default_headers() {
2018-06-21 19:06:23 +02:00
let mw = DefaultHeaders::new().header(CONTENT_TYPE, "0001");
2017-12-04 05:47:15 +01:00
2018-06-25 06:58:04 +02:00
let req = TestRequest::default().finish();
2017-12-04 05:47:15 +01:00
let resp = HttpResponse::Ok().finish();
2018-06-25 06:58:04 +02:00
let resp = match mw.response(&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-05-17 21:20:20 +02:00
let resp = HttpResponse::Ok().header(CONTENT_TYPE, "0002").finish();
2018-06-25 06:58:04 +02:00
let resp = match mw.response(&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");
}
}