XMLHttpRequest.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. class XMLHttpRequest final : public XMLHttpRequestEventTarget {
  23. WEB_PLATFORM_OBJECT(XMLHttpRequest, XMLHttpRequestEventTarget);
  24. public:
  25. enum class ReadyState : u16 {
  26. Unsent = 0,
  27. Opened = 1,
  28. HeadersReceived = 2,
  29. Loading = 3,
  30. Done = 4,
  31. };
  32. static JS::NonnullGCPtr<XMLHttpRequest> construct_impl(JS::Realm&);
  33. virtual ~XMLHttpRequest() override;
  34. ReadyState ready_state() const { return m_ready_state; };
  35. Fetch::Infrastructure::Status status() const { return m_status; };
  36. WebIDL::ExceptionOr<String> response_text() const;
  37. WebIDL::ExceptionOr<JS::Value> response();
  38. Bindings::XMLHttpRequestResponseType response_type() const { return m_response_type; }
  39. WebIDL::ExceptionOr<void> open(String const& method, String const& url);
  40. WebIDL::ExceptionOr<void> open(String const& method, String const& url, bool async, String const& username = {}, String const& password = {});
  41. WebIDL::ExceptionOr<void> send(Optional<Fetch::XMLHttpRequestBodyInit> body);
  42. WebIDL::ExceptionOr<void> set_request_header(String const& header, String const& value);
  43. void set_response_type(Bindings::XMLHttpRequestResponseType type) { m_response_type = type; }
  44. String get_response_header(String const& name) { return m_response_headers.get(name).value_or({}); }
  45. String get_all_response_headers() const;
  46. WebIDL::CallbackType* onreadystatechange();
  47. void set_onreadystatechange(WebIDL::CallbackType*);
  48. WebIDL::ExceptionOr<void> override_mime_type(String const& mime);
  49. WebIDL::ExceptionOr<void> set_timeout(u32 timeout);
  50. u32 timeout() const;
  51. private:
  52. virtual void visit_edges(Cell::Visitor&) override;
  53. void set_ready_state(ReadyState);
  54. void set_status(Fetch::Infrastructure::Status status) { m_status = status; }
  55. void fire_progress_event(String const&, u64, u64);
  56. MimeSniff::MimeType get_response_mime_type() const;
  57. Optional<StringView> get_final_encoding() const;
  58. MimeSniff::MimeType get_final_mime_type() const;
  59. String get_text_response() const;
  60. explicit XMLHttpRequest(HTML::Window&);
  61. JS::NonnullGCPtr<HTML::Window> m_window;
  62. ReadyState m_ready_state { ReadyState::Unsent };
  63. Fetch::Infrastructure::Status m_status { 0 };
  64. bool m_send { false };
  65. u32 m_timeout { 0 };
  66. String m_method;
  67. AK::URL m_url;
  68. Bindings::XMLHttpRequestResponseType m_response_type;
  69. HashMap<String, String, CaseInsensitiveStringTraits> m_request_headers;
  70. HashMap<String, String, CaseInsensitiveStringTraits> m_response_headers;
  71. bool m_synchronous { false };
  72. bool m_upload_complete { false };
  73. bool m_upload_listener { false };
  74. bool m_timed_out { false };
  75. ByteBuffer m_received_bytes;
  76. enum class Failure {
  77. /// ????
  78. };
  79. Variant<JS::Value, Failure, Empty> m_response_object;
  80. // https://xhr.spec.whatwg.org/#override-mime-type
  81. Optional<MimeSniff::MimeType> m_override_mime_type;
  82. };
  83. }