RFC 9111

HTTP Caching

๐Ÿ“… published June 2022, from ietf

RFC 9111 ๐ŸŽ€ HTTP Caching ๐Ÿฆ‹

Bestie, here's the whole vibe check on who made this and what it's about โœจ

This one is an Internet Standards Track doc, which is the top-tier, "this is official, implement it" category. It's STD 98 too ๐Ÿ‘‘. It came out in June 2022, and its ISSN is 2070-1721. The editors are R. Fielding (Adobe), M. Nottingham (Fastly), and J. Reschke (greenbytes), all listed as Ed. (editors) ๐Ÿ’…. The big news is that it obsoletes RFC 7234, so it fully replaces that older caching spec. ๐Ÿ’€ That's a whole glow-up.

Abstract โœจ

The Hypertext Transfer Protocol (HTTP) is a stateless application-level protocol for distributed, collaborative, hypertext information systems. This document defines HTTP caches and the associated header fields that control cache behavior or indicate cacheable response messages.

This document obsoletes RFC 7234.

Status of This Memo ๐Ÿ“œ

This is an Internet Standards Track document. ๐Ÿ”ฅ

It's a product of the Internet Engineering Task Force (IETF), so it represents the consensus of the IETF community. It got public review and was approved for publication by the Internet Engineering Steering Group (IESG). If you want more on Internet Standards, check out Section 2 of RFC 7841.

Info about the current status, any errata, and how to send feedback lives at https://www.rfc-editor.org/info/rfc9111. ๐ŸŒธ

Copyright (c) 2022 IETF Trust and the persons identified as the document authors. All rights reserved. ๐Ÿซถ

This document is subject to BCP 78 and the IETF Trust's Legal Provisions Relating to IETF Documents (https://trustee.ietf.org/license-info), as in effect on the date this document was published. Please actually read these, bestie, because they spell out your rights and restrictions for this doc. Any Code Components pulled out of this document must include Revised BSD License text as described in Section 4.e of the Trust Legal Provisions, and they come with no warranty, as the Revised BSD License describes.

This document may contain material from IETF Documents or IETF Contributions published or made publicly available before November 10, 2008. The person(s) who control the copyright in some of that material may not have granted the IETF Trust the right to allow modifications outside the IETF Standards Process. So, without an adequate license from those copyright holders, this document may not be modified outside the IETF Standards Process, and derivative works of it may not be created outside the IETF Standards Process. The one exceptions are formatting it for publication as an RFC or translating it into languages other than English. ๐Ÿ™ˆ

Table of Contents ๐Ÿ—บ๏ธ

   1.  Introduction
     1.1.  Requirements Notation
     1.2.  Syntax Notation
       1.2.1.  Imported Rules
       1.2.2.  Delta Seconds
   2.  Overview of Cache Operation
   3.  Storing Responses in Caches
     3.1.  Storing Header and Trailer Fields
     3.2.  Updating Stored Header Fields
     3.3.  Storing Incomplete Responses
     3.4.  Combining Partial Content
     3.5.  Storing Responses to Authenticated Requests
   4.  Constructing Responses from Caches
     4.1.  Calculating Cache Keys with the Vary Header Field
     4.2.  Freshness
       4.2.1.  Calculating Freshness Lifetime
       4.2.2.  Calculating Heuristic Freshness
       4.2.3.  Calculating Age
       4.2.4.  Serving Stale Responses
     4.3.  Validation
       4.3.1.  Sending a Validation Request
       4.3.2.  Handling a Received Validation Request
       4.3.3.  Handling a Validation Response
       4.3.4.  Freshening Stored Responses upon Validation
       4.3.5.  Freshening Responses with HEAD
     4.4.  Invalidating Stored Responses
   5.  Field Definitions
     5.1.  Age
     5.2.  Cache-Control
       5.2.1.  Request Directives
         5.2.1.1.  max-age
         5.2.1.2.  max-stale
         5.2.1.3.  min-fresh
         5.2.1.4.  no-cache
         5.2.1.5.  no-store
         5.2.1.6.  no-transform
         5.2.1.7.  only-if-cached
       5.2.2.  Response Directives
         5.2.2.1.  max-age
         5.2.2.2.  must-revalidate
         5.2.2.3.  must-understand
         5.2.2.4.  no-cache
         5.2.2.5.  no-store
         5.2.2.6.  no-transform
         5.2.2.7.  private
         5.2.2.8.  proxy-revalidate
         5.2.2.9.  public
         5.2.2.10. s-maxage
       5.2.3.  Extension Directives
       5.2.4.  Cache Directive Registry
     5.3.  Expires
     5.4.  Pragma
     5.5.  Warning
   6.  Relationship to Applications and Other Caches
   7.  Security Considerations
     7.1.  Cache Poisoning
     7.2.  Timing Attacks
     7.3.  Caching of Sensitive Information
   8.  IANA Considerations
     8.1.  Field Name Registration
     8.2.  Cache Directive Registration
     8.3.  Warn Code Registry
   9.  References
     9.1.  Normative References
     9.2.  Informative References
   Appendix A.  Collected ABNF
   Appendix B.  Changes from RFC 7234
   Acknowledgements
   Index
   Authors' Addresses

1. Introduction โœจ

HTTP is a stateless application-level request/response protocol with extensible semantics and self-descriptive messages, so it can handle flexible interaction with network-based hypertext information systems. ๐Ÿ’– It's typically used for distributed information systems, where response caches can seriously boost performance. This document covers the HTTP stuff related to caching and reusing response messages. ๐Ÿงš

An HTTP "cache" is a local store of response messages, plus the subsystem that handles storing, retrieving, and deleting those messages. A cache stores cacheable responses so it can cut down response time and network bandwidth use for future equivalent requests. ๐Ÿฆ‹ Any client or server MAY use a cache, though not when acting as a tunnel (Section 3.7 of [HTTP]).

A "shared cache" stores responses so more than one user can reuse them. Shared caches are usually (but not always) deployed as part of an intermediary. A "private cache", on the other hand, is dedicated to a single user, and it's often deployed as a component of a user agent. ๐ŸŒˆ

The whole point of HTTP caching is to seriously improve performance by reusing a prior response to satisfy a current request. ๐Ÿ”ฅ A cache considers a stored response "fresh", as defined in Section 4.2, if it can be reused without "validation" (checking with the origin server to see whether the cached response is still valid for this request). A fresh response can reduce both latency and network overhead every time the cache reuses it. ๐Ÿ’… When a cached response isn't fresh, it might still be reusable if validation can freshen it (Section 4.3), or if the origin is unavailable (Section 4.2.4).

This document obsoletes RFC 7234, and Appendix B summarizes the changes. ๐Ÿ‘‘

1.1. Requirements Notation ๐Ÿ“

The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in BCP 14 [RFC2119] [RFC8174] when, and only when, they appear in all capitals, as shown here. ๐ŸŽ€

Section 2 of [HTTP] defines conformance criteria and has considerations about error handling. ๐Ÿซถ

1.2. Syntax Notation ๐Ÿงฉ

This spec uses the Augmented Backus-Naur Form (ABNF) notation of [RFC5234], extended with the notation for case-sensitivity in strings defined in [RFC7405]. ๐Ÿค“

It also uses a list extension, defined in Section 5.6.1 of [HTTP]. That extension lets you define comma-separated lists compactly with a "#" operator, kind of like how the "*" operator shows repetition. Appendix A shows the collected grammar with every list operator expanded into standard ABNF notation. ๐Ÿ“

1.2.1. Imported Rules ๐Ÿ“ฆ

The following core rule is included by reference, as defined in [RFC5234], Appendix B.1: DIGIT (decimal 0-9).

[HTTP] defines the following rules:

  HTTP-date     = <HTTP-date, see [HTTP], Section 5.6.7>
  OWS           = <OWS, see [HTTP], Section 5.6.3>
  field-name    = <field-name, see [HTTP], Section 5.1>
  quoted-string = <quoted-string, see [HTTP], Section 5.6.4>
  token         = <token, see [HTTP], Section 5.6.2>

1.2.2. Delta Seconds โฑ๏ธ

The delta-seconds rule is a non-negative integer that represents time in seconds. ๐Ÿ’œ

  delta-seconds  = 1*DIGIT

A recipient that parses a delta-seconds value and converts it to binary form ought to use an arithmetic type with at least 31 bits of non-negative integer range. ๐Ÿงฎ If a cache receives a delta-seconds value bigger than the greatest integer it can represent, or if any of its later calculations overflow, the cache MUST consider the value to be 2147483648 (2^31) or the greatest positive integer it can conveniently represent. ๐Ÿšจ

Note: The value 2147483648 is here for historical reasons. It represents infinity (over 68 years), and it does not need to be stored in binary form. An implementation could produce it as a string if any overflow occurs, even if the calculations are performed with an arithmetic type that can't directly represent that number. What matters here is that an overflow is detected and not treated as a negative value in later calculations. ๐ŸŒธ

2. Overview of Cache Operation โœจ

Okay so proper cache operation keeps the semantics of HTTP transfers intact while cutting down on sending info the cache already has ๐Ÿงš. For the general terminology and core HTTP concepts, peep Section 3 of [HTTP] ๐Ÿ’–.

