/
Overview.html
2621 lines (2189 loc) · 84.7 KB
/
Overview.html
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
<!DOCTYPE html><meta charset="utf-8">
<title>MIME Sniffing Standard</title>
<style>
@media print { [data-anolis-spec]::after { content:"[" attr(data-anolis-spec) "]"; font-size:.6em; vertical-align:super; text-transform:uppercase } }
</style>
<link href="http://www.whatwg.org/style/specification" rel="stylesheet">
<link href="http://resources.whatwg.org/logo-mime.png" rel="icon" type="image/png">
<link href="http://resources.whatwg.org/logo-mime.svg" rel="icon" type="image/svg+xml">
<div class="head">
<p><a class="logo" href="http://www.whatwg.org/"><img alt="WHATWG" height="101" src="http://resources.whatwg.org/logo-mime.svg" width="101"></a></p>
<h1>MIME Sniffing</h1>
<h2 class="no-num no-toc" id="living-standard-—-last-updated-17-november-2012">Living Standard — Last Updated 17 November 2012</h2>
<dl>
<dt>This Version:
<dd>
<a href="http://mimesniff.spec.whatwg.org/">http://mimesniff.spec.whatwg.org/</a>
<dt>Participate:
<dd>
Send feedback to
<a href="http://www.whatwg.org/mailing-list">whatwg@whatwg.org</a>
(<a href="http://www.whatwg.org/mailing-list#specs">archives</a>)
or
<a href="https://www.w3.org/Bugs/Public/enter_bug.cgi?product=WHATWG&component=MIME">file a bug</a>
(<a href="https://www.w3.org/Bugs/Public/buglist.cgi?product=WHATWG&component=MIME&resolution=---">open bugs</a>)
<dd>
<a href="http://wiki.whatwg.org/wiki/IRC">IRC: #whatwg on Freenode</a>
<dt>Version history:
<dd>
<a href="https://github.com/whatwg/mimesniff/commits">https://github.com/whatwg/mimesniff/commits</a>
<dd>
<a href="https://twitter.com/mimesniff">@mimesniff</a>
<dt>Editor:
<dd>
<a href="http://gphemsley.org/">Gordon P. Hemsley</a> <<a href="mailto:me@gphemsley.org">me@gphemsley.org</a>>
<dt>Past editors:
<dd>
Adam Barth <<a href="mailto:whatwg@adambarth.com">whatwg@adambarth.com</a>> (Google)
<dd>
Ian Hickson <<a href="mailto:ian@hixie.ch">ian@hixie.ch</a>> (Google)
</dl>
<script async="" src="http://resources.whatwg.org/file-bug.js"></script>
<p class="copyright">
<a href="http://creativecommons.org/publicdomain/zero/1.0/" rel="license">CC0 1.0 Universal</a>.
To the extent possible under law, the editor has waived all copyright and
related or neighboring rights to this work.
In addition, as of 17 November 2012, the editor has made this
specification available under the
<a href="http://www.openwebfoundation.org/legal/the-owf-1-0-agreements/owfa-1-0" rel="license">Open Web Foundation Agreement Version 1.0</a>,
which is available at
http://www.openwebfoundation.org/legal/the-owf-1-0-agreements/owfa-1-0.
</div>
<h2 class="no-num no-toc" id="abstract">Abstract</h2>
<p>
Many web servers supply incorrect <code>Content-Type</code> header fields
with their HTTP responses.
In order to be compatible with these servers, user agents consider the
content of HTTP responses as well as the <code>Content-Type</code> header
fields when determining the effective media type of the response.
This document describes an algorithm for determining the effective media type
of HTTP responses that balances security and compatibility considerations.
<h2 class="no-num" id="table-of-contents">Table of contents</h2>
<!--begin-toc-->
<ol class="toc">
<li><a class="no-num" href="#table-of-contents">Table of contents</a></li>
<li><a href="#introduction"><span class="secno">1 </span>Introduction</a></li>
<li><a href="#conventions"><span class="secno">2 </span>Conventions</a></li>
<li><a href="#terminology"><span class="secno">3 </span>Terminology</a></li>
<li><a href="#handling-a-resource"><span class="secno">4 </span>Handling a resource</a></li>
<li><a href="#determining-the-supplied-media-type-of-a-resource"><span class="secno">5 </span>Determining the supplied media type of a resource</a></li>
<li><a href="#reading-the-resource-header"><span class="secno">6 </span>Reading the resource header</a></li>
<li><a href="#matching-a-media-type-pattern"><span class="secno">7 </span>Matching a media type pattern</a>
<ol>
<li><a href="#matching-an-image-type-pattern"><span class="secno">7.1 </span>Matching an image type pattern</a></li>
<li><a href="#matching-an-audio-type-pattern"><span class="secno">7.2 </span>Matching an audio type pattern</a></li>
<li><a href="#matching-a-video-type-pattern"><span class="secno">7.3 </span>Matching a video type pattern</a>
<ol>
<li><a href="#signature-for-mp4"><span class="secno">7.3.1 </span>Signature for MP4</a></ol></li>
<li><a href="#matching-a-font-type-pattern"><span class="secno">7.4 </span>Matching a font type pattern</a></li>
<li><a href="#matching-an-archive-type-pattern"><span class="secno">7.5 </span>Matching an archive type pattern</a></ol></li>
<li><a href="#determining-the-sniffed-media-type-of-a-resource"><span class="secno">8 </span>Determining the sniffed media type of a resource</a>
<ol>
<li><a href="#identifying-a-resource-with-an-unknown-media-type"><span class="secno">8.1 </span>Identifying a resource with an unknown media type</a></li>
<li><a href="#sniffing-a-mislabeled-feed"><span class="secno">8.2 </span>Sniffing a mislabeled feed</a></li>
<li><a href="#sniffing-a-mislabeled-binary-resource"><span class="secno">8.3 </span>Sniffing a mislabeled binary resource</a></li>
<li><a href="#sniffing-images-specifically"><span class="secno">8.4 </span>Sniffing images specifically</a></li>
<li><a href="#sniffing-audio-specifically"><span class="secno">8.5 </span>Sniffing audio specifically</a></li>
<li><a href="#sniffing-video-specifically"><span class="secno">8.6 </span>Sniffing video specifically</a></li>
<li><a href="#sniffing-fonts-specifically"><span class="secno">8.7 </span>Sniffing fonts specifically</a></ol></li>
<li><a class="no-num" href="#references">References</a></li>
<li><a class="no-num" href="#acknowledgements">Acknowledgements</a></ol>
<!--end-toc-->
<h2 id="introduction"><span class="secno">1 </span>Introduction</h2>
<p>
The HTTP <code>Content-Type</code> header field indicates the media type of
an HTTP response.
However, many HTTP servers supply a <code>Content-Type</code> that does not
match the actual contents of the response.
Historically, web browsers have tolerated these servers by examining the
content of HTTP responses in addition to the <code>Content-Type</code> header
field to determine the effective media type of the response.
<p>
Without a clear specification of how to "sniff" the media type, each user
agent has been forced to reverse-engineer the algorithms of other user agents
in order to maintain interoperability.
Inevitably, these efforts have not been entirely successful, resulting in
divergent behaviors among user agents.
In some cases, these divergent behaviors have had security implications, as a
user agent could interpret an HTTP response as a different media type than
the server intended.
<p>
These security issues are most severe when an "honest" server allows
potentially malicious users to upload their own files and then serves the
contents of those files with a low-privilege media type.
For example, if a server believes that the client will treat a contributed
file as an image (and thus treat it as benign), but a user agent believes the
content to be HTML (and thus privileged to execute any scripts contained
therein), an attacker might be able to steal the user's authentication
credentials and mount other cross-site scripting attacks.
(Malicious servers, of course, can specify an arbitrary media type in the
<code>Content-Type</code> header field.)
In the absence of media type sniffing, this user-generated content would not
be interpreted as a high-privilege media type.
<p>
This document describes a content sniffing algorithm that carefully balances
the compatibility needs of user agent with the security constraints imposed
by existing web content.
The algorithm originated from research conducted by Adam Barth, Juan
Caballero, and Dawn Song, based on content sniffing algorithms present in
popular user agents, an extensive database of existing web content, and
metrics collected from implementations deployed to a sizable number of users.
<a class="informative" href="#refsSECCONTSNIFF">[SECCONTSNIFF]</a>
<p>
Whenever possible, user agents should not employ a content sniffing
algorithm.
However, if a user agent does employ a content sniffing algorithm, the user
agent should use the algorithm in this document; using a different content
sniffing algorithm could have unexpected security implications.
<h2 id="conventions"><span class="secno">2 </span>Conventions</h2>
<p>
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 RFC 2119.
<a href="#refsRFC2119">[RFC2119]</a>
<p>
Requirements phrased in the imperative as part of algorithms (such as "strip
any leading space characters" or "return false and abort these steps") are to
be interpreted with the meaning of the key word ("MUST", "SHOULD", "MAY",
etc.) used in introducing the algorithm.
<p>
Conformance requirements phrased as algorithms or specific steps can be
implemented in any manner, so long as the end result is equivalent.
In particular, note that the algorithms defined in this specification are
intended to be easy to understand and are not intended to be performant.
<h2 id="terminology"><span class="secno">3 </span>Terminology</h2>
<p>
A <dfn id="byte">byte</dfn> is a <a data-anolis-spec="encoding" href="#byte">byte</a> as defined
in the Encoding Standard.
When helpful, a <a href="#byte">byte</a> will be accompanied by information about
the ASCII code point it represents.
<a href="#refsENCODING">[ENCODING]</a>
<a class="informative" href="#refsASCII">[ASCII]</a>
<p>
A <dfn id="binary-data-byte">binary data byte</dfn> is a <a href="#byte">byte</a> in the range 0x00 to
0x08 (NUL to BS), the <a href="#byte">byte</a> 0x0B (VT), a <a href="#byte">byte</a> in the
range 0x0E to 0x1A (SO to SUB), or a <a href="#byte">byte</a> in the range 0x1C to
0x1F (FS to US).
<p>
A <dfn id="whitespace-byte">whitespace byte</dfn> (0xWS) is any of the following
<a href="#byte" title="byte">bytes</a>: 0x09 (HT), 0x0A (LF), 0x0C (FF), 0x0D (CR),
0x20 (SP).
<p>
A <dfn id="tag-terminating-byte">tag-terminating byte</dfn> (0xTT) is any of the following
<a href="#byte" title="byte">bytes</a>: 0x20 (SP), 0x3E ("<code title="">></code>").
<p>
A <dfn id="byte-sequence">byte sequence</dfn> is a <span>sequence</span> of
<a href="#byte" title="byte">bytes</a>.
<p>
A <dfn id="resource-header">resource header</dfn> is the <a href="#byte-sequence">byte sequence</a> at the
beginning of a <a href="#resource">resource</a>, as determined by
<a href="#read-the-resource-header" title="read the resource header">reading the resource header</a>.
<p>
The <dfn id="media-type">media type</dfn> of a <a href="#resource">resource</a> is a technical hint
about the use and format of that <a href="#resource">resource</a>.
<a class="informative" href="#refsMIMETYPE">[MIMETYPE]</a>
<p>
A <dfn id="valid-media-type">valid media type</dfn> is a
<a class="external" data-anolis-spec="http" href="http://tools.ietf.org/html/rfc2616/#section-3.7">media-type</a>, as defined in HTTP/1.1 or its
successor, and is made up in part by a
<a class="external" data-anolis-spec="http" href="http://tools.ietf.org/html/rfc2616/#section-3.7">type</a> and a
<a class="external" data-anolis-spec="http" href="http://tools.ietf.org/html/rfc2616/#section-3.7">subtype</a>.
The <dfn id="media-type-portion">media type portion</dfn> of a <a href="#valid-media-type">valid media type</a> is the
string represented by concatenating <a class="external" data-anolis-spec="http" href="http://tools.ietf.org/html/rfc2616/#section-3.7">type</a>,
the string "<code title="">/</code>", and
<a class="external" data-anolis-spec="http" href="http://tools.ietf.org/html/rfc2616/#section-3.7">subtype</a>.
<a href="#refsHTTP">[HTTP]</a>
<p class="note">
The <a href="#media-type-portion">media type portion</a> of a <a href="#valid-media-type">valid media type</a>
excludes all <a class="external" data-anolis-spec="http" href="http://tools.ietf.org/html/rfc2616/#section-3.7" title="parameter">parameters</a>
and is case-insensitive.
<a href="#refsHTTP">[HTTP]</a>
<p>
A <a href="#valid-media-type">valid media type</a> is <dfn id="supported-by-the-user-agent">supported by the user agent</dfn> if
the user agent has the capability to interpret a <a href="#resource">resource</a> of
that <a href="#media-type">media type</a> and present it to the user.
<p>
An <dfn id="image-type">image type</dfn> is any <a href="#valid-media-type">valid media type</a> where
<a class="external" data-anolis-spec="http" href="http://tools.ietf.org/html/rfc2616/#section-3.7">type</a> is equal to
"<code title="">image</code>"<!-- or the <span>media type portion</span> is
equal to one of the following:
<ul>
<li>
</ul>-->.
<p>
An <dfn id="audio-type">audio type</dfn> is any <a href="#valid-media-type">valid media type</a> where
<a class="external" data-anolis-spec="http" href="http://tools.ietf.org/html/rfc2616/#section-3.7">type</a> is equal to
"<code title="">audio</code>" or the <a href="#media-type-portion">media type portion</a> is equal to
one of the following:
<ul>
<li>
<code title="">application/ogg</code>
</ul>
<p>
A <dfn id="video-type">video type</dfn> is any <a href="#valid-media-type">valid media type</a> where
<a class="external" data-anolis-spec="http" href="http://tools.ietf.org/html/rfc2616/#section-3.7">type</a> is equal to
"<code title="">video</code>" or the <a href="#media-type-portion">media type portion</a> is equal to
one of the following:
<ul>
<li>
<code title="">application/ogg</code>
</ul>
<p>
A <dfn id="font-type">font type</dfn> is any <a href="#valid-media-type">valid media type</a> where
<!--<span data-anolis-spec=http>type</span> is equal to "<code title>font</code>"
or--> the <a href="#media-type-portion">media type portion</a> is equal to one of the following:
<ul class="XXX">
<li>
<code title="">application/font-ttf</code>
<li>
<code title="">application/font-cff</code>
<li>
<code title="">application/font-off</code>
<li>
<code title="">application/font-sfnt</code>
<li>
<code title="">application/vnd.ms-opentype</code>
<li>
<code title="">application/font-woff</code>
</ul>
<p>
An <dfn id="archive-type">archive type</dfn> is any <a href="#valid-media-type">valid media type</a> where
<!--<span data-anolis-spec=http>type</span> is equal to
"<code title>archive</code>" or--> the <a href="#media-type-portion">media type portion</a> is equal
to one of the following:
<ul>
<li>
<code title="">application/x-rar-compressed</code>
<li>
<code title="">application/zip</code>
<li>
<code title="">application/x-gzip</code>
</ul>
<p>
An <dfn id="xml-type">XML type</dfn> is any <a href="#valid-media-type">valid media type</a> where
<a class="external" data-anolis-spec="http" href="http://tools.ietf.org/html/rfc2616/#section-3.7">subtype</a> ends in "<code title="">+xml</code>"
or the <a href="#media-type-portion">media type portion</a> is equal to
"<code title="">text/xml</code>" or "<code title="">application/xml</code>".
<a class="informative" href="#refsRFC3023">[RFC3023]</a>
<p>
A <dfn id="scriptable-media-type">scriptable media type</dfn> is an <a href="#xml-type">XML type</a> or any of the
following <a href="#valid-media-type" title="valid media type">valid media types</a>:
<ul>
<li>
<code title="">text/html</code>
<li>
<code title="">application/pdf</code>
</ul>
<p>
A <dfn id="byte-pattern">byte pattern</dfn> is a <a href="#byte-sequence">byte sequence</a> used as a template
to be matched against in the <a href="#pattern-matching-algorithm">pattern matching algorithm</a>.
<p>
A <dfn id="pattern-mask">pattern mask</dfn> is a <a href="#byte-sequence">byte sequence</a> used to determine
the significance of <a href="#byte" title="byte">bytes</a> being compared against a
<a href="#byte-pattern">byte pattern</a> in the <a href="#pattern-matching-algorithm">pattern matching algorithm</a>.
<p class="note">
In a <a href="#pattern-mask">pattern mask</a>, 0xFF indicates the <a href="#byte">byte</a> is
strictly significant, 0xDF indicates that the <a href="#byte">byte</a> is
significant in an ASCII case-insensitive way, and 0x00 indicates that the
<a href="#byte">byte</a> is not significant.
<h2 id="handling-a-resource"><span class="secno">4 </span>Handling a resource</h2>
<p class="XXX">
A <dfn id="resource">resource</dfn> is ….
<p>
Every <a href="#resource">resource</a> has the following metadata associated with it:
<ul>
<li>
A <dfn id="supplied-media-type">supplied media type</dfn>, the <a href="#valid-media-type">valid media type</a>
determined by the <a href="#supplied-media-type-detection-algorithm">supplied media type detection algorithm</a>.
<li>
A <dfn id="sniffed-media-type">sniffed media type</dfn>, the <a href="#valid-media-type">valid media type</a>
determined by the <a href="#media-type-sniffing-algorithm">media type sniffing algorithm</a>.
<li>
A <dfn id="has-content-type-flag">has-content-type flag</dfn>, which defaults to unset.
<li>
A <dfn id="no-sniff-flag">no-sniff flag</dfn>, which defaults to set if the user agent does
not wish to perform sniffing on the <a href="#resource">resource</a> and unset
otherwise.
<p class="note">
The user agent can choose to use outside information, such as previous
experience with a site, to determine whether to opt out of sniffing for a
particular <a href="#resource">resource</a>. The user agent can also choose to opt
out of sniffing for all <a href="#resource" title="resource">resources</a>. However,
opting out of sniffing does not exempt the user agent from using the
<a href="#media-type-sniffing-algorithm">media type sniffing algorithm</a>.
</ul>
<h2 id="determining-the-supplied-media-type-of-a-resource"><span class="secno">5 </span>Determining the supplied media type of a resource</h2>
<p>
The <a href="#supplied-media-type">supplied media type</a> of a <a href="#resource">resource</a> is provided
to the user agent by an external source associated with that
<a href="#resource">resource</a>.
The method of obtaining this information varies depending upon how the
<a href="#resource">resource</a> is retrieved.
<p>
To determine the <a href="#supplied-media-type">supplied media type</a> of a <a href="#resource">resource</a>,
user agents must use the following <dfn id="supplied-media-type-detection-algorithm">supplied media type detection
algorithm</dfn>:
<ol>
<li>
Let <var>supplied-type</var> be null.
<li>
If the <a href="#resource">resource</a> is retrieved via HTTP and one or more
<code>Content-Type</code> headers are associated with the
<a href="#resource">resource</a>, set <var>supplied-type</var> to the value associated
with the last such <code>Content-Type</code> header and set the
<a href="#has-content-type-flag">has-content-type flag</a>.
<a href="#refsHTTP">[HTTP]</a>
<p class="note">
File extensions are not used to determine the <a href="#supplied-media-type">supplied media
type</a> of a <a href="#resource">resource</a> retrieved via HTTP because they are
unreliable and easily spoofed.
<li>
If the <a href="#resource">resource</a> is retrieved directly from the file system,
set <var>supplied-type</var> to the <a href="#media-type">media type</a> provided by the
file system.
<li>
If the <a href="#resource">resource</a> is retrieved via another protocol (such as
FTP), set <var>supplied-type</var> to the <a href="#media-type">media type</a> as
determined by that protocol, if any.
<a class="informative" href="#refsFTP">[FTP]</a>
<li>
If <var>supplied-type</var> is not a <a href="#valid-media-type">valid media type</a>, the
<a href="#supplied-media-type">supplied media type</a> is undefined.
Abort these steps.
<li>
The <a href="#supplied-media-type">supplied media type</a> is <var>supplied-type</var>.
</ol>
<h2 id="reading-the-resource-header"><span class="secno">6 </span>Reading the resource header</h2>
<p>
To <dfn id="read-the-resource-header">read the resource header</dfn>, perform the following steps:
<ol>
<li>
Let <var>buffer</var> be a <a href="#byte-sequence">byte sequence</a>.
<li>
Read <a href="#byte" title="byte">bytes</a> of the <a href="#resource">resource</a> into
<var>buffer</var> until one of the following conditions is met:
<ul>
<li>
the end of the <a href="#resource">resource</a> is reached.
<li>
the number of <a href="#byte" title="byte">bytes</a> in <var>buffer</var> is
greater than or equal to 512.
<li>
a reasonable amount of time has elapsed, as determined by the user
agent.
</ul>
<p class="note">
If the number of <a href="#byte" title="byte">bytes</a> in <var>buffer</var> is
greater than or equal to 512, the <a href="#media-type-sniffing-algorithm">media type sniffing
algorithm</a> will be deterministic for the majority of cases.
However, certain factors (such as a slow connection) may prevent the
user agent from reading 512 <a href="#byte" title="byte">bytes</a> in a
reasonable amount of time.
<li>
The <a href="#resource-header">resource header</a> is <var>buffer</var>.
</ol>
<p class="note">
The <a href="#resource-header">resource header</a> need only be determined once per
<a href="#resource">resource</a>.
<h2 id="matching-a-media-type-pattern"><span class="secno">7 </span>Matching a media type pattern</h2>
<p>
To determine whether a <a href="#byte-sequence">byte sequence</a> matches a particular
<a href="#byte-pattern">byte pattern</a>, use the following <dfn id="pattern-matching-algorithm">pattern matching
algorithm</dfn>:
<ol>
<li>
Let <var>sequence</var> be the <a href="#byte-sequence">byte sequence</a> to be matched,
where <var>sequence</var>[<var>s</var>] is <a href="#byte">byte</a> <var>s</var>
in <var>sequence</var> and <var>sequence</var>[0] is the first
<a href="#byte">byte</a> in <var>sequence</var>.
<li>
Let <var>pattern</var> be the <a href="#byte-pattern">byte pattern</a> to be matched
against, where <var>pattern</var>[<var>p</var>] is <a href="#byte">byte</a>
<var>p</var> in <var>pattern</var> and <var>pattern</var>[0] is the first
<a href="#byte">byte</a> in <var>pattern</var>.
<li>
Let <var>mask</var> be the <a href="#pattern-mask">pattern mask</a>, where
<var>mask</var>[<var>m</var>] is <a href="#byte">byte</a> <var>m</var> in
<var>mask</var> and <var>mask</var>[0] is the first <a href="#byte">byte</a> in
<var>mask</var>.
<li>
Let <var>length</var> be the number of <a href="#byte" title="byte">bytes</a> in
<var>pattern</var>, which is equal to the number of
<a href="#byte" title="byte">bytes</a> in <var>mask</var>.
<li>
If the number of <a href="#byte" title="byte">bytes</a> in <var>sequence</var> is
less than <var>length</var>, return false.
<li>
Initialize <var>s</var>, <var>p</var>, and <var>m</var> to 0.
<li>
Begin loop <var>L</var>:
<ol>
<li>
If <var>sequence</var>[<var>s</var>] is undefined, return false.
<li>
If <var>sequence</var>[<var>s</var>] is not one of the leading
<a href="#byte" title="byte">bytes</a> to be ignored, exit loop <var>L</var>.
<li>
Increment <var>s</var> by 1.
</ol>
<li>
Begin loop <var>L</var>:
<ol>
<li>
If <var>pattern</var>[<var>p</var>] is undefined, exit loop <var>L</var>.
<li>
Let <var>masked-data</var> be the result of applying the bitwise AND
operator to <var>sequence</var>[<var>s</var>] and
<var>mask</var>[<var>m</var>].
<li>
If <var>masked-data</var> is not equal to
<var>pattern</var>[<var>p</var>], return false.
<li>
Increment <var>s</var>, <var>p</var>, and <var>m</var> by 1.
</ol>
<li>
Return true.
</ol>
<h3 id="matching-an-image-type-pattern"><span class="secno">7.1 </span>Matching an image type pattern</h3>
<p>
To determine which <a href="#image-type">image type</a> <a href="#byte-pattern">byte pattern</a> a
<a href="#byte-sequence">byte sequence</a> matches, if any, use the following <dfn id="image-type-pattern-matching-algorithm">image type
pattern matching algorithm</dfn>:
<ol>
<li>
Execute the following steps for each row in the following table:
<ol>
<li>
Let <var>pattern-matched</var> be the result of executing the
<a href="#pattern-matching-algorithm">pattern matching algorithm</a> with the following parameters:
<ul>
<li>
Let the <a href="#byte-sequence">byte sequence</a> to be matched be the <a href="#byte-sequence">byte
sequence</a> to be matched.
<li>
Let the <a href="#byte-pattern">byte pattern</a> to be matched against be the value in
the first column of the current row.
<li>
Let the <a href="#pattern-mask">pattern mask</a> be the value in the second column of
the current row.
<li>
Let the leading <a href="#byte" title="byte">bytes</a> to be ignored be the
value in the third column of the current row.
</ul>
<li>
If <var>pattern-matched</var> is true, return the value in the fourth
column of the current row.
</ol>
<table>
<thead>
<tr>
<th>
<a href="#byte-pattern">Byte Pattern</a>
</th>
<th>
<a href="#pattern-mask">Pattern Mask</a>
</th>
<th>
Leading <a href="#byte" title="byte">Bytes</a> to Be Ignored
</th>
<th>
<a href="#image-type">Image Type</a>
</th>
<th>
Note
</th>
</tr>
</thead>
<tbody>
<!-- http://www.w3.org/Graphics/GIF/spec-gif87.txt -->
<tr>
<td>
47 49 46 38 37 61
</td>
<td>
FF FF FF FF FF FF
</td>
<td>
None.
</td>
<td>
<code title="">image/gif</code>
</td>
<td>
The string "<code title="">GIF87a</code>", a GIF signature.
</td>
</tr>
<!-- http://www.w3.org/Graphics/GIF/spec-gif89a.txt -->
<tr>
<td>
47 49 46 38 39 61
</td>
<td>
FF FF FF FF FF FF
</td>
<td>
None.
</td>
<td>
<code title="">image/gif</code>
</td>
<td>
The string "<code title="">GIF89a</code>", a GIF signature.
</td>
</tr>
<!-- http://www.w3.org/TR/PNG/#5PNG-file-signature -->
<tr>
<td>
89 50 4E 47 0D 0A 1A 0A
</td>
<td>
FF FF FF FF FF FF FF FF
</td>
<td>
None.
</td>
<td>
<code title="">image/png</code>
</td>
<td>
A <a href="#byte">byte</a> with only the highest bit set followed by the
string "<code title="">PNG</code>" followed by CR LF SUB LF, the PNG
signature.
</td>
</tr>
<!-- http://www.digicamsoft.com/itu/itu-t81-36.html -->
<tr>
<td>
FF D8 FF
</td>
<td>
FF FF FF
</td>
<td>
None.
</td>
<td>
<code title="">image/jpeg</code>
</td>
<td>
The JPEG Start of Image marker followed by the indicator
<a href="#byte">byte</a> of another marker.
</td>
</tr>
<tr>
<td>
42 4D
</td>
<td>
FF FF
</td>
<td>
None.
</td>
<td>
<code title="">image/bmp</code>
</td>
<td>
The string "<code title="">BM</code>", a BMP signature.
</td>
</tr>
<!-- https://developers.google.com/speed/webp/docs/riff_container#webp-file-header -->
<tr>
<td>
52 49 46 46 00 00 00 00 57 45 42 50 56 50
</td>
<td>
FF FF FF FF 00 00 00 00 FF FF FF FF FF FF
</td>
<td>
None.
</td>
<td>
<code title="">image/webp</code>
</td>
<td>
The string "<code title="">RIFF</code>" followed by four
<a href="#byte" title="byte">bytes</a> followed by the string
"<code title="">WEBPVP</code>".
</td>
</tr>
<!-- http://www.iana.org/assignments/media-types/image/vnd.microsoft.icon -->
<tr>
<td>
00 00 01 00
</td>
<td>
FF FF FF FF
</td>
<td>
None.
</td>
<td>
<code title="">image/vnd.microsoft.icon</code>
</td>
<td>
A Windows Icon signature.
</td>
</tr>
</tbody>
</table>
<li>
Return undefined.
</ol>
<h3 id="matching-an-audio-type-pattern"><span class="secno">7.2 </span>Matching an audio type pattern</h3>
<p>
To determine which <a href="#audio-type">audio type</a> <a href="#byte-pattern">byte pattern</a> a
<a href="#byte-sequence">byte sequence</a> matches, if any, use the following <dfn id="audio-type-pattern-matching-algorithm">audio type
pattern matching algorithm</dfn>:
<ol>
<li>
Execute the following steps for each row in the following table:
<ol>
<li>
Let <var>pattern-matched</var> be the result of executing the
<a href="#pattern-matching-algorithm">pattern matching algorithm</a> with the following parameters:
<ul>
<li>
Let the <a href="#byte-sequence">byte sequence</a> to be matched be the <a href="#byte-sequence">byte
sequence</a> to be matched.
<li>
Let the <a href="#byte-pattern">byte pattern</a> to be matched against be the value in
the first column of the current row.
<li>
Let the <a href="#pattern-mask">pattern mask</a> be the value in the second column of
the current row.
<li>
Let the leading <a href="#byte" title="byte">bytes</a> to be ignored be the
value in the third column of the current row.
</ul>
<li>
If <var>pattern-matched</var> is true, return the value in the fourth
column of the current row.
</ol>
<table>
<thead>
<tr>
<th>
<a href="#byte-pattern">Byte Pattern</a>
</th>
<th>
<a href="#pattern-mask">Pattern Mask</a>
</th>
<th>
Leading <a href="#byte" title="byte">Bytes</a> to Be Ignored
</th>
<th>
<a href="#audio-type">Audio Type</a>
</th>
<th>
Note
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
4F 67 67 53 00
</td>
<td>
FF FF FF FF FF
</td>
<td>
None.
</td>
<td>
<code title="">application/ogg</code>
</td>
<td>
The string "<code title="">OggS</code>" followed by NUL, the Ogg audio or
video signature.
</td>
</tr>
<!-- http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html -->
<tr>
<td>
52 49 46 46 00 00 00 00 57 41 56 45
</td>
<td>
FF FF FF FF 00 00 00 00 FF FF FF FF
</td>
<td>
None.
</td>
<td>
<code title="">audio/wave</code>
</td>
<td>
The string "<code title="">RIFF</code>" followed by four
<a href="#byte" title="byte">bytes</a> followed by the string
"<code title="">WAVE</code>".
</td>
</tr>
</tbody>
</table>
<p class="XXX">
MP3 audio.
<li>
Return undefined.
</ol>
<h3 id="matching-a-video-type-pattern"><span class="secno">7.3 </span>Matching a video type pattern</h3>
<p>
To determine which <a href="#video-type">video type</a> <a href="#byte-pattern">byte pattern</a> a
<a href="#byte-sequence">byte sequence</a> matches, if any, use the following <dfn id="video-type-pattern-matching-algorithm">video type
pattern matching algorithm</dfn>:
<ol>
<li>
Execute the following steps for each row in the following table:
<ol>
<li>
Let <var>pattern-matched</var> be the result of executing the
<a href="#pattern-matching-algorithm">pattern matching algorithm</a> with the following parameters:
<ul>
<li>
Let the <a href="#byte-sequence">byte sequence</a> to be matched be the <a href="#byte-sequence">byte
sequence</a> to be matched.
<li>
Let the <a href="#byte-pattern">byte pattern</a> to be matched against be the value in
the first column of the current row.
<li>
Let the <a href="#pattern-mask">pattern mask</a> be the value in the second column of
the current row.
<li>
Let the leading <a href="#byte" title="byte">bytes</a> to be ignored be the
value in the third column of the current row.
</ul>
<li>
If <var>pattern-matched</var> is true, return the value in the fourth
column of the current row.
</ol>
<table>
<thead>
<tr>
<th>
<a href="#byte-pattern">Byte Pattern</a>
</th>
<th>
<a href="#pattern-mask">Pattern Mask</a>
</th>
<th>
Leading <a href="#byte" title="byte">Bytes</a> to Be Ignored
</th>
<th>
<a href="#video-type">Video Type</a>
</th>
<th>
Note
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
4F 67 67 53 00
</td>
<td>
FF FF FF FF FF
</td>
<td>
None.
</td>
<td>
<code title="">application/ogg</code>
</td>
<td>
The string "<code title="">OggS</code>" followed by NUL, the Ogg audio or
video signature.
</td>
</tr>
<!-- http://ebml.sourceforge.net/specs/ -->
<!-- http://www.matroska.org/technical/specs/index.html -->
<!-- http://www.webmproject.org/code/specs/container/ -->
<tr>
<td class="XXX">
<!-- This only indicates that the file is EBML-based. -->
1A 45 DF A3
</td>
<td>
FF FF FF FF