pub struct ConditionalGetMiddleware { /* private fields */ }Expand description
Conditional GET middleware
Implements HTTP conditional requests using ETags and Last-Modified headers.
- Supports If-None-Match (ETag-based)
- Supports If-Modified-Since (Last-Modified-based)
- Supports If-Match and If-Unmodified-Since for safe methods
Implementations§
Source§impl ConditionalGetMiddleware
impl ConditionalGetMiddleware
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new ConditionalGetMiddleware
By default, automatic ETag generation is enabled for responses.
§Examples
use std::sync::Arc;
use reinhardt_middleware::ConditionalGetMiddleware;
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).with_body(Bytes::from("content")))
}
}
let middleware = ConditionalGetMiddleware::new();
let handler = Arc::new(TestHandler);
let request = Request::builder()
.method(Method::GET)
.uri("/api/resource")
.version(Version::HTTP_11)
.headers(HeaderMap::new())
.body(Bytes::new())
.build()
.unwrap();
let response = middleware.process(request, handler).await.unwrap();
assert_eq!(response.status, StatusCode::OK);
assert!(response.headers.contains_key(hyper::header::ETAG));Sourcepub fn without_etag() -> Self
pub fn without_etag() -> Self
Create middleware without automatic ETag generation
Use this when you want to handle ETags manually or rely only on Last-Modified headers.
§Examples
use std::sync::Arc;
use reinhardt_middleware::ConditionalGetMiddleware;
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> {
let mut response = Response::new(StatusCode::OK).with_body(Bytes::from("content"));
response.headers.insert(
hyper::header::LAST_MODIFIED,
"Wed, 21 Oct 2015 07:28:00 GMT".parse().unwrap()
);
Ok(response)
}
}
let middleware = ConditionalGetMiddleware::without_etag();
let handler = Arc::new(TestHandler);
let request = Request::builder()
.method(Method::GET)
.uri("/api/resource")
.version(Version::HTTP_11)
.headers(HeaderMap::new())
.body(Bytes::new())
.build()
.unwrap();
let response = middleware.process(request, handler).await.unwrap();
assert_eq!(response.status, StatusCode::OK);
assert!(!response.headers.contains_key(hyper::header::ETAG));
assert!(response.headers.contains_key(hyper::header::LAST_MODIFIED));Trait Implementations§
Source§impl Default for ConditionalGetMiddleware
impl Default for ConditionalGetMiddleware
Source§impl Middleware for ConditionalGetMiddleware
impl Middleware for ConditionalGetMiddleware
Auto Trait Implementations§
impl Freeze for ConditionalGetMiddleware
impl RefUnwindSafe for ConditionalGetMiddleware
impl Send for ConditionalGetMiddleware
impl Sync for ConditionalGetMiddleware
impl Unpin for ConditionalGetMiddleware
impl UnsafeUnpin for ConditionalGetMiddleware
impl UnwindSafe for ConditionalGetMiddleware
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
Read this value from the supplied reader. Same as
ReadEndian::read_from_little_endian().