XMLHttpRequest.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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::String>;
  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 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<String> response_text() const;
  39. WebIDL::ExceptionOr<JS::Value> response();
  40. Bindings::XMLHttpRequestResponseType response_type() const { return m_response_type; }
  41. WebIDL::ExceptionOr<void> open(String const& method, String const& url);
  42. WebIDL::ExceptionOr<void> open(String const& method, String const& url, bool async, String const& username = {}, String const& password = {});
  43. WebIDL::ExceptionOr<void> send(Optional<DocumentOrXMLHttpRequestBodyInit> body);
  44. WebIDL::ExceptionOr<void> set_request_header(String const& header, String const& value);
  45. WebIDL::ExceptionOr<void> set_response_type(Bindings::XMLHttpRequestResponseType);
  46. String get_response_header(String const& name) { return m_response_headers.get(name).value_or({}); }
  47. String get_all_response_headers() const;
  48. WebIDL::CallbackType* onreadystatechange();
  49. void set_onreadystatechange(WebIDL::CallbackType*);
  50. WebIDL::ExceptionOr<void> override_mime_type(String const& mime);
  51. WebIDL::ExceptionOr<void> set_timeout(u32 timeout);
  52. u32 timeout() const;
  53. void abort();
  54. private:
  55. virtual void visit_edges(Cell::Visitor&) override;
  56. virtual bool must_survive_garbage_collection() const override;
  57. void set_status(Fetch::Infrastructure::Status status) { m_status = status; }
  58. void fire_progress_event(String const&, u64, u64);
  59. MimeSniff::MimeType get_response_mime_type() const;
  60. Optional<StringView> get_final_encoding() const;
  61. MimeSniff::MimeType get_final_mime_type() const;
  62. String get_text_response() const;
  63. explicit XMLHttpRequest(HTML::Window&);
  64. // Non-standard
  65. JS::NonnullGCPtr<HTML::Window> m_window;
  66. Fetch::Infrastructure::Status m_status { 0 };
  67. HashMap<String, String, CaseInsensitiveStringTraits> m_response_headers;
  68. // https://xhr.spec.whatwg.org/#concept-xmlhttprequest-state
  69. // state
  70. // One of unsent, opened, headers received, loading, and done; initially unsent.
  71. State m_state { State::Unsent };
  72. // https://xhr.spec.whatwg.org/#send-flag
  73. // send() flag
  74. // A flag, initially unset.
  75. bool m_send { false };
  76. // https://xhr.spec.whatwg.org/#timeout
  77. // timeout
  78. // An unsigned integer, initially 0.
  79. u32 m_timeout { 0 };
  80. // FIXME: https://xhr.spec.whatwg.org/#cross-origin-credentials
  81. // https://xhr.spec.whatwg.org/#request-method
  82. // request method
  83. // A method.
  84. String m_request_method;
  85. // https://xhr.spec.whatwg.org/#request-url
  86. // request URL
  87. // A URL.
  88. AK::URL m_request_url;
  89. // https://xhr.spec.whatwg.org/#author-request-headers
  90. // author request headers
  91. // A header list, initially empty.
  92. HashMap<String, String, CaseInsensitiveStringTraits> m_author_request_headers;
  93. // FIXME: https://xhr.spec.whatwg.org/#request-body
  94. // https://xhr.spec.whatwg.org/#synchronous-flag
  95. // synchronous flag
  96. // A flag, initially unset.
  97. bool m_synchronous { false };
  98. // https://xhr.spec.whatwg.org/#upload-complete-flag
  99. // upload complete flag
  100. // A flag, initially unset.
  101. bool m_upload_complete { false };
  102. // https://xhr.spec.whatwg.org/#upload-listener-flag
  103. // upload listener flag
  104. // A flag, initially unset.
  105. bool m_upload_listener { false };
  106. // https://xhr.spec.whatwg.org/#timed-out-flag
  107. // timed out flag
  108. // A flag, initially unset.
  109. bool m_timed_out { false };
  110. // FIXME: https://xhr.spec.whatwg.org/#response
  111. // https://xhr.spec.whatwg.org/#received-bytes
  112. // received bytes
  113. // A byte sequence, initially the empty byte sequence.
  114. ByteBuffer m_received_bytes;
  115. // https://xhr.spec.whatwg.org/#response-type
  116. // response type
  117. // One of the empty string, "arraybuffer", "blob", "document", "json", and "text"; initially the empty string.
  118. Bindings::XMLHttpRequestResponseType m_response_type;
  119. enum class Failure {
  120. /// ????
  121. };
  122. // https://xhr.spec.whatwg.org/#response-object
  123. // response object
  124. // An object, failure, or null, initially null.
  125. Variant<JS::Value, Failure, Empty> m_response_object;
  126. // FIXME: https://xhr.spec.whatwg.org/#xmlhttprequest-fetch-controller
  127. // https://xhr.spec.whatwg.org/#override-mime-type
  128. // override MIME type
  129. // A MIME type or null, initially null.
  130. // NOTE: Can get a value when overrideMimeType() is invoked.
  131. Optional<MimeSniff::MimeType> m_override_mime_type;
  132. };
  133. }