XMLHttpRequest.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibJS/Runtime/Function.h>
  27. #include <LibWeb/Bindings/EventWrapper.h>
  28. #include <LibWeb/Bindings/EventWrapperFactory.h>
  29. #include <LibWeb/Bindings/XMLHttpRequestWrapper.h>
  30. #include <LibWeb/DOM/Document.h>
  31. #include <LibWeb/DOM/Event.h>
  32. #include <LibWeb/DOM/EventDispatcher.h>
  33. #include <LibWeb/DOM/EventListener.h>
  34. #include <LibWeb/DOM/Window.h>
  35. #include <LibWeb/DOM/XMLHttpRequest.h>
  36. #include <LibWeb/HTML/EventNames.h>
  37. #include <LibWeb/Loader/ResourceLoader.h>
  38. #include <LibWeb/Origin.h>
  39. namespace Web {
  40. XMLHttpRequest::XMLHttpRequest(DOM::Window& window)
  41. : EventTarget(static_cast<Bindings::ScriptExecutionContext&>(window.document()))
  42. , m_window(window)
  43. {
  44. }
  45. XMLHttpRequest::~XMLHttpRequest()
  46. {
  47. }
  48. void XMLHttpRequest::set_ready_state(ReadyState ready_state)
  49. {
  50. // FIXME: call onreadystatechange once we have that
  51. m_ready_state = ready_state;
  52. }
  53. String XMLHttpRequest::response_text() const
  54. {
  55. if (m_response.is_null())
  56. return {};
  57. return String::copy(m_response);
  58. }
  59. void XMLHttpRequest::open(const String& method, const String& url)
  60. {
  61. m_method = method;
  62. m_url = url;
  63. set_ready_state(ReadyState::Opened);
  64. }
  65. void XMLHttpRequest::send()
  66. {
  67. URL request_url = m_window->document().complete_url(m_url);
  68. dbgln("XHR send from {} to {}", m_window->document().url(), request_url);
  69. // TODO: Add support for preflight requests to support CORS requests
  70. Origin request_url_origin = Origin(request_url.protocol(), request_url.host(), request_url.port());
  71. if (!m_window->document().origin().is_same(request_url_origin)) {
  72. dbgln("XHR failed to load: Same-Origin Policy violation: {} may not load {}", m_window->document().url(), request_url);
  73. auto weak_this = make_weak_ptr();
  74. if (!weak_this)
  75. return;
  76. const_cast<XMLHttpRequest&>(*weak_this).set_ready_state(ReadyState::Done);
  77. const_cast<XMLHttpRequest&>(*weak_this).dispatch_event(DOM::Event::create(HTML::EventNames::error));
  78. return;
  79. }
  80. // FIXME: in order to properly set ReadyState::HeadersReceived and ReadyState::Loading,
  81. // we need to make ResourceLoader give us more detailed updates than just "done" and "error".
  82. ResourceLoader::the().load(
  83. m_window->document().complete_url(m_url),
  84. [weak_this = make_weak_ptr()](auto data, auto&) {
  85. if (!weak_this)
  86. return;
  87. const_cast<XMLHttpRequest&>(*weak_this).m_response = ByteBuffer::copy(data);
  88. const_cast<XMLHttpRequest&>(*weak_this).set_ready_state(ReadyState::Done);
  89. const_cast<XMLHttpRequest&>(*weak_this).dispatch_event(DOM::Event::create(HTML::EventNames::load));
  90. },
  91. [weak_this = make_weak_ptr()](auto& error) {
  92. if (!weak_this)
  93. return;
  94. dbgln("XHR failed to load: {}", error);
  95. const_cast<XMLHttpRequest&>(*weak_this).set_ready_state(ReadyState::Done);
  96. const_cast<XMLHttpRequest&>(*weak_this).dispatch_event(DOM::Event::create(HTML::EventNames::error));
  97. });
  98. }
  99. bool XMLHttpRequest::dispatch_event(NonnullRefPtr<DOM::Event> event)
  100. {
  101. return DOM::EventDispatcher::dispatch(*this, move(event));
  102. }
  103. Bindings::EventTargetWrapper* XMLHttpRequest::create_wrapper(JS::GlobalObject& global_object)
  104. {
  105. return wrap(global_object, *this);
  106. }
  107. }