| 
									
										
										
										
											2022-07-12 18:04:24 +01:00
										 |  |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2023-03-02 23:26:35 +00:00
										 |  |  |  |  * Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-07-12 18:04:24 +01:00
										 |  |  |  |  * | 
					
						
							|  |  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  |  */ | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-02 10:59:22 +01:00
										 |  |  |  | #include <LibJS/Runtime/PromiseCapability.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-25 19:25:53 +01:00
										 |  |  |  | #include <LibWeb/Bindings/MainThreadVM.h>
 | 
					
						
							| 
									
										
										
										
											2022-10-30 14:39:32 +00:00
										 |  |  |  | #include <LibWeb/Fetch/BodyInit.h>
 | 
					
						
							| 
									
										
										
										
											2022-07-12 18:04:24 +01:00
										 |  |  |  | #include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
 | 
					
						
							| 
									
										
										
										
											2023-02-28 18:12:44 +00:00
										 |  |  |  | #include <LibWeb/Fetch/Infrastructure/Task.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-25 19:27:02 +01:00
										 |  |  |  | #include <LibWeb/WebIDL/Promise.h>
 | 
					
						
							| 
									
										
										
										
											2022-07-12 18:04:24 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-17 23:52:02 +01:00
										 |  |  |  | namespace Web::Fetch::Infrastructure { | 
					
						
							| 
									
										
										
										
											2022-07-12 18:04:24 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-21 23:54:04 +01:00
										 |  |  |  | Body::Body(JS::Handle<Streams::ReadableStream> stream) | 
					
						
							|  |  |  |  |     : m_stream(move(stream)) | 
					
						
							| 
									
										
										
										
											2022-07-12 18:04:24 +01:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-21 23:54:04 +01:00
										 |  |  |  | Body::Body(JS::Handle<Streams::ReadableStream> stream, SourceType source, Optional<u64> length) | 
					
						
							|  |  |  |  |     : m_stream(move(stream)) | 
					
						
							| 
									
										
										
										
											2022-07-12 18:04:24 +01:00
										 |  |  |  |     , m_source(move(source)) | 
					
						
							|  |  |  |  |     , m_length(move(length)) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-25 19:25:53 +01:00
										 |  |  |  | // https://fetch.spec.whatwg.org/#concept-body-clone
 | 
					
						
							| 
									
										
										
										
											2023-02-28 17:45:49 +00:00
										 |  |  |  | WebIDL::ExceptionOr<Body> Body::clone(JS::Realm& realm) const | 
					
						
							| 
									
										
										
										
											2022-09-25 19:25:53 +01:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     // To clone a body body, run these steps:
 | 
					
						
							|  |  |  |  |     // FIXME: 1. Let « out1, out2 » be the result of teeing body’s stream.
 | 
					
						
							|  |  |  |  |     // FIXME: 2. Set body’s stream to out1.
 | 
					
						
							| 
									
										
										
										
											2023-02-28 17:45:49 +00:00
										 |  |  |  |     auto out2 = MUST_OR_THROW_OOM(realm.heap().allocate<Streams::ReadableStream>(realm, realm)); | 
					
						
							| 
									
										
										
										
											2022-09-25 19:25:53 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 3. Return a body whose stream is out2 and other members are copied from body.
 | 
					
						
							|  |  |  |  |     return Body { JS::make_handle(out2), m_source, m_length }; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-28 18:12:44 +00:00
										 |  |  |  | // https://fetch.spec.whatwg.org/#body-fully-read
 | 
					
						
							|  |  |  |  | WebIDL::ExceptionOr<void> Body::fully_read(JS::Realm& realm, Web::Fetch::Infrastructure::Body::ProcessBodyCallback process_body, Web::Fetch::Infrastructure::Body::ProcessBodyErrorCallback process_body_error, TaskDestination task_destination) const | 
					
						
							| 
									
										
										
										
											2022-09-25 19:27:02 +01:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-02-28 18:12:44 +00:00
										 |  |  |  |     auto& vm = realm.vm(); | 
					
						
							| 
									
										
										
										
											2022-09-25 19:27:02 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-28 18:12:44 +00:00
										 |  |  |  |     // FIXME: 1. If taskDestination is null, then set taskDestination to the result of starting a new parallel queue.
 | 
					
						
							|  |  |  |  |     // FIXME: Handle 'parallel queue' task destination
 | 
					
						
							|  |  |  |  |     VERIFY(!task_destination.has<Empty>()); | 
					
						
							|  |  |  |  |     auto task_destination_object = task_destination.get<JS::NonnullGCPtr<JS::Object>>(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 2. Let successSteps given a byte sequence bytes be to queue a fetch task to run processBody given bytes, with taskDestination.
 | 
					
						
							|  |  |  |  |     auto success_steps = [process_body = move(process_body), task_destination_object = JS::make_handle(task_destination_object)](ByteBuffer const& bytes) mutable -> ErrorOr<void> { | 
					
						
							|  |  |  |  |         // Make a copy of the bytes, as the source of the bytes may disappear between the time the task is queued and executed.
 | 
					
						
							|  |  |  |  |         auto bytes_copy = TRY(ByteBuffer::copy(bytes)); | 
					
						
							|  |  |  |  |         queue_fetch_task(*task_destination_object, [process_body = move(process_body), bytes_copy = move(bytes_copy)]() { | 
					
						
							|  |  |  |  |             process_body(move(bytes_copy)); | 
					
						
							|  |  |  |  |         }); | 
					
						
							|  |  |  |  |         return {}; | 
					
						
							|  |  |  |  |     }; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 3. Let errorSteps be to queue a fetch task to run processBodyError, with taskDestination.
 | 
					
						
							|  |  |  |  |     auto error_steps = [process_body_error = move(process_body_error), task_destination_object = JS::make_handle(task_destination_object)](JS::Error& error) mutable { | 
					
						
							|  |  |  |  |         queue_fetch_task(*task_destination_object, [process_body_error = move(process_body_error), error = JS::make_handle(error)]() { | 
					
						
							|  |  |  |  |             process_body_error(*error); | 
					
						
							|  |  |  |  |         }); | 
					
						
							|  |  |  |  |     }; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 4. Let reader be the result of getting a reader for body’s stream. If that threw an exception, then run errorSteps with that exception and return.
 | 
					
						
							|  |  |  |  |     // 5. Read all bytes from reader, given successSteps and errorSteps.
 | 
					
						
							| 
									
										
										
										
											2022-09-25 19:27:02 +01:00
										 |  |  |  |     // FIXME: Implement the streams spec - this is completely made up for now :^)
 | 
					
						
							|  |  |  |  |     if (auto const* byte_buffer = m_source.get_pointer<ByteBuffer>()) { | 
					
						
							| 
									
										
										
										
											2023-02-28 18:12:44 +00:00
										 |  |  |  |         TRY_OR_THROW_OOM(vm, success_steps(*byte_buffer)); | 
					
						
							|  |  |  |  |     } else { | 
					
						
							|  |  |  |  |         // Empty, Blob, FormData
 | 
					
						
							|  |  |  |  |         error_steps(TRY(JS::InternalError::create(realm, "Reading from Blob, FormData or null source is not yet implemented"sv))); | 
					
						
							| 
									
										
										
										
											2022-09-25 19:27:02 +01:00
										 |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-02-28 18:12:44 +00:00
										 |  |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2022-09-25 19:27:02 +01:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-30 14:39:32 +00:00
										 |  |  |  | // https://fetch.spec.whatwg.org/#byte-sequence-as-a-body
 | 
					
						
							|  |  |  |  | WebIDL::ExceptionOr<Body> byte_sequence_as_body(JS::Realm& realm, ReadonlyBytes bytes) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     // To get a byte sequence bytes as a body, return the body of the result of safely extracting bytes.
 | 
					
						
							|  |  |  |  |     auto [body, _] = TRY(safely_extract_body(realm, bytes)); | 
					
						
							|  |  |  |  |     return body; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-12 18:04:24 +01:00
										 |  |  |  | } |