summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2023-05-10 16:26:51 -0400
committerAndreas Kling <kling@serenityos.org>2023-05-12 05:47:36 +0200
commit29d90ccf3b1cb1146573a7ff3b5e5dbe42863ce3 (patch)
treef3c1828002f8102c556207ba292c1af5e47ddff9 /Userland/Libraries/LibWeb
parent9701128145059ac360e32c6c363eac16fee05ac6 (diff)
downloadserenity-29d90ccf3b1cb1146573a7ff3b5e5dbe42863ce3.zip
LibWeb: Implement the legacy extracting an encoding AO
Diffstat (limited to 'Userland/Libraries/LibWeb')
-rw-r--r--Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp24
-rw-r--r--Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.h1
2 files changed, 25 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp
index c09c3dffb6..628fad1f74 100644
--- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp
+++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp
@@ -15,6 +15,7 @@
#include <LibJS/Heap/Heap.h>
#include <LibJS/Runtime/VM.h>
#include <LibRegex/Regex.h>
+#include <LibTextCodec/Decoder.h>
#include <LibWeb/Fetch/Infrastructure/HTTP.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Methods.h>
@@ -392,6 +393,29 @@ ErrorOr<Optional<MimeSniff::MimeType>> HeaderList::extract_mime_type() const
return mime_type;
}
+// https://fetch.spec.whatwg.org/#legacy-extract-an-encoding
+StringView legacy_extract_an_encoding(Optional<MimeSniff::MimeType> const& mime_type, StringView fallback_encoding)
+{
+ // 1. If mimeType is failure, then return fallbackEncoding.
+ if (!mime_type.has_value())
+ return fallback_encoding;
+
+ // 2. If mimeType["charset"] does not exist, then return fallbackEncoding.
+ auto charset = mime_type->parameters().get("charset"sv);
+ if (!charset.has_value())
+ return fallback_encoding;
+
+ // 3. Let tentativeEncoding be the result of getting an encoding from mimeType["charset"].
+ auto tentative_encoding = TextCodec::get_standardized_encoding(*charset);
+
+ // 4. If tentativeEncoding is failure, then return fallbackEncoding.
+ if (!tentative_encoding.has_value())
+ return fallback_encoding;
+
+ // 5. Return tentativeEncoding.
+ return *tentative_encoding;
+}
+
// 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 23723ddac2..3d778c20ec 100644
--- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.h
+++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.h
@@ -70,6 +70,7 @@ struct RangeHeaderValue {
struct ExtractHeaderParseFailure {
};
+[[nodiscard]] StringView legacy_extract_an_encoding(Optional<MimeSniff::MimeType> const& mime_type, StringView fallback_encoding);
[[nodiscard]] ErrorOr<Optional<Vector<String>>> get_decode_and_split_header_value(ReadonlyBytes);
[[nodiscard]] ErrorOr<OrderedHashTable<ByteBuffer>> convert_header_names_to_a_sorted_lowercase_set(Span<ReadonlyBytes>);
[[nodiscard]] bool is_header_name(ReadonlyBytes);