Skip to content

Instantly share code, notes, and snippets.

View sdasda7777's full-sized avatar

sdasda7777

  • Your walls
View GitHub Profile
@sdasda7777
sdasda7777 / simple_conditional_mw.rs
Created November 30, 2023 19:58
Simple conditional Middleware for Axum with support for If-None-Match/ETag and If-Modified-Since/Last-Modified. Either checks If-None-Match against ETag of the response of the next layer (computes from body if ETag is not present), or if If-None-Match is not present in the request checks Last-Modified header of the next layer response against th…
use etag::EntityTag;
// Based on alextes's implementation (https://gist.github.com/alextes/4095b1fca7d58bd3100825e10e882e57)
// which already provided support for If-None-Match/ETag
// but with support for precomputed ETags and If-Modified-Since/Last-Modified
async fn conditional_mw<B>(request: Request<B>, next: Next<B>) -> Result<Response, StatusCode> {
let if_none_match_header = request.headers().get(header::IF_NONE_MATCH).cloned();
let if_modified_since_header = request.headers().get(header::IF_MODIFIED_SINCE).cloned();
// let path = request.uri().path().to_owned();
let response = next.run(request).await;