Resource.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. * Copyright (c) 2023, Kemal Zebari <kemalzebra@gmail.com>.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Fetch/Infrastructure/URL.h>
  7. #include <LibWeb/MimeSniff/Resource.h>
  8. namespace {
  9. using namespace Web::MimeSniff;
  10. struct BytePatternTableRow {
  11. StringView byte_pattern;
  12. StringView pattern_mask;
  13. ReadonlyBytes ignored_leading_bytes;
  14. StringView mime_type;
  15. // NOTE: If the byte pattern has a tag-terminating byte, add a byte where this byte should be. The value itself is ignored in
  16. // the pattern_matching_algorithm() (see the NOTE in this algorithm for more details).
  17. bool is_tag_terminated { false };
  18. };
  19. // https://mimesniff.spec.whatwg.org/#tag-terminating-byte
  20. bool is_tag_terminating_byte(u8 byte)
  21. {
  22. // A tag-terminating byte (abbreviated 0xTT) is any one of the following bytes: 0x20 (SP), 0x3E (">").
  23. return byte == 0x20 || byte == 0x3E;
  24. }
  25. // https://mimesniff.spec.whatwg.org/#binary-data-byte
  26. bool is_binary_data_byte(u8 byte)
  27. {
  28. // A binary data byte is a byte in the range 0x00 to 0x08 (NUL to BS), the byte 0x0B (VT), a byte in
  29. // the range 0x0E to 0x1A (SO to SUB), or a byte in the range 0x1C to 0x1F (FS to US).
  30. return (byte <= 0x08) || byte == 0x0B || (byte >= 0x0E && byte <= 0x1A) || (byte >= 0x1C && byte <= 0x1F);
  31. }
  32. // https://mimesniff.spec.whatwg.org/#pattern-matching-algorithm
  33. bool pattern_matching_algorithm(ReadonlyBytes input, ReadonlyBytes pattern, ReadonlyBytes mask, ReadonlyBytes ignored, bool is_tag_terminated = false)
  34. {
  35. // 1. Assert: pattern’s length is equal to mask’s length.
  36. VERIFY(pattern.size() == mask.size());
  37. // 2. If input’s length is less than pattern’s length, return false.
  38. if (input.size() < pattern.size())
  39. return false;
  40. // 3. Let s be 0.
  41. size_t s = 0;
  42. // 4. While s < input’s length:
  43. while (s < input.size()) {
  44. // 1. If ignored does not contain input[s], break.
  45. if (!ignored.contains_slow(input[s]))
  46. break;
  47. // 2. Set s to s + 1.
  48. s++;
  49. }
  50. // 5. Let p be 0.
  51. size_t p = 0;
  52. // 6. While p < pattern’s length:
  53. while (p < pattern.size()) {
  54. // 1. Let maskedData be the result of applying the bitwise AND operator to input[s] and mask[p].
  55. u8 masked_data = input[s] & mask[p];
  56. // NOTE: This non-standard branch exists to avoid having to create 2 byte patterns just so that
  57. // they can only differ by their tag-terminating byte (which could be a 0x20 or 0x3E byte).
  58. if (is_tag_terminated && p + 1 == pattern.size())
  59. return is_tag_terminating_byte(masked_data);
  60. // 2. If maskedData is not equal to pattern[p], return false.
  61. if (masked_data != pattern[p])
  62. return false;
  63. // 3. Set s to s + 1.
  64. s++;
  65. // 4. Set p to p + 1.
  66. p++;
  67. }
  68. // 7. Return true.
  69. return true;
  70. }
  71. ReadonlyBytes constexpr no_ignored_bytes;
  72. // https://mimesniff.spec.whatwg.org/#matching-an-image-type-pattern
  73. ErrorOr<Optional<MimeType>> match_an_image_type_pattern(ReadonlyBytes input)
  74. {
  75. // 1. Execute the following steps for each row row in the following table:
  76. static Array<BytePatternTableRow, 8> constexpr pattern_table {
  77. // A Windows Icon signature.
  78. BytePatternTableRow { "\x00\x00\x01\x00"sv, "\xFF\xFF\xFF\xFF"sv, no_ignored_bytes, "image/x-icon"sv },
  79. // A Windows Cursor signature.
  80. BytePatternTableRow { "\x00\x00\x02\x00"sv, "\xFF\xFF\xFF\xFF"sv, no_ignored_bytes, "image/x-icon"sv },
  81. // The string "BM", a BMP signature.
  82. BytePatternTableRow { "\x42\x4D"sv, "\xFF\xFF"sv, no_ignored_bytes, "image/bmp"sv },
  83. // The string "GIF87a", a GIF signature.
  84. BytePatternTableRow { "\x47\x49\x46\x38\x37\x61"sv, "\xFF\xFF\xFF\xFF\xFF\xFF"sv, no_ignored_bytes, "image/gif"sv },
  85. // The string "GIF89a", a GIF signature.
  86. BytePatternTableRow { "\x47\x49\x46\x38\x39\x61"sv, "\xFF\xFF\xFF\xFF\xFF\xFF"sv, no_ignored_bytes, "image/gif"sv },
  87. // The string "RIFF" followed by four bytes followed by the string "WEBPVP".
  88. BytePatternTableRow { "\x52\x49\x46\x46\x00\x00\x00\x00\x57\x45\x42\x50\x56\x50"sv,
  89. "\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF"sv, no_ignored_bytes, "image/webp"sv },
  90. // An error-checking byte followed by the string "PNG" followed by CR LF SUB LF, the PNG signature.
  91. BytePatternTableRow { "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"sv, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"sv, no_ignored_bytes, "image/png"sv },
  92. // The JPEG Start of Image marker followed by the indicator byte of another marker.
  93. BytePatternTableRow { "\xFF\xD8\xFF"sv, "\xFF\xFF\xFF"sv, no_ignored_bytes, "image/jpeg"sv },
  94. };
  95. for (auto const& row : pattern_table) {
  96. // 1. Let patternMatched be the result of the pattern matching algorithm given input, the value in
  97. // the first column of row, the value in the second column of row, and the value in the third
  98. // column of row.
  99. auto pattern_matched = pattern_matching_algorithm(input, row.byte_pattern.bytes(), row.pattern_mask.bytes(), row.ignored_leading_bytes);
  100. // 2. If patternMatched is true, return the value in the fourth column of row.
  101. if (pattern_matched)
  102. return MimeType::parse(row.mime_type);
  103. }
  104. // 2. Return undefined.
  105. return OptionalNone {};
  106. }
  107. // https://mimesniff.spec.whatwg.org/#matching-an-audio-or-video-type-pattern
  108. ErrorOr<Optional<MimeType>> match_an_audio_or_video_type_pattern(ReadonlyBytes input)
  109. {
  110. // 1. Execute the following steps for each row row in the following table:
  111. static Array<BytePatternTableRow, 6> constexpr pattern_table {
  112. // The string "FORM" followed by four bytes followed by the string "AIFF", the AIFF signature.
  113. BytePatternTableRow { "\x46\x4F\x52\x4D\x00\x00\x00\x00\x41\x49\x46\x46"sv,
  114. "\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF"sv, no_ignored_bytes, "audio/aiff"sv },
  115. // The string "ID3", the ID3v2-tagged MP3 signature.
  116. BytePatternTableRow { "\x49\x44\x33"sv, "\xFF\xFF\xFF"sv, no_ignored_bytes, "audio/mpeg"sv },
  117. // The string "OggS" followed by NUL, the Ogg container signature.
  118. BytePatternTableRow { "\x4F\x67\x67\x53\x00"sv, "\xFF\xFF\xFF\xFF\xFF"sv, no_ignored_bytes, "application/ogg"sv },
  119. // The string "MThd" followed by four bytes representing the number 6 in 32 bits (big-endian), the MIDI signature.
  120. BytePatternTableRow { "\x4D\x54\x68\x64\x00\x00\x00\x06"sv, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"sv, no_ignored_bytes, "audio/midi"sv },
  121. // The string "RIFF" followed by four bytes followed by the string "AVI ", the AVI signature.
  122. BytePatternTableRow { "\x52\x49\x46\x46\x00\x00\x00\x00\x41\x56\x49\x20"sv,
  123. "\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF"sv, no_ignored_bytes, "video/avi"sv },
  124. // The string "RIFF" followed by four bytes followed by the string "WAVE", the WAVE signature.
  125. BytePatternTableRow { "\x52\x49\x46\x46\x00\x00\x00\x00\x57\x41\x56\x45"sv,
  126. "\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF"sv, no_ignored_bytes, "audio/wave"sv }
  127. };
  128. for (auto const& row : pattern_table) {
  129. // 1. Let patternMatched be the result of the pattern matching algorithm given input, the
  130. // value in the first column of row, the value in the second column of row, and the
  131. // value in the third column of row.
  132. auto pattern_matched = pattern_matching_algorithm(input, row.byte_pattern.bytes(), row.pattern_mask.bytes(), row.ignored_leading_bytes);
  133. // 2. If patternMatched is true, return the value in the fourth column of row.
  134. if (pattern_matched)
  135. return MimeType::parse(row.mime_type);
  136. }
  137. // FIXME: 2. If input matches the signature for MP4, return "video/mp4".
  138. // FIXME: 3. If input matches the signature for WebM, return "video/webm".
  139. // FIXME: 4. If input matches the signature for MP3 without ID3, return "audio/mpeg".
  140. // 5. Return undefined.
  141. return OptionalNone {};
  142. }
  143. // https://mimesniff.spec.whatwg.org/#matching-an-archive-type-pattern
  144. ErrorOr<Optional<MimeType>> match_an_archive_type_pattern(ReadonlyBytes input)
  145. {
  146. // 1. Execute the following steps for each row row in the following table:
  147. static Array<BytePatternTableRow, 3> constexpr pattern_table {
  148. // The GZIP archive signature.
  149. BytePatternTableRow { "\x1F\x8B\x08"sv, "\xFF\xFF\xFF"sv, no_ignored_bytes, "application/x-gzip"sv },
  150. // The string "PK" followed by ETX EOT, the ZIP archive signature.
  151. BytePatternTableRow { "\x50\x4B\x03\x04"sv, "\xFF\xFF\xFF\xFF"sv, no_ignored_bytes, "application/zip"sv },
  152. // The string "Rar " followed by SUB BEL NUL, the RAR archive signature.
  153. BytePatternTableRow { "\x52\x61\x72\x20\x1A\x07\x00"sv, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF"sv, no_ignored_bytes, "application/x-rar-compressed"sv },
  154. };
  155. for (auto const& row : pattern_table) {
  156. // 1. Let patternMatched be the result of the pattern matching algorithm given input, the
  157. // value in the first column of row, the value in the second column of row, and the
  158. // value in the third column of row.
  159. auto pattern_matched = pattern_matching_algorithm(input, row.byte_pattern.bytes(), row.pattern_mask.bytes(), row.ignored_leading_bytes);
  160. // 2. If patternMatched is true, return the value in the fourth column of row.
  161. if (pattern_matched)
  162. return MimeType::parse(row.mime_type);
  163. }
  164. // 2. Return undefined.
  165. return OptionalNone {};
  166. }
  167. // https://mimesniff.spec.whatwg.org/#rules-for-identifying-an-unknown-mime-type
  168. ErrorOr<MimeType> rules_for_identifying_an_unknown_mime_type(Resource const& resource, bool sniff_scriptable = false)
  169. {
  170. // 1. If the sniff-scriptable flag is set, execute the following steps for each row row in the following table:
  171. if (sniff_scriptable) {
  172. static auto constexpr text_html_mime_type = "text/html"sv;
  173. // https://mimesniff.spec.whatwg.org/#whitespace-byte
  174. // A whitespace byte (abbreviated 0xWS) is any one of the following bytes: 0x09 (HT), 0x0A (LF), 0x0C (FF), 0x0D (CR), 0x20 (SP).
  175. static Array<u8, 5> constexpr ignored_whitespace_bytes { 0x09, 0x0A, 0x0C, 0x0D, 0x20 };
  176. static Array<BytePatternTableRow, 19> constexpr pattern_table {
  177. // The case-insensitive string "<!DOCTYPE HTML" followed by a tag-terminating byte.
  178. BytePatternTableRow { "\x3C\x21\x44\x4F\x43\x54\x59\x50\x45\x20\x48\x54\x4D\x4C\x00"sv,
  179. "\xFF\xFF\xDF\xDF\xDF\xDF\xDF\xDF\xDF\xFF\xDF\xDF\xDF\xDF\xFF"sv, ignored_whitespace_bytes, text_html_mime_type, true },
  180. // The case-insensitive string "<HTML" followed by a tag-terminating byte.
  181. BytePatternTableRow { "\x3C\x48\x54\x4D\x4C\x00"sv, "\xFF\xDF\xDF\xDF\xDF\xFF"sv, ignored_whitespace_bytes, text_html_mime_type, true },
  182. // The case-insensitive string "<HEAD" followed by a tag-terminating byte.
  183. BytePatternTableRow { "\x3C\x48\x45\x41\x44\x00"sv, "\xFF\xDF\xDF\xDF\xDF\xFF"sv, ignored_whitespace_bytes, text_html_mime_type, true },
  184. // The case-insensitive string "<SCRIPT" followed by a tag-terminating byte.
  185. BytePatternTableRow { "\x3C\x53\x43\x52\x49\x50\x54\x00"sv,
  186. "\xFF\xDF\xDF\xDF\xDF\xDF\xDF\xFF"sv, ignored_whitespace_bytes, text_html_mime_type, true },
  187. // The case-insensitive string "<IFRAME" followed by a tag-terminating byte.
  188. BytePatternTableRow { "\x3C\x49\x46\x52\x41\x4D\x45\x00"sv,
  189. "\xFF\xDF\xDF\xDF\xDF\xDF\xDF\xFF"sv, ignored_whitespace_bytes, text_html_mime_type, true },
  190. // The case-insensitive string "<H1" followed by a tag-terminating byte.
  191. BytePatternTableRow { "\x3C\x48\x31\x00"sv, "\xFF\xDF\xFF\xFF"sv, ignored_whitespace_bytes, text_html_mime_type, true },
  192. // The case-insensitive string "<DIV" followed by a tag-terminating byte.
  193. BytePatternTableRow { "\x3C\x44\x49\x56\x00"sv, "\xFF\xDF\xDF\xDF\xFF"sv, ignored_whitespace_bytes, text_html_mime_type, true },
  194. // The case-insensitive string "<FONT" followed by a tag-terminating byte.
  195. BytePatternTableRow { "\x3C\x46\x4F\x4E\x54\x00"sv, "\xFF\xDF\xDF\xDF\xDF\xFF"sv, ignored_whitespace_bytes, text_html_mime_type, true },
  196. // The case-insensitive string "<TABLE" followed by a tag-terminating byte.
  197. BytePatternTableRow { "\x3C\x54\x41\x42\x4C\x45\x00"sv, "\xFF\xDF\xDF\xDF\xDF\xDF\xFF"sv, ignored_whitespace_bytes, text_html_mime_type, true },
  198. // The case-insensitive string "<A" followed by a tag-terminating byte.
  199. BytePatternTableRow { "\x3C\x41\x00"sv, "\xFF\xDF\xFF"sv, ignored_whitespace_bytes, text_html_mime_type, true },
  200. // The case-insensitive string "<STYLE" followed by a tag-terminating byte.
  201. BytePatternTableRow { "\x3C\x53\x54\x59\x4C\x45\x00"sv,
  202. "\xFF\xDF\xDF\xDF\xDF\xDF\xFF"sv, ignored_whitespace_bytes, text_html_mime_type, true },
  203. // The case-insensitive string "<TITLE" followed by a tag-terminating byte.
  204. BytePatternTableRow { "\x3C\x54\x49\x54\x4C\x45\x00"sv,
  205. "\xFF\xDF\xDF\xDF\xDF\xDF\xFF"sv, ignored_whitespace_bytes, text_html_mime_type, true },
  206. // The case-insensitive string "<B" followed by a tag-terminating byte.
  207. BytePatternTableRow { "\x3C\x42\x00"sv, "\xFF\xDF\xFF"sv, ignored_whitespace_bytes, text_html_mime_type, true },
  208. // The case-insensitive string "<BODY" followed by a tag-terminating byte.
  209. BytePatternTableRow { "\x3C\x42\x4F\x44\x59\x00"sv, "\xFF\xDF\xDF\xDF\xDF\xFF"sv, ignored_whitespace_bytes, text_html_mime_type, true },
  210. // The case-insensitive string "<BR" followed by a tag-terminating byte.
  211. BytePatternTableRow { "\x3C\x42\x52\x00"sv, "\xFF\xDF\xDF\xFF"sv, ignored_whitespace_bytes, text_html_mime_type, true },
  212. // The case-insensitive string "<P" followed by a tag-terminating byte.
  213. BytePatternTableRow { "\x3C\x50\x00"sv, "\xFF\xDF\xFF"sv, ignored_whitespace_bytes, text_html_mime_type, true },
  214. // The string "<!--" followed by a tag-terminating byte.
  215. BytePatternTableRow { "\x3C\x21\x2D\x2D\x00"sv, "\xFF\xFF\xFF\xFF\xFF"sv, ignored_whitespace_bytes, text_html_mime_type, true },
  216. // The string "<?xml".
  217. BytePatternTableRow { "\x3C\x3F\x78\x6D\x6C"sv, "\xFF\xFF\xFF\xFF\xFF"sv, ignored_whitespace_bytes, "text/xml"sv },
  218. // The string "%PDF-", the PDF signature.
  219. BytePatternTableRow { "\x25\x50\x44\x46\x2D"sv, "\xFF\xFF\xFF\xFF\xFF"sv, no_ignored_bytes, "application/pdf"sv },
  220. };
  221. for (auto const& row : pattern_table) {
  222. // 1. Let patternMatched be the result of the pattern matching algorithm given resource’s resource header,
  223. // the value in the first column of row, the value in the second column of row, and the value in the
  224. // third column of row.
  225. auto pattern_matched = pattern_matching_algorithm(resource.resource_header(), row.byte_pattern.bytes(), row.pattern_mask.bytes(), row.ignored_leading_bytes, row.is_tag_terminated);
  226. // 2. If patternMatched is true, return the value in the fourth column of row.
  227. if (pattern_matched) {
  228. if (auto maybe_type = TRY(MimeType::parse(row.mime_type)); maybe_type.has_value())
  229. return maybe_type.release_value();
  230. }
  231. }
  232. }
  233. // 2. Execute the following steps for each row row in the following table:
  234. static auto constexpr text_plain_mime_type = "text/plain"sv;
  235. static Array<BytePatternTableRow, 4> constexpr pattern_table {
  236. // The string "%!PS-Adobe-", the PostScript signature.
  237. BytePatternTableRow { "\x25\x21\x50\x53\x2D\x41\x64\x6F\x62\x65\x2D"sv,
  238. "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"sv, no_ignored_bytes, "application/postscript"sv },
  239. // UTF-16BE BOM
  240. BytePatternTableRow { "\xFE\xFF\x00\x00"sv, "\xFF\xFF\x00\x00"sv, no_ignored_bytes, text_plain_mime_type },
  241. // UTF-16LE BOM
  242. BytePatternTableRow { "\xFF\xFE\x00\x00"sv, "\xFF\xFF\x00\x00"sv, no_ignored_bytes, text_plain_mime_type },
  243. // UTF-8 BOM
  244. BytePatternTableRow { "\xEF\xBB\xBF\x00"sv, "\xFF\xFF\xFF\x00"sv, no_ignored_bytes, text_plain_mime_type },
  245. };
  246. for (auto const& row : pattern_table) {
  247. // 1. Let patternMatched be the result of the pattern matching algorithm given resource’s resource header,
  248. // the value in the first column of row, the value in the second column of row, and the value in the
  249. // third column of row.
  250. auto pattern_matched = pattern_matching_algorithm(resource.resource_header(), row.byte_pattern.bytes(), row.pattern_mask.bytes(), row.ignored_leading_bytes);
  251. // 2. If patternMatched is true, return the value in the fourth column of row.
  252. if (pattern_matched) {
  253. if (auto maybe_type = TRY(MimeType::parse(row.mime_type)); maybe_type.has_value())
  254. return maybe_type.release_value();
  255. }
  256. }
  257. // 3. Let matchedType be the result of executing the image type pattern matching algorithm given resource’s resource header.
  258. auto matched_type = TRY(match_an_image_type_pattern(resource.resource_header()));
  259. // 4. If matchedType is not undefined, return matchedType.
  260. if (matched_type.has_value())
  261. return matched_type.release_value();
  262. // 5. Set matchedType to the result of executing the audio or video type pattern matching algorithm given resource’s resource header.
  263. matched_type = TRY(match_an_audio_or_video_type_pattern(resource.resource_header()));
  264. // 6. If matchedType is not undefined, return matchedType.
  265. if (matched_type.has_value())
  266. return matched_type.release_value();
  267. // 7. Set matchedType to the result of executing the archive type pattern matching algorithm given resource’s resource header.
  268. matched_type = TRY(match_an_archive_type_pattern(resource.resource_header()));
  269. // 8. If matchedType is not undefined, return matchedType.
  270. if (matched_type.has_value())
  271. return matched_type.release_value();
  272. // 9. If resource’s resource header contains no binary data bytes, return "text/plain".
  273. if (!any_of(resource.resource_header(), is_binary_data_byte))
  274. return MimeType::create("text"_string, "plain"_string);
  275. // 10. Return "application/octet-stream".
  276. return MimeType::create("application"_string, "octet-stream"_string);
  277. }
  278. }
  279. namespace Web::MimeSniff {
  280. ErrorOr<Resource> Resource::create(ReadonlyBytes data, SniffingConfiguration configuration)
  281. {
  282. // NOTE: Non-standard but for cases where pattern matching fails, let's fall back to the safest MIME type.
  283. auto default_computed_mime_type = TRY(MimeType::create("application"_string, "octet-stream"_string));
  284. auto resource = Resource { data, configuration.no_sniff, move(default_computed_mime_type) };
  285. TRY(resource.supplied_mime_type_detection_algorithm(configuration.scheme, move(configuration.supplied_type)));
  286. TRY(resource.context_specific_sniffing_algorithm(configuration.sniffing_context));
  287. return resource;
  288. }
  289. ErrorOr<MimeType> Resource::sniff(ReadonlyBytes data, SniffingConfiguration configuration)
  290. {
  291. auto resource = TRY(create(data, move(configuration)));
  292. return move(resource.m_computed_mime_type);
  293. }
  294. Resource::Resource(ReadonlyBytes data, bool no_sniff, MimeType&& default_computed_mime_type)
  295. : m_no_sniff(no_sniff)
  296. , m_computed_mime_type(move(default_computed_mime_type))
  297. {
  298. read_the_resource_header(data);
  299. }
  300. Resource::~Resource() = default;
  301. // https://mimesniff.spec.whatwg.org/#supplied-mime-type-detection-algorithm
  302. // NOTE: Parameters are non-standard.
  303. ErrorOr<void> Resource::supplied_mime_type_detection_algorithm(StringView scheme, Optional<MimeType> supplied_type)
  304. {
  305. // 1. Let supplied-type be null.
  306. // 2. If the resource is retrieved via HTTP, execute the following steps:
  307. // 1. If one or more Content-Type headers are associated with the resource, execute the following steps:
  308. // 1. Set supplied-type to the value of the last Content-Type header associated with the resource.
  309. // 2. Set the check-for-apache-bug flag if supplied-type is exactly equal to one of the values in the following table:
  310. // NOTE: Non-standard but this algorithm expects the caller to handle step 2.1.1.
  311. if (supplied_type.has_value()) {
  312. if (Fetch::Infrastructure::is_http_or_https_scheme(scheme)) {
  313. static Array<StringView, 4> constexpr apache_bug_mime_types = {
  314. "text/plain"sv,
  315. "text/plain; charset=ISO-8859-1"sv,
  316. "text/plain; charset=iso-8859-1"sv,
  317. "text/plain; charset=UTF-8"sv
  318. };
  319. auto serialized_supplied_type = TRY(supplied_type->serialized());
  320. for (auto apache_bug_mime_type : apache_bug_mime_types) {
  321. if (serialized_supplied_type == apache_bug_mime_type) {
  322. m_check_for_apache_bug_flag = true;
  323. break;
  324. }
  325. }
  326. }
  327. }
  328. // 3. If the resource is retrieved directly from the file system, set supplied-type
  329. // to the MIME type provided by the file system.
  330. // 4. If the resource is retrieved via another protocol (such as FTP), set
  331. // supplied-type to the MIME type as determined by that protocol, if any.
  332. // 5. If supplied-type is not a MIME type, the supplied MIME type is undefined.
  333. // Abort these steps.
  334. // 6. The supplied MIME type is supplied-type.
  335. // NOTE: The expectation is for the caller to handle these spec steps.
  336. m_supplied_mime_type = supplied_type;
  337. return {};
  338. }
  339. // https://mimesniff.spec.whatwg.org/#read-the-resource-header
  340. void Resource::read_the_resource_header(ReadonlyBytes data)
  341. {
  342. // 1. Let buffer be a byte sequence.
  343. ByteBuffer buffer;
  344. // 2. Read bytes of the resource into buffer until one of the following conditions is met:
  345. // - the end of the resource is reached.
  346. // - the number of bytes in buffer is greater than or equal to 1445.
  347. // - a reasonable amount of time has elapsed, as determined by the user agent.
  348. // FIXME: The spec expects us to be reading from a stream. Reimplement this spec step once
  349. // we have greater support for streaming in areas that calls on this API.
  350. static size_t constexpr MAX_SNIFF_SIZE = 1445;
  351. buffer.append(data.slice(0, min(data.size(), MAX_SNIFF_SIZE)));
  352. // 3. The resource header is buffer.
  353. m_resource_header = move(buffer);
  354. }
  355. // https://mimesniff.spec.whatwg.org/#mime-type-sniffing-algorithm
  356. ErrorOr<void> Resource::mime_type_sniffing_algorithm()
  357. {
  358. // 1. If the supplied MIME type is undefined or if the supplied MIME type’s essence
  359. // is "unknown/unknown", "application/unknown", or "*/*", execute the rules for
  360. // identifying an unknown MIME type with the sniff-scriptable flag equal to the
  361. // inverse of the no-sniff flag and abort these steps.
  362. if (!m_supplied_mime_type.has_value() || m_supplied_mime_type->essence().is_one_of("unknown/unknown", "application/unknown", "*/*")) {
  363. m_computed_mime_type = TRY(rules_for_identifying_an_unknown_mime_type(*this, !m_no_sniff));
  364. return {};
  365. }
  366. // 2. If the no-sniff flag is set, the computed MIME type is the supplied MIME type.
  367. // Abort these steps.
  368. if (m_no_sniff) {
  369. m_computed_mime_type = m_supplied_mime_type.value();
  370. return {};
  371. }
  372. // 3. If the check-for-apache-bug flag is set, execute the rules for distinguishing
  373. // if a resource is text or binary and abort these steps.
  374. if (m_check_for_apache_bug_flag) {
  375. // FIXME: Execute the rules for distinguishing if a resource is text or binary and abort these steps.
  376. return {};
  377. }
  378. // 4. If the supplied MIME type is an XML MIME type, the computed MIME type is the supplied MIME type.
  379. // Abort these steps.
  380. if (m_supplied_mime_type->is_xml()) {
  381. m_computed_mime_type = m_supplied_mime_type.value();
  382. return {};
  383. }
  384. // 5. If the supplied MIME type’s essence is "text/html", execute the rules for distinguishing if a
  385. // resource is a feed or HTML and abort these steps.
  386. if (m_supplied_mime_type->essence() == "text/html") {
  387. // FIXME: Execute the rules for distinguishing if a resource is a feed or HTML and abort these steps.
  388. return {};
  389. }
  390. // FIXME: 6. If the supplied MIME type is an image MIME type supported by the user agent, let matched-type be
  391. // the result of executing the image type pattern matching algorithm with the resource header as
  392. // the byte sequence to be matched.
  393. Optional<MimeType> matched_type;
  394. // 7. If matched-type is not undefined, the computed MIME type is matched-type.
  395. // Abort these steps.
  396. if (matched_type.has_value()) {
  397. m_computed_mime_type = matched_type.release_value();
  398. return {};
  399. }
  400. // FIXME: 8. If the supplied MIME type is an audio or video MIME type supported by the user agent, let matched-type be
  401. // the result of executing the audio or video type pattern matching algorithm with the resource header as
  402. // the byte sequence to be matched.
  403. // 9. If matched-type is not undefined, the computed MIME type is matched-type.
  404. // Abort these steps.
  405. if (matched_type.has_value()) {
  406. m_computed_mime_type = matched_type.release_value();
  407. return {};
  408. }
  409. // 10. The computed MIME type is the supplied MIME type.
  410. m_computed_mime_type = m_supplied_mime_type.value();
  411. return {};
  412. }
  413. // https://mimesniff.spec.whatwg.org/#context-specific-sniffing-algorithm
  414. ErrorOr<void> Resource::context_specific_sniffing_algorithm(SniffingContext sniffing_context)
  415. {
  416. // A context-specific sniffing algorithm determines the computed MIME type of a resource only if
  417. // the resource is a MIME type relevant to a particular context.
  418. if (sniffing_context == SniffingContext::None || sniffing_context == SniffingContext::Browsing) {
  419. // https://mimesniff.spec.whatwg.org/#sniffing-in-a-browsing-context
  420. // Use the MIME type sniffing algorithm.
  421. return mime_type_sniffing_algorithm();
  422. }
  423. return {};
  424. }
  425. }