Преглед на файлове

LibWeb: Make MimeSniff::MimeType::create() infallible

Andreas Kling преди 9 месеца
родител
ревизия
9a8db40a23

+ 9 - 9
Tests/LibWeb/TestMimeSniff.cpp

@@ -12,7 +12,7 @@
 
 TEST_CASE(determine_computed_mime_type_given_no_sniff_is_set)
 {
-    auto mime_type = MUST(Web::MimeSniff::MimeType::create("text"_string, "html"_string));
+    auto mime_type = Web::MimeSniff::MimeType::create("text"_string, "html"_string);
     auto computed_mime_type = MUST(Web::MimeSniff::Resource::sniff("\x00"sv.bytes(), Web::MimeSniff::SniffingConfiguration { .supplied_type = mime_type, .no_sniff = true }));
 
     EXPECT_EQ("text/html"sv, MUST(computed_mime_type.serialized()));
@@ -29,7 +29,7 @@ TEST_CASE(determine_computed_mime_type_given_no_sniff_is_set)
 
 TEST_CASE(determine_computed_mime_type_given_no_sniff_is_unset)
 {
-    auto supplied_type = MUST(Web::MimeSniff::MimeType::create("application"_string, "x-this-is-a-test"_string));
+    auto supplied_type = Web::MimeSniff::MimeType::create("application"_string, "x-this-is-a-test"_string);
     auto computed_mime_type = MUST(Web::MimeSniff::Resource::sniff("\x00"sv.bytes(), Web::MimeSniff::SniffingConfiguration { .supplied_type = supplied_type }));
 
     EXPECT_EQ("application/x-this-is-a-test"sv, MUST(computed_mime_type.serialized()));
@@ -98,7 +98,7 @@ TEST_CASE(determine_computed_mime_type_given_supplied_type_that_is_an_apache_bug
 
     set_text_plain_type_mappings(mime_type_to_headers_map);
 
-    auto supplied_type = MUST(Web::MimeSniff::MimeType::create("text"_string, "plain"_string));
+    auto supplied_type = Web::MimeSniff::MimeType::create("text"_string, "plain"_string);
     for (auto const& mime_type_to_headers : mime_type_to_headers_map) {
         auto mime_type = mime_type_to_headers.key;
 
@@ -114,12 +114,12 @@ TEST_CASE(determine_computed_mime_type_given_supplied_type_that_is_an_apache_bug
 TEST_CASE(determine_computed_mime_type_given_xml_or_html_supplied_type)
 {
     // With HTML supplied type.
-    auto config = Web::MimeSniff::SniffingConfiguration { .supplied_type = MUST(Web::MimeSniff::MimeType::create("text"_string, "html"_string)) };
+    auto config = Web::MimeSniff::SniffingConfiguration { .supplied_type = Web::MimeSniff::MimeType::create("text"_string, "html"_string) };
     auto computed_mime_type = MUST(Web::MimeSniff::Resource::sniff(""sv.bytes(), config));
     EXPECT_EQ("text/html"sv, MUST(computed_mime_type.serialized()));
 
     // With XML supplied type.
-    config = Web::MimeSniff::SniffingConfiguration { .supplied_type = MUST(Web::MimeSniff::MimeType::create("text"_string, "xml"_string)) };
+    config = Web::MimeSniff::SniffingConfiguration { .supplied_type = Web::MimeSniff::MimeType::create("text"_string, "xml"_string) };
     computed_mime_type = MUST(Web::MimeSniff::Resource::sniff(""sv.bytes(), config));
     EXPECT_EQ("text/xml"sv, MUST(computed_mime_type.serialized()));
 }
@@ -179,9 +179,9 @@ TEST_CASE(determine_computed_mime_type_in_both_none_and_browsing_sniffing_contex
 TEST_CASE(compute_mime_type_given_unknown_supplied_type)
 {
     Array<Web::MimeSniff::MimeType, 3> unknown_supplied_types = {
-        MUST(Web::MimeSniff::MimeType::create("unknown"_string, "unknown"_string)),
-        MUST(Web::MimeSniff::MimeType::create("application"_string, "unknown"_string)),
-        MUST(Web::MimeSniff::MimeType::create("*"_string, "*"_string))
+        Web::MimeSniff::MimeType::create("unknown"_string, "unknown"_string),
+        Web::MimeSniff::MimeType::create("application"_string, "unknown"_string),
+        Web::MimeSniff::MimeType::create("*"_string, "*"_string)
     };
     auto header_bytes = "<HTML>"sv.bytes();
 
@@ -338,7 +338,7 @@ TEST_CASE(determine_computed_mime_type_in_a_font_context)
 
 TEST_CASE(determine_computed_mime_type_given_text_or_binary_context)
 {
-    auto supplied_type = MUST(Web::MimeSniff::MimeType::create("text"_string, "plain"_string));
+    auto supplied_type = 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,

+ 1 - 1
Userland/Libraries/LibWeb/Fetch/Infrastructure/URL.cpp

@@ -101,7 +101,7 @@ ErrorOr<DataURL> process_data_url(URL::URL const& data_url)
 
     // 14. If mimeTypeRecord is failure, then set mimeTypeRecord to text/plain;charset=US-ASCII.
     if (!mime_type_record.has_value()) {
-        mime_type_record = TRY(MimeSniff::MimeType::create("text"_string, "plain"_string));
+        mime_type_record = MimeSniff::MimeType::create("text"_string, "plain"_string);
         TRY(mime_type_record->set_parameter("charset"_string, "US-ASCII"_string));
     }
 

+ 3 - 3
Userland/Libraries/LibWeb/MimeSniff/MimeType.cpp

@@ -73,10 +73,10 @@ MimeType& MimeType::operator=(MimeType&& other) = default;
 
 MimeType::~MimeType() = default;
 
-ErrorOr<MimeType> MimeType::create(String type, String subtype)
+MimeType MimeType::create(String type, String subtype)
 {
     auto mime_type = MimeType { move(type), move(subtype) };
-    mime_type.m_cached_essence = TRY(String::formatted("{}/{}", mime_type.m_type, mime_type.m_subtype));
+    mime_type.m_cached_essence = MUST(String::formatted("{}/{}", mime_type.m_type, mime_type.m_subtype));
     return mime_type;
 }
 
@@ -114,7 +114,7 @@ ErrorOr<Optional<MimeType>> MimeType::parse(StringView string)
         return OptionalNone {};
 
     // 10. Let mimeType be a new MIME type record whose type is type, in ASCII lowercase, and subtype is subtype, in ASCII lowercase.
-    auto mime_type = TRY(MimeType::create(TRY(Infra::to_ascii_lowercase(type)), TRY(Infra::to_ascii_lowercase(subtype))));
+    auto mime_type = MimeType::create(TRY(Infra::to_ascii_lowercase(type)), TRY(Infra::to_ascii_lowercase(subtype)));
 
     // 11. While position is not past the end of input:
     while (!lexer.is_eof()) {

+ 1 - 1
Userland/Libraries/LibWeb/MimeSniff/MimeType.h

@@ -17,7 +17,7 @@ bool is_javascript_mime_type_essence_match(StringView);
 // https://mimesniff.spec.whatwg.org/#mime-type
 class MimeType {
 public:
-    static ErrorOr<MimeType> create(String type, String subtype);
+    static MimeType create(String type, String subtype);
     static ErrorOr<Optional<MimeType>> parse(StringView);
 
     MimeType(MimeType const&);

+ 4 - 4
Userland/Libraries/LibWeb/MimeSniff/Resource.cpp

@@ -460,7 +460,7 @@ namespace Web::MimeSniff {
 ErrorOr<Resource> Resource::create(ReadonlyBytes data, SniffingConfiguration configuration)
 {
     // NOTE: Non-standard but for cases where pattern matching fails, let's fall back to the safest MIME type.
-    auto default_computed_mime_type = TRY(MimeType::create("application"_string, "octet-stream"_string));
+    auto default_computed_mime_type = MimeType::create("application"_string, "octet-stream"_string);
     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)));
@@ -624,7 +624,7 @@ ErrorOr<void> Resource::rules_for_distinguishing_if_a_resource_is_text_or_binary
     if (length >= 2
         && (resource_header_span.starts_with(utf_16_be_bom)
             || resource_header_span.starts_with(utf_16_le_bom))) {
-        m_computed_mime_type = TRY(MimeType::create("text"_string, "plain"_string));
+        m_computed_mime_type = MimeType::create("text"_string, "plain"_string);
         return {};
     }
 
@@ -633,14 +633,14 @@ ErrorOr<void> Resource::rules_for_distinguishing_if_a_resource_is_text_or_binary
     //    Abort these steps.
     auto utf_8_bom = "\xEF\xBB\xBF"sv.bytes();
     if (length >= 3 && resource_header_span.starts_with(utf_8_bom)) {
-        m_computed_mime_type = TRY(MimeType::create("text"_string, "plain"_string));
+        m_computed_mime_type = MimeType::create("text"_string, "plain"_string);
         return {};
     }
 
     // 4. If the resource header contains no binary data bytes, the computed MIME type is "text/plain".
     //    Abort these steps.
     if (!any_of(resource_header(), is_binary_data_byte)) {
-        m_computed_mime_type = TRY(MimeType::create("text"_string, "plain"_string));
+        m_computed_mime_type = MimeType::create("text"_string, "plain"_string);
         return {};
     }
 

+ 1 - 1
Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp

@@ -1039,7 +1039,7 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::override_mime_type(String const& mime)
 
     // 3. If this’s override MIME type is failure, then set this’s override MIME type to application/octet-stream.
     if (!m_override_mime_type.has_value())
-        m_override_mime_type = TRY_OR_THROW_OOM(vm, MimeSniff::MimeType::create("application"_string, "octet-stream"_string));
+        m_override_mime_type = MimeSniff::MimeType::create("application"_string, "octet-stream"_string);
 
     return {};
 }