| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-01-23 13:23:17 +01:00
										 |  |  |  * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-07-07 23:15:41 +02:00
										 |  |  |  * Copyright (c) 2022, Kenneth Myhra <kennethmyhra@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>
 | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  | #include <AK/URL.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  | #include <AK/Weakable.h>
 | 
					
						
							| 
									
										
										
										
											2021-02-17 22:53:20 +01:00
										 |  |  | #include <LibWeb/Bindings/WindowObject.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  | #include <LibWeb/Bindings/Wrappable.h>
 | 
					
						
							|  |  |  | #include <LibWeb/DOM/EventTarget.h>
 | 
					
						
							| 
									
										
										
										
											2021-02-20 00:45:24 +01:00
										 |  |  | #include <LibWeb/DOM/ExceptionOr.h>
 | 
					
						
							| 
									
										
										
										
											2022-07-12 18:17:50 +01:00
										 |  |  | #include <LibWeb/Fetch/Infrastructure/HTTP/Statuses.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-11 21:04:42 +00:00
										 |  |  | #include <LibWeb/MimeSniff/MimeType.h>
 | 
					
						
							| 
									
										
										
										
											2022-07-07 23:15:41 +02:00
										 |  |  | #include <LibWeb/URL/URLSearchParams.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-07-07 23:15:41 +02:00
										 |  |  | using XMLHttpRequestBodyInit = Variant<NonnullRefPtr<URL::URLSearchParams>, String>; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  | class XMLHttpRequest final | 
					
						
							|  |  |  |     : public RefCounted<XMLHttpRequest> | 
					
						
							|  |  |  |     , public Weakable<XMLHttpRequest> | 
					
						
							| 
									
										
										
										
											2021-01-23 17:52:16 +00:00
										 |  |  |     , public XMLHttpRequestEventTarget { | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2021-01-23 13:23:17 +01:00
										 |  |  |     enum class ReadyState : u16 { | 
					
						
							|  |  |  |         Unsent = 0, | 
					
						
							|  |  |  |         Opened = 1, | 
					
						
							|  |  |  |         HeadersReceived = 2, | 
					
						
							|  |  |  |         Loading = 3, | 
					
						
							|  |  |  |         Done = 4, | 
					
						
							| 
									
										
										
										
											2020-04-22 19:47:26 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-07 23:15:41 +02:00
										 |  |  |     struct BodyWithType { | 
					
						
							|  |  |  |         ByteBuffer body; | 
					
						
							|  |  |  |         String type; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  |     using WrapperType = Bindings::XMLHttpRequestWrapper; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-07 23:08:26 +01:00
										 |  |  |     static NonnullRefPtr<XMLHttpRequest> create(HTML::Window& window) | 
					
						
							| 
									
										
										
										
											2021-02-17 22:53:20 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-04-23 16:46:57 +02:00
										 |  |  |         return adopt_ref(*new XMLHttpRequest(window)); | 
					
						
							| 
									
										
										
										
											2021-02-17 22:53:20 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     static NonnullRefPtr<XMLHttpRequest> create_with_global_object(Bindings::WindowObject& window) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return XMLHttpRequest::create(window.impl()); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     virtual ~XMLHttpRequest() override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     using RefCounted::ref; | 
					
						
							|  |  |  |     using RefCounted::unref; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-22 19:47:26 +01:00
										 |  |  |     ReadyState ready_state() const { return m_ready_state; }; | 
					
						
							| 
									
										
										
										
											2022-07-12 18:17:50 +01:00
										 |  |  |     Fetch::Status status() const { return m_status; }; | 
					
						
							| 
									
										
										
										
											2022-02-15 14:35:29 +03:30
										 |  |  |     DOM::ExceptionOr<String> response_text() const; | 
					
						
							|  |  |  |     DOM::ExceptionOr<JS::Value> response(); | 
					
						
							|  |  |  |     Bindings::XMLHttpRequestResponseType response_type() const { return m_response_type; } | 
					
						
							| 
									
										
										
										
											2021-02-20 00:46:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     DOM::ExceptionOr<void> open(String const& method, String const& url); | 
					
						
							| 
									
										
										
										
											2022-04-03 18:43:02 +02:00
										 |  |  |     DOM::ExceptionOr<void> open(String const& method, String const& url, bool async, String const& username = {}, String const& password = {}); | 
					
						
							| 
									
										
										
										
											2022-07-07 23:15:41 +02:00
										 |  |  |     DOM::ExceptionOr<void> send(Optional<XMLHttpRequestBodyInit> body); | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     DOM::ExceptionOr<void> set_request_header(String const& header, String const& value); | 
					
						
							| 
									
										
										
										
											2022-02-15 14:35:29 +03:30
										 |  |  |     void set_response_type(Bindings::XMLHttpRequestResponseType type) { m_response_type = type; } | 
					
						
							| 
									
										
										
										
											2021-01-18 14:01:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     String get_response_header(String const& name) { return m_response_headers.get(name).value_or({}); } | 
					
						
							| 
									
										
										
										
											2021-09-19 22:32:33 +02:00
										 |  |  |     String get_all_response_headers() const; | 
					
						
							| 
									
										
										
										
											2021-04-03 15:51:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-14 18:03:08 +01:00
										 |  |  |     Bindings::CallbackType* onreadystatechange(); | 
					
						
							|  |  |  |     void set_onreadystatechange(Optional<Bindings::CallbackType>); | 
					
						
							| 
									
										
										
										
											2021-09-19 01:40:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-11 21:04:42 +00:00
										 |  |  |     DOM::ExceptionOr<void> override_mime_type(String const& mime); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-23 21:42:15 +02:00
										 |  |  |     DOM::ExceptionOr<void> set_timeout(u32 timeout); | 
					
						
							|  |  |  |     u32 timeout() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  | private: | 
					
						
							|  |  |  |     virtual void ref_event_target() override { ref(); } | 
					
						
							|  |  |  |     virtual void unref_event_target() override { unref(); } | 
					
						
							| 
									
										
										
										
											2021-01-18 12:15:02 +01:00
										 |  |  |     virtual JS::Object* create_wrapper(JS::GlobalObject&) override; | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-22 19:47:26 +01:00
										 |  |  |     void set_ready_state(ReadyState); | 
					
						
							| 
									
										
										
										
											2022-07-12 18:17:50 +01:00
										 |  |  |     void set_status(Fetch::Status status) { m_status = status; } | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     void fire_progress_event(String const&, u64, u64); | 
					
						
							| 
									
										
										
										
											2020-04-22 19:47:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-11 21:31:18 +00:00
										 |  |  |     MimeSniff::MimeType get_response_mime_type() const; | 
					
						
							| 
									
										
										
										
											2022-03-21 00:09:28 +01:00
										 |  |  |     Optional<StringView> get_final_encoding() const; | 
					
						
							| 
									
										
										
										
											2022-02-15 14:35:29 +03:30
										 |  |  |     MimeSniff::MimeType get_final_mime_type() const; | 
					
						
							| 
									
										
										
										
											2022-02-11 21:31:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     String get_text_response() const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Optional<Vector<String>> get_decode_and_split(String const& header_name, HashMap<String, String, CaseInsensitiveStringTraits> const& header_list) const; | 
					
						
							|  |  |  |     Optional<MimeSniff::MimeType> extract_mime_type(HashMap<String, String, CaseInsensitiveStringTraits> const& header_list) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-07 23:08:26 +01:00
										 |  |  |     explicit XMLHttpRequest(HTML::Window&); | 
					
						
							| 
									
										
										
										
											2020-04-08 21:18:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-07 23:08:26 +01:00
										 |  |  |     NonnullRefPtr<HTML::Window> m_window; | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-22 19:47:26 +01:00
										 |  |  |     ReadyState m_ready_state { ReadyState::Unsent }; | 
					
						
							| 
									
										
										
										
											2022-07-12 18:17:50 +01:00
										 |  |  |     Fetch::Status m_status { 0 }; | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |     bool m_send { false }; | 
					
						
							| 
									
										
										
										
											2022-04-23 21:42:15 +02:00
										 |  |  |     u32 m_timeout { 0 }; | 
					
						
							| 
									
										
										
										
											2020-04-22 19:47:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  |     String m_method; | 
					
						
							| 
									
										
										
										
											2021-09-13 00:33:23 +03:00
										 |  |  |     AK::URL m_url; | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-15 14:35:29 +03:30
										 |  |  |     Bindings::XMLHttpRequestResponseType m_response_type; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-18 14:01:05 +01:00
										 |  |  |     HashMap<String, String, CaseInsensitiveStringTraits> m_request_headers; | 
					
						
							| 
									
										
										
										
											2021-04-03 15:51:15 +02:00
										 |  |  |     HashMap<String, String, CaseInsensitiveStringTraits> m_response_headers; | 
					
						
							| 
									
										
										
										
											2021-01-18 14:01:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |     bool m_synchronous { false }; | 
					
						
							|  |  |  |     bool m_upload_complete { false }; | 
					
						
							|  |  |  |     bool m_upload_listener { false }; | 
					
						
							|  |  |  |     bool m_timed_out { false }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-15 14:35:29 +03:30
										 |  |  |     ByteBuffer m_received_bytes; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     enum class Failure { | 
					
						
							|  |  |  |         /// ????
 | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     Variant<JS::Handle<JS::Value>, Failure, Empty> m_response_object; | 
					
						
							| 
									
										
										
										
											2022-02-11 21:04:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // https://xhr.spec.whatwg.org/#override-mime-type
 | 
					
						
							|  |  |  |     Optional<MimeSniff::MimeType> m_override_mime_type; | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |