| 
									
										
										
										
											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-08-13 13:05:26 +02:00
										 |  |  |  |     auto prototype = heap().allocate<HeadersIteratorPrototype>(realm, 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 { | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-19 19:47:52 +01:00
										 |  |  |  | JS_DEFINE_ALLOCATOR(HeadersIterator); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-13 13:05:26 +02:00
										 |  |  |  | JS::NonnullGCPtr<HeadersIterator> HeadersIterator::create(Headers const& headers, JS::Object::PropertyKind iteration_kind) | 
					
						
							| 
									
										
										
										
											2022-09-04 12:48:49 +02:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-08-13 13:05:26 +02:00
										 |  |  |  |     return 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); | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  |  | 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-08-08 18:25:57 +02:00
										 |  |  |  |         return create_iterator_result_object(vm(), JS::PrimitiveString::create(vm(), pair_name), false).ptr(); | 
					
						
							| 
									
										
										
										
											2022-07-19 00:19:24 +01:00
										 |  |  |  |     case JS::Object::PropertyKind::Value: | 
					
						
							| 
									
										
										
										
											2023-08-08 18:25:57 +02:00
										 |  |  |  |         return create_iterator_result_object(vm(), JS::PrimitiveString::create(vm(), pair_value), false).ptr(); | 
					
						
							| 
									
										
										
										
											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) }); | 
					
						
							| 
									
										
										
										
											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(); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | } |