1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:07:45 +00:00

LibWeb: Move extract_mime_type() from XMLHttpRequest to HeaderList

Except some minor tweaks, this is a direct copy of Luke's initial
implementation in XMLHttpRequest, now replacing the former.
This commit is contained in:
Linus Groh 2022-07-20 17:47:29 +01:00
parent dfd62437c4
commit 042dfc7284
4 changed files with 63 additions and 60 deletions

View file

@ -1,6 +1,7 @@
/* /*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org> * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org> * Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org>
* Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -252,6 +253,64 @@ ErrorOr<Vector<Header>> HeaderList::sort_and_combine() const
return headers; return headers;
} }
// https://fetch.spec.whatwg.org/#concept-header-extract-mime-type
Optional<MimeSniff::MimeType> HeaderList::extract_mime_type() const
{
// 1. Let charset be null.
Optional<String> charset;
// 2. Let essence be null.
Optional<String> essence;
// 3. Let mimeType be null.
Optional<MimeSniff::MimeType> mime_type;
// 4. Let values be the result of getting, decoding, and splitting `Content-Type` from headers.
auto values_or_error = get_decode_and_split("Content-Type"sv.bytes());
if (values_or_error.is_error())
return {};
auto values = values_or_error.release_value();
// 5. If values is null, then return failure.
if (!values.has_value())
return {};
// 6. For each value of values:
for (auto const& value : *values) {
// 1. Let temporaryMimeType be the result of parsing value.
auto temporary_mime_type = MimeSniff::MimeType::from_string(value);
// 2. If temporaryMimeType is failure or its essence is "*/*", then continue.
if (!temporary_mime_type.has_value() || temporary_mime_type->essence() == "*/*"sv)
continue;
// 3. Set mimeType to temporaryMimeType.
mime_type = temporary_mime_type;
// 4. If mimeTypes essence is not essence, then:
if (mime_type->essence() != essence) {
// 1. Set charset to null.
charset = {};
// 2. If mimeTypes parameters["charset"] exists, then set charset to mimeTypes parameters["charset"].
auto charset_it = mime_type->parameters().find("charset"sv);
if (charset_it != mime_type->parameters().end())
charset = charset_it->value;
// 3. Set essence to mimeTypes essence.
essence = mime_type->essence();
}
// 5. Otherwise, if mimeTypes parameters["charset"] does not exist, and charset is non-null, set mimeTypes parameters["charset"] to charset.
else if (!mime_type->parameters().contains("charset"sv) && charset.has_value()) {
mime_type->set_parameter("charset"sv, charset.value());
}
}
// 7. If mimeType is null, then return failure.
// 8. Return mimeType.
return mime_type;
}
// https://fetch.spec.whatwg.org/#convert-header-names-to-a-sorted-lowercase-set // https://fetch.spec.whatwg.org/#convert-header-names-to-a-sorted-lowercase-set
ErrorOr<OrderedHashTable<ByteBuffer>> convert_header_names_to_a_sorted_lowercase_set(Span<ReadonlyBytes> header_names) ErrorOr<OrderedHashTable<ByteBuffer>> convert_header_names_to_a_sorted_lowercase_set(Span<ReadonlyBytes> header_names)
{ {

View file

@ -13,6 +13,7 @@
#include <AK/Optional.h> #include <AK/Optional.h>
#include <AK/String.h> #include <AK/String.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibWeb/MimeSniff/MimeType.h>
namespace Web::Fetch::Infrastructure { namespace Web::Fetch::Infrastructure {
@ -38,6 +39,7 @@ public:
[[nodiscard]] ErrorOr<void> set(Header); [[nodiscard]] ErrorOr<void> set(Header);
[[nodiscard]] ErrorOr<void> combine(Header); [[nodiscard]] ErrorOr<void> combine(Header);
[[nodiscard]] ErrorOr<Vector<Header>> sort_and_combine() const; [[nodiscard]] ErrorOr<Vector<Header>> sort_and_combine() const;
[[nodiscard]] Optional<MimeSniff::MimeType> extract_mime_type() const;
}; };
[[nodiscard]] ErrorOr<OrderedHashTable<ByteBuffer>> convert_header_names_to_a_sorted_lowercase_set(Span<ReadonlyBytes>); [[nodiscard]] ErrorOr<OrderedHashTable<ByteBuffer>> convert_header_names_to_a_sorted_lowercase_set(Span<ReadonlyBytes>);

View file

@ -201,7 +201,6 @@ MimeSniff::MimeType XMLHttpRequest::get_final_mime_type() const
// https://xhr.spec.whatwg.org/#response-mime-type // https://xhr.spec.whatwg.org/#response-mime-type
MimeSniff::MimeType XMLHttpRequest::get_response_mime_type() const MimeSniff::MimeType XMLHttpRequest::get_response_mime_type() const
{ {
// 1. Let mimeType be the result of extracting a MIME type from xhrs responses header list.
// FIXME: Use an actual HeaderList for XHR headers. // FIXME: Use an actual HeaderList for XHR headers.
Fetch::Infrastructure::HeaderList header_list; Fetch::Infrastructure::HeaderList header_list;
for (auto const& entry : m_response_headers) { for (auto const& entry : m_response_headers) {
@ -212,7 +211,8 @@ MimeSniff::MimeType XMLHttpRequest::get_response_mime_type() const
MUST(header_list.append(move(header))); MUST(header_list.append(move(header)));
} }
auto mime_type = extract_mime_type(header_list); // 1. Let mimeType be the result of extracting a MIME type from xhrs responses header list.
auto mime_type = header_list.extract_mime_type();
// 2. If mimeType is failure, then set mimeType to text/xml. // 2. If mimeType is failure, then set mimeType to text/xml.
if (!mime_type.has_value()) if (!mime_type.has_value())
@ -255,62 +255,6 @@ Optional<StringView> XMLHttpRequest::get_final_encoding() const
return encoding; return encoding;
} }
// https://fetch.spec.whatwg.org/#concept-header-extract-mime-type
// FIXME: This is not only used by XHR, it is also used for multiple things in Fetch.
Optional<MimeSniff::MimeType> XMLHttpRequest::extract_mime_type(Fetch::Infrastructure::HeaderList const& header_list) const
{
// 1. Let charset be null.
Optional<String> charset;
// 2. Let essence be null.
Optional<String> essence;
// 3. Let mimeType be null.
Optional<MimeSniff::MimeType> mime_type;
// 4. Let values be the result of getting, decoding, and splitting `Content-Type` from headers.
auto values = MUST(header_list.get_decode_and_split("Content-Type"sv.bytes()));
// 5. If values is null, then return failure.
if (!values.has_value())
return {};
// 6. For each value of values:
for (auto const& value : *values) {
// 1. Let temporaryMimeType be the result of parsing value.
auto temporary_mime_type = MimeSniff::MimeType::from_string(value);
// 2. If temporaryMimeType is failure or its essence is "*/*", then continue.
if (!temporary_mime_type.has_value() || temporary_mime_type->essence() == "*/*"sv)
continue;
// 3. Set mimeType to temporaryMimeType.
mime_type = temporary_mime_type;
// 4. If mimeTypes essence is not essence, then:
if (mime_type->essence() != essence) {
// 1. Set charset to null.
charset = {};
// 2. If mimeTypes parameters["charset"] exists, then set charset to mimeTypes parameters["charset"].
auto charset_it = mime_type->parameters().find("charset"sv);
if (charset_it != mime_type->parameters().end())
charset = charset_it->value;
// 3. Set essence to mimeTypes essence.
essence = mime_type->essence();
} else {
// 5. Otherwise, if mimeTypes parameters["charset"] does not exist, and charset is non-null, set mimeTypes parameters["charset"] to charset.
if (!mime_type->parameters().contains("charset"sv) && charset.has_value())
mime_type->set_parameter("charset"sv, charset.value());
}
}
// 7. If mimeType is null, then return failure.
// 8. Return mimeType.
return mime_type;
}
// https://fetch.spec.whatwg.org/#concept-bodyinit-extract // https://fetch.spec.whatwg.org/#concept-bodyinit-extract
// FIXME: The parameter 'body_init' should be 'typedef (ReadableStream or XMLHttpRequestBodyInit) BodyInit'. For now we just let it be 'XMLHttpRequestBodyInit'. // FIXME: The parameter 'body_init' should be 'typedef (ReadableStream or XMLHttpRequestBodyInit) BodyInit'. For now we just let it be 'XMLHttpRequestBodyInit'.
static Fetch::Infrastructure::BodyWithType extract_body(XMLHttpRequestBodyInit const& body_init) static Fetch::Infrastructure::BodyWithType extract_body(XMLHttpRequestBodyInit const& body_init)

View file

@ -93,8 +93,6 @@ private:
String get_text_response() const; String get_text_response() const;
Optional<MimeSniff::MimeType> extract_mime_type(Fetch::Infrastructure::HeaderList const& header_list) const;
explicit XMLHttpRequest(HTML::Window&); explicit XMLHttpRequest(HTML::Window&);
NonnullRefPtr<HTML::Window> m_window; NonnullRefPtr<HTML::Window> m_window;