Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

🦀 Parses HTTP headers to correctly compute cacheability of responses. Direct port ofhttps://github.com/kornelski/http-cache-semantics

License

NotificationsYou must be signed in to change notification settings

kornelski/rusty-http-cache-semantics

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CachePolicy tells when responses can be reused from a cache, taking into accountHTTP RFC 7234/9111 rules for user agents and shared caches. It's aware of many tricky details such as theVary header, age updates, proxy revalidation, and authenticated responses.

Usage

Cacheability of an HTTP response depends on how it was requested, so bothrequest andresponse are required to create the policy.

It may be surprising, but it's not enough for an HTTP response to befresh to satisfy a request. It may need to match request headers specified inVary. Even a matching fresh response may still not be usable if the new request restricted cacheability, etc.

The key method isbefore_request(new_request), which checks whether thenew_request is compatible with the original request and whether all caching conditions are met.

Options

Ifoptions.shared istrue (default), then the response is evaluated from a perspective of a shared cache (i.e.private is not cacheable ands-maxage is respected). Ifoptions.shared isfalse, then the response is evaluated from a perspective of a single-user cache (i.e.private is cacheable ands-maxage is ignored).shared: true is recommended for HTTP proxies, andfalse for single-user clients.

options.cache_heuristic is a fraction of response's age that is used as a fallback cache duration. The default is 0.1 (10%), e.g. if a file hasn't been modified for 100 days, it'll be cached for 100×0.1 = 10 days.

options.immutable_min_time_to_live is a duration to assume as the default time to cache responses withCache-Control: immutable. Note thatper RFC these can become stale, somax-age still overrides the default.

Ifoptions.ignore_cargo_cult is true, common anti-cache directives will be completely ignored if the non-standardpre-check andpost-check directives are present. These two useless directives are most commonly found in bad StackOverflow answers and PHP's "session limiter" defaults.

is_storable()

Returnstrue if the response can be stored in a cache. If it'sfalse then you MUST NOT store either the request or the response.

before_request(new_request)

This is the most important method. Use this method to check whether the cached response is still fresh in the context of the new request.

If it returnsFresh, then the givenrequest matches the original response this cache policy has been created with, and the response can be reused without contacting the server. This will contain an updated, filtered set of response headers to return to clients receiving the cached response. This processing is necessary, because proxies MUST always remove hop-by-hop headers (such asTE andConnection) and update response'sAge to avoid doubling cache time.

If it returnsStale, then the response may not be matching at all (e.g. it's for a different URL or method), or may require to be refreshed first. The variant will contain HTTP headers for making a revalidation request to the server.

time_to_live()

Returns approximate time until the response becomes stale (i.e. not fresh). This is equivalent ofmax-age, but with appropriate time correction applied.

After that time (whentime_to_live() == Duration::ZERO) the response might not be usable without revalidation. However, there are exceptions, e.g. a client can explicitly allow stale responses, so always check withbefore_request().

Refreshing stale cache (revalidation)

When a cached response has expired, it can be made fresh again by making a request to the origin server. The server may respond with status 304 (Not Modified) without sending the response body again, saving bandwidth.

after_response(revalidation_request, revalidation_response)

Use this method to update the cache after receiving a new response from the origin server. It returnsModified/NotModified object with a newCachePolicy with HTTP headers updated fromrevalidation_response. You can always replace the old cachedCachePolicy with the new one.

-  If `NotModified`, then a valid 304 Not Modified response has been received, and you can reuse the old cached response body.-  If `Modified`, you should replace the old cached body with the new response's body.

Yo, FRESH

satisfies_without_revalidation

Implemented

  • Cache-Control response header with all the quirks.
  • Expires with check for bad clocks.
  • Pragma response header.
  • Age response header.
  • Vary response header.
  • Default cacheability of statuses and methods.
  • Requests for stale data.
  • Filtering of hop-by-hop headers.
  • Basic revalidation request

Unimplemented

  • Merging of range requests, If-Range (but correctly supports them as non-cacheable)
  • Revalidation of multiple representations

About

🦀 Parses HTTP headers to correctly compute cacheability of responses. Direct port ofhttps://github.com/kornelski/http-cache-semantics

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Rust100.0%

[8]ページ先頭

©2009-2025 Movatter.jp