forked from whatwg/fetch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
fetch.bs
7243 lines (5516 loc) · 286 KB
/
fetch.bs
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
<pre class=metadata>
Group: WHATWG
H1: Fetch
Shortname: fetch
Text Macro: TWITTER fetchstandard
Text Macro: LATESTRD 2020-06
Abstract: The Fetch standard defines requests, responses, and the process that binds them: fetching.
Translation: ja https://triple-underscore.github.io/Fetch-ja.html
Markup Shorthands: css off
Translate IDs: typedefdef-bodyinit bodyinit,dictdef-requestinit requestinit,typedefdef-requestinfo requestinfo,enumdef-requestdestination requestdestination,enumdef-requestmode requestmode,enumdef-requestcredentials requestcredentials,enumdef-requestcache requestcache,enumdef-requestredirect requestredirect,dictdef-responseinit responseinit,enumdef-responsetype responsetype
</pre>
<pre class=anchors>
url:https://tools.ietf.org/html/rfc7230#section-3.1.1;text:method;type:dfn;spec:http
url:https://tools.ietf.org/html/rfc7230#section-3.2;text:field-name;type:dfn;spec:http
url:https://tools.ietf.org/html/rfc7230#section-3.2;text:field-content;type:dfn;spec:http
url:https://tools.ietf.org/html/rfc7230#section-3.2;text:field-value;type:dfn;spec:http
url:https://tools.ietf.org/html/rfc7230#section-3.1.2;text:reason-phrase;type:dfn;spec:http
url:https://tools.ietf.org/html/rfc7234#section-1.2.1;text:delta-seconds;type:dfn;spec:http-caching
url:https://tools.ietf.org/html/draft-ietf-httpbis-header-structure#section-2;text:structured field value;type:dfn;spec:header-structure
url:https://tools.ietf.org/html/draft-ietf-httpbis-header-structure#section-4.1;text:serializing structured fields;type:dfn;spec:header-structure
url:https://tools.ietf.org/html/draft-ietf-httpbis-header-structure#section-4.2;text:parsing structured fields;type:dfn;spec:header-structure
</pre>
<pre class=biblio>
{
"HTTP": {
"aliasOf": "RFC7230"
},
"HTTP-SEMANTICS": {
"aliasOf": "RFC7231"
},
"HTTP-COND": {
"aliasOf": "RFC7232"
},
"HTTP-CACHING": {
"aliasOf": "RFC7234"
},
"HTTP-RANGE": {
"aliasOf": "RFC7233"
},
"HTTP-AUTH": {
"aliasOf": "RFC7235"
},
"REFERRER": {
"aliasOf": "referrer-policy"
},
"STALE-WHILE-REVALIDATE": {
"aliasOf": "RFC5861"
},
"SW": {
"aliasOf": "service-workers"
},
"UPGRADE": {
"aliasOf": "upgrade-insecure-requests"
},
"HSTS": {
"aliasOf": "RFC6797"
},
"WSP": {
"aliasOf": "RFC6455"
},
"HTTPVERBSEC1": {
"publisher": "US-CERT",
"href": "https://www.kb.cert.org/vuls/id/867593",
"title": "Multiple vendors' web servers enable HTTP TRACE method by default."
},
"HTTPVERBSEC2": {
"publisher": "US-CERT",
"href": "https://www.kb.cert.org/vuls/id/288308",
"title": "Microsoft Internet Information Server (IIS) vulnerable to cross-site scripting via HTTP TRACK method."
},
"HTTPVERBSEC3": {
"publisher": "US-CERT",
"href": "https://www.kb.cert.org/vuls/id/150227",
"title": "HTTP proxy default configurations allow arbitrary TCP connections."
},
"EXPECT-CT": {
"authors": ["Emily Stark"],
"href": "https://tools.ietf.org/html/draft-ietf-httpbis-expect-ct-02",
"publisher": "IETF",
"title": "Expect-CT Extension for HTTP"
},
"OCSP": {
"aliasOf": "RFC2560"
},
"HEADER-STRUCTURE": {
"authors": ["Mark Nottingham", "Poul-Henning Kamp"],
"href": "https://tools.ietf.org/html/draft-ietf-httpbis-header-structure",
"publisher": "IETF",
"title": "Structured Field Values for HTTP"
}
}
</pre>
<h2 id=goals class="no-num short">Goals</h2>
<p>The goal is to unify fetching across the web platform and provide consistent handling of
everything that involves, including:
<ul class=brief>
<li>URL schemes
<li>Redirects
<li>Cross-origin semantics
<li>CSP [[!CSP]]
<li>Service workers [[!SW]]
<li>Mixed Content [[!MIX]]
<li>`<code>Referer</code>` [[!REFERRER]]
</ul>
<p>To do so it also supersedes the HTTP `<a http-header><code>Origin</code></a>` header semantics
originally defined in The Web Origin Concept. [[ORIGIN]]
<h2 id=preface class=short>Preface</h2>
<p>At a high level, fetching a resource is a fairly simple operation. A request goes in, a
response comes out. <!--You can't explain that! -->The details of that operation are
however quite involved and used to not be written down carefully and differ from one API
to the next.
<p>Numerous APIs provide the ability to fetch a resource, e.g. HTML's <code>img</code> and
<code>script</code> element, CSS' <code>cursor</code> and <code>list-style-image</code>,
the <code>navigator.sendBeacon()</code> and <code>self.importScripts()</code> JavaScript
APIs. The Fetch Standard provides a unified architecture for these features so they are
all consistent when it comes to various aspects of fetching, such as redirects and the
CORS protocol.
<p>The Fetch Standard also defines the <a method><code>fetch()</code></a> JavaScript API, which
exposes most of the networking functionality at a fairly low level of abstraction.
<h2 id=infrastructure>Infrastructure</h2>
<p>This specification depends on the Infra Standard. [[!INFRA]]
<p>This specification uses terminology from the ABNF, Encoding, HTML, HTTP, IDL, MIME Sniffing,
Streams, and URL Standards.
[[!ABNF]]
[[!ENCODING]]
[[!HTML]]
[[!HTTP]]
[[!WEBIDL]]
[[!MIMESNIFF]]
[[!STREAMS]]
[[!URL]]
<p><dfn>ABNF</dfn> means ABNF as augmented by HTTP (in particular the addition <code>#</code>) and
RFC 7405. [[!RFC7405]]
<hr>
<p><dfn id=credentials export>Credentials</dfn> are HTTP cookies, TLS client certificates, and <a
lt="authentication entry">authentication entries</a> (for HTTP authentication). [[!COOKIES]]
[[!TLS]] [[!HTTP-AUTH]]
<hr>
<p><a>Tasks</a> that are
<a lt="queue a task">queued</a> by this standard are annotated as one
of:
<ul class=brief>
<li><dfn export>process request body</dfn>
<li id=process-request-end-of-file><dfn export>process request end-of-body</dfn>
<li><dfn export>process response</dfn>
<li id=process-response-end-of-file><dfn export>process response end-of-body</dfn>
<li><dfn export>process response done</dfn>
</ul>
<p>To <dfn>queue a fetch task</dfn> on <a for=/>request</a>
<var>request</var> to <var>run an operation</var>, run these steps:
<ol>
<li><p>If <var>request</var>'s <a for=request>client</a> is
null, terminate these steps.
<li><p><a>Queue a task</a> to
<var>run an operation</var> on <var>request</var>'s
<a for=request>client</a>'s
<a>responsible event loop</a> using the
<a>networking task source</a>.
</ol>
<p>To <dfn>queue a fetch-request-done task</dfn>, given a <var>request</var>,
<a>queue a fetch task</a> on <var>request</var> to <a>process request end-of-body</a>
for <var>request</var>.
<p>To <dfn>serialize an integer</dfn>, represent it as a string of the shortest possible decimal
number.
<p class=XXX>This will be replaced by a more descriptive algorithm in Infra. See
<a href="https://github.com/whatwg/infra/issues/201">infra/201</a>.
<h3 id=url>URL</h3>
<p>A <dfn export>local scheme</dfn> is a <a for=url>scheme</a> that is "<code>about</code>",
"<code>blob</code>", or "<code>data</code>".
<p>A <a for=/>URL</a> <dfn export>is local</dfn> if its <a for=url>scheme</a> is a
<a>local scheme</a>.
<p class=note>This definition is also used by <cite>Referrer Policy</cite>. [[REFERRER]]
<p>An <dfn export id=http-scheme>HTTP(S) scheme</dfn> is a <a for=url>scheme</a> that is
"<code>http</code>" or "<code>https</code>".
<p>A <dfn export>network scheme</dfn> is a <a for=url>scheme</a> that is "<code>ftp</code>" or an
<a>HTTP(S) scheme</a>.
<p>A <dfn export>fetch scheme</dfn> is a <a for=url>scheme</a> that is "<code>about</code>",
"<code>blob</code>", "<code>data</code>", "<code>file</code>", or a <a>network scheme</a>.
<p class="note no-backref"><a>HTTP(S) scheme</a>, <a>network scheme</a>, and <a>fetch scheme</a> are
also used by <cite>HTML</cite>. [[HTML]]
<hr>
<p id=fetch-url>A <dfn>response URL</dfn> is a <a for=/>URL</a> for which implementations need not
store the <a for=url>fragment</a> as it is never exposed. When
<a lt="url serializer">serialized</a>, the <i>exclude fragment flag</i> is set, meaning
implementations can store the <a for=url>fragment</a> nonetheless.
<h3 id=http>HTTP</h3>
<p>While <a lt=fetch for=/>fetching</a> encompasses more than just HTTP, it
borrows a number of concepts from HTTP and applies these to resources obtained via other
means (e.g., <code>data</code> URLs).
<p>An <dfn export>HTTP tab or space</dfn> is U+0009 TAB or U+0020 SPACE.
<p><dfn export>HTTP whitespace</dfn> is U+000A LF, U+000D CR, or an <a>HTTP tab or space</a>.
<p class="note no-backref"><a>HTTP whitespace</a> is only useful for specific constructs that are
reused outside the context of HTTP headers (e.g., <a for=/>MIME types</a>). For HTTP header values,
using <a>HTTP tab or space</a> is preferred, and outside that context <a>ASCII whitespace</a> is
preferred. Unlike <a>ASCII whitespace</a> this excludes U+000C FF.
<p>An <dfn export>HTTP newline byte</dfn> is 0x0A (LF) or 0x0D (CR).
<p>An <dfn export>HTTP tab or space byte</dfn> is 0x09 (HT) or 0x20 (SP).
<p>An <dfn export>HTTP whitespace byte</dfn> is an <a>HTTP newline byte</a> or
<a>HTTP tab or space byte</a>.
<p>To
<dfn export lt="collect an HTTP quoted string|collecting an HTTP quoted string">collect an HTTP quoted string</dfn>
from a <a for=/>string</a> <var>input</var>, given a <a>position variable</a> <var>position</var>
and optionally an <var>extract-value flag</var>, run these steps:
<ol>
<li><p>Let <var>positionStart</var> be <var>position</var>.
<li><p>Let <var>value</var> be the empty string.
<li><p>Assert: the <a>code point</a> at <var>position</var> within <var>input</var> is U+0022 (").
<li><p>Advance <var>position</var> by 1.
<li>
<p>While true:
<ol>
<li><p>Append the result of <a>collecting a sequence of code points</a> that are not U+0022 (")
or U+005C (\) from <var>input</var>, given <var>position</var>, to <var>value</var>.
<li><p>If <var>position</var> is past the end of <var>input</var>, then
<a for=iteration>break</a>.
<li><p>Let <var>quoteOrBackslash</var> be the <a>code point</a> at <var>position</var> within
<var>input</var>.
<li><p>Advance <var>position</var> by 1.
<li>
<p>If <var>quoteOrBackslash</var> is U+005C (\), then:
<ol>
<li><p>If <var>position</var> is past the end of <var>input</var>, then append U+005C (\) to
<var>value</var> and <a for=iteration>break</a>.
<li><p>Append the <a>code point</a> at <var>position</var> within <var>input</var> to
<var>value</var>.
<li><p>Advance <var>position</var> by 1.
</ol>
<li>
<p>Otherwise:
<ol>
<li><p>Assert: <var>quoteOrBackslash</var> is U+0022 (").
<li><p><a for=iteration>Break</a>.
</ol>
</ol>
<li><p>If the <var>extract-value flag</var> is set, then return <var>value</var>.
<li><p>Return the <a for=/>code points</a> from <var>positionStart</var> to <var>position</var>,
inclusive, within <var>input</var>.
</ol>
<div class=example id=example-http-quoted-string>
<table>
<tr>
<th>Input
<th>Output
<th>Output with the <var>extract-value flag</var> set
<th>Final <a>position variable</a> value
<tr>
<td>"<code><mark>"\</mark></code>"
<td>"<code>"\</code>"
<td>"<code>\</code>"
<td>2
<tr>
<td>"<code><mark>"Hello"</mark> World</code>"
<td>"<code>"Hello"</code>"
<td>"<code>Hello</code>"
<td>7
<tr>
<td>"<code><mark>"Hello \\ World\""</mark></code>"
<td>"<code>"Hello \\ World\""</code>"
<td>"<code>Hello \ World"</code>"
<td>18
</table>
<p class=tablenote><small>The <a>position variable</a> always starts at 0 in these examples.</small>
</div>
<h4 id=methods>Methods</h4>
<p>A <dfn export id=concept-method>method</dfn> is a byte sequence that matches the
<a spec=http>method</a> token production.
<p id=simple-method>A <dfn export>CORS-safelisted method</dfn> is a
<a for=/>method</a> that is `<code>GET</code>`,
`<code>HEAD</code>`, or `<code>POST</code>`.
<p>A <dfn export>forbidden method</dfn> is a <a for=/>method</a> that is a
<a>byte-case-insensitive</a> match for `<code>CONNECT</code>`,
`<code>TRACE</code>`, or `<code>TRACK</code>`.
[[HTTPVERBSEC1]], [[HTTPVERBSEC2]], [[HTTPVERBSEC3]]
<p>To <dfn export for=method id=concept-method-normalize>normalize</dfn> a
<a for=/>method</a>, if it is a <a>byte-case-insensitive</a>
match for `<code>DELETE</code>`, `<code>GET</code>`,
`<code>HEAD</code>`, `<code>OPTIONS</code>`, `<code>POST</code>`, or
`<code>PUT</code>`, <a>byte-uppercase</a> it.
<p class="note no-backref"><a lt=normalize for=method>Normalization</a> is
done for backwards compatibility and consistency across APIs as
<a for=/>methods</a> are actually "case-sensitive".
<p id=example-normalization class=example>Using `<code>patch</code>` is highly likely to result in a
`<code>405 Method Not Allowed</code>`. `<code>PATCH</code>` is much more likely to
succeed.
<p class="note no-backref">There are no restrictions on <a for=/>methods</a>.
`<code>CHICKEN</code>` is perfectly acceptable (and not a misspelling of
`<code>CHECKIN</code>`). Other than those that are
<a lt=normalize for=method>normalized</a> there are no casing restrictions either.
`<code>Egg</code>` or `<code>eGg</code>` would be fine, though uppercase is encouraged
for consistency.
<h4 id=terminology-headers>Headers</h4>
<p>A <dfn export id=concept-header-list>header list</dfn> is a <a for=/>list</a> of zero or more
<a for=/>headers</a>. It is initially the empty list.
<p class="note no-backref">A <a for=/>header list</a> is essentially a
specialized multimap: an ordered list of key-value pairs with potentially duplicate keys.
<p>To
<dfn export for="header list" id=concept-header-list-get-structured-header>get a structured field value</dfn>
given a <var>name</var> and a <var>type</var> from a <a for=/>header list</a> <var>list</var>, run
these steps:
<ol>
<li><p>Assert: <var>type</var> is one of "<code>dictionary</code>", "<code>list</code>", or
"<code>item</code>".
<li><p>Let <var>value</var> be the result of <a for="header list">getting</a> <var>name</var> from
<var>list</var>.
<li><p>If <var>value</var> is null, then return null.
<li><p>Let <var>result</var> be the result of <a>parsing structured fields</a> with
<var ignore>input_string</var> set to <var>value</var> and <var ignore>header_type</var> set to
<var>type</var>.
<li><p>If parsing failed, then return null.
<li><p>Return <var>result</var>.
</ol>
<p class="note"><a>Get a structured field value</a> intentionally does not distinguish between a
<a for=/>header</a> not being present and its <a for=header>value</a> failing to parse as a
<a>structured field value</a>. This ensures uniform processing across the web platform.
<p>To
<dfn export for="header list" id=concept-header-list-set-structured-header>set a structured field value</dfn>
<a for=header>name</a>/<a>structured field value</a> <var>name</var>/<var>structuredValue</var>
pair in a <a for=/>header list</a> <var>list</var>, run these steps:
<ol>
<li><p>Let <var>serializedValue</var> be the result of executing the
<a>serializing structured fields</a> algorithm on <var>structuredValue</var>.
<li><p><a for="header list">Set</a> <var>name</var>/<var>serializedValue</var> in <var>list</var>.
</ol>
<p class=note><a>Structured field values</a> are defined as objects which HTTP can (eventually)
serialize in interesting and efficient ways. For the moment, Fetch only supports <a for=/>header</a>
<a for=header>values</a> as <a for=/>byte sequences</a>, which means that these objects can be set
in <a for=/>header lists</a> only via serialization, and they can be obtained from
<a for=/>header lists</a> only by parsing. In the future the fact that they are objects might be
preserved end-to-end. [[!HEADER-STRUCTURE]]
<hr>
<p>A <a for=/>header list</a> <var>list</var>
<dfn export for="header list" lt="contains|does not contain">contains</dfn> a <a for=header>name</a>
<var>name</var> if <var>list</var> <a for=list>contains</a> a <a for=/>header</a> whose
<a for=header>name</a> is a <a>byte-case-insensitive</a> match for <var>name</var>.
<p>To <dfn export for="header list" id=concept-header-list-get>get</dfn> a <a for=header>name</a>
<var>name</var> from a <a for=/>header list</a> <var>list</var>, run these steps:
<ol>
<li><p>If <var>list</var> <a for="header list">does not contain</a> <var>name</var>, then return
null.
<li><p>Return the <a lt=value for=header>values</a> of all <a for=/>headers</a> in <var>list</var>
whose <a for=header>name</a> is a <a>byte-case-insensitive</a> match for <var>name</var>, separated
from each other by 0x2C 0x20, in order.
</ol>
<p>To
<dfn export for="header list" lt="get, decode, and split|getting, decoding, and splitting" id=concept-header-list-get-decode-split>get, decode, and split</dfn>
a <a for=header>name</a> <var>name</var> from <a for=/>header list</a> <var>list</var>, run these
steps:
<ol>
<li><p>Let <var>initialValue</var> be the result of <a for="header list">getting</a>
<var>name</var> from <var>list</var>.
<li><p>If <var>initialValue</var> is null, then return null.
<li><p>Let <var>input</var> be the result of <a>isomorphic decoding</a> <var>initialValue</var>.
<li><p>Let <var>position</var> be a <a for=string>position variable</a> for <var>input</var>,
initially pointing at the start of <var>input</var>.
<li><p>Let <var>values</var> be a <a for=/>list</a> of <a for=/>strings</a>, initially empty.
<li><p>Let <var>value</var> be the empty string.
<li>
<p>While <var>position</var> is not past the end of <var>input</var>:
<ol>
<li>
<p>Append the result of <a>collecting a sequence of code points</a> that are not U+0022 (") or
U+002C (,) from <var>input</var>, given <var>position</var>, to <var>value</var>.
<p class=note>The result might be the empty string.
<li>
<p>If <var>position</var> is not past the end of <var>input</var>, then:
<ol>
<li>
<p>If the <a for=/>code point</a> at <var>position</var> within <var>input</var> is
U+0022 ("), then:
<ol>
<li><p>Append the result of <a>collecting an HTTP quoted string</a> from <var>input</var>,
given <var>position</var>, to <var>value</var>.
<li>If <var>position</var> is not past the end of <var>input</var>, then
<a for=iteration>continue</a>.
</ol>
<li>
<p>Otherwise:
<ol>
<li><p>Assert: the <a for=/>code point</a> at <var>position</var> within <var>input</var> is
U+002C (,).
<li><p>Advance <var>position</var> by 1.
</ol>
</ol>
<li><p>Remove all <a>HTTP tab or space</a> from the start and end of <var>value</var>.
<li><p><a for=list>Append</a> <var>value</var> to <var>values</var>.
<li><p>Set <var>value</var> to the empty string.
</ol>
<li><p>Return <var>values</var>.
</ol>
<div class=example id=example-header-list-get-decode-split>
<p>This is how <a>get, decode, and split</a> functions in practice with `<code>A</code>` as the
<var>name</var> argument:
<table>
<tr>
<th>Headers (as on the network)
<th>Output
<tr>
<td>
<pre><code class=lang-http>
A: nosniff,
</code></pre>
<td rowspan=2>« "<code>nosniff</code>", "" »
<tr>
<td>
<pre><code class=lang-http>
A: nosniff
B: sniff
A:
</code></pre>
<tr>
<td>
<pre><code class=lang-http>A: text/html;", x/x</code></pre>
<td rowspan=2>« "<code>text/html;", x/x</code>" »
<tr>
<td>
<pre><code class=lang-http>
A: text/html;"
A: x/x
</code></pre>
<tr>
<td>
<pre><code class=lang-http>
A: x/x;test="hi",y/y
</code></pre>
<td rowspan=2>« "<code>x/x;test="hi"</code>", "<code>y/y</code>" »
<tr>
<td>
<pre><code class=lang-http>
A: x/x;test="hi"
C: **bingo**
A: y/y
</code></pre>
<tr>
<td>
<pre><code class=lang-http>
A: x / x,,,1
</code></pre>
<td rowspan=2>« "<code>x / x</code>", "", "", "<code>1</code>" »
<tr>
<td>
<pre><code class=lang-http>
A: x / x
A: ,
A: 1
</code></pre>
<tr>
<td>
<pre><code class=lang-http>
A: "1,2", 3
</code></pre>
<td rowspan=2>« "<code>"1,2"</code>", "<code>3</code>" »
<tr>
<td>
<pre><code class=lang-http>
A: "1,2"
D: 4
A: 3
</code></pre>
</table>
</div>
<p>To <dfn export for="header list" id=concept-header-list-append>append</dfn> a
<a for=header>name</a>/<a for=header>value</a> <var>name</var>/<var>value</var> pair to a
<a for=/>header list</a> <var>list</var>, run these steps:
<ol>
<li>
<p>If <var>list</var> <a for="header list">contains</a> <var>name</var>, then set <var>name</var>
to the first such <a for=/>header</a>'s <a for=header>name</a>.
<p class="note no-backref">This reuses the casing of the <a for=header>name</a> of the
<a for=/>header</a> already in <var>list</var>, if any. If there are multiple matched
<a for=/>headers</a> their <a for=header>names</a> will all be identical.
<li><p><a for=list>Append</a> a new <a for=/>header</a> whose <a for=header>name</a> is
<var>name</var> and <a for=header>value</a> is <var>value</var> to <var>list</var>.
</ol>
<p>To <dfn export for="header list" id=concept-header-list-delete>delete</dfn> a
<a for=header>name</a> <var>name</var> from a <a for=/>header list</a> <var>list</var>,
<a for=list>remove</a> all <a for=/>headers</a> whose <a for=header>name</a> is a
<a>byte-case-insensitive</a> match for <var>name</var> from <var>list</var>.
<p>To <dfn export for="header list" id=concept-header-list-set>set</dfn> a
<a for=header>name</a>/<a for=header>value</a> <var>name</var>/<var>value</var> pair in a
<a for=/>header list</a> <var>list</var>, run these steps:
<ol>
<li><p>If <var>list</var> <a for="header list">contains</a> <var>name</var>, then set the
<a for=header>value</a> of the first such <a for=/>header</a> to <var>value</var> and
<a for=list>remove</a> the others.
<li><p>Otherwise, <a for=list>append</a> a new <a for=/>header</a> whose <a for=header>name</a> is
<var>name</var> and <a for=header>value</a> is <var>value</var> to <var>list</var>.
</ol>
<p>To <dfn export for="header list" id=concept-header-list-combine>combine</dfn> a
<a for=header>name</a>/<a for=header>value</a> <var>name</var>/<var>value</var> pair in a
<a for=/>header list</a> <var>list</var>, run these steps:
<ol>
<li><p>If <var>list</var> <a for="header list">contains</a> <var>name</var>, then set the
<a for=header>value</a> of the first such <a for=/>header</a> to its <a for=header>value</a>,
followed by 0x2C 0x20, followed by <var>value</var>.
<li><p>Otherwise, <a for=list>append</a> a new <a for=/>header</a> whose <a for=header>name</a> is
<var>name</var> and <a for=header>value</a> is <var>value</var> to <var>list</var>.
</ol>
<p class="note no-backref"><a for="header list">Combine</a> is used by {{XMLHttpRequest}} and the
<a lt="establish a WebSocket connection">WebSocket protocol handshake</a>.
<p>To <dfn>convert header names to a sorted-lowercase set</dfn>, given a <a for=/>list</a> of
<a lt=name for=header>names</a> <var>headerNames</var>, run these steps:
<ol>
<li><p>Let <var>headerNamesSet</var> be a new <a for/>ordered set</a>.
<li><p><a for=list>For each</a> <var>name</var> of <var>headerNames</var>, <a for=set>append</a>
the result of <a lt=byte-lowercased>byte-lowercasing</a> <var>name</var> to
<var>headerNamesSet</var>.
<li><p>Return the result of <a for=set>sorting</a> <var>headerNamesSet</var> in ascending order
with <a>byte less than</a>.
</ol>
<p>To
<dfn export for="header list" id=concept-header-list-sort-and-combine>sort and combine</dfn>
a <a for=/>header list</a> <var>list</var>, run these steps:
<ol>
<li><p>Let <var>headers</var> be an empty <a for=/>list</a> of
<a for=header>name</a>-<a for=header>value</a> pairs with the key being the <a for=header>name</a>
and value the <a for=header>value</a>.
<li><p>Let <var>names</var> be the result of
<a>convert header names to a sorted-lowercase set</a> with all the <a lt=name for=header>names</a>
of the <a for=/>headers</a> in <var>list</var>.
<li>
<p><a for=list>For each</a> <var>name</var> in <var>names</var>:
<ol>
<li><p>Let <var>value</var> be the result of <a for="header list">getting</a> <var>name</var>
from <var>list</var>.
<li><p>Assert: <var>value</var> is not null.
<li><p><a for=list>Append</a> <var>name</var>-<var>value</var> to <var>headers</var>.
</ol>
<li><p>Return <var>headers</var>.
</ol>
<hr>
<p>A <dfn export id=concept-header>header</dfn> consists of a
<dfn export for=header id=concept-header-name>name</dfn> and
<dfn export for=header id=concept-header-value>value</dfn>.
<p>A <a for=header>name</a> is a <a>byte sequence</a> that matches the <a spec=http>field-name</a>
token production.
<p>A <a for=header>value</a> is a <a>byte sequence</a> that matches the following conditions:
<ul class=brief>
<li><p>Has no leading or trailing <a>HTTP tab or space bytes</a>.
<li><p>Contains no 0x00 (NUL) or <a>HTTP newline bytes</a>.
</ul>
<p class=note>The definition of <a for=header>value</a> is not defined in terms of an HTTP token
production as
<a href=https://github.com/httpwg/http11bis/issues/19 title="fix field-value ABNF">it is broken</a>.
<p>To <dfn export for=header/value id=concept-header-value-normalize>normalize</dfn> a
<var>potentialValue</var>, remove any leading and trailing <a>HTTP whitespace bytes</a> from
<var>potentialValue</var>.
<hr>
<p id=simple-header>To determine whether a <a for=/>header</a> <var>header</var> is a
<dfn export>CORS-safelisted request-header</dfn>, run these steps:
<ol>
<li><p>Let <var>value</var> be <var>header</var>'s <a for=header>value</a>.
<li>
<p><a>Byte-lowercase</a> <var>header</var>'s <a for=header>name</a> and switch on the result:
<dl class=switch>
<dt>`<code>accept</code>`
<dd>
<p>If <var>value</var> contains a <a>CORS-unsafe request-header byte</a>, then return false.
<dt>`<code>accept-language</code>`
<dt>`<code>content-language</code>`
<dd><p>If <var>value</var> contains a byte that is not in the range 0x30 (0) to 0x39 (9),
inclusive, is not in the range 0x41 (A) to 0x5A (Z), inclusive, is not in the range 0x61 (a) to
0x7A (z), inclusive, and is not 0x20 (SP), 0x2A (*), 0x2C (,), 0x2D (-), 0x2E (.), 0x3B (;), or
0x3D (=), then return false.
<!-- Maybe give Infra "byte-alphanumeric"? -->
<dt>`<code>content-type</code>`
<dd>
<ol>
<li><p>If <var>value</var> contains a <a>CORS-unsafe request-header byte</a>, then return
false.
<li><p>Let <var>mimeType</var> be the result of <a lt="parse a MIME type">parsing</a>
<var>value</var>.
<li><p>If <var>mimeType</var> is failure, then return false.
<li><p>If <var>mimeType</var>'s <a for="MIME type">essence</a> is not
"<code>application/x-www-form-urlencoded</code>", "<code>multipart/form-data</code>", or
"<code>text/plain</code>", then return false.
</ol>
<p class=warning>This intentionally does not use <a>extract a MIME type</a> as that algorithm is
rather forgiving and servers are not expected to implement it.
<div class="example no-backref" id=example-cors-safelisted-request-header-content-type>
<p>If <a>extract a MIME type</a> were used the following request would not result in a CORS
preflight and a naïve parser on the server might treat the request body as JSON:
<pre><code class=lang-javascript>
fetch("https://victim.example/naïve-endpoint", {
method: "POST",
headers: [
["Content-Type", "application/json"],
["Content-Type", "text/plain"]
],
credentials: "include",
body: JSON.stringify(exerciseForTheReader)
});
</code></pre>
</div>
<dt>Otherwise
<dd><p>Return false.
</dl>
<li><p>If <var>value</var>'s <a for="byte sequence">length</a> is greater than 128, then return
false.
<li><p>Return true.
</ol>
<p class="note">There are limited exceptions to the `<code>Content-Type</code>` header safelist, as
documented in <a href=#cors-protocol-exceptions>CORS protocol exceptions</a>.
<p>A <dfn>CORS-unsafe request-header byte</dfn> is a byte <var>byte</var> for which one of the
following is true:
<ul class=brief>
<li><p><var>byte</var> is less than 0x20 and is not 0x09 HT
<li><p><var>byte</var> is 0x22 ("), 0x28 (left parenthesis), 0x29 (right parenthesis), 0x3A (:),
0x3C (<), 0x3E (>), 0x3F (?), 0x40 (@), 0x5B ([), 0x5C (\), 0x5D (]), 0x7B ({), 0x7D (}), or
0x7F DEL.
<!-- Delimiters from https://tools.ietf.org/html/rfc7230#section-3.2.6 except for ,/;= and
including DEL -->
</ul>
<p>The <dfn noexport>CORS-unsafe request-header names</dfn>, given a <a for=/>header list</a>
<var>headers</var>, are determined as follows:
<ol>
<li><p>Let <var>unsafeNames</var> be a new <a for=/>list</a>.
<li><p>Let <var>potentiallyUnsafeNames</var> be a new <a for=/>list</a>.
<li><p>Let <var>safelistValueSize</var> be 0.
<li>
<p><a for=list>For each</a> <var>header</var> of <var>headers</var>:
<ol>
<li><p>If <var>header</var> is not a <a>CORS-safelisted request-header</a>, then
<a for=list>append</a> <var>header</var>'s <a for=header>name</a> to <var>unsafeNames</var>.
<li><p>Otherwise, <a for=list>append</a> <var>header</var>'s <a for=header>name</a> to
<var>potentiallyUnsafeNames</var> and increase <var>safelistValueSize</var> by
<var>header</var>'s <a for=header>value</a>'s <a for="byte sequence">length</a>.
</ol>
<li><p>If <var>safelistValueSize</var> is greater than 1024, then <a for=list>for each</a>
<var>name</var> of <var>potentiallyUnsafeNames</var>, <a for=list>append</a> <var>name</var> to
<var>unsafeNames</var>.
<li><p>Return the result of <a>convert header names to a sorted-lowercase set</a> with
<var>unsafeNames</var>.
</ol>
<p>A <dfn export>CORS non-wildcard request-header name</dfn> is a <a>byte-case-insensitive</a> match
for `<code>Authorization</code>`.
<p>A <dfn export>privileged no-CORS request-header name</dfn> is a <a for=/>header</a>
<a for=header>name</a> that is a <a>byte-case-insensitive</a> match for one of
<ul class=brief>
<li>`<code>Range</code>`.
</ul>
<div class="note no-backref">
<p>These are headers that can be set by privileged APIs, and will be preserved if their associated
request object is copied, but will be removed if the request is modified by unprivilaged APIs.
<p>`<code>Range</code>` headers are commonly used by <a lt="downloads a hyperlink">downloads</a>
and <a lt="resource fetch algorithm">media fetches</a>, although neither of these currently specify
how. <a href=https://github.com/whatwg/html/pull/2814>html/2914</a> aims to solve this.
<p>A helper is provided to <a for=request>add a range header</a> to a particular request.
</div>
<p>A <dfn export>CORS-safelisted response-header name</dfn>, given a
<a for=response>CORS-exposed header-name list</a> <var>list</var>, is a <a for=/>header</a>
<a for=header>name</a> that is a <a>byte-case-insensitive</a> match for one of
<ul class=brief>
<li>`<code>Cache-Control</code>`
<li>`<code>Content-Language</code>`
<li>`<code>Content-Length</code>`
<li>`<code>Content-Type</code>`
<li>`<code>Expires</code>`
<li>`<code>Last-Modified</code>`
<li>`<code>Pragma</code>`
<li>Any <a for=header>value</a> in <var>list</var> that is not a
<a>forbidden response-header name</a>.
</ul>
<p>A <dfn noexport>no-CORS-safelisted request-header name</dfn> is a <a for=/>header</a>
<a for=header>name</a> that is a <a>byte-case-insensitive</a> match for one of
<ul class=brief>
<li>`<code>Accept</code>`
<li>`<code>Accept-Language</code>`
<li>`<code>Content-Language</code>`
<li>`<code>Content-Type</code>`
</ul>
<p>To determine whether a <a for/>header</a> <var>header</var> is a
<dfn noexport>no-CORS-safelisted request-header</dfn>, run these steps:
<ol>
<li><p>If <var>header</var>'s <a for=header>name</a> is not a
<a>no-CORS-safelisted request-header name</a>, then return false.
<li><p>Return whether <var>header</var> is a <a>CORS-safelisted request-header</a>.
</ol>
<p>A <dfn export>forbidden header name</dfn> is a <a for=/>header</a> <a for=header>name</a> that
is a <a>byte-case-insensitive</a> match for one of
<ul class=brief>
<li>`<code>Accept-Charset</code>`
<li>`<code>Accept-Encoding</code>`
<li>`<a http-header><code>Access-Control-Request-Headers</code></a>`
<li>`<a http-header><code>Access-Control-Request-Method</code></a>`
<li>`<code>Connection</code>`
<li>`<code>Content-Length</code>`
<li>`<code>Cookie</code>`
<li>`<code>Cookie2</code>`
<li>`<code>Date</code>`
<li>`<code>DNT</code>`
<li>`<code>Expect</code>`
<li>`<code>Host</code>`
<li>`<code>Keep-Alive</code>`
<li>`<a http-header><code>Origin</code></a>`
<li>`<code>Referer</code>`
<li>`<code>TE</code>`
<li>`<code>Trailer</code>`
<li>`<code>Transfer-Encoding</code>`
<li>`<code>Upgrade</code>`
<li>`<code>Via</code>`
</ul>
<p>or a <a for=/>header</a> <a for=header>name</a> that
starts with a <a>byte-case-insensitive</a> match for `<code>Proxy-</code>` or `<code>Sec-</code>`
(including being a <a>byte-case-insensitive</a> match for just `<code>Proxy-</code>` or
`<code>Sec-</code>`).
<p class=note>These are forbidden so the user agent remains in full control over them.
<a for=header>Names</a> starting with `<code>Sec-</code>` are
reserved to allow new <a for=/>headers</a> to be minted that are safe
from APIs using <a for=/>fetch</a> that allow control over
<a for=/>headers</a> by developers, such as
{{XMLHttpRequest}}.
[[XHR]]
<p>A <dfn export>forbidden response-header name</dfn> is a <a for=/>header</a>
<a for=header>name</a> that is a <a>byte-case-insensitive</a> match for one of:
<ul class=brief>
<li>`<code>Set-Cookie</code>`
<li>`<code>Set-Cookie2</code>`
</ul>
<p>A <dfn export>request-body-header name</dfn> is a <a for=/>header</a> <a for=header>name</a> that
is a <a>byte-case-insensitive</a> match for one of:
<ul class=brief>
<li>`<code>Content-Encoding</code>`
<li>`<code>Content-Language</code>`
<li>`<code>Content-Location</code>`
<li>`<code>Content-Type</code>`
</ul>
<hr>
<p>To <dfn export lt="extract header values|extracting header values">extract header values</dfn>
given a <a for=/>header</a> <var>header</var>, run these steps:
<ol>
<li><p>If parsing <var>header</var>'s <a for=header>value</a>, per the <a>ABNF</a> for
<var>header</var>'s <a for=header>name</a>, fails, then return failure.
<li><p>Return one or more <a for=header>values</a> resulting from parsing <var>header</var>'s
<a for=header>value</a>, per the <a>ABNF</a> for <var>header</var>'s <a for=header>name</a>.
</ol>
<p>To
<dfn export lt="extract header list values|extracting header list values">extract header list values</dfn>
given a <a for=header>name</a> <var>name</var> and a <a for=/>header list</a> <var>list</var>,
run these steps:
<ol>
<li><p>If <var>list</var> <a for="header list">does not contain</a> <var>name</var>, then return
null.
<li>
<p>If the <a>ABNF</a> for <var>name</var> allows a single <a for=/>header</a> and <var>list</var>
<a for="header list">contains</a> more than one, then return failure.
<p class="note no-backref">If different error handling is needed, extract the desired
<a for=/>header</a> first.
<li><p>Let <var>values</var> be an empty <a for=/>list</a>.
<li>
<p>For each <a for=/>header</a> <var>header</var> <var>list</var>
<a for="header list">contains</a> whose <a for=header>name</a> is <var>name</var>:
<ol>
<li><p>Let <var>extract</var> be the result of <a>extracting header values</a> from
<var>header</var>.
<li><p>If <var>extract</var> is failure, then return failure.
<li><p>Append each <a for=header>value</a> in <var>extract</var>, in order, to <var>values</var>.
</ol>
<li><p>Return <var>values</var>.
</ol>
<hr>
<p>A <dfn id=default-user-agent-value export>default `<code>User-Agent</code>` value</dfn> is a
user-agent-defined <a for=header>value</a> for the
`<code>User-Agent</code>` <a for=/>header</a>.
<h4 id=statuses>Statuses</h4>
<p>A <dfn export id=concept-status>status</dfn> is a code.
<p>A <dfn export>null body status</dfn> is a <a for=/>status</a> that is 101, 204, 205, or 304.
<p>An <dfn export>ok status</dfn> is any <a for=/>status</a> in the range 200 to 299, inclusive.
<p>A <dfn export>redirect status</dfn> is a <a for=/>status</a> that is 301, 302, 303, 307, or 308.
<h4 id=bodies>Bodies</h4>
<p>A <dfn export id=concept-body>body</dfn> consists of: