소스 검색

LibWeb/MimeSniff: Add non-standard text or binary context sniffing

This is used in cases where the spec expects us to only run the
"rules for distinguishing if a resource is a text or binary" algo.
Kemal Zebari 1 년 전
부모
커밋
8e5410347b
3개의 변경된 파일16개의 추가작업 그리고 0개의 파일을 삭제
  1. 10 0
      Tests/LibWeb/TestMimeSniff.cpp
  2. 2 0
      Userland/Libraries/LibWeb/MimeSniff/Resource.cpp
  3. 4 0
      Userland/Libraries/LibWeb/MimeSniff/Resource.h

+ 10 - 0
Tests/LibWeb/TestMimeSniff.cpp

@@ -320,3 +320,13 @@ TEST_CASE(determine_computed_mime_type_in_a_font_context)
 
     EXPECT_EQ(mime_type, computed_mime_type.essence());
 }
+
+TEST_CASE(determine_computed_mime_type_given_text_or_binary_context)
+{
+    auto supplied_type = MUST(Web::MimeSniff::MimeType::create("text"_string, "plain"_string));
+    auto computed_mime_type = MUST(Web::MimeSniff::Resource::sniff("\x00"sv.bytes(), Web::MimeSniff::SniffingConfiguration {
+                                                                                         .sniffing_context = Web::MimeSniff::SniffingContext::TextOrBinary,
+                                                                                         .supplied_type = supplied_type,
+                                                                                     }));
+    EXPECT_EQ("application/octet-stream"sv, MUST(computed_mime_type.serialized()));
+}

+ 2 - 0
Userland/Libraries/LibWeb/MimeSniff/Resource.cpp

@@ -680,6 +680,8 @@ ErrorOr<void> Resource::context_specific_sniffing_algorithm(SniffingContext snif
         return rules_for_sniffing_audio_or_video_specifically();
     if (sniffing_context == SniffingContext::Font)
         return rules_for_sniffing_fonts_specifically();
+    if (sniffing_context == SniffingContext::TextOrBinary)
+        return rules_for_distinguishing_if_a_resource_is_text_or_binary();
 
     return {};
 }

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

@@ -16,6 +16,10 @@ enum class SniffingContext {
     Image,
     AudioOrVideo,
     Font,
+
+    // Non-standard but used in cases where the spec expects us to only run
+    // https://mimesniff.spec.whatwg.org/#sniffing-a-mislabeled-binary-resource
+    TextOrBinary,
 };
 
 struct SniffingConfiguration {