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>
|
2024-12-06 09:35:02 -08:00
|
|
|
|
#include <LibWeb/Infra/Strings.h>
|
2022-07-19 00:19:24 +01:00
|
|
|
|
|
2023-01-09 17:49:06 -05:00
|
|
|
|
namespace Web::Bindings {
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
void Intrinsics::create_web_prototype_and_constructor<HeadersIteratorPrototype>(JS::Realm& realm)
|
|
|
|
|
{
|
2024-11-14 05:50:17 +13:00
|
|
|
|
auto prototype = realm.create<HeadersIteratorPrototype>(realm);
|
2023-11-22 12:55:21 +13:00
|
|
|
|
m_prototypes.set("HeadersIterator"_fly_string, prototype);
|
2023-01-09 17:49:06 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-19 00:19:24 +01:00
|
|
|
|
namespace Web::Fetch {
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC_DEFINE_ALLOCATOR(HeadersIterator);
|
2023-11-19 19:47:52 +01:00
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ref<HeadersIterator> HeadersIterator::create(Headers const& headers, JS::Object::PropertyKind iteration_kind)
|
2022-09-04 12:48:49 +02:00
|
|
|
|
{
|
2024-11-14 05:50:17 +13:00
|
|
|
|
return headers.realm().create<HeadersIterator>(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);
|
2024-03-16 13:13:08 +01:00
|
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(HeadersIterator);
|
2023-01-10 06:28:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
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
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ref<JS::Object> HeadersIterator::next()
|
2022-07-19 00:19:24 +01:00
|
|
|
|
{
|
|
|
|
|
// The value pairs to iterate over are the return value of running sort and combine with this’s header list.
|
2024-04-26 13:24:20 -04:00
|
|
|
|
auto value_pairs_to_iterate_over = [&]() {
|
|
|
|
|
return m_headers->m_header_list->sort_and_combine();
|
2022-07-19 00:19:24 +01:00
|
|
|
|
};
|
|
|
|
|
|
2024-04-26 13:24:20 -04:00
|
|
|
|
auto pairs = value_pairs_to_iterate_over();
|
2022-07-19 00:19:24 +01:00
|
|
|
|
|
|
|
|
|
if (m_index >= pairs.size())
|
2024-04-26 13:24:20 -04:00
|
|
|
|
return create_iterator_result_object(vm(), JS::js_undefined(), true);
|
2022-07-19 00:19:24 +01:00
|
|
|
|
|
|
|
|
|
auto const& pair = pairs[m_index++];
|
2024-12-06 09:35:02 -08:00
|
|
|
|
auto pair_name = Infra::isomorphic_decode(pair.name);
|
|
|
|
|
auto pair_value = Infra::isomorphic_decode(pair.value);
|
2022-07-19 00:19:24 +01:00
|
|
|
|
|
|
|
|
|
switch (m_iteration_kind) {
|
|
|
|
|
case JS::Object::PropertyKind::Key:
|
2024-04-26 13:24:20 -04:00
|
|
|
|
return create_iterator_result_object(vm(), JS::PrimitiveString::create(vm(), pair_name), false);
|
2022-07-19 00:19:24 +01:00
|
|
|
|
case JS::Object::PropertyKind::Value:
|
2024-04-26 13:24:20 -04:00
|
|
|
|
return create_iterator_result_object(vm(), JS::PrimitiveString::create(vm(), pair_value), false);
|
2022-07-19 00:19:24 +01:00
|
|
|
|
case JS::Object::PropertyKind::KeyAndValue: {
|
2023-08-08 18:25:57 +02:00
|
|
|
|
auto array = JS::Array::create_from(realm(), { JS::PrimitiveString::create(vm(), pair_name), JS::PrimitiveString::create(vm(), pair_value) });
|
2024-04-26 13:24:20 -04:00
|
|
|
|
return create_iterator_result_object(vm(), array, false);
|
2022-07-19 00:19:24 +01:00
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|