pub struct DefaultHeaders { /* private fields */ }Expand description
Middleware for setting default response headers.
This middleware does not set header if response headers already contains it. A header name configured with several values gets all of them when the response does not contain that name.
use ntex::http;
use ntex::web::{self, middleware, App, HttpResponse};
fn main() {
let app = App::default()
.middleware(middleware::DefaultHeaders::new().header("X-Version", "0.2"))
.service(
web::resource("/test")
.route(web::get().to(async || { HttpResponse::Ok() }))
.route(web::method(http::Method::HEAD).to(async || { HttpResponse::MethodNotAllowed() }))
);
}Implementations§
Source§impl DefaultHeaders
impl DefaultHeaders
Sourcepub fn new() -> DefaultHeaders
pub fn new() -> DefaultHeaders
Construct DefaultHeaders middleware.
Sourcepub fn header<K, V>(self, key: K, value: V) -> Selfwhere
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
HeaderValue: TryFrom<V>,
<HeaderValue as TryFrom<V>>::Error: Into<HttpError>,
pub fn header<K, V>(self, key: K, value: V) -> Selfwhere
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
HeaderValue: TryFrom<V>,
<HeaderValue as TryFrom<V>>::Error: Into<HttpError>,
Set a header.
Calling it again with the same name adds another value.
Sourcepub fn content_type(self) -> Self
pub fn content_type(self) -> Self
Set CONTENT-TYPE header if response does not contain this header.
The header is set to application/octet-stream.
Trait Implementations§
Source§impl Debug for DefaultHeaders
impl Debug for DefaultHeaders
Source§impl Default for DefaultHeaders
impl Default for DefaultHeaders
Source§impl<S, St> Middleware<S, St> for DefaultHeaders
impl<S, St> Middleware<S, St> for DefaultHeaders
Source§type Service = DefaultHeadersMiddleware<S>
type Service = DefaultHeadersMiddleware<S>
Wrapper returned by
Self::create. Read moreSource§fn create(&self, _: &St, service: S) -> Self::Service
fn create(&self, _: &St, service: S) -> Self::Service
Wraps
service using the supplied construction state. Read moreSource§fn apply_to<Sf, Req>(
self,
factory: Sf,
) -> ServiceChainFactory<ApplyMiddleware<Self, Sf>, St, Req>
fn apply_to<Sf, Req>( self, factory: Sf, ) -> ServiceChainFactory<ApplyMiddleware<Self, Sf>, St, Req>
Creates a service factory that instantiates a service and applies
the current middleware to it. Read more
Auto Trait Implementations§
impl !Send for DefaultHeaders
impl !Sync for DefaultHeaders
impl Freeze for DefaultHeaders
impl RefUnwindSafe for DefaultHeaders
impl Unpin for DefaultHeaders
impl UnsafeUnpin for DefaultHeaders
impl UnwindSafe for DefaultHeaders
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more