diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp index 98c7cb91cd..2cf23df0b6 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp @@ -8,13 +8,13 @@ namespace Web::Fetch::Infrastructure { -Body::Body(ReadableStreamDummy stream) - : m_stream(stream) +Body::Body(JS::Handle stream) + : m_stream(move(stream)) { } -Body::Body(ReadableStreamDummy stream, SourceType source, Optional length) - : m_stream(stream) +Body::Body(JS::Handle stream, SourceType source, Optional length) + : m_stream(move(stream)) , m_source(move(source)) , m_length(move(length)) { diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h index 8f4dfcad4e..815f62e08e 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h @@ -13,6 +13,7 @@ #include #include #include +#include namespace Web::Fetch::Infrastructure { @@ -21,20 +22,17 @@ class Body final { public: using SourceType = Variant>; - struct ReadableStreamDummy { }; + explicit Body(JS::Handle); + Body(JS::Handle, SourceType, Optional); - explicit Body(ReadableStreamDummy); - Body(ReadableStreamDummy, SourceType, Optional); - - [[nodiscard]] ReadableStreamDummy const& stream() { return m_stream; } + [[nodiscard]] JS::NonnullGCPtr stream() { return *m_stream; } [[nodiscard]] SourceType const& source() const { return m_source; } [[nodiscard]] Optional const& length() const { return m_length; } private: // https://fetch.spec.whatwg.org/#concept-body-stream // A stream (a ReadableStream object). - // FIXME: Just a placeholder for now. - ReadableStreamDummy m_stream; + JS::Handle m_stream; // https://fetch.spec.whatwg.org/#concept-body-source // A source (null, a byte sequence, a Blob object, or a FormData object), initially null. diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp index 80b5e025cd..d67b89fb5c 100644 --- a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp +++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp @@ -270,10 +270,12 @@ Optional XMLHttpRequest::get_final_encoding() const // https://fetch.spec.whatwg.org/#concept-bodyinit-extract // FIXME: The parameter 'body_init' should be 'typedef (ReadableStream or XMLHttpRequestBodyInit) BodyInit'. For now we just let it be 'XMLHttpRequestBodyInit'. -static ErrorOr extract_body(Fetch::XMLHttpRequestBodyInit const& body_init) +static ErrorOr extract_body(JS::Realm& realm, Fetch::XMLHttpRequestBodyInit const& body_init) { + auto& window = verify_cast(realm.global_object()); + // FIXME: 1. Let stream be object if object is a ReadableStream object. Otherwise, let stream be a new ReadableStream, and set up stream. - Fetch::Infrastructure::Body::ReadableStreamDummy stream {}; + auto* stream = realm.heap().allocate(realm, window); // FIXME: 2. Let action be null. // 3. Let source be null. Fetch::Infrastructure::Body::SourceType source {}; @@ -321,7 +323,7 @@ static ErrorOr extract_body(Fetch::XMLHttpR // FIXME: 8. If action is non-null, then run these steps in in parallel: // 9. Let body be a body whose stream is stream, source is source, and length is length. - auto body = Fetch::Infrastructure::Body { move(stream), move(source), move(length) }; + auto body = Fetch::Infrastructure::Body { JS::make_handle(stream), move(source), move(length) }; // 10. Return (body, type). return Fetch::Infrastructure::BodyWithType { .body = move(body), .type = move(type) }; } @@ -455,6 +457,9 @@ DOM::ExceptionOr XMLHttpRequest::open(String const& method_string, String // https://xhr.spec.whatwg.org/#dom-xmlhttprequest-send DOM::ExceptionOr XMLHttpRequest::send(Optional body) { + auto& vm = this->vm(); + auto& realm = *vm.current_realm(); + if (m_ready_state != ReadyState::Opened) return DOM::InvalidStateError::create(global_object(), "XHR readyState is not OPENED"); @@ -465,7 +470,7 @@ DOM::ExceptionOr XMLHttpRequest::send(Optional {}; + auto body_with_type = body.has_value() ? TRY_OR_RETURN_OOM(global_object(), extract_body(realm, body.value())) : Optional {}; AK::URL request_url = m_window->associated_document().parse_url(m_url.to_string()); dbgln("XHR send from {} to {}", m_window->associated_document().url(), request_url);