Caching is totally OPTIONAL in HTTP ๐Ÿซถ, but it's fair to assume that reusing a cached response is desirable and is the default unless a requirement or local configuration says otherwise ๐Ÿ“. So the HTTP cache rules are mostly about stopping a cache from storing something it shouldn't, or reusing something in the wrong way ๐Ÿ’…. They're not about forcing caches to always store and reuse particular responses.

The "cache key" is the info a cache uses to pick a response ๐Ÿ”‘. It's built from, at a minimum, the request method and target URI used to get the stored response. The method decides under which circumstances that response can answer a later request ๐Ÿฆ‹. Many caches in common use today only cache GET responses, so they only use the URI as the key ๐Ÿ˜ญ.

A cache might store multiple responses for a request target that does content negotiation. It tells them apart by putting some of the original request's header fields into the cache key too, using info from the Vary response header field, as per Section 4.1 โœจ.

Caches might add extra stuff to the cache key ๐Ÿ‘€. For example, user agent caches might include the referring site's identity, "double keying" the cache to avoid some privacy risks (see Section 7.2) ๐Ÿ™ˆ.

Most commonly, caches store the successful result of a retrieval request: a 200 (OK) response to a GET request, which contains a representation of the target resource (Section 9.3.1 of [HTTP]) ๐Ÿ’œ. But it's also possible to store redirects, negative results (e.g., 404 (Not Found)), incomplete results (e.g., 206 (Partial Content)), and responses to methods other than GET ๐ŸŒˆ. That last one only works if the method's definition allows that kind of caching and defines something suitable to use as a cache key.

A cache is "disconnected" when it can't contact the origin server or otherwise find a forward path for a request ๐Ÿ“ต. A disconnected cache can serve stale responses in some circumstances (Section 4.2.4) ๐Ÿฅ€.


3. Storing Responses in Caches ๐Ÿ‘‘

Here's the big rule ๐Ÿ“œ: a cache MUST NOT store a response to a request unless all of the following are true:

  • the request method is understood by the cache;
  • the response status code is final (see Section 15 of [HTTP]);
  • if the response status code is 206 or 304, or the must-understand cache directive (see Section 5.2.2.3) is present: the cache understands the response status code;
  • the no-store cache directive is not present in the response (see Section 5.2.2.5);
  • if the cache is shared: the private response directive is either not present or allows a shared cache to store a modified response; see Section 5.2.2.7);
  • if the cache is shared: the Authorization header field is not present in the request (see Section 11.6.2 of [HTTP]) or a response directive is present that explicitly allows shared caching (see Section 3.5); and
  • the response contains at least one of the following:
    • a public response directive (see Section 5.2.2.9);
    • a private response directive, if the cache is not shared (see Section 5.2.2.7);
    • an Expires header field (see Section 5.3);
    • a max-age response directive (see Section 5.2.2.1);
    • if the cache is shared: an s-maxage response directive (see Section 5.2.2.10);
    • a cache extension that allows it to be cached (see Section 5.2.3); or
    • a status code that is defined as heuristically cacheable (see Section 4.2.2).

Note that a cache extension can override any of the requirements listed; see Section 5.2.3 ๐Ÿ”“.

In this context, a cache has "understood" a request method or a response status code if it recognizes it and implements all specified caching-related behavior ๐Ÿง .

Note that, in normal operation, some caches won't store a response that has neither a cache validator nor an explicit expiration time, since those responses usually aren't useful to store ๐Ÿคทโ€โ™€๏ธ. Caches aren't prohibited from storing such responses, though ๐Ÿซ .

3.1. Storing Header and Trailer Fields ๐ŸŽ€

Caches MUST include all received response header fields, including unrecognized ones, when storing a response ๐Ÿ’Œ. This is what lets new HTTP header fields get deployed successfully. However, there are some exceptions:

  • The Connection header field and fields whose names are listed in it are required by Section 7.6.1 of [HTTP] to be removed before forwarding the message. This MAY be implemented by doing so before storage.
  • Likewise, some fields' semantics require them to be removed before forwarding the message, and this MAY be implemented by doing so before storage; see Section 7.6.1 of [HTTP] for some examples.
  • The no-cache (Section 5.2.2.4) and private (Section 5.2.2.7) cache directives can have arguments that prevent storage of header fields by all caches and shared caches, respectively.
  • Header fields that are specific to the proxy a cache uses when forwarding a request MUST NOT be stored, unless the cache incorporates the identity of the proxy into the cache key. Effectively, this is limited to Proxy-Authenticate (Section 11.7.1 of [HTTP]), Proxy-Authentication-Info (Section 11.7.3 of [HTTP]), and Proxy-Authorization (Section 11.7.2 of [HTTP]) ๐Ÿ”’.

Caches MAY either store trailer fields separate from header fields or discard them. Caches MUST NOT combine trailer fields with header fields ๐Ÿšซ.

3.2. Updating Stored Header Fields ๐Ÿ”„

Caches are required to update a stored response's header fields from another (typically newer) response in several situations. For example, see Sections 3.4, 4.3.4, and 4.3.5 โœจ.

When doing so, the cache MUST add each header field in the provided response to the stored response, replacing field values that are already present. There are some exceptions to this:

  • Header fields excepted from storage in Section 3.1,
  • Header fields that the cache's stored response depends upon, as described below,
  • Header fields that are automatically processed and removed by the recipient, as described below, and
  • The Content-Length header field.

Here's the tricky bit ๐Ÿ˜ฌ. In some cases, caches (especially in user agents) store the results of processing the received response, rather than the response itself. Updating header fields that affect that processing can lead to inconsistent behavior and security issues. Caches in this situation MAY omit these header fields from updating stored responses on an exceptional basis, but SHOULD limit that omission to the fields necessary to assure integrity of the stored response ๐Ÿ›ก๏ธ.

For example, a browser might decode the content coding of a response while it's being received. That creates a disconnect between the data it has stored and the response's original metadata. Updating that stored metadata with a different Content-Encoding header field would be problematic ๐Ÿ™ƒ. Likewise, a browser might store a post-parse HTML tree rather than the content received in the response. Updating the Content-Type header field wouldn't be workable in this case, because any assumptions about the format made in parsing would now be invalid ๐Ÿ’€.

Furthermore, some fields are automatically processed and removed by the HTTP implementation, such as the Content-Range header field. Implementations MAY automatically omit such header fields from updates, even when the processing doesn't actually occur ๐Ÿค–.

Note that the Content-* prefix is not a signal that a header field is omitted from update. It's a convention for MIME header fields, not HTTP ๐Ÿ“Ž.

3.3. Storing Incomplete Responses ๐Ÿงฉ

If the request method is GET, the response status code is 200 (OK), and the entire response header section has been received, a cache MAY store a response that is not complete (Section 6.1 of [HTTP]), as long as the stored response is recorded as being incomplete. Likewise, a 206 (Partial Content) response MAY be stored as if it were an incomplete 200 (OK) response. But here's the catch: a cache MUST NOT store incomplete or partial-content responses if it doesn't support the Range and Content-Range header fields, or if it doesn't understand the range units used in those fields ๐Ÿšง.

A cache MAY complete a stored incomplete response by making a subsequent range request (Section 14.2 of [HTTP]) and combining the successful response with the stored response, as defined in Section 3.4 ๐Ÿงต. A cache MUST NOT use an incomplete response to answer requests unless the response has been made complete, or the request is partial and specifies a range wholly within the incomplete response ๐Ÿ™…โ€โ™€๏ธ. And a cache MUST NOT send a partial response to a client without explicitly marking it using the 206 (Partial Content) status code ๐Ÿท๏ธ.

3.4. Combining Partial Content ๐Ÿงฒ

A response might transfer only a partial representation if the connection closed prematurely, or if the request used one or more Range specifiers (Section 14.2 of [HTTP]). After several of those transfers, a cache might have received several ranges of the same representation ๐Ÿฐ. A cache MAY combine these ranges into a single stored response and reuse that response to satisfy later requests, but only if they all share the same strong validator and the cache complies with the client requirements in Section 15.3.7.3 of [HTTP] ๐Ÿ’ž.

When combining the new response with one or more stored responses, a cache MUST update the stored response header fields using the header fields provided in the new response, as per Section 3.2 ๐Ÿ“.

3.5. Storing Responses to Authenticated Requests ๐Ÿ”

A shared cache MUST NOT use a cached response to a request with an Authorization header field (Section 11.6.2 of [HTTP]) to satisfy any subsequent request, unless the response contains a Cache-Control field with a response directive (Section 5.2.2) that allows it to be stored by a shared cache. The cache also has to conform to the requirements of that directive for that response ๐Ÿซฃ.

In this specification, the following response directives have such an effect: must-revalidate (Section 5.2.2.2), public (Section 5.2.2.9), and s-maxage (Section 5.2.2.10) ๐Ÿ’œ.

4. Constructing Responses from Caches โœจ

okay bestie, this is the moment the cache actually answers a request ๐Ÿ’… When a request comes in, a cache MUST NOT reuse a stored response unless ALL of these vibes check out:

  • the presented target URI (Section 7.1 of [HTTP]) and that of the stored response match, and
  • the request method associated with the stored response allows it to be used for the presented request, and
  • request header fields nominated by the stored response (if any) match those presented (see Section 4.1), and
  • the stored response does not contain the no-cache directive (Section 5.2.2.4), unless it is successfully validated (Section 4.3), and
  • the stored response is one of the following:
    • fresh (see Section 4.2), or
    • allowed to be served stale (see Section 4.2.4), or
    • successfully validated (see Section 4.3).

