summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2022-07-19 00:19:14 +0100
committerLinus Groh <mail@linusgroh.de>2022-07-19 00:27:35 +0100
commitb5ab1f6b4aed38dfbdd098826245dfc8eb46c88e (patch)
tree7dedfe598255ba13350bc66ea79a706ba2111ec9
parentbad6ad8861bf3b24b89400a0de464436e0330add (diff)
downloadserenity-b5ab1f6b4aed38dfbdd098826245dfc8eb46c88e.zip
LibWeb: Implement HeaderList::sort_and_combine()
-rw-r--r--Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp33
-rw-r--r--Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.h1
2 files changed, 32 insertions, 2 deletions
diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp
index 96daf55e00..083205fb82 100644
--- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp
+++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp
@@ -220,8 +220,37 @@ ErrorOr<void> HeaderList::combine(Header header)
return {};
}
-// TODO: https://fetch.spec.whatwg.org/#convert-header-names-to-a-sorted-lowercase-set
-// TODO: https://fetch.spec.whatwg.org/#concept-header-list-sort-and-combine
+// https://fetch.spec.whatwg.org/#concept-header-list-sort-and-combine
+ErrorOr<Vector<Header>> HeaderList::sort_and_combine() const
+{
+ // To sort and combine a header list list, run these steps:
+
+ // 1. Let headers be an empty list of headers with the key being the name and value the value.
+ Vector<Header> headers;
+
+ // 2. Let names be the result of convert header names to a sorted-lowercase set with all the names of the headers in list.
+ Vector<ReadonlyBytes> names_list;
+ for (auto const& header : *this)
+ names_list.append(header.name);
+ auto names = TRY(convert_header_names_to_a_sorted_lowercase_set(names_list));
+
+ // 3. For each name in names:
+ for (auto& name : names) {
+ // 1. Let value be the result of getting name from list.
+ // 2. Assert: value is not null.
+ auto value = TRY(get(name)).value();
+
+ // 3. Append (name, value) to headers.
+ auto header = Infrastructure::Header {
+ .name = move(name),
+ .value = move(value),
+ };
+ headers.append(move(header));
+ }
+
+ // 4. Return headers.
+ return headers;
+}
// 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 3040c5706b..47dc89dccf 100644
--- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.h
+++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.h
@@ -37,6 +37,7 @@ public:
void delete_(ReadonlyBytes name);
[[nodiscard]] ErrorOr<void> set(Header);
[[nodiscard]] ErrorOr<void> combine(Header);
+ [[nodiscard]] ErrorOr<Vector<Header>> sort_and_combine() const;
};
[[nodiscard]] ErrorOr<OrderedHashTable<ByteBuffer>> convert_header_names_to_a_sorted_lowercase_set(Span<ReadonlyBytes>);