XMLHttpRequest.h 6.5 KB

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