mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-28 11:49:44 +00:00
LibWeb: Remove OOM propagation from Fetch::Infrastructure::Headers
This commit is contained in:
parent
2925fcd4bc
commit
c79f46fe6f
Notes:
sideshowbarker
2024-07-17 09:47:09 +09:00
Author: https://github.com/trflynn89
Commit: c79f46fe6f
Pull-request: https://github.com/SerenityOS/serenity/pull/24124
23 changed files with 212 additions and 225 deletions
|
@ -69,12 +69,11 @@ WebIDL::ExceptionOr<void> Headers::append(String const& name_string, String cons
|
|||
WebIDL::ExceptionOr<void> Headers::delete_(String const& name_string)
|
||||
{
|
||||
// The delete(name) method steps are:
|
||||
auto& vm = this->vm();
|
||||
auto name = name_string.bytes();
|
||||
|
||||
// 1. If validating (name, ``) for headers returns false, then return.
|
||||
// NOTE: Passing a dummy header value ought not to have any negative repercussions.
|
||||
auto header = TRY_OR_THROW_OOM(vm, Infrastructure::Header::from_string_pair(name, ""sv));
|
||||
auto header = Infrastructure::Header::from_string_pair(name, ""sv);
|
||||
if (!TRY(validate(header)))
|
||||
return {};
|
||||
|
||||
|
@ -108,7 +107,7 @@ WebIDL::ExceptionOr<Optional<String>> Headers::get(String const& name_string)
|
|||
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header name"sv };
|
||||
|
||||
// 2. Return the result of getting name from this’s header list.
|
||||
auto byte_buffer = TRY_OR_THROW_OOM(vm, m_header_list->get(name));
|
||||
auto byte_buffer = m_header_list->get(name);
|
||||
return byte_buffer.has_value() ? TRY_OR_THROW_OOM(vm, String::from_utf8(*byte_buffer)) : Optional<String> {};
|
||||
}
|
||||
|
||||
|
@ -157,7 +156,7 @@ WebIDL::ExceptionOr<void> Headers::set(String const& name_string, String const&
|
|||
auto value = value_string.bytes();
|
||||
|
||||
// 1. Normalize value.
|
||||
auto normalized_value = TRY_OR_THROW_OOM(vm, Infrastructure::normalize_header_value(value));
|
||||
auto normalized_value = Infrastructure::normalize_header_value(value);
|
||||
|
||||
auto header = Infrastructure::Header {
|
||||
.name = TRY_OR_THROW_OOM(vm, ByteBuffer::copy(name)),
|
||||
|
@ -173,7 +172,7 @@ WebIDL::ExceptionOr<void> Headers::set(String const& name_string, String const&
|
|||
return {};
|
||||
|
||||
// 4. Set (name, value) in this’s header list.
|
||||
TRY_OR_THROW_OOM(vm, m_header_list->set(move(header)));
|
||||
m_header_list->set(move(header));
|
||||
|
||||
// 5. If this’s guard is "request-no-cors", then remove privileged no-CORS request-headers from this.
|
||||
if (m_guard == Guard::RequestNoCORS)
|
||||
|
@ -188,14 +187,14 @@ JS::ThrowCompletionOr<void> Headers::for_each(ForEachCallback callback)
|
|||
auto& vm = this->vm();
|
||||
|
||||
// The value pairs to iterate over are the return value of running sort and combine with this’s header list.
|
||||
auto value_pairs_to_iterate_over = [&]() -> JS::ThrowCompletionOr<Vector<Fetch::Infrastructure::Header>> {
|
||||
return TRY_OR_THROW_OOM(vm, m_header_list->sort_and_combine());
|
||||
auto value_pairs_to_iterate_over = [&]() {
|
||||
return m_header_list->sort_and_combine();
|
||||
};
|
||||
|
||||
// 1-5. Are done in the generated wrapper code.
|
||||
|
||||
// 6. Let pairs be idlObject’s list of value pairs to iterate over.
|
||||
auto pairs = TRY(value_pairs_to_iterate_over());
|
||||
auto pairs = value_pairs_to_iterate_over();
|
||||
|
||||
// 7. Let i be 0.
|
||||
size_t i = 0;
|
||||
|
@ -209,7 +208,7 @@ JS::ThrowCompletionOr<void> Headers::for_each(ForEachCallback callback)
|
|||
TRY(callback(TRY_OR_THROW_OOM(vm, String::from_utf8(pair.name)), TRY_OR_THROW_OOM(vm, String::from_utf8(pair.value))));
|
||||
|
||||
// 3. Set pairs to idlObject’s current list of value pairs to iterate over. (It might have changed.)
|
||||
pairs = TRY(value_pairs_to_iterate_over());
|
||||
pairs = value_pairs_to_iterate_over();
|
||||
|
||||
// 4. Set i to i + 1.
|
||||
++i;
|
||||
|
@ -221,8 +220,6 @@ JS::ThrowCompletionOr<void> Headers::for_each(ForEachCallback callback)
|
|||
// https://fetch.spec.whatwg.org/#headers-validate
|
||||
WebIDL::ExceptionOr<bool> Headers::validate(Infrastructure::Header const& header) const
|
||||
{
|
||||
auto& realm = this->realm();
|
||||
|
||||
// To validate a header (name, value) for a Headers object headers:
|
||||
auto const& [name, value] = header;
|
||||
|
||||
|
@ -237,7 +234,7 @@ WebIDL::ExceptionOr<bool> Headers::validate(Infrastructure::Header const& header
|
|||
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Headers object is immutable"sv };
|
||||
|
||||
// 3. If headers’s guard is "request" and (name, value) is a forbidden request-header, then return false.
|
||||
if (m_guard == Guard::Request && TRY_OR_THROW_OOM(realm.vm(), Infrastructure::is_forbidden_request_header(header)))
|
||||
if (m_guard == Guard::Request && Infrastructure::is_forbidden_request_header(header))
|
||||
return false;
|
||||
|
||||
// 4. If headers’s guard is "response" and name is a forbidden response-header name, then return false.
|
||||
|
@ -258,7 +255,7 @@ WebIDL::ExceptionOr<void> Headers::append(Infrastructure::Header header)
|
|||
auto& [name, value] = header;
|
||||
|
||||
// 1. Normalize value.
|
||||
value = TRY_OR_THROW_OOM(vm, Infrastructure::normalize_header_value(value));
|
||||
value = Infrastructure::normalize_header_value(value);
|
||||
|
||||
// 2. If validating (name, value) for headers returns false, then return.
|
||||
if (!TRY(validate(header)))
|
||||
|
@ -267,7 +264,7 @@ WebIDL::ExceptionOr<void> Headers::append(Infrastructure::Header header)
|
|||
// 3. If headers’s guard is "request-no-cors":
|
||||
if (m_guard == Guard::RequestNoCORS) {
|
||||
// 1. Let temporaryValue be the result of getting name from headers’s header list.
|
||||
auto temporary_value = TRY_OR_THROW_OOM(vm, m_header_list->get(name));
|
||||
auto temporary_value = m_header_list->get(name);
|
||||
|
||||
// 2. If temporaryValue is null, then set temporaryValue to value.
|
||||
if (!temporary_value.has_value()) {
|
||||
|
@ -291,7 +288,7 @@ WebIDL::ExceptionOr<void> Headers::append(Infrastructure::Header header)
|
|||
}
|
||||
|
||||
// 4. Append (name, value) to headers’s header list.
|
||||
TRY_OR_THROW_OOM(vm, m_header_list->append(move(header)));
|
||||
m_header_list->append(move(header));
|
||||
|
||||
// 5. If headers’s guard is "request-no-cors", then remove privileged no-CORS request-headers from headers.
|
||||
if (m_guard == Guard::RequestNoCORS)
|
||||
|
@ -303,8 +300,6 @@ WebIDL::ExceptionOr<void> Headers::append(Infrastructure::Header header)
|
|||
// https://fetch.spec.whatwg.org/#concept-headers-fill
|
||||
WebIDL::ExceptionOr<void> Headers::fill(HeadersInit const& object)
|
||||
{
|
||||
auto& vm = realm().vm();
|
||||
|
||||
// To fill a Headers object headers with a given object object, run these steps:
|
||||
return object.visit(
|
||||
// 1. If object is a sequence, then for each header of object:
|
||||
|
@ -315,7 +310,7 @@ WebIDL::ExceptionOr<void> Headers::fill(HeadersInit const& object)
|
|||
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Array must contain header key/value pair"sv };
|
||||
|
||||
// 2. Append (header[0], header[1]) to headers.
|
||||
auto header = TRY_OR_THROW_OOM(vm, Infrastructure::Header::from_string_pair(entry[0], entry[1]));
|
||||
auto header = Infrastructure::Header::from_string_pair(entry[0], entry[1]);
|
||||
TRY(append(move(header)));
|
||||
}
|
||||
return {};
|
||||
|
@ -323,7 +318,7 @@ WebIDL::ExceptionOr<void> Headers::fill(HeadersInit const& object)
|
|||
// 2. Otherwise, object is a record, then for each key → value of object, append (key, value) to headers.
|
||||
[&](OrderedHashMap<String, String> const& object) -> WebIDL::ExceptionOr<void> {
|
||||
for (auto const& entry : object) {
|
||||
auto header = TRY_OR_THROW_OOM(vm, Infrastructure::Header::from_string_pair(entry.key, entry.value));
|
||||
auto header = Infrastructure::Header::from_string_pair(entry.key, entry.value);
|
||||
TRY(append(move(header)));
|
||||
}
|
||||
return {};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue