From ee68eba0acc380a3b52efb3ed4bddffdab2eceaa Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Fri, 10 Feb 2023 22:02:18 +0000 Subject: LibWeb: Implement Headers.getSetCookie() This is a normative change in the Fetch spec. See: https://github.com/whatwg/fetch/commit/e4d3480 This also implements the changes to the 'sort and combine' algorithm, which now treats "set-cookie" headers differently, and is exposed to JS via the Headers' iterator. Passes all 21 WPT tests :^) http://wpt.live/fetch/api/headers/header-setcookie.any.html --- Userland/Libraries/LibWeb/Fetch/Headers.cpp | 22 +++++++++++- Userland/Libraries/LibWeb/Fetch/Headers.h | 3 +- Userland/Libraries/LibWeb/Fetch/Headers.idl | 1 + .../LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp | 42 +++++++++++++++------- 4 files changed, 54 insertions(+), 14 deletions(-) (limited to 'Userland') diff --git a/Userland/Libraries/LibWeb/Fetch/Headers.cpp b/Userland/Libraries/LibWeb/Fetch/Headers.cpp index 0441b5d8d8..7aae86b938 100644 --- a/Userland/Libraries/LibWeb/Fetch/Headers.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Headers.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Linus Groh + * Copyright (c) 2022-2023, Linus Groh * * SPDX-License-Identifier: BSD-2-Clause */ @@ -112,6 +112,26 @@ WebIDL::ExceptionOr Headers::get(DeprecatedString const& name_ return byte_buffer.has_value() ? DeprecatedString { byte_buffer->span() } : DeprecatedString {}; } +// https://fetch.spec.whatwg.org/#dom-headers-getsetcookie +WebIDL::ExceptionOr> Headers::get_set_cookie() +{ + // The getSetCookie() method steps are: + auto& vm = this->vm(); + auto values = Vector {}; + + // 1. If this’s header list does not contain `Set-Cookie`, then return « ». + if (!m_header_list->contains("Set-Cookie"sv.bytes())) + return values; + + // 2. Return the values of all headers in this’s header list whose name is a byte-case-insensitive match for + // `Set-Cookie`, in order. + for (auto const& header : *m_header_list) { + if (StringView { header.name }.equals_ignoring_case("Set-Cookie"sv)) + TRY_OR_THROW_OOM(vm, values.try_append(DeprecatedString { header.value.bytes() })); + } + return values; +} + // https://fetch.spec.whatwg.org/#dom-headers-has WebIDL::ExceptionOr Headers::has(DeprecatedString const& name_string) { diff --git a/Userland/Libraries/LibWeb/Fetch/Headers.h b/Userland/Libraries/LibWeb/Fetch/Headers.h index 683a4f7327..c366b5fcbf 100644 --- a/Userland/Libraries/LibWeb/Fetch/Headers.h +++ b/Userland/Libraries/LibWeb/Fetch/Headers.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Linus Groh + * Copyright (c) 2022-2023, Linus Groh * * SPDX-License-Identifier: BSD-2-Clause */ @@ -50,6 +50,7 @@ public: WebIDL::ExceptionOr append(DeprecatedString const& name, DeprecatedString const& value); WebIDL::ExceptionOr delete_(DeprecatedString const& name); WebIDL::ExceptionOr get(DeprecatedString const& name); + WebIDL::ExceptionOr> get_set_cookie(); WebIDL::ExceptionOr has(DeprecatedString const& name); WebIDL::ExceptionOr set(DeprecatedString const& name, DeprecatedString const& value); diff --git a/Userland/Libraries/LibWeb/Fetch/Headers.idl b/Userland/Libraries/LibWeb/Fetch/Headers.idl index f2dbd9e4c0..a495339dd5 100644 --- a/Userland/Libraries/LibWeb/Fetch/Headers.idl +++ b/Userland/Libraries/LibWeb/Fetch/Headers.idl @@ -7,6 +7,7 @@ interface Headers { undefined append(ByteString name, ByteString value); undefined delete(ByteString name); ByteString? get(ByteString name); + sequence getSetCookie(); boolean has(ByteString name); undefined set(ByteString name, ByteString value); iterable; diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp index 2b5addf6fd..abc8d56889 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Linus Groh + * Copyright (c) 2022-2023, Linus Groh * Copyright (c) 2022, Kenneth Myhra * Copyright (c) 2022, Luke Wilde * @@ -257,22 +257,40 @@ ErrorOr> HeaderList::sort_and_combine() const // 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; + TRY(names_list.try_ensure_capacity(size())); for (auto const& header : *this) - names_list.append(header.name); + names_list.unchecked_append(header.name); auto names = TRY(convert_header_names_to_a_sorted_lowercase_set(names_list)); // 3. For each name of 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)); + // 1. If name is `set-cookie`, then: + if (name == "set-cookie"sv.bytes()) { + // 1. Let values be a list of all values of headers in list whose name is a byte-case-insensitive match for name, in order. + // 2. For each value of values: + for (auto const& [header_name, value] : *this) { + if (StringView { header_name }.equals_ignoring_case(name)) { + // 1. Append (name, value) to headers. + auto header = TRY(Header::from_string_pair(name, value)); + TRY(headers.try_append(move(header))); + } + } + } + // 2. Otherwise: + else { + // 1. Let value be the result of getting name from list. + auto value = TRY(get(name)); + + // 2. Assert: value is not null. + VERIFY(value.has_value()); + + // 3. Append (name, value) to headers. + auto header = Header { + .name = move(name), + .value = value.release_value(), + }; + TRY(headers.try_append(move(header))); + } } // 4. Return headers. -- cgit v1.2.3