소스 검색

LibWeb/MimeSniff: Add sniffing in a browsing context

Kemal Zebari 1 년 전
부모
커밋
ea15501f37
3개의 변경된 파일29개의 추가작업 그리고 2개의 파일을 삭제
  1. 7 1
      Tests/LibWeb/TestMimeSniff.cpp
  2. 15 1
      Userland/Libraries/LibWeb/MimeSniff/Resource.cpp
  3. 7 0
      Userland/Libraries/LibWeb/MimeSniff/Resource.h

+ 7 - 1
Tests/LibWeb/TestMimeSniff.cpp

@@ -31,7 +31,7 @@ TEST_CASE(determine_computed_mime_type_given_no_sniff_is_unset)
     EXPECT_EQ(xml_mime_type, MUST(computed_mime_type.serialized()));
     EXPECT_EQ(xml_mime_type, MUST(computed_mime_type.serialized()));
 }
 }
 
 
-TEST_CASE(compute_unknown_mime_type)
+TEST_CASE(determine_computed_mime_type_in_both_none_and_browsing_sniffing_context)
 {
 {
     HashMap<StringView, Vector<StringView>> mime_type_to_headers_map;
     HashMap<StringView, Vector<StringView>> mime_type_to_headers_map;
 
 
@@ -84,8 +84,14 @@ TEST_CASE(compute_unknown_mime_type)
         auto mime_type = mime_type_to_headers.key;
         auto mime_type = mime_type_to_headers.key;
 
 
         for (auto const& header : mime_type_to_headers.value) {
         for (auto const& header : mime_type_to_headers.value) {
+
+            // Test in a non-specific sniffing context.
             auto computed_mime_type = MUST(Web::MimeSniff::Resource::sniff(header.bytes()));
             auto computed_mime_type = MUST(Web::MimeSniff::Resource::sniff(header.bytes()));
             EXPECT_EQ(mime_type, computed_mime_type.essence());
             EXPECT_EQ(mime_type, computed_mime_type.essence());
+
+            // Test sniffing in a browsing context.
+            computed_mime_type = MUST(Web::MimeSniff::Resource::sniff(header.bytes(), Web::MimeSniff::SniffingConfiguration { .sniffing_context = Web::MimeSniff::SniffingContext::Browsing }));
+            EXPECT_EQ(mime_type, computed_mime_type.essence());
         }
         }
     }
     }
 }
 }

+ 15 - 1
Userland/Libraries/LibWeb/MimeSniff/Resource.cpp

@@ -370,7 +370,7 @@ ErrorOr<Resource> Resource::create(ReadonlyBytes data, SniffingConfiguration con
     auto resource = Resource { data, configuration.no_sniff, move(default_computed_mime_type) };
     auto resource = Resource { data, configuration.no_sniff, move(default_computed_mime_type) };
 
 
     TRY(resource.supplied_mime_type_detection_algorithm(configuration.scheme, move(configuration.supplied_type)));
     TRY(resource.supplied_mime_type_detection_algorithm(configuration.scheme, move(configuration.supplied_type)));
-    TRY(resource.mime_type_sniffing_algorithm());
+    TRY(resource.context_specific_sniffing_algorithm(configuration.sniffing_context));
 
 
     return resource;
     return resource;
 }
 }
@@ -520,4 +520,18 @@ ErrorOr<void> Resource::mime_type_sniffing_algorithm()
     return {};
     return {};
 }
 }
 
 
+// https://mimesniff.spec.whatwg.org/#context-specific-sniffing-algorithm
+ErrorOr<void> Resource::context_specific_sniffing_algorithm(SniffingContext sniffing_context)
+{
+    // A context-specific sniffing algorithm determines the computed MIME type of a resource only if
+    // the resource is a MIME type relevant to a particular context.
+    if (sniffing_context == SniffingContext::None || sniffing_context == SniffingContext::Browsing) {
+        // https://mimesniff.spec.whatwg.org/#sniffing-in-a-browsing-context
+        // Use the MIME type sniffing algorithm.
+        return mime_type_sniffing_algorithm();
+    }
+
+    return {};
+}
+
 }
 }

+ 7 - 0
Userland/Libraries/LibWeb/MimeSniff/Resource.h

@@ -10,7 +10,13 @@
 
 
 namespace Web::MimeSniff {
 namespace Web::MimeSniff {
 
 
+enum class SniffingContext {
+    None,
+    Browsing
+};
+
 struct SniffingConfiguration {
 struct SniffingConfiguration {
+    SniffingContext sniffing_context { SniffingContext::None };
     StringView scheme { ""sv };
     StringView scheme { ""sv };
     Optional<MimeType> supplied_type = {};
     Optional<MimeType> supplied_type = {};
     bool no_sniff { false };
     bool no_sniff { false };
@@ -33,6 +39,7 @@ private:
     void read_the_resource_header(ReadonlyBytes data);
     void read_the_resource_header(ReadonlyBytes data);
     ErrorOr<void> supplied_mime_type_detection_algorithm(StringView scheme, Optional<MimeType> supplied_type);
     ErrorOr<void> supplied_mime_type_detection_algorithm(StringView scheme, Optional<MimeType> supplied_type);
     ErrorOr<void> mime_type_sniffing_algorithm();
     ErrorOr<void> mime_type_sniffing_algorithm();
+    ErrorOr<void> context_specific_sniffing_algorithm(SniffingContext sniffing_context);
 
 
     // https://mimesniff.spec.whatwg.org/#supplied-mime-type
     // https://mimesniff.spec.whatwg.org/#supplied-mime-type
     // A supplied MIME type, the MIME type determined by the supplied MIME type detection algorithm.
     // A supplied MIME type, the MIME type determined by the supplied MIME type detection algorithm.