mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-28 01:32:57 +01:00
simplify DefaultHeaders middleware
This commit is contained in:
parent
3b93bff602
commit
476b1fb36a
@ -140,9 +140,8 @@ use actix_web::{http, middleware, App, HttpResponse};
|
|||||||
fn main() {
|
fn main() {
|
||||||
let app = App::new()
|
let app = App::new()
|
||||||
.middleware(
|
.middleware(
|
||||||
middleware::DefaultHeaders::build()
|
middleware::DefaultHeaders::new()
|
||||||
.header("X-Version", "0.2")
|
.header("X-Version", "0.2"))
|
||||||
.finish())
|
|
||||||
.resource("/test", |r| {
|
.resource("/test", |r| {
|
||||||
r.method(http::Method::GET).f(|req| HttpResponse::Ok());
|
r.method(http::Method::GET).f(|req| HttpResponse::Ok());
|
||||||
r.method(http::Method::HEAD).f(|req| HttpResponse::MethodNotAllowed());
|
r.method(http::Method::HEAD).f(|req| HttpResponse::MethodNotAllowed());
|
||||||
|
@ -18,9 +18,8 @@ use middleware::{Response, Middleware};
|
|||||||
/// fn main() {
|
/// fn main() {
|
||||||
/// let app = App::new()
|
/// let app = App::new()
|
||||||
/// .middleware(
|
/// .middleware(
|
||||||
/// middleware::DefaultHeaders::build()
|
/// middleware::DefaultHeaders::new()
|
||||||
/// .header("X-Version", "0.2")
|
/// .header("X-Version", "0.2"))
|
||||||
/// .finish())
|
|
||||||
/// .resource("/test", |r| {
|
/// .resource("/test", |r| {
|
||||||
/// r.method(http::Method::GET).f(|_| HttpResponse::Ok());
|
/// r.method(http::Method::GET).f(|_| HttpResponse::Ok());
|
||||||
/// r.method(http::Method::HEAD).f(|_| HttpResponse::MethodNotAllowed());
|
/// r.method(http::Method::HEAD).f(|_| HttpResponse::MethodNotAllowed());
|
||||||
@ -33,9 +32,41 @@ pub struct DefaultHeaders{
|
|||||||
headers: HeaderMap,
|
headers: HeaderMap,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for DefaultHeaders {
|
||||||
|
fn default() -> Self {
|
||||||
|
DefaultHeaders{ct: false, headers: HeaderMap::new()}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl DefaultHeaders {
|
impl DefaultHeaders {
|
||||||
pub fn build() -> DefaultHeadersBuilder {
|
/// Construct `DefaultHeaders` middleware.
|
||||||
DefaultHeadersBuilder{ct: false, headers: Some(HeaderMap::new())}
|
pub fn new() -> DefaultHeaders {
|
||||||
|
DefaultHeaders::default()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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) -> Self
|
||||||
|
where HeaderName: HttpTryFrom<K>,
|
||||||
|
HeaderValue: HttpTryFrom<V>
|
||||||
|
{
|
||||||
|
match HeaderName::try_from(key) {
|
||||||
|
Ok(key) => {
|
||||||
|
match HeaderValue::try_from(value) {
|
||||||
|
Ok(value) => { self.headers.append(key, value); }
|
||||||
|
Err(_) => panic!("Can not create header value"),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(_) => panic!("Can not create header name"),
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set *CONTENT-TYPE* header if response does not contain this header.
|
||||||
|
pub fn content_type(mut self) -> Self {
|
||||||
|
self.ct = true;
|
||||||
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,49 +87,6 @@ impl<S> Middleware<S> for DefaultHeaders {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Structure that follows the builder pattern for building `DefaultHeaders` middleware.
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct DefaultHeadersBuilder {
|
|
||||||
ct: bool,
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Set *CONTENT-TYPE* header if response does not contain this header.
|
|
||||||
pub fn content_type(&mut self) -> &mut Self {
|
|
||||||
self.ct = true;
|
|
||||||
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{ ct: self.ct, headers }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@ -106,9 +94,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_default_headers() {
|
fn test_default_headers() {
|
||||||
let mw = DefaultHeaders::build()
|
let mw = DefaultHeaders::new()
|
||||||
.header(CONTENT_TYPE, "0001")
|
.header(CONTENT_TYPE, "0001");
|
||||||
.finish();
|
|
||||||
|
|
||||||
let mut req = HttpRequest::default();
|
let mut req = HttpRequest::default();
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ pub mod cors;
|
|||||||
pub mod csrf;
|
pub mod csrf;
|
||||||
pub use self::logger::Logger;
|
pub use self::logger::Logger;
|
||||||
pub use self::errhandlers::ErrorHandlers;
|
pub use self::errhandlers::ErrorHandlers;
|
||||||
pub use self::defaultheaders::{DefaultHeaders, DefaultHeadersBuilder};
|
pub use self::defaultheaders::DefaultHeaders;
|
||||||
|
|
||||||
#[cfg(feature = "session")]
|
#[cfg(feature = "session")]
|
||||||
pub use self::session::{RequestSession, Session, SessionImpl, SessionBackend, SessionStorage,
|
pub use self::session::{RequestSession, Session, SessionImpl, SessionBackend, SessionStorage,
|
||||||
|
Loading…
Reference in New Issue
Block a user