| 
									
										
										
										
											2022-07-12 18:04:24 +01:00
										 |  |  |  | /*
 | 
					
						
							|  |  |  |  |  * Copyright (c) 2022, Linus Groh <linusg@serenityos.org> | 
					
						
							|  |  |  |  |  * | 
					
						
							|  |  |  |  |  * 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-07-12 18:04:24 +01:00
										 |  |  |  | #include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.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
 | 
					
						
							|  |  |  |  | WebIDL::ExceptionOr<Body> Body::clone() const | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     // To clone a body body, run these steps:
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     auto& vm = Bindings::main_thread_vm(); | 
					
						
							|  |  |  |  |     auto& realm = *vm.current_realm(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // FIXME: 1. Let « out1, out2 » be the result of teeing body’s stream.
 | 
					
						
							|  |  |  |  |     // FIXME: 2. Set body’s stream to out1.
 | 
					
						
							| 
									
										
										
											
												LibWeb: Remove unecessary dependence on Window from assorted classes
These classes only needed Window to get at its realm. Pass a realm
directly to construct Crypto, Encoding, HRT, IntersectionObserver,
NavigationTiming, Page, RequestIdleCallback, Selection, Streams, URL,
and XML classes.
											
										 
											2022-09-25 18:11:21 -06:00
										 |  |  |  |     auto* out2 = vm.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 }; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-25 19:27:02 +01:00
										 |  |  |  | // https://fetch.spec.whatwg.org/#fully-reading-body-as-promise
 | 
					
						
							| 
									
										
										
										
											2022-10-02 12:11:30 +01:00
										 |  |  |  | JS::NonnullGCPtr<JS::PromiseCapability> Body::fully_read_as_promise() const | 
					
						
							| 
									
										
										
										
											2022-09-25 19:27:02 +01:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     auto& vm = Bindings::main_thread_vm(); | 
					
						
							|  |  |  |  |     auto& realm = *vm.current_realm(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // FIXME: Implement the streams spec - this is completely made up for now :^)
 | 
					
						
							|  |  |  |  |     if (auto const* byte_buffer = m_source.get_pointer<ByteBuffer>()) { | 
					
						
							|  |  |  |  |         auto result = String::copy(*byte_buffer); | 
					
						
							|  |  |  |  |         return WebIDL::create_resolved_promise(realm, JS::js_string(vm, move(result))); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  |     // Empty, Blob, FormData
 | 
					
						
							|  |  |  |  |     return WebIDL::create_rejected_promise(realm, JS::InternalError::create(realm, "Reading body isn't fully implemented"sv)); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-12 18:04:24 +01:00
										 |  |  |  | } |