pub struct HttpsRedirectMiddleware { /* private fields */ }Expand description
Middleware to redirect HTTP requests to HTTPS
Implementations§
Source§impl HttpsRedirectMiddleware
impl HttpsRedirectMiddleware
Sourcepub fn new(config: HttpsRedirectConfig) -> Self
pub fn new(config: HttpsRedirectConfig) -> Self
Create a new HttpsRedirectMiddleware with the given configuration
§Arguments
config- HTTPS redirect configuration
§Examples
use std::sync::Arc;
use reinhardt_middleware::{HttpsRedirectMiddleware, HttpsRedirectConfig};
use reinhardt_http::{Handler, Middleware, Request, Response};
use hyper::{StatusCode, Method, Version, HeaderMap};
use bytes::Bytes;
struct TestHandler;
#[async_trait::async_trait]
impl Handler for TestHandler {
async fn handle(&self, _request: Request) -> reinhardt_core::exception::Result<Response> {
Ok(Response::new(StatusCode::OK))
}
}
let mut config = HttpsRedirectConfig::default();
config.enabled = true;
config.exempt_paths = vec!["/health".to_string()];
config.status_code = StatusCode::MOVED_PERMANENTLY;
config.allowed_hosts = vec!["example.com".to_string()];
let middleware = HttpsRedirectMiddleware::new(config);
let handler = Arc::new(TestHandler);
let mut headers = HeaderMap::new();
headers.insert(hyper::header::HOST, "example.com".parse().unwrap());
let request = Request::builder()
.method(Method::GET)
.uri("/api/data")
.version(Version::HTTP_11)
.headers(headers)
.body(Bytes::new())
.build()
.unwrap();
let response = middleware.process(request, handler).await.unwrap();
assert_eq!(response.status, StatusCode::MOVED_PERMANENTLY);
assert_eq!(response.headers.get("Location").unwrap(), "https://example.com/api/data");Sourcepub fn default_config() -> Self
pub fn default_config() -> Self
Create with default configuration
Default configuration enables HTTPS redirect with 301 status code and no exempt paths.
§Examples
use std::sync::Arc;
use reinhardt_middleware::{HttpsRedirectConfig, HttpsRedirectMiddleware};
use reinhardt_http::{Handler, Middleware, Request, Response};
use hyper::{StatusCode, Method, Version, HeaderMap};
use bytes::Bytes;
struct TestHandler;
#[async_trait::async_trait]
impl Handler for TestHandler {
async fn handle(&self, _request: Request) -> reinhardt_core::exception::Result<Response> {
Ok(Response::new(StatusCode::OK))
}
}
let mut config = HttpsRedirectConfig::default();
config.allowed_hosts = vec!["api.example.com".to_string()];
let middleware = HttpsRedirectMiddleware::new(config);
let handler = Arc::new(TestHandler);
let mut headers = HeaderMap::new();
headers.insert(hyper::header::HOST, "api.example.com".parse().unwrap());
let request = Request::builder()
.method(Method::GET)
.uri("/users?page=1")
.version(Version::HTTP_11)
.headers(headers)
.body(Bytes::new())
.build()
.unwrap();
let response = middleware.process(request, handler).await.unwrap();
assert_eq!(response.status, StatusCode::MOVED_PERMANENTLY);
assert_eq!(response.headers.get("Location").unwrap(), "https://api.example.com/users?page=1");Trait Implementations§
Source§impl Middleware for HttpsRedirectMiddleware
impl Middleware for HttpsRedirectMiddleware
Auto Trait Implementations§
impl Freeze for HttpsRedirectMiddleware
impl RefUnwindSafe for HttpsRedirectMiddleware
impl Send for HttpsRedirectMiddleware
impl Sync for HttpsRedirectMiddleware
impl Unpin for HttpsRedirectMiddleware
impl UnsafeUnpin for HttpsRedirectMiddleware
impl UnwindSafe for HttpsRedirectMiddleware
Blanket Implementations§
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more