diff options
author | Linus Groh <mail@linusgroh.de> | 2022-07-20 17:47:29 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-07-20 18:03:21 +0100 |
commit | 042dfc72848d640f03e40005ff54142aa392d65b (patch) | |
tree | 82302d4effdbd99a445044e99a3a4823780213b5 /Userland/Libraries/LibWeb/Fetch | |
parent | dfd62437c4864520faf9c545c5864bf6feaa5720 (diff) | |
download | serenity-042dfc72848d640f03e40005ff54142aa392d65b.zip |
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.
Diffstat (limited to 'Userland/Libraries/LibWeb/Fetch')
-rw-r--r-- | Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp | 59 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.h | 2 |
2 files changed, 61 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp index 083205fb82..db61a91ad8 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp @@ -1,6 +1,7 @@ /* * Copyright (c) 2022, Linus Groh <linusg@serenityos.org> * Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org> + * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -252,6 +253,64 @@ ErrorOr<Vector<Header>> HeaderList::sort_and_combine() const 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 mimeType’s essence is not essence, then: + if (mime_type->essence() != essence) { + // 1. Set charset to null. + charset = {}; + + // 2. If mimeType’s parameters["charset"] exists, then set charset to mimeType’s 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 mimeType’s essence. + essence = mime_type->essence(); + } + // 5. Otherwise, if mimeType’s parameters["charset"] does not exist, and charset is non-null, set mimeType’s 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 ErrorOr<OrderedHashTable<ByteBuffer>> convert_header_names_to_a_sorted_lowercase_set(Span<ReadonlyBytes> header_names) { diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.h b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.h index 47dc89dccf..db36c03f75 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.h +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.h @@ -13,6 +13,7 @@ #include <AK/Optional.h> #include <AK/String.h> #include <AK/Vector.h> +#include <LibWeb/MimeSniff/MimeType.h> namespace Web::Fetch::Infrastructure { @@ -38,6 +39,7 @@ public: [[nodiscard]] ErrorOr<void> set(Header); [[nodiscard]] ErrorOr<void> combine(Header); [[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>); |