XMLHttpRequest.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/Bindings/WindowObject.h>
  13. #include <LibWeb/Bindings/Wrappable.h>
  14. #include <LibWeb/DOM/EventTarget.h>
  15. #include <LibWeb/DOM/ExceptionOr.h>
  16. #include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
  17. #include <LibWeb/Fetch/Infrastructure/HTTP/Statuses.h>
  18. #include <LibWeb/MimeSniff/MimeType.h>
  19. #include <LibWeb/URL/URLSearchParams.h>
  20. #include <LibWeb/XHR/XMLHttpRequestEventTarget.h>
  21. namespace Web::XHR {
  22. // https://fetch.spec.whatwg.org/#typedefdef-xmlhttprequestbodyinit
  23. using XMLHttpRequestBodyInit = Variant<NonnullRefPtr<FileAPI::Blob>, JS::Handle<JS::Object>, NonnullRefPtr<URL::URLSearchParams>, String>;
  24. class XMLHttpRequest final
  25. : public RefCounted<XMLHttpRequest>
  26. , public Weakable<XMLHttpRequest>
  27. , public XMLHttpRequestEventTarget {
  28. public:
  29. enum class ReadyState : u16 {
  30. Unsent = 0,
  31. Opened = 1,
  32. HeadersReceived = 2,
  33. Loading = 3,
  34. Done = 4,
  35. };
  36. using WrapperType = Bindings::XMLHttpRequestWrapper;
  37. static NonnullRefPtr<XMLHttpRequest> create(HTML::Window& window)
  38. {
  39. return adopt_ref(*new XMLHttpRequest(window));
  40. }
  41. static NonnullRefPtr<XMLHttpRequest> create_with_global_object(Bindings::WindowObject& window)
  42. {
  43. return XMLHttpRequest::create(window.impl());
  44. }
  45. virtual ~XMLHttpRequest() override;
  46. using RefCounted::ref;
  47. using RefCounted::unref;
  48. ReadyState ready_state() const { return m_ready_state; };
  49. Fetch::Infrastructure::Status status() const { return m_status; };
  50. DOM::ExceptionOr<String> response_text() const;
  51. DOM::ExceptionOr<JS::Value> response();
  52. Bindings::XMLHttpRequestResponseType response_type() const { return m_response_type; }
  53. DOM::ExceptionOr<void> open(String const& method, String const& url);
  54. DOM::ExceptionOr<void> open(String const& method, String const& url, bool async, String const& username = {}, String const& password = {});
  55. DOM::ExceptionOr<void> send(Optional<XMLHttpRequestBodyInit> body);
  56. DOM::ExceptionOr<void> set_request_header(String const& header, String const& value);
  57. void set_response_type(Bindings::XMLHttpRequestResponseType type) { m_response_type = type; }
  58. String get_response_header(String const& name) { return m_response_headers.get(name).value_or({}); }
  59. String get_all_response_headers() const;
  60. Bindings::CallbackType* onreadystatechange();
  61. void set_onreadystatechange(Optional<Bindings::CallbackType>);
  62. DOM::ExceptionOr<void> override_mime_type(String const& mime);
  63. DOM::ExceptionOr<void> set_timeout(u32 timeout);
  64. u32 timeout() const;
  65. private:
  66. virtual void ref_event_target() override { ref(); }
  67. virtual void unref_event_target() override { unref(); }
  68. virtual JS::Object* create_wrapper(JS::Realm&) override;
  69. void set_ready_state(ReadyState);
  70. void set_status(Fetch::Infrastructure::Status status) { m_status = status; }
  71. void fire_progress_event(String const&, u64, u64);
  72. MimeSniff::MimeType get_response_mime_type() const;
  73. Optional<StringView> get_final_encoding() const;
  74. MimeSniff::MimeType get_final_mime_type() const;
  75. String get_text_response() const;
  76. explicit XMLHttpRequest(HTML::Window&);
  77. NonnullRefPtr<HTML::Window> m_window;
  78. ReadyState m_ready_state { ReadyState::Unsent };
  79. Fetch::Infrastructure::Status m_status { 0 };
  80. bool m_send { false };
  81. u32 m_timeout { 0 };
  82. String m_method;
  83. AK::URL m_url;
  84. Bindings::XMLHttpRequestResponseType m_response_type;
  85. HashMap<String, String, CaseInsensitiveStringTraits> m_request_headers;
  86. HashMap<String, String, CaseInsensitiveStringTraits> m_response_headers;
  87. bool m_synchronous { false };
  88. bool m_upload_complete { false };
  89. bool m_upload_listener { false };
  90. bool m_timed_out { false };
  91. ByteBuffer m_received_bytes;
  92. enum class Failure {
  93. /// ????
  94. };
  95. Variant<JS::Handle<JS::Value>, Failure, Empty> m_response_object;
  96. // https://xhr.spec.whatwg.org/#override-mime-type
  97. Optional<MimeSniff::MimeType> m_override_mime_type;
  98. };
  99. }