1
0
mirror of https://github.com/SerenityOS/serenity synced 2024-07-09 03:50:45 +00:00

LibWeb/Fetch: Use MimeType in DataURL

This commit is contained in:
Jamie Mansfield 2024-06-02 17:34:46 +01:00 committed by Andreas Kling
parent 2b59ba19e0
commit 295c4ef51a
5 changed files with 21 additions and 19 deletions

View File

@ -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<Empty>());
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<Empty>());
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);
}

View File

@ -851,8 +851,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<PendingResponse>> 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 dataURLStructs 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 dataURLStructs body as a body.

View File

@ -8,6 +8,7 @@
#include <LibURL/URL.h>
#include <LibWeb/Fetch/Infrastructure/URL.h>
#include <LibWeb/MimeSniff/MimeType.h>
#include <Userland/Libraries/LibWeb/Infra/Base64.h>
namespace Web::Fetch::Infrastructure {
@ -95,16 +96,17 @@ ErrorOr<DataURL> 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 };
}
}

View File

@ -13,6 +13,7 @@
#include <AK/String.h>
#include <AK/StringView.h>
#include <LibURL/Forward.h>
#include <LibWeb/MimeSniff/MimeType.h>
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;
};

View File

@ -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<ByteString, ByteString, CaseInsensitiveStringTraits> 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);