mirror of
https://github.com/SerenityOS/serenity
synced 2024-09-06 08:56:40 +00:00
LibWeb/Fetch: Use MimeType in DataURL
This commit is contained in:
parent
2b59ba19e0
commit
295c4ef51a
|
@ -18,7 +18,7 @@ TEST_CASE(data_url)
|
||||||
EXPECT_EQ(url.serialize(), "data:text/html,test");
|
EXPECT_EQ(url.serialize(), "data:text/html,test");
|
||||||
|
|
||||||
auto data_url = TRY_OR_FAIL(Web::Fetch::Infrastructure::process_data_url(url));
|
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);
|
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");
|
EXPECT_EQ(url.serialize(), "data:,test");
|
||||||
|
|
||||||
auto data_url = TRY_OR_FAIL(Web::Fetch::Infrastructure::process_data_url(url));
|
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);
|
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");
|
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));
|
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);
|
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==");
|
EXPECT_EQ(url.serialize(), "data:text/html;base64,dGVzdA==");
|
||||||
|
|
||||||
auto data_url = TRY_OR_FAIL(Web::Fetch::Infrastructure::process_data_url(url));
|
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);
|
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==");
|
EXPECT_EQ(url.serialize(), "data:;base64,dGVzdA==");
|
||||||
|
|
||||||
auto data_url = TRY_OR_FAIL(Web::Fetch::Infrastructure::process_data_url(url));
|
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);
|
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==");
|
EXPECT_EQ(url.serialize(), "data: text/html ; bAsE64 , dGVz dA==");
|
||||||
|
|
||||||
auto data_url = TRY_OR_FAIL(Web::Fetch::Infrastructure::process_data_url(url));
|
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");
|
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>());
|
EXPECT(url.host().has<Empty>());
|
||||||
|
|
||||||
auto data_url = TRY_OR_FAIL(Web::Fetch::Infrastructure::process_data_url(url));
|
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);
|
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>());
|
EXPECT(url.host().has<Empty>());
|
||||||
|
|
||||||
auto data_url = TRY_OR_FAIL(Web::Fetch::Infrastructure::process_data_url(url));
|
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);
|
EXPECT_EQ(StringView(data_url.body.bytes()), "test"sv);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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));
|
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.
|
// 3. Let mimeType be dataURLStruct’s MIME type, serialized.
|
||||||
// FIXME: Serialize MIME type.
|
auto const& mime_type = MUST(data_url_struct.value().mime_type.serialized());
|
||||||
auto const& mime_type = data_url_struct.value().mime_type;
|
|
||||||
|
|
||||||
// 4. Return a new response whose status message is `OK`, header list is « (`Content-Type`, mimeType) », and
|
// 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.
|
// body is dataURLStruct’s body as a body.
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
#include <LibURL/URL.h>
|
#include <LibURL/URL.h>
|
||||||
#include <LibWeb/Fetch/Infrastructure/URL.h>
|
#include <LibWeb/Fetch/Infrastructure/URL.h>
|
||||||
|
#include <LibWeb/MimeSniff/MimeType.h>
|
||||||
#include <Userland/Libraries/LibWeb/Infra/Base64.h>
|
#include <Userland/Libraries/LibWeb/Infra/Base64.h>
|
||||||
|
|
||||||
namespace Web::Fetch::Infrastructure {
|
namespace Web::Fetch::Infrastructure {
|
||||||
|
@ -95,16 +96,17 @@ ErrorOr<DataURL> process_data_url(URL::URL const& data_url)
|
||||||
mime_type = builder.string_view();
|
mime_type = builder.string_view();
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: Parse the MIME type's components according to https://mimesniff.spec.whatwg.org/#parse-a-mime-type
|
// 13. Let mimeTypeRecord be the result of parsing mimeType.
|
||||||
// FIXME: 13. Let mimeTypeRecord be the result of parsing mimeType.
|
auto mime_type_record = TRY(MimeSniff::MimeType::parse(mime_type));
|
||||||
auto mime_type_record = mime_type.trim("\n\r\t "sv, TrimMode::Both);
|
|
||||||
|
|
||||||
// 14. If mimeTypeRecord is failure, then set mimeTypeRecord to text/plain;charset=US-ASCII.
|
// 14. If mimeTypeRecord is failure, then set mimeTypeRecord to text/plain;charset=US-ASCII.
|
||||||
if (mime_type_record.is_empty())
|
if (!mime_type_record.has_value()) {
|
||||||
mime_type_record = "text/plain;charset=US-ASCII"sv;
|
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.
|
// 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 };
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
#include <AK/StringView.h>
|
#include <AK/StringView.h>
|
||||||
#include <LibURL/Forward.h>
|
#include <LibURL/Forward.h>
|
||||||
|
#include <LibWeb/MimeSniff/MimeType.h>
|
||||||
|
|
||||||
namespace Web::Fetch::Infrastructure {
|
namespace Web::Fetch::Infrastructure {
|
||||||
|
|
||||||
|
@ -39,7 +40,7 @@ inline constexpr Array FETCH_SCHEMES = {
|
||||||
|
|
||||||
// https://fetch.spec.whatwg.org/#data-url-struct
|
// https://fetch.spec.whatwg.org/#data-url-struct
|
||||||
struct DataURL {
|
struct DataURL {
|
||||||
String mime_type;
|
MimeSniff::MimeType mime_type;
|
||||||
ByteBuffer body;
|
ByteBuffer body;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -301,11 +301,11 @@ void ResourceLoader::load(LoadRequest& request, SuccessCallback success_callback
|
||||||
auto data_url = data_url_or_error.release_value();
|
auto data_url = data_url_or_error.release_value();
|
||||||
|
|
||||||
dbgln_if(SPAM_DEBUG, "ResourceLoader loading a data URL with mime-type: '{}', payload='{}'",
|
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()));
|
StringView(data_url.body.bytes()));
|
||||||
|
|
||||||
HashMap<ByteString, ByteString, CaseInsensitiveStringTraits> response_headers;
|
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);
|
log_success(request);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue