XMLHttpRequest.h 7.4 KB

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