Note that a cache extension can override any of the requirements listed; see Section 5.2.3. ๐ŸŽ€

When a stored response is used to satisfy a request without validation, a cache MUST generate an Age header field (Section 5.1), replacing any present in the response with a value equal to the stored response's current_age; see Section 4.2.3. ๐Ÿ•ฐ๏ธ

A cache MUST write through requests with methods that are unsafe (Section 9.2.1 of [HTTP]) to the origin server. So, no faking a reply to those ๐Ÿ™…โ€โ™€๏ธ it has to forward the request and get a corresponding response back first, then it can answer.

Also, note that unsafe requests might invalidate already-stored responses; see Section 4.4. ๐Ÿ’€

A cache can use a response that is stored or storable to satisfy multiple requests, provided that it is allowed to reuse that response for the requests in question. This is the "collapse requests" trick ๐Ÿงš where the cache combines multiple incoming requests into a single forward request upon a cache miss, which cuts down load on the origin server and the network. Note, however, that if the cache cannot use the returned response for some or all of the collapsed requests, it will need to forward those requests to satisfy them, potentially adding some latency.

When more than one suitable response is stored, a cache MUST use the most recent one (as determined by the Date header field). It can also forward the request with Cache-Control: max-age=0 or Cache-Control: no-cache to disambiguate which response to use. ๐Ÿ“

A cache without a clock (Section 5.6.7 of [HTTP]) MUST revalidate stored responses upon every use. No clock, no trust, bestie ๐Ÿซถ


4.1. Calculating Cache Keys with the Vary Header Field

When a cache receives a request that can be satisfied by a stored response, and that stored response contains a Vary header field (Section 12.5.5 of [HTTP]), the cache MUST NOT use that stored response without revalidation unless all the presented request header fields nominated by that Vary field value match those fields in the original request (i.e., the request that caused the cached response to be stored). ๐Ÿ”

The header fields from two requests are defined to match if and only if those in the first request can be transformed to those in the second request by applying any of the following:

  • adding or removing whitespace, where allowed in the header field's syntax
  • combining multiple header field lines with the same field name (see Section 5.2 of [HTTP])
  • normalizing both header field values in a way that is known to have identical semantics, according to the header field's specification (e.g., reordering field values when order is not significant; case-normalization, where values are defined to be case-insensitive)

If (after any normalization that might take place) a header field is absent from a request, it can only match another request if it is also absent there. ๐Ÿ‘ป

A stored response with a Vary header field value containing a member "*" always fails to match. Literally never a match, no exceptions ๐Ÿ™…โ€โ™€๏ธ

If multiple stored responses match, the cache will need to choose one to use. When a nominated request header field has a known mechanism for ranking preference (e.g., qvalues on Accept and similar request header fields), that mechanism MAY be used to choose a preferred response. If such a mechanism is not available, or leads to equally preferred responses, the most recent response (as determined by the Date header field) is chosen, as per Section 4. ๐ŸŒˆ

Some resources mistakenly omit the Vary header field from their default response (i.e., the one sent when the request does not express any preferences). That's a whole mess ๐Ÿ˜ญ because it gets chosen for later requests to that resource even when more preferable responses are available. When a cache has multiple stored responses for a target URI and one or more omits the Vary header field, the cache SHOULD choose the most recent (see Section 4.2.3) stored response with a valid Vary field value. ๐Ÿง 

If no stored response matches, the cache cannot satisfy the presented request. Typically, the request is forwarded to the origin server, potentially with preconditions added to describe what responses the cache has already stored (Section 4.3). ๐Ÿ“จ


4.2. Freshness

A "fresh" response is one whose age has not yet exceeded its freshness lifetime. Conversely, a "stale" response is one where it has. โณ

A response's "freshness lifetime" is the length of time between its generation by the origin server and its expiration time. An "explicit expiration time" is the time at which the origin server intends that a stored response can no longer be used by a cache without further validation, whereas a "heuristic expiration time" is assigned by a cache when no explicit expiration time is available. ๐Ÿฆ‹

A response's "age" is the time that has passed since it was generated by, or successfully validated with, the origin server. ๐Ÿ•ฐ๏ธ

When a response is fresh, it can be used to satisfy subsequent requests without contacting the origin server, which makes everything way more efficient โœจ

The primary mechanism for determining freshness is for an origin server to provide an explicit expiration time in the future, using either the Expires header field (Section 5.3) or the max-age response directive (Section 5.2.2.1). Generally, origin servers will assign future explicit expiration times to responses because they believe the representation is not likely to change in a semantically significant way before the expiration time is reached. ๐Ÿ’œ

If an origin server wishes to force a cache to validate every request, it can assign an explicit expiration time in the past to indicate that the response is already stale. Compliant caches will normally validate a stale cached response before reusing it for subsequent requests (see Section 4.2.4). Ooh, the dramatic "I'm already expired" move ๐Ÿ’€

Since origin servers do not always provide explicit expiration times, caches are also allowed to use a heuristic to determine an expiration time under certain circumstances (see Section 4.2.2). ๐ŸŒธ

The calculation to determine if a response is fresh is:

response_is_fresh = (freshness_lifetime > current_age)

freshness_lifetime is defined in Section 4.2.1; current_age is defined in Section 4.2.3. ๐Ÿงฎ

Clients can send the max-age or min-fresh request directives (Section 5.2.1) to suggest limits on the freshness calculations for the corresponding response. However, caches are not required to honor them. Suggestions, not orders ๐Ÿ™ˆ

When calculating freshness, to avoid common problems in date parsing:

  • Although all date formats are specified to be case-sensitive, a cache recipient SHOULD match the field value case-insensitively.
  • If a cache recipient's internal implementation of time has less resolution than the value of an HTTP-date, the recipient MUST internally represent a parsed Expires date as the nearest time equal to or earlier than the received value.
  • A cache recipient MUST NOT allow local time zones to influence the calculation or comparison of an age or expiration time. Your timezone is not invited to this party ๐ŸŒ
  • A cache recipient SHOULD consider a date with a zone abbreviation other than "GMT" to be invalid for calculating expiration. ๐Ÿ™…โ€โ™€๏ธ

Note that freshness applies only to cache operation; it cannot be used to force a user agent to refresh its display or reload a resource. See Section 6 for an explanation of the difference between caches and history mechanisms. So the back button is its own thing, periodt ๐ŸŽ€


4.2.1. Calculating Freshness Lifetime

A cache can calculate the freshness lifetime (denoted as freshness_lifetime) of a response by evaluating the following rules and using the first match:

  • If the cache is shared and the s-maxage response directive (Section 5.2.2.10) is present, use its value, or
  • If the max-age response directive (Section 5.2.2.1) is present, use its value, or
  • If the Expires response header field (Section 5.3) is present, use its value minus the value of the Date response header field (using the time the message was received if it is not present, as per Section 6.6.1 of [HTTP]), or
  • Otherwise, no explicit expiration time is present in the response. A heuristic freshness lifetime might be applicable; see Section 4.2.2.

Note that this calculation is intended to reduce clock skew by using the clock information provided by the origin server whenever possible. It's all about trusting the origin's clock first ๐Ÿ•ฐ๏ธโœจ

When there is more than one value present for a given directive (e.g., two Expires header field lines or multiple Cache-Control: max-age directives), either the first occurrence should be used or the response should be considered stale. If directives conflict (e.g., both max-age and no-cache are present), the most restrictive directive should be honored. Caches are encouraged to consider responses that have invalid freshness information (e.g., a max-age directive with non-integer content) to be stale. Messy headers get the side-eye ๐Ÿ‘€๐Ÿ’…


4.2.2. Calculating Heuristic Freshness

Since origin servers do not always provide explicit expiration times, a cache MAY assign a heuristic expiration time when an explicit time is not specified, employing algorithms that use other field values (such as the Last-Modified time) to estimate a plausible expiration time. This specification does not provide specific algorithms, but it does impose worst-case constraints on their results. ๐Ÿ”ฎ

A cache MUST NOT use heuristics to determine freshness when an explicit expiration time is present in the stored response. Because of the requirements in Section 3, heuristics can only be used on responses without explicit freshness whose status codes are defined as "heuristically cacheable" (e.g., see Section 15.1 of [HTTP]) and on responses without explicit freshness that have been marked as explicitly cacheable (e.g., with a public response directive). Explicit time beats a guess, every single time ๐Ÿ™…โ€โ™€๏ธ

Note that in previous specifications, heuristically cacheable response status codes were called "cacheable by default". Old vocab, new vibes ๐Ÿงƒ

If the response has a Last-Modified header field (Section 8.8.2 of [HTTP]), caches are encouraged to use a heuristic expiration value that is no more than some fraction of the interval since that time. A typical setting of this fraction might be 10%. ๐Ÿฐ

Note: A previous version of the HTTP specification (Section 13.9 of [RFC2616]) prohibited caches from calculating heuristic freshness for URIs with query components (i.e., those containing "?"). In practice, this has not been widely implemented. Therefore, origin servers are encouraged to send explicit directives (e.g., Cache-Control: no-cache) if they wish to prevent caching. โœจ Basically, if you don't want it cached, just say so, bestie ๐Ÿซถ

