From 65f5c7adbceb0e7becc67f6eb16bc9db3f1ec3d2 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Mon, 24 Oct 2022 09:16:32 +0100 Subject: [PATCH] LibWeb: Add Fetch::Infrastructure::Header::from_string_pair() helper This allows us to use this: ```cpp auto header = TRY_OR_RETURN_OOM(realm, Infrastructure::Header::from_string_pair(name, value)); ``` Instead of the somewhat unwieldly: ```cpp auto header = Infrastructure::Header { .name = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy(name.bytes())), .value = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy(value.bytes())), }; ``` --- Userland/Libraries/LibWeb/Fetch/Headers.cpp | 10 ++-------- .../LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp | 8 ++++++++ .../LibWeb/Fetch/Infrastructure/HTTP/Headers.h | 2 ++ Userland/Libraries/LibWeb/Fetch/Response.cpp | 5 +---- Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp | 7 ++----- 5 files changed, 15 insertions(+), 17 deletions(-) diff --git a/Userland/Libraries/LibWeb/Fetch/Headers.cpp b/Userland/Libraries/LibWeb/Fetch/Headers.cpp index 5f659bf1b6..257f070161 100644 --- a/Userland/Libraries/LibWeb/Fetch/Headers.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Headers.cpp @@ -277,10 +277,7 @@ WebIDL::ExceptionOr Headers::fill(HeadersInit const& object) return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Array must contain header key/value pair" }; // 2. Append (header’s first item, header’s second item) to headers. - auto header = Fetch::Infrastructure::Header { - .name = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(entry[0].bytes())), - .value = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(entry[1].bytes())), - }; + auto header = TRY_OR_RETURN_OOM(realm(), Infrastructure::Header::from_string_pair(entry[0], entry[1].bytes())); TRY(append(move(header))); } return {}; @@ -288,10 +285,7 @@ WebIDL::ExceptionOr Headers::fill(HeadersInit const& object) // 2. Otherwise, object is a record, then for each key → value in object, append (key, value) to headers. [this](OrderedHashMap const& object) -> WebIDL::ExceptionOr { for (auto const& entry : object) { - auto header = Fetch::Infrastructure::Header { - .name = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(entry.key.bytes())), - .value = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(entry.value.bytes())), - }; + auto header = TRY_OR_RETURN_OOM(realm(), Infrastructure::Header::from_string_pair(entry.key, entry.value)); TRY(append(move(header))); } return {}; diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp index 244db6f262..1888476bfd 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.cpp @@ -34,6 +34,14 @@ requires(IsSameIgnoringCV) struct CaseInsensitiveBytesTraits : public Tra } }; +ErrorOr
Header::from_string_pair(StringView name, StringView value) +{ + return Header { + .name = TRY(ByteBuffer::copy(name.bytes())), + .value = TRY(ByteBuffer::copy(value.bytes())), + }; +} + // https://fetch.spec.whatwg.org/#header-list-contains bool HeaderList::contains(ReadonlyBytes name) const { diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.h b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.h index 4c4dfbecc6..71a8932644 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.h +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Headers.h @@ -22,6 +22,8 @@ namespace Web::Fetch::Infrastructure { struct Header { ByteBuffer name; ByteBuffer value; + + static ErrorOr
from_string_pair(StringView, StringView); }; // https://fetch.spec.whatwg.org/#concept-header-list diff --git a/Userland/Libraries/LibWeb/Fetch/Response.cpp b/Userland/Libraries/LibWeb/Fetch/Response.cpp index 990b80aa86..4c88f77dc1 100644 --- a/Userland/Libraries/LibWeb/Fetch/Response.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Response.cpp @@ -187,10 +187,7 @@ WebIDL::ExceptionOr> Response::redirect(JS::VM& vm, S auto value = parsed_url.serialize(); // 7. Append (`Location`, value) to responseObject’s response’s header list. - auto header = Infrastructure::Header { - .name = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy("Location"sv.bytes())), - .value = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy(value.bytes())), - }; + auto header = TRY_OR_RETURN_OOM(realm, Infrastructure::Header::from_string_pair("Location"sv, value)); TRY_OR_RETURN_OOM(realm, response_object->response()->header_list()->append(move(header))); // 8. Return responseObject. diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp index a8c9e62eea..2d4c99c226 100644 --- a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp +++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp @@ -218,11 +218,8 @@ MimeSniff::MimeType XMLHttpRequest::get_response_mime_type() const // FIXME: Use an actual HeaderList for XHR headers. auto header_list = make_ref_counted(); for (auto const& entry : m_response_headers) { - auto header = Fetch::Infrastructure::Header { - .name = MUST(ByteBuffer::copy(entry.key.bytes())), - .value = MUST(ByteBuffer::copy(entry.value.bytes())), - }; - MUST(header_list->append(move(header))); + auto header = Fetch::Infrastructure::Header::from_string_pair(entry.key, entry.value).release_value_but_fixme_should_propagate_errors(); + header_list->append(move(header)).release_value_but_fixme_should_propagate_errors(); } // 1. Let mimeType be the result of extracting a MIME type from xhr’s response’s header list.