Truly independent web browser
LibHTTP: Add Fetch's "get a structured field value" to header lists
Shannon Bootha9d1966dbca5
diff --git a/Libraries/LibHTTP/HeaderList.cpp b/Libraries/LibHTTP/HeaderList.cppindex fbd0cc88574..6b3328ce0f5 100644--- a/Libraries/LibHTTP/HeaderList.cpp+++ b/Libraries/LibHTTP/HeaderList.cpp@@ -73,6 +73,36 @@ Optional<Vector<String>> HeaderList::get_decode_and_split(StringView name) constreturn get_decode_and_split_header_value(*value);}+// https://fetch.spec.whatwg.org/#concept-header-list-get-structured-header+template<StructuredFields::FieldType T>+Optional<T> HeaderList::get_structured_field_value(StringView name) const+{+ // 1. Assert: type is one of "dictionary", "list", or "item".+ // NB: FieldType constrains T to these.++ // 2. Let value be the result of getting name from list.+ auto value = get(name);++ // 3. If value is null, then return null.+ if (!value.has_value())+ return {};++ // 4. Let result be the result of parsing structured fields with input_string set to value and header_type set to+ // type.+ auto result = StructuredFields::parse<T>(*value);++ // 5. If parsing failed, then return null.+ if (result.is_error())+ return {};++ // 6. Return result.+ return result.release_value();+}++template Optional<StructuredFields::List> HeaderList::get_structured_field_value(StringView) const;+template Optional<StructuredFields::Dictionary> HeaderList::get_structured_field_value(StringView) const;+template Optional<StructuredFields::Item> HeaderList::get_structured_field_value(StringView) const;+// https://fetch.spec.whatwg.org/#concept-header-list-appendvoid HeaderList::append(Header header){diff --git a/Libraries/LibHTTP/HeaderList.h b/Libraries/LibHTTP/HeaderList.hindex 42770b725ac..9d837be25c0 100644--- a/Libraries/LibHTTP/HeaderList.h+++ b/Libraries/LibHTTP/HeaderList.h@@ -14,6 +14,7 @@#include <AK/String.h>#include <AK/Vector.h>#include <LibHTTP/Header.h>+#include <LibHTTP/StructuredFields.h>namespace HTTP {@@ -37,6 +38,8 @@ public:[[nodiscard]] bool contains(StringView) const;Optional<ByteString> get(StringView) const;Optional<Vector<String>> get_decode_and_split(StringView) const;+ template<StructuredFields::FieldType T>+ Optional<T> get_structured_field_value(StringView) const;void append(Header);// The caller must match the casing of existing headers with this name.void append_with_normalized_name(Header header) { m_headers.append(move(header)); }