2022-07-19 00:19:24 +01:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <LibJS/Runtime/Array.h>
|
2023-07-19 06:54:48 -04:00
|
|
|
|
#include <LibJS/Runtime/Iterator.h>
|
2022-09-04 12:48:49 +02:00
|
|
|
|
#include <LibWeb/Bindings/HeadersIteratorPrototype.h>
|
2022-09-25 18:08:29 -06:00
|
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2022-07-19 00:19:24 +01:00
|
|
|
|
#include <LibWeb/Fetch/HeadersIterator.h>
|
|
|
|
|
|
2023-01-09 17:49:06 -05:00
|
|
|
|
namespace Web::Bindings {
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
void Intrinsics::create_web_prototype_and_constructor<HeadersIteratorPrototype>(JS::Realm& realm)
|
|
|
|
|
{
|
2023-01-28 13:39:44 -05:00
|
|
|
|
auto prototype = heap().allocate<HeadersIteratorPrototype>(realm, realm).release_allocated_value_but_fixme_should_propagate_errors();
|
2023-01-09 17:49:06 -05:00
|
|
|
|
m_prototypes.set("HeadersIterator"sv, prototype);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-19 00:19:24 +01:00
|
|
|
|
namespace Web::Fetch {
|
|
|
|
|
|
2023-02-19 15:33:03 +01:00
|
|
|
|
WebIDL::ExceptionOr<JS::NonnullGCPtr<HeadersIterator>> HeadersIterator::create(Headers const& headers, JS::Object::PropertyKind iteration_kind)
|
2022-09-04 12:48:49 +02:00
|
|
|
|
{
|
2023-02-19 15:33:03 +01:00
|
|
|
|
return MUST_OR_THROW_OOM(headers.heap().allocate<HeadersIterator>(headers.realm(), headers, iteration_kind));
|
2022-09-04 12:48:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HeadersIterator::HeadersIterator(Headers const& headers, JS::Object::PropertyKind iteration_kind)
|
|
|
|
|
: PlatformObject(headers.realm())
|
|
|
|
|
, m_headers(headers)
|
|
|
|
|
, m_iteration_kind(iteration_kind)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HeadersIterator::~HeadersIterator() = default;
|
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
|
void HeadersIterator::initialize(JS::Realm& realm)
|
2023-01-10 06:28:20 -05:00
|
|
|
|
{
|
2023-08-07 08:41:28 +02:00
|
|
|
|
Base::initialize(realm);
|
2023-01-10 06:28:20 -05:00
|
|
|
|
set_prototype(&Bindings::ensure_web_prototype<Bindings::HeadersIteratorPrototype>(realm, "HeadersIterator"));
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-04 12:48:49 +02:00
|
|
|
|
void HeadersIterator::visit_edges(JS::Cell::Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
2023-02-26 16:09:02 -07:00
|
|
|
|
visitor.visit(m_headers);
|
2022-09-04 12:48:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-19 00:19:24 +01:00
|
|
|
|
// https://webidl.spec.whatwg.org/#es-iterable, Step 2
|
|
|
|
|
JS::ThrowCompletionOr<JS::Object*> HeadersIterator::next()
|
|
|
|
|
{
|
|
|
|
|
// 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>> {
|
2023-02-26 16:09:02 -07:00
|
|
|
|
auto headers_or_error = m_headers->m_header_list->sort_and_combine();
|
2022-07-19 00:19:24 +01:00
|
|
|
|
if (headers_or_error.is_error())
|
2022-09-04 12:48:49 +02:00
|
|
|
|
return vm().throw_completion<JS::InternalError>(JS::ErrorType::NotEnoughMemoryToAllocate);
|
2022-07-19 00:19:24 +01:00
|
|
|
|
return headers_or_error.release_value();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto pairs = TRY(value_pairs_to_iterate_over());
|
|
|
|
|
|
|
|
|
|
if (m_index >= pairs.size())
|
2023-04-15 16:49:37 +02:00
|
|
|
|
return create_iterator_result_object(vm(), JS::js_undefined(), true).ptr();
|
2022-07-19 00:19:24 +01:00
|
|
|
|
|
|
|
|
|
auto const& pair = pairs[m_index++];
|
2023-02-09 09:00:14 -05:00
|
|
|
|
StringView pair_name { pair.name };
|
|
|
|
|
StringView pair_value { pair.value };
|
2022-07-19 00:19:24 +01:00
|
|
|
|
|
|
|
|
|
switch (m_iteration_kind) {
|
|
|
|
|
case JS::Object::PropertyKind::Key:
|
2023-04-15 16:49:37 +02:00
|
|
|
|
return create_iterator_result_object(vm(), MUST_OR_THROW_OOM(JS::PrimitiveString::create(vm(), pair_name)), false).ptr();
|
2022-07-19 00:19:24 +01:00
|
|
|
|
case JS::Object::PropertyKind::Value:
|
2023-04-15 16:49:37 +02:00
|
|
|
|
return create_iterator_result_object(vm(), MUST_OR_THROW_OOM(JS::PrimitiveString::create(vm(), pair_value)), false).ptr();
|
2022-07-19 00:19:24 +01:00
|
|
|
|
case JS::Object::PropertyKind::KeyAndValue: {
|
2023-02-09 09:00:14 -05:00
|
|
|
|
auto array = JS::Array::create_from(realm(), { MUST_OR_THROW_OOM(JS::PrimitiveString::create(vm(), pair_name)), MUST_OR_THROW_OOM(JS::PrimitiveString::create(vm(), pair_value)) });
|
2023-04-15 16:49:37 +02:00
|
|
|
|
return create_iterator_result_object(vm(), array, false).ptr();
|
2022-07-19 00:19:24 +01:00
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|