| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-10-04 13:19:50 +02:00
										 |  |  |  * Copyright (c) 2020-2021, Andreas Kling <andreas@ladybird.org> | 
					
						
							| 
									
										
										
										
											2022-07-07 23:15:41 +02:00
										 |  |  |  * Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org> | 
					
						
							| 
									
										
										
										
											2023-02-28 18:27:52 +00:00
										 |  |  |  * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/ByteBuffer.h>
 | 
					
						
							|  |  |  | #include <AK/RefCounted.h>
 | 
					
						
							|  |  |  | #include <AK/Weakable.h>
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:22:27 +13:00
										 |  |  | #include <LibURL/URL.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  | #include <LibWeb/DOM/EventTarget.h>
 | 
					
						
							| 
									
										
										
										
											2024-02-11 19:48:56 +13:00
										 |  |  | #include <LibWeb/DOMURL/URLSearchParams.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-21 23:14:40 +01:00
										 |  |  | #include <LibWeb/Fetch/BodyInit.h>
 | 
					
						
							| 
									
										
										
										
											2023-02-28 18:27:52 +00:00
										 |  |  | #include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
 | 
					
						
							| 
									
										
										
										
											2022-07-18 08:08:21 +01:00
										 |  |  | #include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
 | 
					
						
							| 
									
										
										
										
											2022-07-12 18:17:50 +01:00
										 |  |  | #include <LibWeb/Fetch/Infrastructure/HTTP/Statuses.h>
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  | #include <LibWeb/HTML/Window.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-11 21:04:42 +00:00
										 |  |  | #include <LibWeb/MimeSniff/MimeType.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-25 17:03:42 +01:00
										 |  |  | #include <LibWeb/WebIDL/ExceptionOr.h>
 | 
					
						
							| 
									
										
										
										
											2021-01-23 17:52:16 +00:00
										 |  |  | #include <LibWeb/XHR/XMLHttpRequestEventTarget.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-23 11:51:36 +01:00
										 |  |  | namespace Web::XHR { | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-10 20:11:25 +01:00
										 |  |  | // https://fetch.spec.whatwg.org/#typedefdef-xmlhttprequestbodyinit
 | 
					
						
							| 
									
										
										
										
											2024-02-11 19:48:56 +13:00
										 |  |  | using DocumentOrXMLHttpRequestBodyInit = Variant<JS::Handle<Web::DOM::Document>, JS::Handle<Web::FileAPI::Blob>, JS::Handle<WebIDL::BufferSource>, JS::Handle<XHR::FormData>, JS::Handle<Web::DOMURL::URLSearchParams>, AK::String>; | 
					
						
							| 
									
										
										
										
											2022-11-10 20:11:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  | class XMLHttpRequest final : public XMLHttpRequestEventTarget { | 
					
						
							|  |  |  |     WEB_PLATFORM_OBJECT(XMLHttpRequest, XMLHttpRequestEventTarget); | 
					
						
							| 
									
										
										
										
											2023-11-19 19:47:52 +01:00
										 |  |  |     JS_DECLARE_ALLOCATOR(XMLHttpRequest); | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2022-11-13 13:55:40 +00:00
										 |  |  |     enum class State : u16 { | 
					
						
							| 
									
										
										
										
											2021-01-23 13:23:17 +01:00
										 |  |  |         Unsent = 0, | 
					
						
							|  |  |  |         Opened = 1, | 
					
						
							|  |  |  |         HeadersReceived = 2, | 
					
						
							|  |  |  |         Loading = 3, | 
					
						
							|  |  |  |         Done = 4, | 
					
						
							| 
									
										
										
										
											2020-04-22 19:47:26 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-14 20:34:37 +01:00
										 |  |  |     static WebIDL::ExceptionOr<JS::NonnullGCPtr<XMLHttpRequest>> construct_impl(JS::Realm&); | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     virtual ~XMLHttpRequest() override; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 22:48:11 -04:00
										 |  |  |     State ready_state() const { return m_state; } | 
					
						
							| 
									
										
										
										
											2023-02-28 18:27:52 +00:00
										 |  |  |     Fetch::Infrastructure::Status status() const; | 
					
						
							|  |  |  |     WebIDL::ExceptionOr<String> status_text() const; | 
					
						
							|  |  |  |     WebIDL::ExceptionOr<String> response_text() const; | 
					
						
							| 
									
										
										
										
											2023-09-14 21:07:53 +02:00
										 |  |  |     WebIDL::ExceptionOr<JS::GCPtr<DOM::Document>> response_xml(); | 
					
						
							| 
									
										
										
										
											2022-09-25 17:03:42 +01:00
										 |  |  |     WebIDL::ExceptionOr<JS::Value> response(); | 
					
						
							| 
									
										
										
										
											2022-02-15 14:35:29 +03:30
										 |  |  |     Bindings::XMLHttpRequestResponseType response_type() const { return m_response_type; } | 
					
						
							| 
									
										
										
										
											2024-05-01 11:10:15 +02:00
										 |  |  |     String response_url(); | 
					
						
							| 
									
										
										
										
											2021-02-20 00:46:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-28 18:27:52 +00:00
										 |  |  |     WebIDL::ExceptionOr<void> open(String const& method, String const& url); | 
					
						
							|  |  |  |     WebIDL::ExceptionOr<void> open(String const& method, String const& url, bool async, Optional<String> const& username = Optional<String> {}, Optional<String> const& password = Optional<String> {}); | 
					
						
							| 
									
										
										
										
											2022-11-10 20:11:25 +01:00
										 |  |  |     WebIDL::ExceptionOr<void> send(Optional<DocumentOrXMLHttpRequestBodyInit> body); | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-28 18:27:52 +00:00
										 |  |  |     WebIDL::ExceptionOr<void> set_request_header(String const& header, String const& value); | 
					
						
							| 
									
										
										
										
											2022-10-24 15:30:54 +02:00
										 |  |  |     WebIDL::ExceptionOr<void> set_response_type(Bindings::XMLHttpRequestResponseType); | 
					
						
							| 
									
										
										
										
											2021-01-18 14:01:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-28 18:27:52 +00:00
										 |  |  |     WebIDL::ExceptionOr<Optional<String>> get_response_header(String const& name) const; | 
					
						
							|  |  |  |     WebIDL::ExceptionOr<String> get_all_response_headers() const; | 
					
						
							| 
									
										
										
										
											2021-04-03 15:51:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-24 16:02:41 +01:00
										 |  |  |     WebIDL::CallbackType* onreadystatechange(); | 
					
						
							|  |  |  |     void set_onreadystatechange(WebIDL::CallbackType*); | 
					
						
							| 
									
										
										
										
											2021-09-19 01:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-28 18:27:52 +00:00
										 |  |  |     WebIDL::ExceptionOr<void> override_mime_type(String const& mime); | 
					
						
							| 
									
										
										
										
											2022-02-11 21:04:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-23 21:42:15 +02:00
										 |  |  |     u32 timeout() const; | 
					
						
							| 
									
										
										
										
											2022-11-13 14:47:21 +00:00
										 |  |  |     WebIDL::ExceptionOr<void> set_timeout(u32 timeout); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool with_credentials() const; | 
					
						
							|  |  |  |     WebIDL::ExceptionOr<void> set_with_credentials(bool); | 
					
						
							| 
									
										
										
										
											2022-04-23 21:42:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-05 03:35:02 +00:00
										 |  |  |     void abort(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-28 18:27:52 +00:00
										 |  |  |     JS::NonnullGCPtr<XMLHttpRequestUpload> upload() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  |     virtual void initialize(JS::Realm&) override; | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     virtual void visit_edges(Cell::Visitor&) override; | 
					
						
							| 
									
										
										
										
											2022-10-24 14:57:28 +02:00
										 |  |  |     virtual bool must_survive_garbage_collection() const override; | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-03 09:27:51 +00:00
										 |  |  |     ErrorOr<MimeSniff::MimeType> get_response_mime_type() const; | 
					
						
							|  |  |  |     ErrorOr<Optional<StringView>> get_final_encoding() const; | 
					
						
							|  |  |  |     ErrorOr<MimeSniff::MimeType> get_final_mime_type() const; | 
					
						
							| 
									
										
										
										
											2022-02-11 21:31:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-28 18:27:52 +00:00
										 |  |  |     String get_text_response() const; | 
					
						
							| 
									
										
										
										
											2023-09-14 21:07:53 +02:00
										 |  |  |     void set_document_response(); | 
					
						
							| 
									
										
										
										
											2022-02-11 21:31:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-28 18:27:52 +00:00
										 |  |  |     WebIDL::ExceptionOr<void> handle_response_end_of_body(); | 
					
						
							|  |  |  |     WebIDL::ExceptionOr<void> handle_errors(); | 
					
						
							| 
									
										
										
										
											2023-04-09 10:57:50 +02:00
										 |  |  |     JS::ThrowCompletionOr<void> request_error_steps(FlyString const& event_name, JS::GCPtr<WebIDL::DOMException> exception = nullptr); | 
					
						
							| 
									
										
										
										
											2020-04-08 21:18:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-28 18:27:52 +00:00
										 |  |  |     XMLHttpRequest(JS::Realm&, XMLHttpRequestUpload&, Fetch::Infrastructure::HeaderList&, Fetch::Infrastructure::Response&, Fetch::Infrastructure::FetchController&); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // https://xhr.spec.whatwg.org/#upload-object
 | 
					
						
							|  |  |  |     // upload object
 | 
					
						
							|  |  |  |     //     An XMLHttpRequestUpload object.
 | 
					
						
							|  |  |  |     JS::NonnullGCPtr<XMLHttpRequestUpload> m_upload_object; | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-13 14:09:46 +00:00
										 |  |  |     // https://xhr.spec.whatwg.org/#concept-xmlhttprequest-state
 | 
					
						
							|  |  |  |     // state
 | 
					
						
							|  |  |  |     //     One of unsent, opened, headers received, loading, and done; initially unsent.
 | 
					
						
							| 
									
										
										
										
											2022-11-13 13:55:40 +00:00
										 |  |  |     State m_state { State::Unsent }; | 
					
						
							| 
									
										
										
										
											2022-11-13 14:09:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // https://xhr.spec.whatwg.org/#send-flag
 | 
					
						
							|  |  |  |     // send() flag
 | 
					
						
							|  |  |  |     //     A flag, initially unset.
 | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |     bool m_send { false }; | 
					
						
							| 
									
										
										
										
											2022-11-13 14:09:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // https://xhr.spec.whatwg.org/#timeout
 | 
					
						
							|  |  |  |     // timeout
 | 
					
						
							|  |  |  |     //     An unsigned integer, initially 0.
 | 
					
						
							| 
									
										
										
										
											2022-04-23 21:42:15 +02:00
										 |  |  |     u32 m_timeout { 0 }; | 
					
						
							| 
									
										
										
										
											2020-04-22 19:47:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-13 14:47:21 +00:00
										 |  |  |     // https://xhr.spec.whatwg.org/#cross-origin-credentials
 | 
					
						
							|  |  |  |     // cross-origin credentials
 | 
					
						
							|  |  |  |     //     A boolean, initially false.
 | 
					
						
							|  |  |  |     bool m_cross_origin_credentials { false }; | 
					
						
							| 
									
										
										
										
											2022-11-13 14:09:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // https://xhr.spec.whatwg.org/#request-method
 | 
					
						
							|  |  |  |     // request method
 | 
					
						
							|  |  |  |     //     A method.
 | 
					
						
							| 
									
										
										
										
											2023-12-16 17:49:34 +03:30
										 |  |  |     ByteString m_request_method; | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-13 14:09:46 +00:00
										 |  |  |     // https://xhr.spec.whatwg.org/#request-url
 | 
					
						
							|  |  |  |     // request URL
 | 
					
						
							|  |  |  |     //     A URL.
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:22:27 +13:00
										 |  |  |     URL::URL m_request_url; | 
					
						
							| 
									
										
										
										
											2022-02-15 14:35:29 +03:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-13 14:09:46 +00:00
										 |  |  |     // https://xhr.spec.whatwg.org/#author-request-headers
 | 
					
						
							|  |  |  |     // author request headers
 | 
					
						
							|  |  |  |     //     A header list, initially empty.
 | 
					
						
							| 
									
										
										
										
											2022-11-13 14:31:31 +00:00
										 |  |  |     JS::NonnullGCPtr<Fetch::Infrastructure::HeaderList> m_author_request_headers; | 
					
						
							| 
									
										
										
										
											2021-01-18 14:01:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-28 18:27:52 +00:00
										 |  |  |     // https://xhr.spec.whatwg.org/#request-body
 | 
					
						
							|  |  |  |     // request body
 | 
					
						
							|  |  |  |     //     Initially null.
 | 
					
						
							| 
									
										
										
										
											2023-08-18 19:38:13 +02:00
										 |  |  |     JS::GCPtr<Fetch::Infrastructure::Body> m_request_body; | 
					
						
							| 
									
										
										
										
											2022-11-13 14:09:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // https://xhr.spec.whatwg.org/#synchronous-flag
 | 
					
						
							|  |  |  |     // synchronous flag
 | 
					
						
							|  |  |  |     //     A flag, initially unset.
 | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |     bool m_synchronous { false }; | 
					
						
							| 
									
										
										
										
											2022-11-13 14:09:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // https://xhr.spec.whatwg.org/#upload-complete-flag
 | 
					
						
							|  |  |  |     // upload complete flag
 | 
					
						
							|  |  |  |     //     A flag, initially unset.
 | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |     bool m_upload_complete { false }; | 
					
						
							| 
									
										
										
										
											2022-11-13 14:09:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // https://xhr.spec.whatwg.org/#upload-listener-flag
 | 
					
						
							|  |  |  |     // upload listener flag
 | 
					
						
							|  |  |  |     //     A flag, initially unset.
 | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |     bool m_upload_listener { false }; | 
					
						
							| 
									
										
										
										
											2022-11-13 14:09:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // https://xhr.spec.whatwg.org/#timed-out-flag
 | 
					
						
							|  |  |  |     // timed out flag
 | 
					
						
							|  |  |  |     //     A flag, initially unset.
 | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |     bool m_timed_out { false }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-28 18:27:52 +00:00
										 |  |  |     // https://xhr.spec.whatwg.org/#response
 | 
					
						
							|  |  |  |     // response
 | 
					
						
							|  |  |  |     //     A response, initially a network error.
 | 
					
						
							|  |  |  |     JS::NonnullGCPtr<Fetch::Infrastructure::Response> m_response; | 
					
						
							| 
									
										
										
										
											2022-11-13 14:09:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // https://xhr.spec.whatwg.org/#received-bytes
 | 
					
						
							|  |  |  |     // received bytes
 | 
					
						
							|  |  |  |     //     A byte sequence, initially the empty byte sequence.
 | 
					
						
							| 
									
										
										
										
											2022-02-15 14:35:29 +03:30
										 |  |  |     ByteBuffer m_received_bytes; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-13 14:09:46 +00:00
										 |  |  |     // https://xhr.spec.whatwg.org/#response-type
 | 
					
						
							|  |  |  |     // response type
 | 
					
						
							|  |  |  |     //     One of the empty string, "arraybuffer", "blob", "document", "json", and "text"; initially the empty string.
 | 
					
						
							|  |  |  |     Bindings::XMLHttpRequestResponseType m_response_type; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-15 14:35:29 +03:30
										 |  |  |     enum class Failure { | 
					
						
							|  |  |  |         /// ????
 | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2022-11-13 14:09:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // https://xhr.spec.whatwg.org/#response-object
 | 
					
						
							|  |  |  |     // response object
 | 
					
						
							|  |  |  |     //     An object, failure, or null, initially null.
 | 
					
						
							| 
									
										
										
										
											2022-11-13 14:37:53 +00:00
										 |  |  |     //     NOTE: This needs to be a JS::Value as the JSON response might not actually be an object.
 | 
					
						
							| 
									
										
										
										
											2023-10-07 07:57:16 +02:00
										 |  |  |     Variant<JS::NonnullGCPtr<JS::Object>, Failure, Empty> m_response_object; | 
					
						
							| 
									
										
										
										
											2022-02-11 21:04:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-28 18:27:52 +00:00
										 |  |  |     // https://xhr.spec.whatwg.org/#xmlhttprequest-fetch-controller
 | 
					
						
							|  |  |  |     // fetch controller
 | 
					
						
							|  |  |  |     //     A fetch controller, initially a new fetch controller.
 | 
					
						
							|  |  |  |     //     NOTE: The send() method sets it to a useful fetch controller, but for simplicity it always holds a fetch controller.
 | 
					
						
							|  |  |  |     JS::NonnullGCPtr<Fetch::Infrastructure::FetchController> m_fetch_controller; | 
					
						
							| 
									
										
										
										
											2022-11-13 14:09:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-11 21:04:42 +00:00
										 |  |  |     // https://xhr.spec.whatwg.org/#override-mime-type
 | 
					
						
							| 
									
										
										
										
											2022-11-13 14:09:46 +00:00
										 |  |  |     // override MIME type
 | 
					
						
							|  |  |  |     //     A MIME type or null, initially null.
 | 
					
						
							|  |  |  |     //     NOTE: Can get a value when overrideMimeType() is invoked.
 | 
					
						
							| 
									
										
										
										
											2022-02-11 21:04:42 +00:00
										 |  |  |     Optional<MimeSniff::MimeType> m_override_mime_type; | 
					
						
							| 
									
										
										
										
											2023-02-28 18:27:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Non-standard, see async path in `send()`
 | 
					
						
							|  |  |  |     u64 m_request_body_transmitted { 0 }; | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |