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 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> 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
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 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> convert_header_names_to_a_sorted_lowercase_set(Span 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 set(Header); [[nodiscard]] ErrorOr combine(Header); + [[nodiscard]] ErrorOr> sort_and_combine() const; }; [[nodiscard]] ErrorOr> convert_header_names_to_a_sorted_lowercase_set(Span);