4.2.3. Calculating Age ๐Ÿ•ฐ๏ธ

Okay so, the Age header field is how a cache tells you how old a response is feeling when it reaches you ๐Ÿง It carries the cache's best guess of how many seconds have passed since the origin server generated or validated the response โœจ The Age value is basically all the time the response spent chilling in each cache along the path from the origin server, plus the time it spent traveling over the network ๐Ÿ›ค๏ธ

Here's the data the age math runs on ๐Ÿ’…

  • "age_value": the value of the Age header field (Section 5.1), in a form you can do arithmetic with. If it's not available, it's 0.
  • "date_value": the value of the Date header field, in a form you can do arithmetic with. Section 6.6.1 of [HTTP] has the definition of the Date header field and the rules for responses that don't have one ๐Ÿ™ˆ
  • "now": the current value of this implementation's clock (Section 5.6.7 of [HTTP]) โฐ
  • "request_time": the value of the clock when the request that led to the stored response was made ๐Ÿš€
  • "response_time": the value of the clock when the response was received ๐Ÿ“ฌ

Now, a response's age can be worked out in two totally independent ways, and it's giving math nerd ๐Ÿงฎ

  1. the "apparent_age": response_time minus date_value, but only if the implementation's clock is reasonably well synchronized with the origin server's clock. If the result is negative, it gets replaced by zero.
  2. the "corrected_age_value", but only if all of the caches along the response path implement HTTP/1.1 or greater. A cache MUST interpret this value relative to the time the request was initiated, not the time the response was received ๐ŸŽฏ
apparent_age = max(0, response_time - date_value);

response_delay = response_time - request_time;
corrected_age_value = age_value + response_delay;

The corrected_age_value MAY be used as the corrected_initial_age ๐Ÿ’– Now, for really old cache implementations that might not insert Age correctly, you can calculate corrected_initial_age more conservatively, like this ๐Ÿ‘ต

corrected_initial_age = max(apparent_age, corrected_age_value);

Finally, the current_age of a stored response is found by adding the time (in seconds) since the stored response was last validated by the origin server to the corrected_initial_age ๐ŸŽ€

resident_time = now - response_time;
current_age = corrected_initial_age + resident_time;

4.2.4. Serving Stale Responses ๐Ÿฅ€

A "stale" response is one that either has explicit expiry information or is allowed to have heuristic expiry calculated, but isn't fresh according to the calculations in Section 4.2 ๐Ÿ˜ญ Stale doesn't automatically mean you can serve it, though. There are rules!

A cache MUST NOT generate a stale response if an explicit in-protocol directive prohibits it. Examples are a no-cache response directive, a must-revalidate response directive, or an applicable s-maxage or proxy-revalidate response directive (see Section 5.2.2). Hard no, bestie ๐Ÿšซ

A cache MUST NOT generate a stale response unless it is disconnected, or doing so is explicitly permitted by the client or origin server. That permission can come from the max-stale request directive in Section 5.2.1, extension directives such as the ones defined in [RFC5861], or configuration in accordance with an out-of-band contract ๐Ÿค


4.3. Validation ๐Ÿ”

When a cache has one or more stored responses for a requested URI but can't serve any of them (e.g., because they aren't fresh, or because one can't be chosen; see Section 4.1), it can use the conditional request mechanism (Section 13 of [HTTP]) in the forwarded request ๐Ÿช„ This gives the next inbound server a chance to pick a valid stored response to use and update the stored metadata in the process, or to replace the stored response(s) with a brand new response. This whole process is called "validating" or "revalidating" the stored response ๐Ÿ’œ

4.3.1. Sending a Validation Request ๐Ÿ“ฎ

When a cache makes a conditional request for validation, it either starts with a request it's trying to satisfy or, if it's kicking off the request on its own, it builds one from a stored response. To do that, it copies the method, the target URI, and the request header fields identified by the Vary header field (Section 4.1) ๐Ÿงฉ

Then it updates that request with one or more precondition header fields. These carry validator metadata from the stored response(s) that have the same URI. Usually that's only the stored response(s) with the same cache key, although a cache is allowed to validate a response it can't choose with the request header fields it's sending (see Section 4.1) ๐Ÿซถ

Recipients then compare those precondition header fields to figure out whether any stored response is equivalent to a current representation of the resource ๐Ÿ‘€

One validator is the timestamp in a Last-Modified header field (Section 8.8.2 of [HTTP]). It can be used in an If-Modified-Since header field for response validation, or in an If-Unmodified-Since or If-Range header field for representation selection. In that second case, the client is talking specifically about a previously obtained representation with that timestamp ๐Ÿ•’

Another validator is the entity tag in an ETag field (Section 8.8.3 of [HTTP]). One or more entity tags, each pointing at a stored response, can be used in an If-None-Match header field for response validation. They can also go in an If-Match or If-Range header field for representation selection, which means the client is referring specifically to one or more previously obtained representations with those entity tags ๐Ÿท๏ธ

When a cache generates a conditional request for validation, it:

  • MUST send the relevant entity tags (using If-Match, If-None-Match, or If-Range) if the entity tags were provided in the stored response(s) being validated.
  • SHOULD send the Last-Modified value (using If-Modified-Since) if the request is not for a subrange, a single stored response is being validated, and that response contains a Last-Modified value.
  • MAY send the Last-Modified value (using If-Unmodified-Since or If-Range) if the request is for a subrange, a single stored response is being validated, and that response contains only a Last-Modified value (not an entity tag).

In most cases, both validators get generated in cache validation requests, even when entity tags are clearly superior ๐ŸŒˆ The reason is so that older intermediaries that don't understand entity tag preconditions can still respond appropriately ๐Ÿ‘ด

4.3.2. Handling a Received Validation Request ๐Ÿ“ฅ

Every client in the request chain can have its own cache, so it's super common for a cache at an intermediary to get conditional requests from other (outbound) caches ๐Ÿ” Some user agents also use conditional requests to limit data transfers to recently modified representations, or to finish completing the transfer of a partially retrieved representation ๐Ÿงƒ

If a cache receives a request that can be satisfied by reusing a stored 200 (OK) or 206 (Partial Content) response, as per Section 4, the cache SHOULD evaluate any applicable conditional header field preconditions received in that request against the matching validators in the stored response ๐ŸŽฏ

A cache MUST NOT evaluate conditional header fields that only apply to an origin server, occur in a request whose semantics can't be satisfied with a cached response, or occur in a request for a target resource it has no stored responses for. Those preconditions are likely meant for some other (inbound) server ๐Ÿ™…โ€โ™€๏ธ

How a cache evaluates conditional requests depends on the precondition header fields it received and their precedence ๐Ÿ’… In short, the If-Match and If-Unmodified-Since conditional header fields aren't applicable to a cache, and If-None-Match takes precedence over If-Modified-Since. For the full precedence spec, check Section 13.2.2 of [HTTP] ๐Ÿ“–

A request containing an If-None-Match header field (Section 13.1.2 of [HTTP]) means the client wants to validate one or more of its own stored responses against the stored response the cache chose (as per Section 4) ๐Ÿง

If there's no If-None-Match header field, a request containing an If-Modified-Since header field (Section 13.1.3 of [HTTP]) means the client wants to validate one or more of its own stored responses by modification date ๐Ÿ“…

If a request contains an If-Modified-Since header field and the stored response has no Last-Modified header field, the cache SHOULD use the stored response's Date field value to evaluate the conditional. If there's no Date field either, it uses the time the stored response was received instead โณ

A cache that implements partial responses to range requests, as defined in Section 14.2 of [HTTP], also needs to evaluate a received If-Range header field (Section 13.1.5 of [HTTP]) against its chosen response ๐Ÿงฉ

When a cache decides to forward a request to revalidate its own stored responses for a request that contains an If-None-Match list of entity tags, the cache MAY combine the received list with a list of entity tags from its own stored set of responses (fresh or stale). It can then send the union of the two lists as a replacement If-None-Match header field value in the forwarded request ๐Ÿ”— If a stored response contains only partial content, the cache MUST NOT include its entity tag in the union, unless the request is for a range that would be fully satisfied by that partial stored response ๐Ÿงฑ

If the response to the forwarded request is 304 (Not Modified) and has an ETag field value with an entity tag that isn't in the client's list, the cache MUST generate a 200 (OK) response for the client. It does this by reusing its corresponding stored response, as updated by the 304 response metadata (Section 4.3.4) โœจ

4.3.3. Handling a Validation Response ๐Ÿงพ

How a cache handles a response to a conditional request depends on its status code ๐Ÿ’–

  • A 304 (Not Modified) response status code means the stored response can be updated and reused. See Section 4.3.4 ๐ŸŽ‰
  • A full response (i.e., one containing content) means none of the stored responses nominated in the conditional request are suitable. Instead, the cache MUST use the full response to satisfy the request. The cache MAY store such a full response, subject to its constraints (see Section 3) ๐Ÿ“ฆ
  • However, if a cache receives a 5xx (Server Error) response while trying to validate a response, it can either forward that response to the requesting client, or act as if the server failed to respond. In the latter case, the cache can send a previously stored response, subject to its constraints on doing so (see Section 4.2.4), or it can retry the validation request ๐Ÿ”„

4.3.4. Freshening Stored Responses upon Validation โœจ

Okay so bestie, when a cache gets a 304 (Not Modified) response ๐ŸŒธ, it has to figure out which stored responses can take the new info, and then actually update them ๐Ÿ’….

The starting pool is the stored responses that could've been picked for that request. Basically, they meet the requirements in Section 4, except the very last one, which is about being fresh, OK to serve stale, or just validated. ๐Ÿงš

Then the pool gets narrowed down by the first match of these:

  • If the new response has one or more "strong validators" (see Section 8.8.1 of [HTTP]), each of those strong validators points to a selected representation for update. Every stored response in the starting pool with one of those same strong validators gets picked for update. If none of the pool has at least one of those same strong validators, then the cache MUST NOT use the new response to update any stored responses. ๐Ÿ˜ญ
  • If the new response has no strong validators but does have one or more "weak validators", and those match one of the pool's stored responses, then the most recent of those matching stored responses gets picked for update. ๐Ÿฆ‹
  • If the new response doesn't include any form of validator (like when a client makes an If-Modified-Since request from somewhere other than the Last-Modified response header field), there's only one stored response in the pool, and that one also has no validator, then that stored response gets picked for update. ๐Ÿ™ˆ

For each stored response that gets picked, the cache MUST update its header fields with the header fields in the 304 (Not Modified) response, as per Section 3.2. ๐ŸŽ€


4.3.5. Freshening Responses with HEAD ๐Ÿ”ฎ

A response to the HEAD method is identical to what an equivalent GET request would've gotten, just without the content. Ngl this is so useful ๐ŸคŒ. You can use that to invalidate or update a cached GET response when the more efficient conditional GET mechanism isn't available (because the stored response has no validators), or when you just don't want to transfer the content even if it changed. ๐Ÿ’œ

When a cache makes an inbound HEAD request for a target URI and gets a 200 (OK) response, the cache SHOULD update or invalidate each of its stored GET responses that could've been chosen for that request (see Section 4.1). ๐ŸŒˆ

For each of the stored responses that could've been chosen, if the stored response and the HEAD response have matching values for any received validator fields (ETag and Last-Modified), and if the HEAD response has a Content-Length header field, the value of Content-Length matches the stored response's, then the cache SHOULD update the stored response as described below. Otherwise, the cache SHOULD consider the stored response to be stale. ๐Ÿ“

If a cache updates a stored response with the metadata from a HEAD response, the cache MUST use the header fields in the HEAD response to update the stored response (see Section 3.2). โœจ


4.4. Invalidating Stored Responses ๐Ÿงน

Unsafe request methods (Section 9.2.1 of [HTTP]), like PUT, POST, or DELETE, can change state on the origin server ๐Ÿ‘€. So intervening caches are required to invalidate stored responses to keep their contents up to date. That's the cache being a good friend ๐Ÿซถ.

A cache MUST invalidate the target URI (Section 7.1 of [HTTP]) when it receives a non-error status code in response to an unsafe request method (including methods whose safety is unknown). No skipping this one, periodt ๐Ÿ’….

A cache MAY invalidate other URIs when it receives a non-error status code in response to an unsafe request method (including methods whose safety is unknown). Specifically, the URI(s) in the Location and Content-Location response header fields (if present) are candidates for invalidation. Other URIs might get discovered through mechanisms that aren't specified in this document. However, a cache MUST NOT trigger an invalidation under these conditions if the origin (Section 4.3.1 of [HTTP]) of the URI to be invalidated is different from the origin of the target URI (Section 7.1 of [HTTP]). This helps prevent denial-of-service attacks ๐Ÿ›ก๏ธ. Don't let anyone weaponize your cache, bestie.

"Invalidate" means the cache will either remove all stored responses whose target URI matches the given URI, or mark them as "invalid" and in need of a mandatory validation before they can be sent in response to a later request. ๐Ÿ—‘๏ธ

A "non-error response" is one with a 2xx (Successful) or 3xx (Redirection) status code. โœ…

Note that this doesn't guarantee all the appropriate responses are invalidated globally ๐Ÿ˜ฌ. A state-changing request only invalidates responses in the caches it actually travels through. Main character energy, but only locally ๐ŸŒ.


5. Field Definitions ๐Ÿ’…

This section defines the syntax and semantics of HTTP fields related to caching. Let's get into it ๐Ÿง.

5.1. Age โณ

The "Age" response header field conveys the sender's estimate of the time since the response was generated or successfully validated at the origin server. Age values are calculated as specified in Section 4.2.3. ๐Ÿ•ฐ๏ธ

Age = delta-seconds

The Age field value is a non-negative integer, representing time in seconds (see Section 1.2.2). ๐Ÿ”ข

Although it's defined as a singleton header field, a cache that runs into a message with a list-based Age field value SHOULD use the first member of the field value and discard the rest. ๐Ÿซ 

If the field value (after discarding additional members, as above) is invalid (for example, it contains something other than a non-negative integer), a cache SHOULD ignore the field. ๐Ÿ™ƒ

The presence of an Age header field implies the response was not generated or validated by the origin server for this request. However, the lack of an Age header field does not imply the origin was contacted. Plot twist, I know ๐Ÿ˜ฑ.

5.2. Cache-Control ๐ŸŽ›๏ธ

The "Cache-Control" header field is used to list directives for caches along the request/response chain. Cache directives are unidirectional, so the presence of a directive in a request doesn't mean that same directive is present or copied in the response. One-way street, babe ๐Ÿ›ฃ๏ธ.

See Section 5.2.3 for info about how Cache-Control directives defined elsewhere are handled. ๐Ÿ“š

A proxy, whether or not it implements a cache, MUST pass cache directives through in forwarded messages, regardless of their significance to that application, since the directives might apply to all recipients along the request/response chain. It's not possible to target a directive to a specific cache, so don't even try ๐Ÿ™…โ€โ™€๏ธ.

Cache directives are identified by a token, compared case-insensitively, and have an optional argument that can use both token and quoted-string syntax. For the directives defined below that define arguments, recipients ought to accept both forms, even if a specific form is required for generation. Be generous with what you accept ๐Ÿซถ.

Cache-Control   = #cache-directive

cache-directive = token [ "=" ( token / quoted-string ) ]

For the cache directives defined below, no argument is defined (nor allowed) unless stated otherwise. No extra args, no drama ๐Ÿ’–.

5.2.1. Request Directives ๐Ÿ“จ

This section defines cache request directives. They're advisory: caches MAY implement them, but they're not required to. Think of them as vibes, not rules ๐ŸŒท.

5.2.1.1. max-age โŒ›

Argument syntax:

delta-seconds (see Section 1.2.2)

The max-age request directive indicates the client prefers a response whose age is less than or equal to the specified number of seconds. Unless the max-stale request directive is also present, the client does not wish to receive a stale response. Fresh or nothing, bestie ๐Ÿ‘.

This directive uses the token form of the argument syntax: e.g., 'max-age=5' not 'max-age="5"'. A sender MUST NOT generate the quoted-string form. ๐Ÿšซ

5.2.1.2. max-stale ๐Ÿฅ€

Argument syntax:

delta-seconds (see Section 1.2.2)

The max-stale request directive indicates the client will accept a response that has exceeded its freshness lifetime. If a value is present, the client is willing to accept a response that has exceeded its freshness lifetime by no more than the specified number of seconds. If no value is assigned to max-stale, the client will accept a stale response of any age. Living on the wild side ๐Ÿ˜ˆ.

This directive uses the token form of the argument syntax: e.g., 'max-stale=10' not 'max-stale="10"'. A sender MUST NOT generate the quoted-string form. ๐Ÿšซ

5.2.1.3. min-fresh ๐ŸŒฑ

Argument syntax:

delta-seconds (see Section 1.2.2)

The min-fresh request directive indicates the client prefers a response whose freshness lifetime is no less than its current age plus the specified time in seconds. In other words, the client wants a response that will still be fresh for at least that many seconds. Future-proof energy ๐Ÿ”ญ.

This directive uses the token form of the argument syntax: e.g., 'min-fresh=20' not 'min-fresh="20"'. A sender MUST NOT generate the quoted-string form. ๐Ÿšซ

5.2.1.4. no-cache ๐Ÿ™…

The no-cache request directive indicates the client prefers a stored response not be used to satisfy the request without successful validation on the origin server. Check with the source first, always ๐Ÿง.

5.2.1.5. no-store ๐Ÿ”’

The no-store request directive indicates that a cache MUST NOT store any part of either this request or any response to it. This directive applies to both private and shared caches. "MUST NOT store" in this context means the cache MUST NOT intentionally store the information in non-volatile storage, and MUST make a best-effort attempt to remove the information from volatile storage as promptly as possible after forwarding it. Delete it, forget it, move on ๐Ÿ—‘๏ธโœจ.

This directive is NOT a reliable or sufficient mechanism for ensuring privacy ๐Ÿšจ. Malicious or compromised caches might not recognize or obey this directive, and communications networks might be vulnerable to eavesdropping. Don't rely on this one alone for your secrets, bestie ๐Ÿ™ˆ.

Note that if a request containing this directive is satisfied from a cache, the no-store request directive doesn't apply to the already stored response. Yes, it's a little confusing, but that's the rule ๐Ÿคทโ€โ™€๏ธ.

5.2.1.6. no-transform ๐ŸŽจ

The no-transform request directive indicates the client is asking intermediaries to avoid transforming the content, as defined in Section 7.7 of [HTTP]. Hands off the content, please ๐Ÿซด.

5.2.1.7. only-if-cached ๐Ÿ“ฆ

The only-if-cached request directive indicates the client only wishes to obtain a stored response. Caches that honor this request directive SHOULD, upon receiving it, respond with either a stored response consistent with the other constraints of the request, or a 504 (Gateway Timeout) status code. Cache or bust, lol ๐Ÿ’€.

5.2.2. Response Directives ๐Ÿ“ฌ

This section defines cache response directives. A cache MUST obey the Cache-Control directives defined in this section. No cap, this part is non-negotiable ๐Ÿ‘‘.

5.2.2.1. max-age ๐Ÿ•

Argument syntax:

delta-seconds (see Section 1.2.2)

The max-age response directive indicates the response is to be considered stale after its age is greater than the specified number of seconds. Stale on schedule, iconic ๐ŸŒธ.

This directive uses the token form of the argument syntax: e.g., 'max-age=5' not 'max-age="5"'. A sender MUST NOT generate the quoted-string form. ๐Ÿ’…

5.2.2.2. must-revalidate โœจ

okay so the must-revalidate response directive is giving "do NOT serve me stale, go check with the origin first" ๐Ÿ’… once the response goes stale, a cache MUST NOT reuse it for another request until the origin has successfully validated it, as defined by Section 4.3. bestie, that's a whole vibe ๐ŸŽ€

this directive is literally necessary for reliable operation of certain protocol features ๐Ÿ”ฅ and in every situation a cache MUST NOT ignore must-revalidate ๐Ÿ’€ like, if a cache is disconnected, it MUST generate an error response instead of reusing the stale response. the generated status code SHOULD be 504 (Gateway Timeout) unless another error status code is more applicable ๐Ÿ™ˆ

servers ought to use must-revalidate if and only if failing to validate could cause incorrect operation, like a silently unexecuted financial transaction ๐Ÿ˜ญ no cap, that's the main character moment for this one ๐Ÿ‘‘

also, must-revalidate permits a shared cache to reuse a response to a request containing an Authorization header field (Section 11.6.2 of [HTTP]), subject to the above requirement on revalidation (Section 3.5) ๐Ÿฆ‹

## 5.2.2.3. must-understand

Wait, I need to fix that heading, it's a level-3 heading and should be used as written. Here it is properly:

5.2.2.3. must-understand ๐Ÿงš

okay so the must-understand response directive limits caching of the response to a cache that understands and conforms to the requirements for that response's status code โœจ it's giving "only the caches that actually know what they're doing get to store this" ๐Ÿ’œ

a response that contains must-understand SHOULD also contain the no-store directive ๐ŸŒธ and when a cache that implements must-understand gets a response with it, the cache SHOULD ignore no-store if it understands and implements the status code's caching requirements ๐Ÿ“ this is lowkey the plot twist of the section ๐Ÿซถ

5.2.2.4. no-cache ๐Ÿ’–

Argument syntax:

#field-name

okay so the unqualified no-cache response directive (no argument) says the response MUST NOT be used to satisfy any other request without forwarding it for validation and getting a successful response back, see Section 4.3 ๐Ÿ™…โ€โ™€๏ธ this lets an origin server stop a cache from serving the response without even contacting it, even caches that have been set up to send stale responses ๐Ÿ˜ญ

the qualified form has an argument listing one or more field names ๐ŸŽ€ and it says a cache MAY use the response for a later request, subject to any other caching restrictions, if the listed header fields are excluded from the later response or that response has been successfully revalidated with the origin server (updating or removing those fields) โœจ so an origin can stop certain header fields from being reused while the rest of the response still gets cached ๐Ÿฆ‹

the field names aren't limited to the header fields defined in this spec ๐ŸŒˆ and field names are case-insensitive ๐Ÿ’…

this directive uses the quoted-string form of the argument syntax ๐Ÿ’œ a sender SHOULD NOT generate the token form, even if quoting looks unnecessary for a single-entry list ๐Ÿ™ˆ

Note: the qualified form is often handled by caches as if an unqualified no-cache was received ๐Ÿ’€ the special handling for the qualified form is not widely implemented ๐Ÿ˜ญ

5.2.2.5. no-store ๐Ÿ”ฅ

okay so the no-store response directive says a cache MUST NOT store any part of either the immediate request or the response, and MUST NOT use the response to satisfy any other request ๐Ÿ™…โ€โ™€๏ธ this applies to both private and shared caches ๐Ÿ’–

"MUST NOT store" here means the cache MUST NOT intentionally store the info in non-volatile storage, and MUST make a best-effort attempt to remove the info from volatile storage as promptly as possible after forwarding it ๐Ÿงš

bestie, this directive is NOT a reliable or sufficient privacy mechanism ๐Ÿ’€ malicious or compromised caches might not recognize or obey it, and communications networks might be vulnerable to eavesdropping ๐Ÿ˜ญ so don't rely on it alone ๐Ÿซถ

note that the must-understand cache directive overrides no-store in certain circumstances, see Section 5.2.2.3 ๐ŸŒธ

5.2.2.6. no-transform ๐Ÿฆ‹

the no-transform response directive says an intermediary (regardless of whether it implements a cache) MUST NOT transform the content, as defined in Section 7.7 of [HTTP] ๐Ÿ’… it's giving "hands off the payload" โœจ

5.2.2.7. private ๐Ÿ’œ

Argument syntax:

#field-name

the unqualified private response directive says a shared cache MUST NOT store the response, because it's intended for a single user ๐ŸŽ€ it also says a private cache MAY store the response, subject to the constraints in Section 3, even if the response wouldn't otherwise be heuristically cacheable by a private cache โœจ

if a qualified private directive is present, with an argument listing one or more field names, then only the listed header fields are limited to a single user ๐ŸŒˆ a shared cache MUST NOT store the listed header fields if they're present in the original response, but MAY store the rest of the response message without those header fields, subject the constraints defined in Section 3 ๐Ÿฆ‹

the field names aren't limited to the header fields defined in this spec, and they're case-insensitive ๐Ÿ’–

this directive uses the quoted-string form of the argument syntax ๐Ÿ“ a sender SHOULD NOT generate the token form, even if quoting looks unnecessary for a single-entry list ๐Ÿ™ˆ

Note: this use of the word "private" only controls where the response can be stored ๐Ÿ’€ it CANNOT ensure the privacy of the message content ๐Ÿ˜ญ also, the qualified form is often handled by caches as if an unqualified private was received, since the special handling isn't widely implemented ๐Ÿซถ

5.2.2.8. proxy-revalidate ๐ŸŒธ

the proxy-revalidate response directive says that once the response has gone stale, a shared cache MUST NOT reuse it to satisfy another request until the origin has successfully validated it, as defined by Section 4.3 ๐Ÿ”ฅ it's analogous to must-revalidate (Section 5.2.2.2), except proxy-revalidate does NOT apply to private caches ๐Ÿ’…

note that proxy-revalidate on its own does NOT imply a response is cacheable ๐Ÿ‘€ for example, it might be combined with the public directive (Section 5.2.2.9), letting the response be cached while requiring only a shared cache to revalidate when stale โœจ

5.2.2.9. public ๐Ÿงš

the public response directive says a cache MAY store the response even if it would otherwise be prohibited, subject to the constraints in Section 3 ๐ŸŽ€ in other words, public explicitly marks the response as cacheable ๐Ÿ’œ for example, public permits a shared cache to reuse a response to a request containing an Authorization header field (Section 3.5) ๐Ÿฆ‹

it's unnecessary to add public to a response that's already cacheable according to Section 3 ๐Ÿ™ˆ

if a response with public has no explicit freshness information, it's heuristically cacheable (Section 4.2.2) ๐ŸŒˆ

5.2.2.10. s-maxage ๐Ÿ‘‘

Argument syntax:

delta-seconds (see Section 1.2.2)

the s-maxage response directive says that, for a shared cache, the maximum age specified by this directive overrides the maximum age specified by either the max-age directive or the Expires header field ๐Ÿ”ฅ

s-maxage incorporates the semantics of the proxy-revalidate response directive (Section 5.2.2.8) for a shared cache ๐Ÿ’– a shared cache MUST NOT reuse a stale response with s-maxage to satisfy another request until it has been successfully validated by the origin, as defined by Section 4.3 ๐Ÿ˜ญ this directive also permits a shared cache to reuse a response to a request containing an Authorization header field, subject to the above requirements on maximum age and revalidation (Section 3.5) โœจ

this directive uses the token form of the argument syntax: e.g., s-maxage=10 not s-maxage="10" ๐Ÿ“ a sender MUST NOT generate the quoted-string form ๐Ÿ’…

5.2.3. Extension Directives ๐ŸŒˆ

the Cache-Control header field can be extended with one or more extension cache directives ๐Ÿฆ‹ a cache MUST ignore unrecognized cache directives ๐Ÿ’€

