pub struct ValidateRequestHeaderLayer<T> { /* private fields */ }Expand description
Layer that applies ValidateRequestHeader which validates all requests.
See the module docs for an example.
Implementations§
Source§impl<ResBody> ValidateRequestHeaderLayer<Basic<ResBody>>
impl<ResBody> ValidateRequestHeaderLayer<Basic<ResBody>>
Sourcepub fn basic(username: &str, password: &str) -> Selfwhere
ResBody: Default,
👎Deprecated since 0.6.7: too basic to be useful in real applications
pub fn basic(username: &str, password: &str) -> Selfwhere
ResBody: Default,
too basic to be useful in real applications
Authorize requests using a username and password pair.
The Authorization header is required to be Basic {credentials} where credentials is
base64_encode("{username}:{password}").
Since the username and password is sent in clear text it is recommended to use HTTPS/TLS with this method. However use of HTTPS/TLS is not enforced by this middleware.
Source§impl<ResBody> ValidateRequestHeaderLayer<Bearer<ResBody>>
impl<ResBody> ValidateRequestHeaderLayer<Bearer<ResBody>>
Sourcepub fn bearer(token: &str) -> Selfwhere
ResBody: Default,
👎Deprecated since 0.6.7: too basic to be useful in real applications
pub fn bearer(token: &str) -> Selfwhere
ResBody: Default,
too basic to be useful in real applications
Authorize requests using a “bearer token”. Commonly used for OAuth 2.
The Authorization header is required to be Bearer {token}.
§Panics
Panics if the token is not a valid HeaderValue.
Source§impl<ResBody> ValidateRequestHeaderLayer<AcceptHeader<ResBody>>
impl<ResBody> ValidateRequestHeaderLayer<AcceptHeader<ResBody>>
Sourcepub fn accept(value: &str) -> Selfwhere
ResBody: Default,
pub fn accept(value: &str) -> Selfwhere
ResBody: Default,
Validate requests have the required Accept header.
The Accept header is required to be */*, type/* or type/subtype,
as configured.
§Panics
Panics if header_value is not in the form: type/subtype, such as application/json
See AcceptHeader::new for when this method panics.
§Example
use http_body_util::Full;
use bytes::Bytes;
use tower_http::validate_request::{AcceptHeader, ValidateRequestHeaderLayer};
let layer = ValidateRequestHeaderLayer::<AcceptHeader<Full<Bytes>>>::accept("application/json");Source§impl<ResBody> ValidateRequestHeaderLayer<RequiredHeaderValue<ResBody>>
impl<ResBody> ValidateRequestHeaderLayer<RequiredHeaderValue<ResBody>>
Sourcepub fn has_header_value(
expected_header_name: &str,
expected_header_value: &str,
) -> Result<Self, InvalidHeaderName>where
ResBody: Default,
pub fn has_header_value(
expected_header_name: &str,
expected_header_value: &str,
) -> Result<Self, InvalidHeaderName>where
ResBody: Default,
Validate requests have a required header with a specific value.
Rejects with 403 Forbidden if the header is missing or does not have the expected value.
Header values that are not valid UTF-8 are treated as non-matching.
If the request contains multiple values for the header, only the first occurrence is checked.
§Errors
Returns an error if expected_header_name is not a valid HTTP header name per RFC 7230
(non-empty, at most 32,768 bytes, containing only valid token characters).
§Example
use http::{Request, Response, StatusCode};
use http_body_util::Full;
use bytes::Bytes;
use tower::{Service, ServiceBuilder, ServiceExt, service_fn};
use tower_http::validate_request::ValidateRequestHeaderLayer;
async fn handle(request: Request<Full<Bytes>>) -> Result<Response<Full<Bytes>>, std::convert::Infallible> {
Ok(Response::new(request.into_body()))
}
let mut service = ServiceBuilder::new()
.layer(ValidateRequestHeaderLayer::has_header_value(
"x-custom-header",
"random-value-1234567890",
).expect("invalid validate header"))
.service_fn(handle);
let request = Request::builder()
.header("x-custom-header", "random-value-1234567890")
.body(Full::default())
.unwrap();
let response = service.ready().await.unwrap().call(request).await.unwrap();
assert_eq!(response.status(), StatusCode::OK);Source§impl<T> ValidateRequestHeaderLayer<T>
impl<T> ValidateRequestHeaderLayer<T>
Sourcepub fn custom(validate: T) -> ValidateRequestHeaderLayer<T>
pub fn custom(validate: T) -> ValidateRequestHeaderLayer<T>
Validate requests using a custom method.
Trait Implementations§
Source§impl<T: Clone> Clone for ValidateRequestHeaderLayer<T>
impl<T: Clone> Clone for ValidateRequestHeaderLayer<T>
Source§fn clone(&self) -> ValidateRequestHeaderLayer<T>
fn clone(&self) -> ValidateRequestHeaderLayer<T>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more