| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  |  | /*
 | 
					
						
							|  |  |  |  |  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2021-04-22 22:51:19 +02:00
										 |  |  |  |  * Copyright (c) 2021, Linus Groh <linusg@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
										 |  |  |  |  */ | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-19 22:32:33 +02:00
										 |  |  |  | #include <AK/QuickSort.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-27 21:48:34 +02:00
										 |  |  |  | #include <LibJS/Runtime/FunctionObject.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  |  | #include <LibWeb/Bindings/EventWrapper.h>
 | 
					
						
							|  |  |  |  | #include <LibWeb/Bindings/XMLHttpRequestWrapper.h>
 | 
					
						
							| 
									
										
										
										
											2021-02-20 00:45:24 +01:00
										 |  |  |  | #include <LibWeb/DOM/DOMException.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-08 21:18:41 +02:00
										 |  |  |  | #include <LibWeb/DOM/Document.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  |  | #include <LibWeb/DOM/Event.h>
 | 
					
						
							| 
									
										
										
										
											2020-09-06 14:28:41 +02:00
										 |  |  |  | #include <LibWeb/DOM/EventDispatcher.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  |  | #include <LibWeb/DOM/EventListener.h>
 | 
					
						
							| 
									
										
										
										
											2021-02-20 00:45:24 +01:00
										 |  |  |  | #include <LibWeb/DOM/ExceptionOr.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-08 21:18:41 +02:00
										 |  |  |  | #include <LibWeb/DOM/Window.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-19 01:40:13 +02:00
										 |  |  |  | #include <LibWeb/HTML/EventHandler.h>
 | 
					
						
							| 
									
										
										
										
											2020-11-21 19:15:57 +00:00
										 |  |  |  | #include <LibWeb/HTML/EventNames.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-01 20:42:50 +02:00
										 |  |  |  | #include <LibWeb/Loader/ResourceLoader.h>
 | 
					
						
							| 
									
										
										
										
											2020-10-31 20:36:50 +00:00
										 |  |  |  | #include <LibWeb/Origin.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-12 02:10:43 +02:00
										 |  |  |  | #include <LibWeb/Page/Page.h>
 | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |  | #include <LibWeb/XHR/EventNames.h>
 | 
					
						
							|  |  |  |  | #include <LibWeb/XHR/ProgressEvent.h>
 | 
					
						
							| 
									
										
										
										
											2021-01-23 11:51:36 +01:00
										 |  |  |  | #include <LibWeb/XHR/XMLHttpRequest.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
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 19:37:56 +02:00
										 |  |  |  | XMLHttpRequest::XMLHttpRequest(DOM::Window& window) | 
					
						
							| 
									
										
										
										
											2021-09-09 13:55:31 +02:00
										 |  |  |  |     : XMLHttpRequestEventTarget(static_cast<Bindings::ScriptExecutionContext&>(window.associated_document())) | 
					
						
							| 
									
										
										
										
											2020-09-20 19:22:44 +02:00
										 |  |  |  |     , m_window(window) | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | XMLHttpRequest::~XMLHttpRequest() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-22 19:47:26 +01:00
										 |  |  |  | void XMLHttpRequest::set_ready_state(ReadyState ready_state) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     m_ready_state = ready_state; | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |  |     dispatch_event(DOM::Event::create(EventNames::readystatechange)); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void XMLHttpRequest::fire_progress_event(const String& event_name, u64 transmitted, u64 length) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     dispatch_event(ProgressEvent::create(event_name, transmitted, length)); | 
					
						
							| 
									
										
										
										
											2020-04-22 19:47:26 +01:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  |  | String XMLHttpRequest::response_text() const | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-16 08:47:46 +02:00
										 |  |  |  |     if (m_response_object.is_empty()) | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |  |     return String::copy(m_response_object); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // https://fetch.spec.whatwg.org/#forbidden-header-name
 | 
					
						
							|  |  |  |  | static bool is_forbidden_header_name(const String& header_name) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     if (header_name.starts_with("Proxy-", CaseSensitivity::CaseInsensitive) || header_name.starts_with("Sec-", CaseSensitivity::CaseInsensitive)) | 
					
						
							|  |  |  |  |         return true; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     auto lowercase_header_name = header_name.to_lowercase(); | 
					
						
							|  |  |  |  |     return lowercase_header_name.is_one_of("accept-charset", "accept-encoding", "access-control-request-headers", "access-control-request-method", "connection", "content-length", "cookie", "cookie2", "date", "dnt", "expect", "host", "keep-alive", "origin", "referer", "te", "trailer", "transfer-encoding", "upgrade", "via"); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // https://fetch.spec.whatwg.org/#forbidden-method
 | 
					
						
							|  |  |  |  | static bool is_forbidden_method(const String& method) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     auto lowercase_method = method.to_lowercase(); | 
					
						
							|  |  |  |  |     return lowercase_method.is_one_of("connect", "trace", "track"); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // https://fetch.spec.whatwg.org/#concept-method-normalize
 | 
					
						
							|  |  |  |  | static String normalize_method(const String& method) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     auto lowercase_method = method.to_lowercase(); | 
					
						
							|  |  |  |  |     if (lowercase_method.is_one_of("delete", "get", "head", "options", "post", "put")) | 
					
						
							|  |  |  |  |         return method.to_uppercase(); | 
					
						
							|  |  |  |  |     return method; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // https://fetch.spec.whatwg.org/#concept-header-value-normalize
 | 
					
						
							|  |  |  |  | static String normalize_header_value(const String& header_value) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     // FIXME: I'm not sure if this is the right trim, it should only be HTML whitespace bytes.
 | 
					
						
							|  |  |  |  |     return header_value.trim_whitespace(); | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |  | // https://xhr.spec.whatwg.org/#dom-xmlhttprequest-setrequestheader
 | 
					
						
							| 
									
										
										
										
											2021-02-20 00:45:24 +01:00
										 |  |  |  | DOM::ExceptionOr<void> XMLHttpRequest::set_request_header(const String& header, const String& value) | 
					
						
							| 
									
										
										
										
											2021-01-18 14:01:05 +01:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-02-20 00:45:24 +01:00
										 |  |  |  |     if (m_ready_state != ReadyState::Opened) | 
					
						
							|  |  |  |  |         return DOM::InvalidStateError::create("XHR readyState is not OPENED"); | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-20 00:45:24 +01:00
										 |  |  |  |     if (m_send) | 
					
						
							|  |  |  |  |         return DOM::InvalidStateError::create("XHR send() flag is already set"); | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // FIXME: Check if name matches the name production.
 | 
					
						
							|  |  |  |  |     // FIXME: Check if value matches the value production.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (is_forbidden_header_name(header)) | 
					
						
							| 
									
										
										
										
											2021-02-20 00:45:24 +01:00
										 |  |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // FIXME: Combine
 | 
					
						
							|  |  |  |  |     m_request_headers.set(header, normalize_header_value(value)); | 
					
						
							| 
									
										
										
										
											2021-02-20 00:45:24 +01:00
										 |  |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2021-01-18 14:01:05 +01:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-03 15:15:31 +02:00
										 |  |  |  | // https://xhr.spec.whatwg.org/#dom-xmlhttprequest-open
 | 
					
						
							| 
									
										
										
										
											2021-02-20 00:46:19 +01:00
										 |  |  |  | DOM::ExceptionOr<void> XMLHttpRequest::open(const String& method, const String& url) | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |  |     // FIXME: Let settingsObject be this’s relevant settings object.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // FIXME: If settingsObject has a responsible document and it is not fully active, then throw an "InvalidStateError" DOMException.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // FIXME: Check that the method matches the method token production. https://tools.ietf.org/html/rfc7230#section-3.1.1
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-20 00:46:19 +01:00
										 |  |  |  |     if (is_forbidden_method(method)) | 
					
						
							|  |  |  |  |         return DOM::SecurityError::create("Forbidden method, must not be 'CONNECT', 'TRACE', or 'TRACK'"); | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-03 15:15:31 +02:00
										 |  |  |  |     auto normalized_method = normalize_method(method); | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 18:08:56 +02:00
										 |  |  |  |     auto parsed_url = m_window->associated_document().parse_url(url); | 
					
						
							| 
									
										
										
										
											2021-02-20 00:46:19 +01:00
										 |  |  |  |     if (!parsed_url.is_valid()) | 
					
						
							|  |  |  |  |         return DOM::SyntaxError::create("Invalid URL"); | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (!parsed_url.host().is_null()) { | 
					
						
							|  |  |  |  |         // FIXME: If the username argument is not null, set the username given parsedURL and username.
 | 
					
						
							|  |  |  |  |         // FIXME: If the password argument is not null, set the password given parsedURL and password.
 | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // FIXME: If async is false, the current global object is a Window object, and either this’s timeout is
 | 
					
						
							|  |  |  |  |     //        not 0 or this’s response type is not the empty string, then throw an "InvalidAccessError" DOMException.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // FIXME: If the async argument is omitted, set async to true, and set username and password to null.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // FIXME: Terminate the ongoing fetch operated by the XMLHttpRequest object.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     m_send = false; | 
					
						
							|  |  |  |  |     m_upload_listener = false; | 
					
						
							|  |  |  |  |     m_method = normalized_method; | 
					
						
							|  |  |  |  |     m_url = parsed_url; | 
					
						
							|  |  |  |  |     // FIXME: Set this’s synchronous flag if async is false; otherwise unset this’s synchronous flag.
 | 
					
						
							|  |  |  |  |     //        (We're currently defaulting to async)
 | 
					
						
							|  |  |  |  |     m_synchronous = false; | 
					
						
							| 
									
										
										
										
											2021-01-18 14:01:05 +01:00
										 |  |  |  |     m_request_headers.clear(); | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // FIXME: Set this’s response to a network error.
 | 
					
						
							|  |  |  |  |     // FIXME: Set this’s received bytes to the empty byte sequence.
 | 
					
						
							|  |  |  |  |     m_response_object = {}; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (m_ready_state != ReadyState::Opened) | 
					
						
							|  |  |  |  |         set_ready_state(ReadyState::Opened); | 
					
						
							| 
									
										
										
										
											2021-02-20 00:46:19 +01:00
										 |  |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |  | // https://xhr.spec.whatwg.org/#dom-xmlhttprequest-send
 | 
					
						
							| 
									
										
										
										
											2021-09-27 01:40:21 +02:00
										 |  |  |  | DOM::ExceptionOr<void> XMLHttpRequest::send(String const& body) | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-02-20 00:46:52 +01:00
										 |  |  |  |     if (m_ready_state != ReadyState::Opened) | 
					
						
							|  |  |  |  |         return DOM::InvalidStateError::create("XHR readyState is not OPENED"); | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-20 00:46:52 +01:00
										 |  |  |  |     if (m_send) | 
					
						
							|  |  |  |  |         return DOM::InvalidStateError::create("XHR send() flag is already set"); | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // FIXME: If this’s request method is `GET` or `HEAD`, then set body to null.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // FIXME: If body is not null, then:
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-13 00:33:23 +03:00
										 |  |  |  |     AK::URL request_url = m_window->associated_document().parse_url(m_url.to_string()); | 
					
						
							| 
									
										
										
										
											2021-09-09 13:55:31 +02:00
										 |  |  |  |     dbgln("XHR send from {} to {}", m_window->associated_document().url(), request_url); | 
					
						
							| 
									
										
										
										
											2020-10-31 20:36:50 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // TODO: Add support for preflight requests to support CORS requests
 | 
					
						
							| 
									
										
										
										
											2021-09-13 23:12:16 +03:00
										 |  |  |  |     Origin request_url_origin = Origin(request_url.protocol(), request_url.host(), request_url.port_or_default()); | 
					
						
							| 
									
										
										
										
											2020-10-31 20:36:50 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-12 02:10:43 +02:00
										 |  |  |  |     bool should_enforce_same_origin_policy = true; | 
					
						
							|  |  |  |  |     if (auto* page = m_window->page()) | 
					
						
							|  |  |  |  |         should_enforce_same_origin_policy = page->is_same_origin_policy_enabled(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (should_enforce_same_origin_policy && !m_window->associated_document().origin().is_same(request_url_origin)) { | 
					
						
							| 
									
										
										
										
											2021-09-09 13:55:31 +02:00
										 |  |  |  |         dbgln("XHR failed to load: Same-Origin Policy violation: {} may not load {}", m_window->associated_document().url(), request_url); | 
					
						
							| 
									
										
										
										
											2021-09-19 14:59:02 +02:00
										 |  |  |  |         set_ready_state(ReadyState::Done); | 
					
						
							|  |  |  |  |         dispatch_event(DOM::Event::create(HTML::EventNames::error)); | 
					
						
							| 
									
										
										
										
											2021-02-20 00:46:52 +01:00
										 |  |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2020-10-31 20:36:50 +00:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 13:45:03 +02:00
										 |  |  |  |     auto request = LoadRequest::create_for_url_on_page(request_url, m_window->page()); | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |  |     request.set_method(m_method); | 
					
						
							| 
									
										
										
										
											2021-09-27 01:40:21 +02:00
										 |  |  |  |     if (!body.is_null()) | 
					
						
							|  |  |  |  |         request.set_body(body.to_byte_buffer()); | 
					
						
							| 
									
										
										
										
											2021-01-18 14:01:05 +01:00
										 |  |  |  |     for (auto& it : m_request_headers) | 
					
						
							|  |  |  |  |         request.set_header(it.key, it.value); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |  |     m_upload_complete = false; | 
					
						
							|  |  |  |  |     m_timed_out = false; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // FIXME: If req’s body is null (which it always is currently)
 | 
					
						
							|  |  |  |  |     m_upload_complete = true; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     m_send = true; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (!m_synchronous) { | 
					
						
							|  |  |  |  |         fire_progress_event(EventNames::loadstart, 0, 0); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         // FIXME: If this’s upload complete flag is unset and this’s upload listener flag is set,
 | 
					
						
							|  |  |  |  |         //        then fire a progress event named loadstart at this’s upload object with 0 and req’s body’s total bytes.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         if (m_ready_state != ReadyState::Opened || !m_send) | 
					
						
							| 
									
										
										
										
											2021-02-20 00:46:52 +01:00
										 |  |  |  |             return {}; | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         // FIXME: in order to properly set ReadyState::HeadersReceived and ReadyState::Loading,
 | 
					
						
							|  |  |  |  |         // we need to make ResourceLoader give us more detailed updates than just "done" and "error".
 | 
					
						
							|  |  |  |  |         ResourceLoader::the().load( | 
					
						
							|  |  |  |  |             request, | 
					
						
							| 
									
										
										
										
											2021-04-03 15:51:15 +02:00
										 |  |  |  |             [weak_this = make_weak_ptr()](auto data, auto& response_headers, auto status_code) { | 
					
						
							| 
									
										
										
										
											2021-09-19 15:02:04 +02:00
										 |  |  |  |                 auto strong_this = weak_this.strong_ref(); | 
					
						
							|  |  |  |  |                 if (!strong_this) | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |  |                     return; | 
					
						
							|  |  |  |  |                 auto& xhr = const_cast<XMLHttpRequest&>(*weak_this); | 
					
						
							| 
									
										
										
										
											2021-09-06 03:29:52 +04:30
										 |  |  |  |                 // FIXME: Handle OOM failure.
 | 
					
						
							|  |  |  |  |                 auto response_data = ByteBuffer::copy(data).release_value(); | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |  |                 // FIXME: There's currently no difference between transmitted and length.
 | 
					
						
							|  |  |  |  |                 u64 transmitted = response_data.size(); | 
					
						
							|  |  |  |  |                 u64 length = response_data.size(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |                 if (!xhr.m_synchronous) { | 
					
						
							|  |  |  |  |                     xhr.m_response_object = response_data; | 
					
						
							|  |  |  |  |                     xhr.fire_progress_event(EventNames::progress, transmitted, length); | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |                 xhr.m_ready_state = ReadyState::Done; | 
					
						
							| 
									
										
										
										
											2021-04-03 15:17:29 +02:00
										 |  |  |  |                 xhr.m_status = status_code.value_or(0); | 
					
						
							| 
									
										
										
										
											2021-04-03 15:51:15 +02:00
										 |  |  |  |                 xhr.m_response_headers = move(response_headers); | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |  |                 xhr.m_send = false; | 
					
						
							|  |  |  |  |                 xhr.dispatch_event(DOM::Event::create(EventNames::readystatechange)); | 
					
						
							|  |  |  |  |                 xhr.fire_progress_event(EventNames::load, transmitted, length); | 
					
						
							|  |  |  |  |                 xhr.fire_progress_event(EventNames::loadend, transmitted, length); | 
					
						
							|  |  |  |  |             }, | 
					
						
							| 
									
										
										
										
											2021-04-03 15:17:29 +02:00
										 |  |  |  |             [weak_this = make_weak_ptr()](auto& error, auto status_code) { | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |  |                 dbgln("XHR failed to load: {}", error); | 
					
						
							| 
									
										
										
										
											2021-09-19 15:02:04 +02:00
										 |  |  |  |                 auto strong_this = weak_this.strong_ref(); | 
					
						
							|  |  |  |  |                 if (!strong_this) | 
					
						
							|  |  |  |  |                     return; | 
					
						
							|  |  |  |  |                 auto& xhr = const_cast<XMLHttpRequest&>(*strong_this); | 
					
						
							|  |  |  |  |                 xhr.set_ready_state(ReadyState::Done); | 
					
						
							|  |  |  |  |                 xhr.set_status(status_code.value_or(0)); | 
					
						
							|  |  |  |  |                 xhr.dispatch_event(DOM::Event::create(HTML::EventNames::error)); | 
					
						
							| 
									
										
										
										
											2021-01-23 17:50:22 +00:00
										 |  |  |  |             }); | 
					
						
							|  |  |  |  |     } else { | 
					
						
							|  |  |  |  |         TODO(); | 
					
						
							|  |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-02-20 00:46:52 +01:00
										 |  |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-18 12:15:02 +01:00
										 |  |  |  | JS::Object* XMLHttpRequest::create_wrapper(JS::GlobalObject& global_object) | 
					
						
							| 
									
										
										
										
											2020-09-06 14:28:41 +02:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     return wrap(global_object, *this); | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-19 01:40:13 +02:00
										 |  |  |  | HTML::EventHandler XMLHttpRequest::onreadystatechange() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     return event_handler_attribute(Web::XHR::EventNames::readystatechange); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void XMLHttpRequest::set_onreadystatechange(HTML::EventHandler value) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     set_event_handler_attribute(Web::XHR::EventNames::readystatechange, move(value)); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-19 22:32:33 +02:00
										 |  |  |  | // https://xhr.spec.whatwg.org/#the-getallresponseheaders()-method
 | 
					
						
							|  |  |  |  | String XMLHttpRequest::get_all_response_headers() const | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     // FIXME: Implement the spec-compliant sort order.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     StringBuilder builder; | 
					
						
							|  |  |  |  |     auto keys = m_response_headers.keys(); | 
					
						
							|  |  |  |  |     quick_sort(keys); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     for (auto& key : keys) { | 
					
						
							|  |  |  |  |         builder.append(key); | 
					
						
							|  |  |  |  |         builder.append(": "); | 
					
						
							|  |  |  |  |         builder.append(m_response_headers.get(key).value()); | 
					
						
							|  |  |  |  |         builder.append("\r\n"); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  |     return builder.to_string(); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 21:11:51 +02:00
										 |  |  |  | } |