informational extensions (the ones that don't require a change in cache behavior) can be added without changing the semantics of other directives ๐ŸŽ€

behavioral extensions work by acting as modifiers to the existing base of cache directives ๐Ÿงš both the new directive and the old directive are supplied, so apps that don't understand the new one default to the old behavior, and apps that do understand it treat it as modifying the requirements of the old directive ๐Ÿ’œ that's how you extend cache directives without breaking deployed caches ๐Ÿ™Œ

for example, picture a hypothetical new response directive called "community" that modifies the private directive ๐ŸŒธ in addition to private caches, only a cache shared by members of the named community is allowed to store the response ๐Ÿ‘‘ an origin server wanting to let the UCI community use an otherwise private response in their shared caches could include:

Cache-Control: private, community="UCI"

a cache that recognizes the community directive could broaden its behavior accordingly ๐Ÿ“ a cache that doesn't recognize it would ignore it and stick with private ๐Ÿ’…

new extension directives ought to consider defining:

  • What it means for a directive to be specified multiple times,
  • When the directive does not take an argument, what it means when an argument is present,
  • When the directive requires an argument, what it means when it is missing, and
  • Whether the directive is specific to requests, specific to responses, or able to be used in either. ๐Ÿซถ

5.2.4. Cache Directive Registry ๐Ÿ’œ

the "Hypertext Transfer Protocol (HTTP) Cache Directive Registry" defines the namespace for the cache directives โœจ it's been created and is maintained at <https://www.iana.org/assignments/http-cache-directives> ๐ŸŒˆ

a registration MUST include the following fields:

  • Cache Directive Name
  • Pointer to specification text

values to be added to this namespace require IETF Review (see [RFC8126], Section 4.8) ๐Ÿ”ฅ periodt ๐Ÿ’…

5.3. Expires โœจ

okay so the Expires response header field tells you the date/time after which a response is considered stale ๐Ÿ’€ for the whole freshness vibe, peep Section 4.2 ๐Ÿฆ‹

just because a response has an Expires header doesn't mean the original resource is gonna change or disappear at, before, or after that time ๐Ÿ™ˆ

The Expires field value is an HTTP-date timestamp, as defined in Section 5.6.7 of [HTTP] ๐Ÿ•ฐ๏ธ Section 4.2 also has the parsing rules that apply to caches specifically ๐Ÿ’…

Expires = HTTP-date

for example:

Expires: Thu, 01 Dec 1994 16:00:00 GMT
๐Ÿšจ A cache recipient MUST interpret invalid date formats, especially the value "0", as representing a time in the past (i.e., "already expired"). no exceptions, bestie ๐Ÿ’€

if a response has a Cache-Control header field with the max-age directive (Section 5.2.2.1), a recipient MUST ignore the Expires header field ๐Ÿ™…โ€โ™€๏ธ same energy if the response includes the s-maxage directive (Section 5.2.2.10): a shared cache recipient MUST ignore the Expires header field. in both of these cases, the Expires value is only there for recipients that haven't implemented Cache-Control yet ๐Ÿฅน

an origin server without a clock (Section 5.6.7 of [HTTP]) MUST NOT generate an Expires header field, unless its value is a fixed time in the past (always expired) or its value was associated with the resource by a system that has a clock โฐ no vibes-based timestamps allowed ๐Ÿ˜ญ

historically, HTTP required the Expires field value to be no more than a year in the future. longer freshness lifetimes aren't prohibited anymore, but really huge values have been shown to cause problems (like clock overflows from using 32-bit integers for time values) ๐Ÿ’€ and lots of caches will evict a response way sooner than that anyway ๐Ÿงน


5.4. Pragma ๐ŸŽ€

the Pragma request header field was defined for HTTP/1.0 caches, so clients could ask for a "no-cache" request (since Cache-Control didn't exist until HTTP/1.1) ๐Ÿ•ฐ๏ธ

BUT Cache-Control is everywhere now ๐ŸŒ so this spec deprecates Pragma ๐Ÿชฆ

๐Ÿ“ Note: Because the meaning of "Pragma: no-cache" in responses was never specified, it does not provide a reliable replacement for "Cache-Control: no-cache" in them.

5.5. Warning ๐Ÿซฅ

the Warning header field used to carry extra info about the status or transformation of a message that might not show up in the status code ๐Ÿคทโ€โ™€๏ธ this spec obsoletes it, because it's not widely generated or shown to users. the info it carried can be figured out from other header fields, like Age ๐Ÿง 


6. Relationship to Applications and Other Caches ๐ŸŒˆ

apps using HTTP often do their own kind of caching ๐Ÿ“ฑ like web browsers with history mechanisms, where "Back" buttons can redisplay something you loaded earlier in the session โฌ…๏ธ

browsers also sometimes cache images and other assets within a page view, and they may or may not follow HTTP caching semantics ๐Ÿ–ผ๏ธ

the requirements in this spec don't necessarily apply to how apps use data after it comes out of an HTTP cache ๐Ÿซถ for example, a history mechanism can show a previous representation even if it's expired, and an app can use cached data in other ways beyond its freshness lifetime ๐ŸŒธ

this spec doesn't stop apps from taking HTTP caching into account ๐Ÿ’– a history mechanism might tell the user a view is stale, or it might honor cache directives (e.g., Cache-Control: no-store) โœจ

BUT, when an app caches data and doesn't make that apparent to the user (or easily controllable by them), it's strongly encouraged to define its behavior around HTTP cache directives, so it doesn't surprise authors who expect caching semantics to be honored ๐Ÿ‘€ for example, it might be reasonable to define an app cache "above" HTTP that lets a response with Cache-Control: no-store be reused for requests directly related to the one that fetched it (like ones made during the same page load) ๐Ÿง but it would likely be surprising and confusing for users and authors if it got reused for requests that are totally unrelated to the original one ๐Ÿ’€


7. Security Considerations ๐Ÿ”

this section is here to inform developers, info providers, and users about security concerns specific to HTTP caching ๐Ÿ›ก๏ธ the more general security considerations live in "HTTP/1.1" (Section 11 of [HTTP/1.1]) and "HTTP Semantics" (Section 17 of [HTTP]) ๐Ÿ“š

caches expose an extra attack surface, because the cache contents are a juicy target for malicious exploitation ๐Ÿ˜ฑ cache contents persist after an HTTP request is done, so an attack on the cache can reveal info long after a user thinks it's been removed from the network ๐Ÿ‘ป which means cache contents need to be protected as sensitive information ๐Ÿ”’ treat it like your diary, periodt ๐Ÿ““

in particular, private caches are restricted to a single user, so they can be used to reconstruct that user's activity ๐Ÿ•ต๏ธโ€โ™€๏ธ so it's important for user agents to let end users control them, for example by letting stored responses be removed for some or all origin servers ๐Ÿ—‘๏ธ your data, your call ๐Ÿ’…

7.1. Cache Poisoning โ˜ ๏ธ

storing malicious content in a cache can extend an attacker's reach to affect multiple users ๐Ÿ˜ญ these "cache poisoning" attacks happen when an attacker uses implementation flaws, elevated privileges, or other techniques to insert a response into a cache ๐Ÿงช it's especially effective when shared caches are used to distribute malicious content to lots of clients ๐Ÿšจ

one common attack vector for cache poisoning is exploiting differences in how proxies and user agents parse messages ๐Ÿงฉ see Section 6.3 of [HTTP/1.1] for the relevant HTTP/1.1 requirements ๐Ÿ“–

7.2. Timing Attacks โฑ๏ธ

one of the main jobs of a cache is to optimize performance, but that means its use can "leak" info about which resources were requested before ๐Ÿซฃ

for example, if a user visits a site and their browser caches some of its responses, and then they navigate to a second site, that second site can try to load responses it knows exist on the first site ๐Ÿ‘€ if they load quickly, it can be assumed the user visited that site, or even a specific page on it ๐Ÿ˜ฌ

these "timing attacks" can be mitigated by adding more info to the cache key, like the identity of the referring site (to stop the attack described above) ๐Ÿ›‘ this is sometimes called "double keying" ๐Ÿ”‘

7.3. Caching of Sensitive Information ๐Ÿ™Š

implementation and deployment flaws (often led to by misunderstanding how caching works) might cause sensitive info (like authentication credentials) that's thought to be private to get cached, exposing it to unauthorized parties ๐Ÿ˜ฑ

note that the Set-Cookie response header field [COOKIE] does NOT stop caching ๐Ÿช a cacheable response with Set-Cookie can be (and often is) used to satisfy later requests from caches ๐Ÿฅฒ servers that want to control caching of these responses are encouraged to send appropriate Cache-Control response header fields ๐ŸŽ€


8. IANA Considerations ๐Ÿ“‹

the change controller for the following registrations is: "IETF (iesg@ietf.org) - Internet Engineering Task Force" ๐Ÿ›๏ธ

8.1. Field Name Registration ๐Ÿท๏ธ

IANA has updated the "Hypertext Transfer Protocol (HTTP) Field Name Registry" at <https://www.iana.org/assignments/http-fields>, as described in Section 18.4 of [HTTP], with the field names in the table below ๐ŸŒธ

+===============+============+=========+==========+
| Field Name    | Status     | Section | Comments |
+===============+============+=========+==========+
| Age           | permanent  | 5.1     |          |
+---------------+------------+---------+----------+
| Cache-Control | permanent  | 5.2     |          |
+---------------+------------+---------+----------+
| Expires       | permanent  | 5.3     |          |
+---------------+------------+---------+----------+
| Pragma        | deprecated | 5.4     |          |
+---------------+------------+---------+----------+
| Warning       | obsoleted  | 5.5     |          |
+---------------+------------+---------+----------+

                         Table 1

8.2. Cache Directive Registration ๐Ÿช„

IANA has updated the "Hypertext Transfer Protocol (HTTP) Cache Directive Registry" at <https://www.iana.org/assignments/http-cache-directives> ๐Ÿ”— with the registration procedure per Section 5.2.4 and the cache directive names in the table below ๐Ÿ’œ

+==================+==================+
| Cache Directive  | Section          |
+==================+==================+
| max-age          | 5.2.1.1, 5.2.2.1 |
+------------------+------------------+
| max-stale        | 5.2.1.2          |
+------------------+------------------+
| min-fresh        | 5.2.1.3          |
+------------------+------------------+
| must-revalidate  | 5.2.2.2          |
+------------------+------------------+
| must-understand  | 5.2.2.3          |
+------------------+------------------+
| no-cache         | 5.2.1.4, 5.2.2.4 |
+------------------+------------------+
| no-store         | 5.2.1.5, 5.2.2.5 |
+------------------+------------------+
| no-transform     | 5.2.1.6, 5.2.2.6 |
+------------------+------------------+
| only-if-cached   | 5.2.1.7          |
+------------------+------------------+
| private          | 5.2.2.7          |
+------------------+------------------+
| proxy-revalidate | 5.2.2.8          |
+------------------+------------------+
| public           | 5.2.2.9          |
+------------------+------------------+
| s-maxage         | 5.2.2.10         |
+------------------+------------------+

                   Table 2

8.3. Warn Code Registry โš ๏ธ

IANA has added the following note to the "Hypertext Transfer Protocol (HTTP) Warn Codes" registry at <https://www.iana.org/assignments/http-warn-codes> saying that "Warning" has been obsoleted ๐Ÿฅ€

The Warning header field (and the warn codes that it uses) has been obsoleted for HTTP per [RFC9111].

9. References ๐Ÿ“š

9.1. Normative References ๐Ÿค“

[HTTP]     Fielding, R., Ed., Nottingham, M., Ed., and J. Reschke,
           Ed., "HTTP Semantics", STD 97, RFC 9110,
           DOI 10.17487/RFC9110, June 2022,
           <https://www.rfc-editor.org/info/rfc9110>.

[RFC2119]  Bradner, S., "Key words for use in RFCs to Indicate
           Requirement Levels", BCP 14, RFC 2119,
           DOI 10.17487/RFC2119, March 1997,
           <https://www.rfc-editor.org/info/rfc2119>.

[RFC5234]  Crocker, D., Ed. and P. Overell, "Augmented BNF for Syntax
           Specifications: ABNF", STD 68, RFC 5234,
           DOI 10.17487/RFC5234, January 2008,
           <https://www.rfc-editor.org/info/rfc5234>.

[RFC7405]  Kyzivat, P., "Case-Sensitive String Support in ABNF",
           RFC 7405, DOI 10.17487/RFC7405, December 2014,
           <https://www.rfc-editor.org/info/rfc7405>.

[RFC8174]  Leiba, B., "Ambiguity of Uppercase vs Lowercase in RFC
           2119 Key Words", BCP 14, RFC 8174, DOI 10.17487/RFC8174,
           May 2017, <https://www.rfc-editor.org/info/rfc8174>.

9.2. Informative References ๐Ÿ“š

Okay bestie, these are the extra reads. They're not required to implement the spec, but they're the context behind it. ๐Ÿงš


Appendix A. Collected ABNF ๐ŸŽ€

Here's the whole grammar in one place, so you can see every rule together. โœจ

In the collected ABNF below, list rules are expanded per Section 5.6.1 of [HTTP].

Age = delta-seconds

Cache-Control = [ cache-directive *( OWS "," OWS cache-directive ) ]

Expires = HTTP-date

HTTP-date = <HTTP-date, see [HTTP], Section 5.6.7>

OWS = <OWS, see [HTTP], Section 5.6.3>

cache-directive = token [ "=" ( token / quoted-string ) ]

delta-seconds = 1*DIGIT

field-name = <field-name, see [HTTP], Section 5.1>

quoted-string = <quoted-string, see [HTTP], Section 5.6.4>

token = <token, see [HTTP], Section 5.6.2>

Appendix B. Changes from RFC 7234 ๐Ÿฆ‹

Okay, the glow-up list! Here's everything that changed from RFC 7234. ๐Ÿ’…

Handling of duplicate and conflicting cache directives has been clarified. (Section 4.2.1)

Cache invalidation of the URIs in the Location and Content-Location header fields is no longer required but is still allowed. (Section 4.4)

Cache invalidation of the URIs in the Location and Content-Location header fields is disallowed when the origin is different; previously, it was the host. (Section 4.4)

Handling invalid and multiple Age header field values has been clarified. (Section 5.1)

Some cache directives defined by this specification now have stronger prohibitions against generating the quoted form of their values, since this has been found to create interoperability problems. Consumers of extension cache directives are no longer required to accept both token and quoted-string forms, but they still need to parse them properly for unknown extensions. (Section 5.2)

The public and private cache directives were clarified, so that they do not make responses reusable under any condition. (Section 5.2.2)

The must-understand cache directive was introduced; caches are no longer required to understand the semantics of new response status codes unless it is present. (Section 5.2.2.3)

The Warning response header was obsoleted. Much of the information supported by Warning could be gleaned by examining the response, and the remaining information -- although potentially useful -- was entirely advisory. In practice, Warning was not added by caches or intermediaries. (Section 5.5)


Acknowledgements ๐Ÿซถ

See Appendix "Acknowledgements" of [HTTP], which applies to this document as well.


Index ๐ŸŒˆ

Here's the index, all sorted out for you, bestie. ๐Ÿ—‚๏ธ

   A C E F G H M N O P S V W

      A

         age  Section 4.2
         Age header field  *_Section 5.1_*

      C

         cache  Section 1
         cache key  Section 2; Section 2
         Cache-Control header field  *_Section 5.2_*
         collapsed requests  Section 4

      E

         Expires header field  *_Section 5.3_*
         explicit expiration time  Section 4.2

      F

         Fields
            Age  *_Section 5.1_*; *_Section 5.1_*
            Cache-Control  *_Section 5.2_*
            Expires  *_Section 5.3_*; *_Section 5.3_*
            Pragma  *_Section 5.4_*; *_Section 5.4_*
            Warning  *_Section 5.5_*
         fresh  Section 4.2
         freshness lifetime  Section 4.2

      G

         Grammar
            Age  *_Section 5.1_*
            Cache-Control  *_Section 5.2_*
            DIGIT  *_Section 1.2_*
            Expires  *_Section 5.3_*
            cache-directive  *_Section 5.2_*
            delta-seconds  *_Section 1.2.2_*

      H

         Header Fields
            Age  *_Section 5.1_*; *_Section 5.1_*
            Cache-Control  *_Section 5.2_*
            Expires  *_Section 5.3_*; *_Section 5.3_*
            Pragma  *_Section 5.4_*; *_Section 5.4_*
            Warning  *_Section 5.5_*
         heuristic expiration time  Section 4.2
         heuristically cacheable  Section 4.2.2

      M

         max-age (cache directive)  *_Section 5.2.1.1_*;
            *_Section 5.2.2.1_*
         max-stale (cache directive)  *_Section 5.2.1.2_*
         min-fresh (cache directive)  *_Section 5.2.1.3_*
         must-revalidate (cache directive)  *_Section 5.2.2.2_*
         must-understand (cache directive)  *_Section 5.2.2.3_*

      N

         no-cache (cache directive)  *_Section 5.2.1.4_*;
            *_Section 5.2.2.4_*
         no-store (cache directive)  *_Section 5.2.1.5_*;
            *_Section 5.2.2.5_*
         no-transform (cache directive)  *_Section 5.2.1.6_*;
            *_Section 5.2.2.6_*

      O

         only-if-cached (cache directive)  *_Section 5.2.1.7_*

      P

         Pragma header field  *_Section 5.4_*
         private (cache directive)  *_Section 5.2.2.7_*
         private cache  Section 1
         proxy-revalidate (cache directive)  *_Section 5.2.2.8_*
         public (cache directive)  *_Section 5.2.2.9_*

      S

         s-maxage (cache directive)  *_Section 5.2.2.10_*
         shared cache  Section 1
         stale  Section 4.2

      V

         validator  Section 4.3.1

      W

         Warning header field  *_Section 5.5_*

Authors' Addresses ๐Ÿ’Œ

Here's where to find the editors, so you can slide into their DMs if you ever need to. ๐Ÿ’œ

   Roy T. Fielding (editor)
   Adobe
   345 Park Ave
   San Jose, CA 95110
   United States of America
   Email: fielding@gbiv.com
   URI:   https://roy.gbiv.com/


   Mark Nottingham (editor)
   Fastly
   Prahran
   Australia
   Email: mnot@mnot.net
   URI:   https://www.mnot.net/


   Julian Reschke (editor)
   greenbytes GmbH
   Hafenweg 16
   48155 Mรผnster
   Germany
   Email: julian.reschke@greenbytes.de
   URI:   https://greenbytes.de/tech/webdav/
๐Ÿ“ฆ where this page comes from