mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:17:45 +00:00
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())), }; ```
This commit is contained in:
parent
c12c6fd5ea
commit
65f5c7adbc
5 changed files with 15 additions and 17 deletions
|
@ -277,10 +277,7 @@ WebIDL::ExceptionOr<void> Headers::fill(HeadersInit const& object)
|
||||||
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Array must contain header key/value pair" };
|
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.
|
// 2. Append (header’s first item, header’s second item) to headers.
|
||||||
auto header = Fetch::Infrastructure::Header {
|
auto header = TRY_OR_RETURN_OOM(realm(), Infrastructure::Header::from_string_pair(entry[0], entry[1].bytes()));
|
||||||
.name = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(entry[0].bytes())),
|
|
||||||
.value = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(entry[1].bytes())),
|
|
||||||
};
|
|
||||||
TRY(append(move(header)));
|
TRY(append(move(header)));
|
||||||
}
|
}
|
||||||
return {};
|
return {};
|
||||||
|
@ -288,10 +285,7 @@ WebIDL::ExceptionOr<void> Headers::fill(HeadersInit const& object)
|
||||||
// 2. Otherwise, object is a record, then for each key → value in object, append (key, value) to headers.
|
// 2. Otherwise, object is a record, then for each key → value in object, append (key, value) to headers.
|
||||||
[this](OrderedHashMap<String, String> const& object) -> WebIDL::ExceptionOr<void> {
|
[this](OrderedHashMap<String, String> const& object) -> WebIDL::ExceptionOr<void> {
|
||||||
for (auto const& entry : object) {
|
for (auto const& entry : object) {
|
||||||
auto header = Fetch::Infrastructure::Header {
|
auto header = TRY_OR_RETURN_OOM(realm(), Infrastructure::Header::from_string_pair(entry.key, entry.value));
|
||||||
.name = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(entry.key.bytes())),
|
|
||||||
.value = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(entry.value.bytes())),
|
|
||||||
};
|
|
||||||
TRY(append(move(header)));
|
TRY(append(move(header)));
|
||||||
}
|
}
|
||||||
return {};
|
return {};
|
||||||
|
|
|
@ -34,6 +34,14 @@ requires(IsSameIgnoringCV<T, u8>) struct CaseInsensitiveBytesTraits : public Tra
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
ErrorOr<Header> 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
|
// https://fetch.spec.whatwg.org/#header-list-contains
|
||||||
bool HeaderList::contains(ReadonlyBytes name) const
|
bool HeaderList::contains(ReadonlyBytes name) const
|
||||||
{
|
{
|
||||||
|
|
|
@ -22,6 +22,8 @@ namespace Web::Fetch::Infrastructure {
|
||||||
struct Header {
|
struct Header {
|
||||||
ByteBuffer name;
|
ByteBuffer name;
|
||||||
ByteBuffer value;
|
ByteBuffer value;
|
||||||
|
|
||||||
|
static ErrorOr<Header> from_string_pair(StringView, StringView);
|
||||||
};
|
};
|
||||||
|
|
||||||
// https://fetch.spec.whatwg.org/#concept-header-list
|
// https://fetch.spec.whatwg.org/#concept-header-list
|
||||||
|
|
|
@ -187,10 +187,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::redirect(JS::VM& vm, S
|
||||||
auto value = parsed_url.serialize();
|
auto value = parsed_url.serialize();
|
||||||
|
|
||||||
// 7. Append (`Location`, value) to responseObject’s response’s header list.
|
// 7. Append (`Location`, value) to responseObject’s response’s header list.
|
||||||
auto header = Infrastructure::Header {
|
auto header = TRY_OR_RETURN_OOM(realm, Infrastructure::Header::from_string_pair("Location"sv, value));
|
||||||
.name = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy("Location"sv.bytes())),
|
|
||||||
.value = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy(value.bytes())),
|
|
||||||
};
|
|
||||||
TRY_OR_RETURN_OOM(realm, response_object->response()->header_list()->append(move(header)));
|
TRY_OR_RETURN_OOM(realm, response_object->response()->header_list()->append(move(header)));
|
||||||
|
|
||||||
// 8. Return responseObject.
|
// 8. Return responseObject.
|
||||||
|
|
|
@ -218,11 +218,8 @@ MimeSniff::MimeType XMLHttpRequest::get_response_mime_type() const
|
||||||
// FIXME: Use an actual HeaderList for XHR headers.
|
// FIXME: Use an actual HeaderList for XHR headers.
|
||||||
auto header_list = make_ref_counted<Fetch::Infrastructure::HeaderList>();
|
auto header_list = make_ref_counted<Fetch::Infrastructure::HeaderList>();
|
||||||
for (auto const& entry : m_response_headers) {
|
for (auto const& entry : m_response_headers) {
|
||||||
auto header = Fetch::Infrastructure::Header {
|
auto header = Fetch::Infrastructure::Header::from_string_pair(entry.key, entry.value).release_value_but_fixme_should_propagate_errors();
|
||||||
.name = MUST(ByteBuffer::copy(entry.key.bytes())),
|
header_list->append(move(header)).release_value_but_fixme_should_propagate_errors();
|
||||||
.value = MUST(ByteBuffer::copy(entry.value.bytes())),
|
|
||||||
};
|
|
||||||
MUST(header_list->append(move(header)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1. Let mimeType be the result of extracting a MIME type from xhr’s response’s header list.
|
// 1. Let mimeType be the result of extracting a MIME type from xhr’s response’s header list.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue