XMLHttpRequest.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org>
  4. * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/ByteBuffer.h>
  10. #include <AK/RefCounted.h>
  11. #include <AK/URL.h>
  12. #include <AK/Weakable.h>
  13. #include <LibWeb/DOM/EventTarget.h>
  14. #include <LibWeb/DOMURL/URLSearchParams.h>
  15. #include <LibWeb/Fetch/BodyInit.h>
  16. #include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
  17. #include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
  18. #include <LibWeb/Fetch/Infrastructure/HTTP/Statuses.h>
  19. #include <LibWeb/HTML/Window.h>
  20. #include <LibWeb/MimeSniff/MimeType.h>
  21. #include <LibWeb/WebIDL/ExceptionOr.h>
  22. #include <LibWeb/XHR/XMLHttpRequestEventTarget.h>
  23. namespace Web::XHR {
  24. // https://fetch.spec.whatwg.org/#typedefdef-xmlhttprequestbodyinit
  25. 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>;
  26. class XMLHttpRequest final : public XMLHttpRequestEventTarget {
  27. WEB_PLATFORM_OBJECT(XMLHttpRequest, XMLHttpRequestEventTarget);
  28. JS_DECLARE_ALLOCATOR(XMLHttpRequest);
  29. public:
  30. enum class State : u16 {
  31. Unsent = 0,
  32. Opened = 1,
  33. HeadersReceived = 2,
  34. Loading = 3,
  35. Done = 4,
  36. };
  37. static WebIDL::ExceptionOr<JS::NonnullGCPtr<XMLHttpRequest>> construct_impl(JS::Realm&);
  38. virtual ~XMLHttpRequest() override;
  39. State ready_state() const { return m_state; }
  40. Fetch::Infrastructure::Status status() const;
  41. WebIDL::ExceptionOr<String> status_text() const;
  42. WebIDL::ExceptionOr<String> response_text() const;
  43. WebIDL::ExceptionOr<JS::GCPtr<DOM::Document>> response_xml();
  44. WebIDL::ExceptionOr<JS::Value> response();
  45. Bindings::XMLHttpRequestResponseType response_type() const { return m_response_type; }
  46. WebIDL::ExceptionOr<void> open(String const& method, String const& url);
  47. WebIDL::ExceptionOr<void> open(String const& method, String const& url, bool async, Optional<String> const& username = Optional<String> {}, Optional<String> const& password = Optional<String> {});
  48. WebIDL::ExceptionOr<void> send(Optional<DocumentOrXMLHttpRequestBodyInit> body);
  49. WebIDL::ExceptionOr<void> set_request_header(String const& header, String const& value);
  50. WebIDL::ExceptionOr<void> set_response_type(Bindings::XMLHttpRequestResponseType);
  51. WebIDL::ExceptionOr<Optional<String>> get_response_header(String const& name) const;
  52. WebIDL::ExceptionOr<String> get_all_response_headers() const;
  53. WebIDL::CallbackType* onreadystatechange();
  54. void set_onreadystatechange(WebIDL::CallbackType*);
  55. WebIDL::ExceptionOr<void> override_mime_type(String const& mime);
  56. u32 timeout() const;
  57. WebIDL::ExceptionOr<void> set_timeout(u32 timeout);
  58. bool with_credentials() const;
  59. WebIDL::ExceptionOr<void> set_with_credentials(bool);
  60. void abort();
  61. JS::NonnullGCPtr<XMLHttpRequestUpload> upload() const;
  62. private:
  63. virtual void initialize(JS::Realm&) override;
  64. virtual void visit_edges(Cell::Visitor&) override;
  65. virtual bool must_survive_garbage_collection() const override;
  66. ErrorOr<MimeSniff::MimeType> get_response_mime_type() const;
  67. ErrorOr<Optional<StringView>> get_final_encoding() const;
  68. ErrorOr<MimeSniff::MimeType> get_final_mime_type() const;
  69. String get_text_response() const;
  70. void set_document_response();
  71. WebIDL::ExceptionOr<void> handle_response_end_of_body();
  72. WebIDL::ExceptionOr<void> handle_errors();
  73. JS::ThrowCompletionOr<void> request_error_steps(FlyString const& event_name, JS::GCPtr<WebIDL::DOMException> exception = nullptr);
  74. XMLHttpRequest(JS::Realm&, XMLHttpRequestUpload&, Fetch::Infrastructure::HeaderList&, Fetch::Infrastructure::Response&, Fetch::Infrastructure::FetchController&);
  75. // https://xhr.spec.whatwg.org/#upload-object
  76. // upload object
  77. // An XMLHttpRequestUpload object.
  78. JS::NonnullGCPtr<XMLHttpRequestUpload> m_upload_object;
  79. // https://xhr.spec.whatwg.org/#concept-xmlhttprequest-state
  80. // state
  81. // One of unsent, opened, headers received, loading, and done; initially unsent.
  82. State m_state { State::Unsent };
  83. // https://xhr.spec.whatwg.org/#send-flag
  84. // send() flag
  85. // A flag, initially unset.
  86. bool m_send { false };
  87. // https://xhr.spec.whatwg.org/#timeout
  88. // timeout
  89. // An unsigned integer, initially 0.
  90. u32 m_timeout { 0 };
  91. // https://xhr.spec.whatwg.org/#cross-origin-credentials
  92. // cross-origin credentials
  93. // A boolean, initially false.
  94. bool m_cross_origin_credentials { false };
  95. // https://xhr.spec.whatwg.org/#request-method
  96. // request method
  97. // A method.
  98. ByteString m_request_method;
  99. // https://xhr.spec.whatwg.org/#request-url
  100. // request URL
  101. // A URL.
  102. URL m_request_url;
  103. // https://xhr.spec.whatwg.org/#author-request-headers
  104. // author request headers
  105. // A header list, initially empty.
  106. JS::NonnullGCPtr<Fetch::Infrastructure::HeaderList> m_author_request_headers;
  107. // https://xhr.spec.whatwg.org/#request-body
  108. // request body
  109. // Initially null.
  110. JS::GCPtr<Fetch::Infrastructure::Body> m_request_body;
  111. // https://xhr.spec.whatwg.org/#synchronous-flag
  112. // synchronous flag
  113. // A flag, initially unset.
  114. bool m_synchronous { false };
  115. // https://xhr.spec.whatwg.org/#upload-complete-flag
  116. // upload complete flag
  117. // A flag, initially unset.
  118. bool m_upload_complete { false };
  119. // https://xhr.spec.whatwg.org/#upload-listener-flag
  120. // upload listener flag
  121. // A flag, initially unset.
  122. bool m_upload_listener { false };
  123. // https://xhr.spec.whatwg.org/#timed-out-flag
  124. // timed out flag
  125. // A flag, initially unset.
  126. bool m_timed_out { false };
  127. // https://xhr.spec.whatwg.org/#response
  128. // response
  129. // A response, initially a network error.
  130. JS::NonnullGCPtr<Fetch::Infrastructure::Response> m_response;
  131. // https://xhr.spec.whatwg.org/#received-bytes
  132. // received bytes
  133. // A byte sequence, initially the empty byte sequence.
  134. ByteBuffer m_received_bytes;
  135. // https://xhr.spec.whatwg.org/#response-type
  136. // response type
  137. // One of the empty string, "arraybuffer", "blob", "document", "json", and "text"; initially the empty string.
  138. Bindings::XMLHttpRequestResponseType m_response_type;
  139. enum class Failure {
  140. /// ????
  141. };
  142. // https://xhr.spec.whatwg.org/#response-object
  143. // response object
  144. // An object, failure, or null, initially null.
  145. // NOTE: This needs to be a JS::Value as the JSON response might not actually be an object.
  146. Variant<JS::NonnullGCPtr<JS::Object>, Failure, Empty> m_response_object;
  147. // https://xhr.spec.whatwg.org/#xmlhttprequest-fetch-controller
  148. // fetch controller
  149. // A fetch controller, initially a new fetch controller.
  150. // NOTE: The send() method sets it to a useful fetch controller, but for simplicity it always holds a fetch controller.
  151. JS::NonnullGCPtr<Fetch::Infrastructure::FetchController> m_fetch_controller;
  152. // https://xhr.spec.whatwg.org/#override-mime-type
  153. // override MIME type
  154. // A MIME type or null, initially null.
  155. // NOTE: Can get a value when overrideMimeType() is invoked.
  156. Optional<MimeSniff::MimeType> m_override_mime_type;
  157. // Non-standard, see async path in `send()`
  158. u64 m_request_body_transmitted { 0 };
  159. };
  160. }