From 9a8db40a239e3641bba138d6ec6c564c0cc0e2c5 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 14 Oct 2024 10:58:23 +0200 Subject: [PATCH] LibWeb: Make MimeSniff::MimeType::create() infallible --- Tests/LibWeb/TestMimeSniff.cpp | 18 +++++++++--------- .../LibWeb/Fetch/Infrastructure/URL.cpp | 2 +- .../Libraries/LibWeb/MimeSniff/MimeType.cpp | 6 +++--- Userland/Libraries/LibWeb/MimeSniff/MimeType.h | 2 +- .../Libraries/LibWeb/MimeSniff/Resource.cpp | 8 ++++---- .../Libraries/LibWeb/XHR/XMLHttpRequest.cpp | 2 +- 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/Tests/LibWeb/TestMimeSniff.cpp b/Tests/LibWeb/TestMimeSniff.cpp index 6810c22bf75..d81757b4386 100644 --- a/Tests/LibWeb/TestMimeSniff.cpp +++ b/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 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 = ""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, diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/URL.cpp b/Userland/Libraries/LibWeb/Fetch/Infrastructure/URL.cpp index 779d89efbaf..7ecc847a0b0 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/URL.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/URL.cpp @@ -101,7 +101,7 @@ ErrorOr 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)); } diff --git a/Userland/Libraries/LibWeb/MimeSniff/MimeType.cpp b/Userland/Libraries/LibWeb/MimeSniff/MimeType.cpp index 0da85956340..407f34e0a21 100644 --- a/Userland/Libraries/LibWeb/MimeSniff/MimeType.cpp +++ b/Userland/Libraries/LibWeb/MimeSniff/MimeType.cpp @@ -73,10 +73,10 @@ MimeType& MimeType::operator=(MimeType&& other) = default; MimeType::~MimeType() = default; -ErrorOr 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> 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()) { diff --git a/Userland/Libraries/LibWeb/MimeSniff/MimeType.h b/Userland/Libraries/LibWeb/MimeSniff/MimeType.h index 40695cf6f1d..94bda2bd1e7 100644 --- a/Userland/Libraries/LibWeb/MimeSniff/MimeType.h +++ b/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 create(String type, String subtype); + static MimeType create(String type, String subtype); static ErrorOr> parse(StringView); MimeType(MimeType const&); diff --git a/Userland/Libraries/LibWeb/MimeSniff/Resource.cpp b/Userland/Libraries/LibWeb/MimeSniff/Resource.cpp index d70bcf4b649..036c573a62e 100644 --- a/Userland/Libraries/LibWeb/MimeSniff/Resource.cpp +++ b/Userland/Libraries/LibWeb/MimeSniff/Resource.cpp @@ -460,7 +460,7 @@ namespace Web::MimeSniff { ErrorOr 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 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 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 {}; } diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp index 47e3817f31f..d82ae0b8fe5 100644 --- a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp +++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp @@ -1039,7 +1039,7 @@ WebIDL::ExceptionOr 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 {}; }