diff --git a/Tests/LibWeb/TestFetchURL.cpp b/Tests/LibWeb/TestFetchURL.cpp index 590fe9fd7e..737f114f21 100644 --- a/Tests/LibWeb/TestFetchURL.cpp +++ b/Tests/LibWeb/TestFetchURL.cpp @@ -18,7 +18,7 @@ TEST_CASE(data_url) EXPECT_EQ(url.serialize(), "data:text/html,test"); auto data_url = TRY_OR_FAIL(Web::Fetch::Infrastructure::process_data_url(url)); - EXPECT_EQ(data_url.mime_type, "text/html"); + EXPECT_EQ(TRY_OR_FAIL(data_url.mime_type.serialized()), "text/html"); EXPECT_EQ(StringView(data_url.body.bytes()), "test"sv); } @@ -31,7 +31,7 @@ TEST_CASE(data_url_default_mime_type) EXPECT_EQ(url.serialize(), "data:,test"); auto data_url = TRY_OR_FAIL(Web::Fetch::Infrastructure::process_data_url(url)); - EXPECT_EQ(data_url.mime_type, "text/plain;charset=US-ASCII"); + EXPECT_EQ(TRY_OR_FAIL(data_url.mime_type.serialized()), "text/plain;charset=US-ASCII"); EXPECT_EQ(StringView(data_url.body.bytes()), "test"sv); } @@ -44,7 +44,7 @@ TEST_CASE(data_url_encoded) EXPECT_EQ(url.serialize(), "data:text/html,Hello%20friends%2C%0X%X0"); auto data_url = TRY_OR_FAIL(Web::Fetch::Infrastructure::process_data_url(url)); - EXPECT_EQ(data_url.mime_type, "text/html"); + EXPECT_EQ(TRY_OR_FAIL(data_url.mime_type.serialized()), "text/html"); EXPECT_EQ(StringView(data_url.body.bytes()), "Hello friends,%0X%X0"sv); } @@ -57,7 +57,7 @@ TEST_CASE(data_url_base64_encoded) EXPECT_EQ(url.serialize(), "data:text/html;base64,dGVzdA=="); auto data_url = TRY_OR_FAIL(Web::Fetch::Infrastructure::process_data_url(url)); - EXPECT_EQ(data_url.mime_type, "text/html"); + EXPECT_EQ(TRY_OR_FAIL(data_url.mime_type.serialized()), "text/html"); EXPECT_EQ(StringView(data_url.body.bytes()), "test"sv); } @@ -70,7 +70,7 @@ TEST_CASE(data_url_base64_encoded_default_mime_type) EXPECT_EQ(url.serialize(), "data:;base64,dGVzdA=="); auto data_url = TRY_OR_FAIL(Web::Fetch::Infrastructure::process_data_url(url)); - EXPECT_EQ(data_url.mime_type, "text/plain;charset=US-ASCII"); + EXPECT_EQ(TRY_OR_FAIL(data_url.mime_type.serialized()), "text/plain;charset=US-ASCII"); EXPECT_EQ(StringView(data_url.body.bytes()), "test"sv); } @@ -83,7 +83,7 @@ TEST_CASE(data_url_base64_encoded_with_whitespace) EXPECT_EQ(url.serialize(), "data: text/html ; bAsE64 , dGVz dA=="); auto data_url = TRY_OR_FAIL(Web::Fetch::Infrastructure::process_data_url(url)); - EXPECT_EQ(data_url.mime_type, "text/html"); + EXPECT_EQ(TRY_OR_FAIL(data_url.mime_type.serialized()), "text/html"); EXPECT_EQ(StringView(data_url.body.bytes()), "test"); } @@ -95,7 +95,7 @@ TEST_CASE(data_url_base64_encoded_with_inline_whitespace) EXPECT(url.host().has()); auto data_url = TRY_OR_FAIL(Web::Fetch::Infrastructure::process_data_url(url)); - EXPECT_EQ(data_url.mime_type, "text/javascript"); + EXPECT_EQ(TRY_OR_FAIL(data_url.mime_type.serialized()), "text/javascript"); EXPECT_EQ(StringView(data_url.body.bytes()), "d4 = 'four';"sv); } @@ -108,6 +108,6 @@ TEST_CASE(data_url_completed_with_fragment) EXPECT(url.host().has()); auto data_url = TRY_OR_FAIL(Web::Fetch::Infrastructure::process_data_url(url)); - EXPECT_EQ(data_url.mime_type, "text/plain"); + EXPECT_EQ(TRY_OR_FAIL(data_url.mime_type.serialized()), "text/plain"); EXPECT_EQ(StringView(data_url.body.bytes()), "test"sv); } diff --git a/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp b/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp index cddb12841c..13b58caf9a 100644 --- a/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp @@ -851,8 +851,7 @@ WebIDL::ExceptionOr> scheme_fetch(JS::Realm& r return PendingResponse::create(vm, request, Infrastructure::Response::network_error(vm, "Failed to process 'data:' URL"sv)); // 3. Let mimeType be dataURLStruct’s MIME type, serialized. - // FIXME: Serialize MIME type. - auto const& mime_type = data_url_struct.value().mime_type; + auto const& mime_type = MUST(data_url_struct.value().mime_type.serialized()); // 4. Return a new response whose status message is `OK`, header list is « (`Content-Type`, mimeType) », and // body is dataURLStruct’s body as a body. diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/URL.cpp b/Userland/Libraries/LibWeb/Fetch/Infrastructure/URL.cpp index 51f00a8108..47c3533f33 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/URL.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/URL.cpp @@ -8,6 +8,7 @@ #include #include +#include #include namespace Web::Fetch::Infrastructure { @@ -95,16 +96,17 @@ ErrorOr process_data_url(URL::URL const& data_url) mime_type = builder.string_view(); } - // FIXME: Parse the MIME type's components according to https://mimesniff.spec.whatwg.org/#parse-a-mime-type - // FIXME: 13. Let mimeTypeRecord be the result of parsing mimeType. - auto mime_type_record = mime_type.trim("\n\r\t "sv, TrimMode::Both); + // 13. Let mimeTypeRecord be the result of parsing mimeType. + auto mime_type_record = TRY(MimeSniff::MimeType::parse(mime_type)); // 14. If mimeTypeRecord is failure, then set mimeTypeRecord to text/plain;charset=US-ASCII. - if (mime_type_record.is_empty()) - mime_type_record = "text/plain;charset=US-ASCII"sv; + if (!mime_type_record.has_value()) { + mime_type_record = TRY(MimeSniff::MimeType::create("text"_string, "plain"_string)); + TRY(mime_type_record->set_parameter("charset"_string, "US-ASCII"_string)); + } // 15. Return a new data: URL struct whose MIME type is mimeTypeRecord and body is body. - return DataURL { TRY(String::from_utf8(mime_type_record)), body }; + return DataURL { mime_type_record.release_value(), body }; } } diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/URL.h b/Userland/Libraries/LibWeb/Fetch/Infrastructure/URL.h index 0b6ea07731..b14c16dcd5 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/URL.h +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/URL.h @@ -13,6 +13,7 @@ #include #include #include +#include namespace Web::Fetch::Infrastructure { @@ -39,7 +40,7 @@ inline constexpr Array FETCH_SCHEMES = { // https://fetch.spec.whatwg.org/#data-url-struct struct DataURL { - String mime_type; + MimeSniff::MimeType mime_type; ByteBuffer body; }; diff --git a/Userland/Libraries/LibWeb/Loader/ResourceLoader.cpp b/Userland/Libraries/LibWeb/Loader/ResourceLoader.cpp index b08593a776..95d86e099c 100644 --- a/Userland/Libraries/LibWeb/Loader/ResourceLoader.cpp +++ b/Userland/Libraries/LibWeb/Loader/ResourceLoader.cpp @@ -301,11 +301,11 @@ void ResourceLoader::load(LoadRequest& request, SuccessCallback success_callback auto data_url = data_url_or_error.release_value(); dbgln_if(SPAM_DEBUG, "ResourceLoader loading a data URL with mime-type: '{}', payload='{}'", - data_url.mime_type, + MUST(data_url.mime_type.serialized()), StringView(data_url.body.bytes())); HashMap response_headers; - response_headers.set("Content-Type", data_url.mime_type.to_byte_string()); + response_headers.set("Content-Type", MUST(data_url.mime_type.serialized()).to_byte_string()); log_success(request);