Skip to main content

Crate http_fresh

Crate http_fresh 

Source
Expand description

§http-fresh — HTTP response freshness (conditional GET) checking

Decide whether a cached response is still fresh for a request, i.e. whether the server may answer 304 Not Modified instead of resending the body. This evaluates the If-None-Match / If-Modified-Since request headers against the response’s ETag / Last-Modified, honoring Cache-Control: no-cache.

A faithful Rust port of the fresh npm package (the logic behind Express’s req.fresh). Zero dependencies, #![no_std], and zero heap allocation.

use http_fresh::{fresh, Request, Response};

// ETag matches → response is fresh → caller can send 304.
let req = Request::new().if_none_match("\"abc\"");
let res = Response::new().etag("\"abc\"");
assert!(fresh(&req, &res));

// A different ETag → stale → caller must send the body.
let res = Response::new().etag("\"xyz\"");
assert!(!fresh(&req, &res));

// `Cache-Control: no-cache` always forces stale.
let req = req.cache_control("no-cache");
assert!(!fresh(&req, &Response::new().etag("\"abc\"")));

§Differences from the npm package

The npm package parses dates with JavaScript’s Date.parse, which is lenient and — for timezone-less formats such as asctime — interprets them in the host machine’s local timezone (so its result is not reproducible across machines). This crate instead parses exactly the three date formats mandated by RFC 9110 §5.6.7 (IMF-fixdate, RFC 850, and asctime), always in GMT. Any string outside those formats is treated as unparseable, which makes the response stale (a safe, revalidating default). For real-world HTTP traffic — which uses IMF-fixdate — the behavior is identical to the npm package.

Structs§

Request
The request-side headers that drive a freshness check.
Response
The response-side validators that a fresh request must match.

Functions§

fresh
Returns true if the response is fresh for this request — meaning the server may answer 304 Not Modified rather than resending the body.