forked from httpwg/http-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraft-ietf-httpbis-cache-latest.xml
2367 lines (2242 loc) · 97.5 KB
/
draft-ietf-httpbis-cache-latest.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type='text/xsl' href='lib/myxml2rfc.xslt'?>
<!DOCTYPE rfc [
<!ENTITY MAY "<bcp14>MAY</bcp14>">
<!ENTITY MUST "<bcp14>MUST</bcp14>">
<!ENTITY MUST-NOT "<bcp14>MUST NOT</bcp14>">
<!ENTITY OPTIONAL "<bcp14>OPTIONAL</bcp14>">
<!ENTITY RECOMMENDED "<bcp14>RECOMMENDED</bcp14>">
<!ENTITY REQUIRED "<bcp14>REQUIRED</bcp14>">
<!ENTITY SHALL "<bcp14>SHALL</bcp14>">
<!ENTITY SHALL-NOT "<bcp14>SHALL NOT</bcp14>">
<!ENTITY SHOULD "<bcp14>SHOULD</bcp14>">
<!ENTITY SHOULD-NOT "<bcp14>SHOULD NOT</bcp14>">
<!ENTITY ID-VERSION "latest">
<!ENTITY mdash "—">
<!ENTITY Note "<x:h xmlns:x='http://purl.org/net/xml2rfc/ext'>Note:</x:h>">
]>
<?rfc toc="yes" ?>
<?rfc tocdepth="4" ?>
<?rfc symrefs="yes" ?>
<?rfc sortrefs="yes" ?>
<?rfc compact="yes"?>
<?rfc subcompact="no" ?>
<?rfc linkmailto="no" ?>
<?rfc editing="no" ?>
<?rfc comments="yes"?>
<?rfc inline="yes"?>
<?rfc rfcedstyle="yes"?>
<?rfc-ext allow-markup-in-artwork="yes" ?>
<?rfc-ext html-pretty-print="prettyprint https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"?>
<?rfc-ext include-references-in-index="yes" ?>
<?github-issue-label cache?>
<rfc category="std" docName="draft-ietf-httpbis-cache-&ID-VERSION;" ipr="pre5378Trust200902"
obsoletes="7234" x:maturity-level="proposed" xmlns:x="http://purl.org/net/xml2rfc/ext" xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>
<x:feedback template="mailto:[email protected]?subject={docname},%20%22{section}%22&body=<{ref}>:"/>
<front>
<title>HTTP Caching</title>
<author fullname="Roy T. Fielding" initials="R." surname="Fielding" role="editor">
<organization>Adobe</organization>
<address>
<postal>
<street>345 Park Ave</street>
<city>San Jose</city>
<region>CA</region>
<code>95110</code>
<country>United States of America</country>
</postal>
<email>[email protected]</email>
<uri>https://roy.gbiv.com/</uri>
</address>
</author>
<author fullname="Mark Nottingham" initials="M." surname="Nottingham" role="editor">
<organization>Fastly</organization>
<address>
<email>[email protected]</email>
<uri>https://www.mnot.net/</uri>
</address>
</author>
<author fullname="Julian F. Reschke" initials="J. F." surname="Reschke" role="editor">
<organization abbrev="greenbytes">greenbytes GmbH</organization>
<address>
<postal>
<street>Hafenweg 16</street>
<city>Muenster</city><code>48155</code>
<country>Germany</country>
</postal>
<email>[email protected]</email>
<uri>https://greenbytes.de/tech/webdav/</uri>
</address>
</author>
<date/>
<area>Applications and Real-Time</area>
<workgroup>HTTP</workgroup>
<keyword>Hypertext Transfer Protocol</keyword>
<keyword>HTTP</keyword>
<keyword>HTTP Caching</keyword>
<abstract>
<t>
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.
</t>
<t>
This document obsoletes RFC 7234.
</t>
</abstract>
<note title="Editorial Note" removeInRFC="true">
<t>
Discussion of this draft takes place on the HTTP working group
mailing list ([email protected]), which is archived at
<eref target="https://lists.w3.org/Archives/Public/ietf-http-wg/"/>.
</t>
<t>
Working Group information can be found at <eref target="https://httpwg.org/"/>;
source code and issues list for this draft can be found at
<eref target="https://github.com/httpwg/http-core"/>.
</t>
<t>
The changes in this draft are summarized in <xref target="changes.since.06"/>.
</t>
</note>
</front>
<middle>
<section title="Introduction" anchor="caching">
<t>
The Hypertext Transfer Protocol (HTTP) is a stateless application-level
request/response protocol that uses extensible semantics and
self-descriptive messages for flexible interaction with network-based
hypertext information systems. HTTP is defined by a series of documents
that collectively form the HTTP/1.1 specification:
</t>
<ul>
<li>"HTTP Semantics" <xref target="Semantics"/></li>
<li>"HTTP Caching" (this document)</li>
<li>"HTTP/1.1 Messaging" <xref target="Messaging"/></li>
</ul>
<t>
HTTP is typically used for distributed information systems, where
performance can be improved by the use of response caches. This document
defines aspects of HTTP related to caching and reusing response
messages.
</t>
<iref item="cache" />
<t>
An HTTP <x:dfn>cache</x:dfn> is a local store of response messages and the
subsystem that controls storage, retrieval, and deletion of messages in it.
A cache stores cacheable responses in order to reduce the response time and
network bandwidth consumption on future, equivalent requests. Any client or
server &MAY; employ a cache, though a cache cannot be used by a server that
is acting as a tunnel.
</t>
<iref item="shared cache" />
<iref item="private cache" />
<t anchor="shared.and.private.caches">
A <x:dfn>shared cache</x:dfn> is a cache that stores responses to be reused
by more than one user; shared caches are usually (but not always) deployed
as a part of an intermediary. A <x:dfn>private cache</x:dfn>, in contrast,
is dedicated to a single user; often, they are deployed as a component of
a user agent.
</t>
<t>
The goal of caching in HTTP is to significantly improve performance
by reusing a prior response message to satisfy a current request.
A stored response is considered "fresh", as defined in
<xref target="expiration.model" />, if the response can be reused without
"validation" (checking with the origin server to see if the cached response
remains valid for this request). A fresh response can therefore
reduce both latency and network overhead each time it is reused.
When a cached response is not fresh, it might still be reusable if it can
be freshened by validation (<xref target="validation.model" />) or if the
origin is unavailable (<xref target="serving.stale.responses" />).
</t>
<t>
This document obsoletes <xref target="RFC7234" x:fmt="none">RFC 7234</xref>,
with the changes being summarized in <xref target="changes.from.rfc.7234"/>.
</t>
<section title="Requirements Notation" anchor="intro.requirements">
<t>
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
"SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
document are to be interpreted as described in <xref target="RFC2119"/>.
</t>
<t>
Conformance criteria and considerations regarding error handling
are defined in <xref target="Semantics-conformance"/>.
</t>
</section>
<section title="Syntax Notation" anchor="notation">
<iref primary="true" item="Grammar" subitem="ALPHA"/>
<iref primary="true" item="Grammar" subitem="CR"/>
<iref primary="true" item="Grammar" subitem="CRLF"/>
<iref primary="true" item="Grammar" subitem="CTL"/>
<iref primary="true" item="Grammar" subitem="DIGIT"/>
<iref primary="true" item="Grammar" subitem="DQUOTE"/>
<iref primary="true" item="Grammar" subitem="HEXDIG"/>
<iref primary="true" item="Grammar" subitem="HTAB"/>
<iref primary="true" item="Grammar" subitem="LF"/>
<iref primary="true" item="Grammar" subitem="OCTET"/>
<iref primary="true" item="Grammar" subitem="SP"/>
<iref primary="true" item="Grammar" subitem="VCHAR"/>
<t>
This specification uses the Augmented Backus-Naur Form (ABNF) notation of
<xref target="RFC5234"/>, extended with the notation for case-sensitivity
in strings defined in <xref target="RFC7405"/>.
</t>
<t>
It also uses a list extension, defined in <xref target="abnf.extension"/>,
that allows for compact definition of comma-separated lists using a '#'
operator (similar to how the '*' operator indicates repetition). <xref
target="collected.abnf"/> shows the collected grammar with all list
operators expanded to standard ABNF notation.
</t>
<t anchor="core.rules">
<x:anchor-alias value="ALPHA"/>
<x:anchor-alias value="CR"/>
<x:anchor-alias value="CRLF"/>
<x:anchor-alias value="CTL"/>
<x:anchor-alias value="DIGIT"/>
<x:anchor-alias value="DQUOTE"/>
<x:anchor-alias value="HEXDIG"/>
<x:anchor-alias value="HTAB"/>
<x:anchor-alias value="LF"/>
<x:anchor-alias value="OCTET"/>
<x:anchor-alias value="SP"/>
<x:anchor-alias value="VCHAR"/>
The following core rules are included by
reference, as defined in <xref target="RFC5234" x:fmt="," x:sec="B.1"/>:
ALPHA (letters), CR (carriage return), CRLF (CR LF), CTL (controls),
DIGIT (decimal 0-9), DQUOTE (double quote),
HEXDIG (hexadecimal 0-9/A-F/a-f), HTAB (horizontal tab), LF (line feed),
OCTET (any 8-bit sequence of data), SP (space), and
VCHAR (any visible <xref target="USASCII"/> character).
</t>
<t anchor="imported.rules">
<x:anchor-alias value="HTTP-date"/>
<x:anchor-alias value="OWS"/>
<x:anchor-alias value="field-name"/>
<x:anchor-alias value="quoted-string"/>
<x:anchor-alias value="token"/>
The rules below are defined in <xref target="Semantics"/>:
</t>
<sourcecode type="abnf7230">
<x:ref>HTTP-date</x:ref> = <HTTP-date, see <xref target="http.date"/>>
<x:ref>OWS</x:ref> = <OWS, see <xref target="whitespace"/>>
<x:ref>field-name</x:ref> = <field-name, see <xref target="field.names"/>>
<x:ref>quoted-string</x:ref> = <quoted-string, see <xref target="quoted.strings"/>>
<x:ref>token</x:ref> = <token, see <xref target="tokens"/>>
</sourcecode>
</section>
<section title="Delta Seconds" anchor="delta-seconds">
<t>
The delta-seconds rule specifies a non-negative integer, representing time
in seconds.
</t>
<sourcecode type="abnf7230"><iref item="Grammar" primary="true" subitem="delta-seconds" />
<x:ref>delta-seconds</x:ref> = 1*<x:ref>DIGIT</x:ref>
</sourcecode>
<t>
A recipient parsing a delta-seconds value and converting it to binary form
ought to use an arithmetic type of at least 31 bits of non-negative integer
range.
If a cache receives a delta-seconds value greater than the greatest integer
it can represent, or if any of its subsequent calculations overflows,
the cache &MUST; consider the value to be either 2147483648
(2<sup>31</sup>) or the greatest positive integer it can conveniently
represent.
</t>
<aside>
<t>
&Note; The value 2147483648 is here for historical reasons, effectively
represents infinity (over 68 years), and does not need to be stored in
binary form; an implementation could produce it as a canned string if
any overflow occurs, even if the calculations are performed with an
arithmetic type incapable of directly representing that number.
What matters here is that an overflow be detected and not treated as a
negative value in later calculations.
</t>
</aside>
</section>
</section>
<section title="Overview of Cache Operation" anchor="caching.overview">
<iref item="cache key" />
<t>
Proper cache operation preserves the semantics of HTTP transfers (<xref
target="Semantics"/>) while reducing the transfer of information already
held in the cache. Although caching is an entirely &OPTIONAL; feature of
HTTP, it can be assumed that reusing a cached response is desirable and
that such reuse is the default behavior when no requirement or local
configuration prevents it. Therefore, HTTP cache requirements are focused
on preventing a cache from either storing a non-reusable response or
reusing a stored response inappropriately, rather than mandating that
caches always store and reuse particular responses.
</t>
<iref item="cache key" />
<t>
The base <x:dfn>cache key</x:dfn> consists of the request method and target
URI used to retrieve the stored response; the method determines under which
circumstances that response can be used to satisfy a request. However, many
HTTP caches in common use today only cache GET responses, and therefore only
use the URI as the cache key, forwarding other methods.
</t>
<t>
If a request target is subject to content negotiation, the cache might
store multiple responses for it. Caches differentiate these responses
by incorporating values of the original request's selecting header fields
into the cache key as well, as per <xref target="caching.negotiated.responses"/>.
</t>
<t>
Furthermore, caches might incorporate additional material into the cache key.
For example, user agent caches might include the referring site's identity,
thereby "double keying" the cache to avoid some privacy risks (see <xref
target="security.timing"/>).
</t>
<t>
Most commonly, caches store the successful result of a retrieval
request: i.e., a <x:ref>200 (OK)</x:ref> response to a GET request, which
contains a representation of the resource identified by the request target
(<xref target="GET"/>). However, it is also possible to store
redirects, negative results (e.g., <x:ref>404 (Not Found)</x:ref>),
incomplete results (e.g., <x:ref>206 (Partial Content)</x:ref>), and
responses to methods other than GET if the method's definition allows such
caching and defines something suitable for use as a cache key.
</t>
<t>
A cache is <x:dfn>disconnected</x:dfn> when it cannot contact the origin
server or otherwise find a forward path for a given request. A
disconnected cache can serve stale responses in some circumstances (<xref
target="serving.stale.responses"/>).
</t>
</section>
<section title="Storing Responses in Caches" anchor="response.cacheability">
<t>
A cache &MUST-NOT; store a response to any request, unless:
</t>
<ul>
<li><t>The request method is understood by the cache, and</t></li>
<li><t>the response status code is final (see <xref
target="associating.response.to.request"/>), and</t></li>
<li><t>the response status code is understood by the cache, and</t></li>
<li><t>the "no-store" cache directive (see <xref
target="header.cache-control" />) does not appear in the response,
and</t></li>
<li><t>the "private" response directive (see <xref
target="cache-response-directive.private" />) does not appear in the
response, if the cache is shared, and</t></li>
<li><t>the <x:ref>Authorization</x:ref> header field (see
<xref target="header.authorization"/>) does not appear in the request, if the cache is
shared, unless the response explicitly allows it (see <xref
target="caching.authenticated.responses" />), and</t></li>
<li><t>the response either:</t>
<ul>
<li>contains an <x:ref>Expires</x:ref> header field (see
<xref target="header.expires"/>), or</li>
<li>contains a max-age response directive (see <xref
target="cache-response-directive.max-age" />), or</li>
<li>contains a s-maxage response directive (see <xref
target="cache-response-directive.s-maxage" />) and the cache is
shared, or</li>
<li>contains a Cache Control Extension (see <xref
target="cache.control.extensions" />) that allows it to be cached,
or</li>
<li>has a status code that is defined as heuristically cacheable
(see <xref target="heuristic.freshness" />), or</li>
<li>contains a public response directive (see <xref
target="cache-response-directive.public"/>).</li>
</ul>
</li>
</ul>
<t>
Note that any of the requirements listed above can be overridden by a
cache-control extension; see <xref target="cache.control.extensions" />.
</t>
<t>
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.
</t>
<t>
Note that, in normal operation, some caches will not store a response that
has neither a cache validator nor an explicit expiration time, as such
responses are not usually useful to store. However, caches are not
prohibited from storing such responses.
</t>
<section title="Storing Incomplete Responses" anchor="incomplete.responses">
<t>
A response message is considered complete when all of the octets indicated
by its framing are available. Note that, when no explicit framing is
provided, a response message that is ended by the connection's close is
considered complete even though it might be indistinguishable from an
incomplete response (see <xref target="Messaging" x:fmt=","
x:rel="#message.body.length"/>). A cache &SHOULD; consider a close-terminated
response incomplete if the connection termination is detected to be an
error. A server that wishes to avoid premature termination resulting in an
incorrect cached response &SHOULD; send the response with explicit framing.
</t>
<t>
If the request method is GET, the response status code is <x:ref>200
(OK)</x:ref>, and the entire response header section has been received, a
cache &MAY; store an incomplete response message body if the stored response
is recorded as incomplete. Likewise, a <x:ref>206 (Partial
Content)</x:ref> response &MAY; be stored as if it were an incomplete
<x:ref>200 (OK)</x:ref> response. However, a cache &MUST-NOT; store
incomplete or partial-content responses if it does not support the
<x:ref>Range</x:ref> and <x:ref>Content-Range</x:ref> header fields or if
it does not understand the range units used in those fields.
</t>
<t>
A cache &MAY; complete a stored incomplete response by making a subsequent
range request (<xref target="header.range"/>) and combining the successful response with the
stored response, as defined in <xref target="combining.responses"/>. 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 that is wholly within the incomplete response. A cache &MUST-NOT;
send a partial response to a client without explicitly marking it as such
using the <x:ref>206 (Partial Content)</x:ref> status code.
</t>
</section>
<section title="Storing Responses to Authenticated Requests" anchor="caching.authenticated.responses">
<t>
A shared cache &MUST-NOT; use a cached response to a request with an
<x:ref>Authorization</x:ref> header field (<xref target="header.authorization"/>) to
satisfy any subsequent request unless a response directive that allows such
responses to be stored is present.
</t>
<t>
In this specification, the following <x:ref>Cache-Control</x:ref> response
directives (<xref target="cache-response-directive"/>) have such an effect:
must-revalidate, public, and s-maxage.
</t>
</section>
<section title="Combining Partial Content" anchor="combining.responses">
<t>
A response might transfer only a partial representation if the
connection closed prematurely or if the request used one or more Range
specifiers (<xref target="header.range"/>). After several such 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, if they all share the same strong validator and
the cache complies with the client requirements in <xref target="combining.byte.ranges"/>.
</t>
<t>
When combining the new response with one or more stored responses, a cache
&MUST; use the header fields provided in the new response, aside from
<x:ref>Content-Range</x:ref>, to replace all instances of the
corresponding header fields in the stored response.
</t>
</section>
</section>
<section title="Constructing Responses from Caches" anchor="constructing.responses.from.caches">
<t>
When presented with a request, a cache &MUST-NOT; reuse a stored response,
unless:
</t>
<ul>
<li><t>The presented effective request URI (<xref target="effective.request.uri"/>) and
that of the stored response match, and</t></li>
<li><t>the request method associated with the stored response allows it to
be used for the presented request, and</t></li>
<li><t>selecting header fields nominated by the stored response (if any)
match those presented (see <xref target="caching.negotiated.responses"
/>), and</t></li>
<li><t>the stored response does not contain the no-cache cache directive
(<xref target="cache-response-directive.no-cache"/>), unless it is
successfully validated (<xref target="validation.model"/>), and</t></li>
<li><t>the stored response is either:</t>
<ul>
<li>fresh (see <xref target="expiration.model" />), or</li>
<li>allowed to be served stale (see <xref
target="serving.stale.responses" />), or</li>
<li>successfully validated (see <xref target="validation.model"
/>).</li>
</ul>
</li>
</ul>
<t>
Note that any of the requirements listed above can be overridden by a
cache-control extension; see <xref target="cache.control.extensions" />.
</t>
<t>
When a stored response is used to satisfy a request without validation, a
cache &MUST; generate an <x:ref>Age</x:ref> header field (<xref
target="header.age"/>), replacing any present in the response with a value
equal to the stored response's current_age; see <xref
target="age.calculations" />.
</t>
<t>
A cache &MUST; write through requests with methods that are unsafe
(<xref target="safe.methods"/>) to the origin server; i.e., a cache is not allowed to
generate a reply to such a request before having forwarded the request and
having received a corresponding response.
</t>
<t>
Also, note that unsafe requests might invalidate already-stored responses;
see <xref target="invalidation" />.
</t>
<t>
When more than one suitable response is stored, a cache &MUST; use the
most recent one (as determined by the <x:ref>Date</x:ref> 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.
</t>
<t>
A cache that does not have a clock available &MUST-NOT; use stored
responses without revalidating them upon every use.
</t>
<section title="Calculating Cache Keys with Vary" anchor="caching.negotiated.responses">
<t>
When a cache receives a request that can be satisfied by a stored response
that has a <x:ref>Vary</x:ref> header field (<xref target="header.vary"/>),
it &MUST-NOT; use that response unless all of the selecting header fields
nominated by the Vary header field match in both the original request
(i.e., that associated with the stored response), and the presented
request.
</t>
<t>
The selecting 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:
</t>
<ul>
<li>
adding or removing whitespace, where allowed in the header field's
syntax
</li>
<li>
combining multiple header fields with the same field name
(see <xref target="field.values"/>)
</li>
<li>
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)
</li>
</ul>
<t>
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.
</t>
<t>
A <x:ref>Vary</x:ref> header field-value of "*" always fails to match.
</t>
<t>
The stored response with matching selecting header fields is known as the
selected response.
</t>
<t>
If multiple selected responses are available (potentially including
responses without a Vary header field), the cache will need to choose one to use.
When a selecting header field has a known mechanism for doing so (e.g., qvalues on
<x:ref>Accept</x:ref> and similar request header fields), that mechanism &MAY; be
used to select preferred responses; of the remainder, the most recent
response (as determined by the <x:ref>Date</x:ref> header field) is used, as
per <xref target="constructing.responses.from.caches"/>.
</t>
<t>
Note that in practice, some resources might send the Vary header field on
responses inconsistently. When a cache has multiple responses for a given
target URI, and one or more omits the Vary header field, it &SHOULD; use the
most recent non-empty value available to select an appropriate response
for the request.
</t>
<t>
If no selected response is available, the cache cannot satisfy the
presented request. Typically, it is forwarded to the origin server
in a (possibly conditional; see <xref target="validation.model"/>) request.
</t>
</section>
<section title="Freshness" anchor="expiration.model">
<iref item="fresh" />
<iref item="stale" />
<t>
A <x:dfn>fresh</x:dfn> response is one whose age has not yet exceeded its
freshness lifetime. Conversely, a <x:dfn>stale</x:dfn>
response is one where it has.
</t>
<iref item="freshness lifetime" />
<iref item="explicit expiration time" />
<iref item="heuristic expiration time" />
<t>
A response's <x:dfn>freshness lifetime</x:dfn> is the length of time
between its generation by the origin server and its expiration time. An
<x:dfn>explicit expiration time</x:dfn> 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 <x:dfn>heuristic expiration
time</x:dfn> is assigned by a cache when no explicit expiration time is
available.
</t>
<iref item="age" />
<t>
A response's <x:dfn>age</x:dfn> is the time that has passed since it was
generated by, or successfully validated with, the origin server.
</t>
<t>
When a response is "fresh" in the cache, it can be used to satisfy
subsequent requests without contacting the origin server, thereby improving
efficiency.
</t>
<t>
The primary mechanism for determining freshness is for an origin server to
provide an explicit expiration time in the future, using either the
<x:ref>Expires</x:ref> header field (<xref target="header.expires" />) or
the max-age response directive (<xref
target="cache-response-directive.max-age" />). Generally, origin servers
will assign future explicit expiration times to responses in the belief
that the representation is not likely to change in a semantically
significant way before the expiration time is reached.
</t>
<t>
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 <xref
target="serving.stale.responses" />).
</t>
<t>
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 <xref target="heuristic.freshness"/>).
</t>
<t>
The calculation to determine if a response is fresh is:
</t>
<artwork type="code">
response_is_fresh = (freshness_lifetime > current_age)
</artwork>
<t>
freshness_lifetime is defined in <xref
target="calculating.freshness.lifetime" />; current_age is defined in
<xref target="age.calculations" />.
</t>
<t>
Clients can send the max-age or min-fresh request directives (<xref
target="cache-request-directive" />) to constrain or relax freshness
calculations for the corresponding response. However, caches are not
required to honor them.
</t>
<t>
When calculating freshness, to avoid common problems in date parsing:
</t>
<ul>
<li>Although all date formats are specified to be case-sensitive,
a cache recipient &SHOULD; match day, week, and time-zone names
case-insensitively.</li>
<li>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 <x:ref>Expires</x:ref> date as the
nearest time equal to or earlier than the received value.</li>
<li>A cache recipient &MUST-NOT; allow local time zones to influence the
calculation or comparison of an age or expiration time.</li>
<li>A cache recipient &SHOULD; consider a date with a zone abbreviation
other than GMT or UTC to be invalid for calculating expiration.</li>
</ul>
<t>
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 <xref
target="history.lists" /> for an explanation of the difference between
caches and history mechanisms.
</t>
<section title="Calculating Freshness Lifetime" anchor="calculating.freshness.lifetime">
<t>
A cache can calculate the freshness lifetime (denoted as
freshness_lifetime) of a response by using the first match of the following:
</t>
<ul>
<li>If the cache is shared and the s-maxage response directive
(<xref target="cache-response-directive.s-maxage" />) is present, use its value,
or</li>
<li>If the max-age response directive (<xref
target="cache-response-directive.max-age" />) is present, use its value, or</li>
<li>If the <x:ref>Expires</x:ref> response header field
(<xref target="header.expires" />) is present, use its value minus the
value of the <x:ref>Date</x:ref> response header field, or</li>
<li>Otherwise, no explicit expiration time is present in the response. A
heuristic freshness lifetime might be applicable; see <xref
target="heuristic.freshness" />.</li>
</ul>
<t>
Note that this calculation is not vulnerable to clock skew, since all of
the information comes from the origin server.
</t>
<t>
When there is more than one value present for a given directive (e.g., two
<x:ref>Expires</x:ref> header fields, multiple Cache-Control: max-age
directives), the directive's value is considered invalid. Caches are
encouraged to consider responses that have invalid freshness information to
be stale.
</t>
</section>
<section title="Calculating Heuristic Freshness" anchor="heuristic.freshness">
<iref item="heuristically cacheable"/>
<t>
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 header field values (such as
the <x:ref>Last-Modified</x:ref> time) to estimate a plausible expiration
time. This specification does not provide specific algorithms, but does
impose worst-case constraints on their results.
</t>
<t>
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 <xref target="response.cacheability"/>, this means that,
effectively, heuristics can only be used on responses without explicit
freshness whose status codes are defined as "<x:dfn>heuristically cacheable</x:dfn>" (e.g., see
<xref target="overview.of.status.codes"/>), and those responses without
explicit freshness that have been marked as explicitly cacheable (e.g.,
with a "public" response directive).
</t>
<t>
Note that in previous specifications heuristically cacheable response status
codes were called "cacheable by default."
</t>
<t>
If the response has a <x:ref>Last-Modified</x:ref> header field
(<xref target="header.last-modified"/>), 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%.
</t>
<aside>
<t>
&Note; <xref target="RFC2616" x:fmt="of" x:sec="13.9"/> 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 preclude
caching.
</t>
</aside>
</section>
<section title="Calculating Age" anchor="age.calculations">
<t>
The <x:ref>Age</x:ref> header field is used to convey an estimated
age of the response message when obtained from a cache. The Age field value
is the cache's estimate of the number of seconds since the response was
generated or validated by the origin server. In essence, the Age value is
the sum of the time that the response has been resident in each of the
caches along the path from the origin server, plus the amount of time it
has been in transit along network paths.
</t>
<t>
The following data is used for the age calculation:
</t>
<dl newline="false">
<dt><x:dfn>age_value</x:dfn></dt>
<dd>
The term "age_value" denotes the value of the <x:ref>Age</x:ref>
header field (<xref target="header.age"/>), in a form appropriate for
arithmetic operation; or 0, if not available.
</dd>
<dt><x:dfn>date_value</x:dfn></dt>
<dd>
The term "date_value" denotes the value of
the Date header field, in a form appropriate for arithmetic
operations. See <xref target="header.date"/> for the definition of the Date header
field, and for requirements regarding responses without it.
</dd>
<dt><x:dfn>now</x:dfn></dt>
<dd>
The term "now" means "the current value of the clock at the host
performing the calculation". A host ought to use NTP (<xref
target="RFC5905"/>) or some similar protocol to synchronize its
clocks to Coordinated Universal Time.
</dd>
<dt><x:dfn>request_time</x:dfn></dt>
<dd>
The current value of the clock at the host at the time the request
resulting in the stored response was made.
</dd>
<dt><x:dfn>response_time</x:dfn></dt>
<dd>
The current value of the clock at the host at the time the response
was received.
</dd>
</dl>
<t>
A response's age can be calculated in two entirely independent ways:
</t>
<ol>
<li>the "apparent_age": response_time minus date_value, if the local
clock is reasonably well synchronized to the origin server's clock. If
the result is negative, the result is replaced by zero.</li>
<li>the "corrected_age_value", 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 that the
response was received.</li>
</ol>
<artwork type="code">
apparent_age = max(0, response_time - date_value);
response_delay = response_time - request_time;
corrected_age_value = age_value + response_delay;
</artwork>
<t>
These are combined as
</t>
<artwork type="code">
corrected_initial_age = max(apparent_age, corrected_age_value);
</artwork>
<t>
unless the cache is confident in the value of the <x:ref>Age</x:ref> header
field (e.g., because there are no HTTP/1.0 hops in the <x:ref>Via</x:ref>
header field), in which case the corrected_age_value &MAY; be used as the
corrected_initial_age.
</t>
<t>
The current_age of a stored response can then be calculated by adding the
amount of time (in seconds) since the stored response was last validated by
the origin server to the corrected_initial_age.
</t>
<artwork type="code">
resident_time = now - response_time;
current_age = corrected_initial_age + resident_time;
</artwork>
</section>
<section title="Serving Stale Responses" anchor="serving.stale.responses">
<t>
A "stale" response is one that either has explicit expiry information or is
allowed to have heuristic expiry calculated, but is not fresh according to
the calculations in <xref target="expiration.model" />.
</t>
<t>
A cache &MUST-NOT; generate a stale response if it is prohibited by an
explicit in-protocol directive (e.g., by a "no-store" or "no-cache" cache
directive, a "must-revalidate" cache-response-directive, or an applicable
"s-maxage" or "proxy-revalidate" cache-response-directive; see <xref
target="cache-response-directive"/>).
</t>
<t>
A cache &MUST-NOT; generate a stale response unless it is disconnected
or doing so is explicitly permitted by the client or origin server
(e.g., by the max-stale request directive in <xref
target="cache-request-directive" />, by extension directives such as those
defined in <xref target="RFC5861"/>, or by configuration in accordance
with an out-of-band contract).
</t>
</section>
</section>
<section title="Validation" anchor="validation.model">
<t>
When a cache has one or more stored responses for a requested URI, but
cannot serve any of them (e.g., because they are not fresh, or one cannot
be selected; see <xref target="caching.negotiated.responses"/>), it can use
the conditional request mechanism <xref target="preconditions"/> in the forwarded request to
give the next inbound server an opportunity to select a valid stored
response to use, updating the stored metadata in the process, or to replace
the stored response(s) with a new response. This process is known as
"validating" or "revalidating" the stored response.
</t>
<section title="Sending a Validation Request" anchor="validation.sent"><iref item="validator" />
<t>
When generating a conditional request for validation, a cache starts with
either a request it is attempting to satisfy, or — if it is initiating
the request independently — it synthesises a request using a stored
response by copying the method, request-target, and request header fields
identified by the Vary header field <xref
target="caching.negotiated.responses"/>.
</t>
<t>
It then updates that request with one or more precondition header fields.
These contain validator metadata sourced from stored response(s) that have
the same cache key.
</t>
<t>
The precondition header fields are then compared by recipients to
determine whether any stored response is equivalent to a current
representation of the resource.
</t>
<t>
One such validator is the timestamp given in a <x:ref>Last-Modified</x:ref>
header field (<xref target="header.last-modified"/>), which can be used in an
<x:ref>If-Modified-Since</x:ref> header field for response validation, or
in an <x:ref>If-Unmodified-Since</x:ref> or <x:ref>If-Range</x:ref> header
field for representation selection (i.e., the client is referring
specifically to a previously obtained representation with that timestamp).
</t>
<t>
Another validator is the entity-tag given in an <x:ref>ETag</x:ref> header
field (<xref target="header.etag"/>). One or more entity-tags, indicating one or more
stored responses, can be used in an <x:ref>If-None-Match</x:ref> header
field for response validation, or in an <x:ref>If-Match</x:ref> or
<x:ref>If-Range</x:ref> header field for representation selection (i.e.,
the client is referring specifically to one or more previously obtained
representations with the listed entity-tags).
</t>
</section>
<section title="Handling a Received Validation Request" anchor="validation.received">
<t>
Each client in the request chain may have its own cache, so it is common
for a cache at an intermediary to receive conditional requests from other
(outbound) caches. Likewise, some user agents make use of conditional
requests to limit data transfers to recently modified representations or to
complete the transfer of a partially retrieved representation.
</t>
<t>
If a cache receives a request that can be satisfied by reusing one of
its stored <x:ref>200 (OK)</x:ref> or <x:ref>206 (Partial Content)</x:ref>
responses, the cache &SHOULD; evaluate any applicable conditional header
field preconditions received in that request with respect to the
corresponding validators contained within the selected response.
A cache &MUST-NOT; evaluate conditional header fields that are only
applicable to an origin server, found in a request with semantics that
cannot be satisfied with a cached response, or applied to a target resource
for which it has no stored responses; such preconditions are likely
intended for some other (inbound) server.
</t>
<t>
The proper evaluation of conditional requests by a cache depends on the
received precondition header fields and their precedence, as defined in
<xref target="precedence"/>. The <x:ref>If-Match</x:ref> and
<x:ref>If-Unmodified-Since</x:ref> conditional header fields are not
applicable to a cache.
</t>
<t>
A request containing an <x:ref>If-None-Match</x:ref> header field
(<xref target="header.if-none-match"/>) indicates that the client wants to validate one
or more of its own stored responses in comparison to whichever stored
response is selected by the cache.
If the field-value is "*", or if the field-value is a list of entity-tags
and at least one of them matches the entity-tag of the selected stored
response, a cache recipient &SHOULD; generate a
<x:ref>304 (Not Modified)</x:ref> response (using the metadata of the
selected stored response) instead of sending that stored response.
</t>
<t>
When a cache decides to revalidate its own stored responses for a
request that contains an <x:ref>If-None-Match</x:ref> 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) and send the union of
the two lists as a replacement <x:ref>If-None-Match</x:ref> 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
<x:ref>304 (Not Modified)</x:ref> and has an ETag header field value with
an entity-tag that is not in the client's list, the cache &MUST;
generate a <x:ref>200 (OK)</x:ref> response for the client by reusing its
corresponding stored response, as updated by the 304 response metadata
(<xref target="freshening.responses"/>).
</t>
<t>
If an <x:ref>If-None-Match</x:ref> header field is not present, a request
containing an <x:ref>If-Modified-Since</x:ref> header field
(<xref target="header.if-modified-since"/>) indicates that the client wants to validate
one or more of its own stored responses by modification date.
A cache recipient &SHOULD; generate a <x:ref>304 (Not Modified)</x:ref>
response (using the metadata of the selected stored response) if one of the
following cases is true:
1) the selected stored response has a <x:ref>Last-Modified</x:ref>
field-value that is earlier than or equal to the conditional timestamp;
2) no <x:ref>Last-Modified</x:ref> field is present in the selected stored
response, but it has a <x:ref>Date</x:ref> field-value that is earlier than
or equal to the conditional timestamp; or,
3) neither <x:ref>Last-Modified</x:ref> nor <x:ref>Date</x:ref> is present
in the selected stored response, but the cache recorded it as having been
received at a time earlier than or equal to the conditional timestamp.
</t>
<t>
A cache that implements partial responses to range requests, as defined in
<xref target="header.range"/>, also needs to evaluate a received <x:ref>If-Range</x:ref> header
field (<xref target="header.if-range"/>) with respect to its selected stored response.
</t>
</section>
<section title="Handling a Validation Response" anchor="validation.response">
<t>
Cache handling of a response to a conditional request is dependent upon its
status code:
</t>
<ul>
<li>
A <x:ref>304 (Not Modified)</x:ref> response status code indicates
that the stored response can be updated and reused; see <xref
target="freshening.responses"/>.
</li>
<li>
A full response (i.e., one with a payload body) indicates that none
of the stored responses nominated in the conditional request is
suitable. Instead, the cache &MUST; use the full response to
satisfy the request and &MAY; replace the stored response(s).
</li>
<li>