1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 11:27:35 +00:00

LibWeb: Remove unecessary dependence on Window from Fetch, XHR, FileAPI

These classes only needed Window to get at its realm. Pass a realm
directly to construct Fetch, XMLHttpRequest and FileAPI classes.
This commit is contained in:
Andrew Kaster 2022-09-25 18:08:29 -06:00 committed by Linus Groh
parent 6a10352712
commit 4878a18ee7
17 changed files with 144 additions and 169 deletions

View file

@ -11,7 +11,6 @@
#include <LibWeb/Fetch/Body.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
#include <LibWeb/FileAPI/Blob.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/Infra/JSON.h>
#include <LibWeb/MimeSniff/MimeType.h>
#include <LibWeb/Streams/ReadableStream.h>
@ -99,7 +98,6 @@ JS::NonnullGCPtr<JS::Promise> BodyMixin::text() const
WebIDL::ExceptionOr<JS::Value> package_data(JS::Realm& realm, ByteBuffer bytes, PackageDataType type, Optional<MimeSniff::MimeType> const& mime_type)
{
auto& vm = realm.vm();
auto& window = verify_cast<HTML::Window>(realm.global_object());
switch (type) {
case PackageDataType::ArrayBuffer:
@ -109,7 +107,7 @@ WebIDL::ExceptionOr<JS::Value> package_data(JS::Realm& realm, ByteBuffer bytes,
// Return a Blob whose contents are bytes and type attribute is mimeType.
// NOTE: If extracting the mime type returns failure, other browsers set it to an empty string - not sure if that's spec'd.
auto mime_type_string = mime_type.has_value() ? mime_type->serialized() : String::empty();
return FileAPI::Blob::create(window, move(bytes), move(mime_type_string));
return FileAPI::Blob::create(realm, move(bytes), move(mime_type_string));
}
case PackageDataType::FormData:
// If mimeTypes essence is "multipart/form-data", then:

View file

@ -17,14 +17,12 @@ namespace Web::Fetch {
// https://fetch.spec.whatwg.org/#concept-bodyinit-extract
WebIDL::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm, BodyInitOrReadbleBytes const& object, bool keepalive)
{
auto& window = verify_cast<HTML::Window>(realm.global_object());
// 1. Let stream be object if object is a ReadableStream object. Otherwise, let stream be a new ReadableStream, and set up stream.
Streams::ReadableStream* stream;
if (auto const* handle = object.get_pointer<JS::Handle<Streams::ReadableStream>>()) {
stream = const_cast<Streams::ReadableStream*>(handle->cell());
} else {
stream = realm.heap().allocate<Streams::ReadableStream>(realm, window);
stream = realm.heap().allocate<Streams::ReadableStream>(realm, verify_cast<HTML::Window>(realm.global_object()));
}
// FIXME: 2. Let action be null.
@ -51,19 +49,19 @@ WebIDL::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm,
},
[&](ReadonlyBytes bytes) -> WebIDL::ExceptionOr<void> {
// Set source to object.
source = TRY_OR_RETURN_OOM(window, ByteBuffer::copy(bytes));
source = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy(bytes));
return {};
},
[&](JS::Handle<JS::Object> const& buffer_source) -> WebIDL::ExceptionOr<void> {
// Set source to a copy of the bytes held by object.
source = TRY_OR_RETURN_OOM(window, WebIDL::get_buffer_source_copy(*buffer_source.cell()));
source = TRY_OR_RETURN_OOM(realm, WebIDL::get_buffer_source_copy(*buffer_source.cell()));
return {};
},
[&](JS::Handle<URL::URLSearchParams> const& url_search_params) -> WebIDL::ExceptionOr<void> {
// Set source to the result of running the application/x-www-form-urlencoded serializer with objects list.
source = url_search_params->to_string().to_byte_buffer();
// Set type to `application/x-www-form-urlencoded;charset=UTF-8`.
type = TRY_OR_RETURN_OOM(window, ByteBuffer::copy("application/x-www-form-urlencoded;charset=UTF-8"sv.bytes()));
type = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy("application/x-www-form-urlencoded;charset=UTF-8"sv.bytes()));
return {};
},
[&](String const& scalar_value_string) -> WebIDL::ExceptionOr<void> {
@ -71,7 +69,7 @@ WebIDL::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm,
// Set source to the UTF-8 encoding of object.
source = scalar_value_string.to_byte_buffer();
// Set type to `text/plain;charset=UTF-8`.
type = TRY_OR_RETURN_OOM(window, ByteBuffer::copy("text/plain;charset=UTF-8"sv.bytes()));
type = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy("text/plain;charset=UTF-8"sv.bytes()));
return {};
},
[&](JS::Handle<Streams::ReadableStream> const& stream) -> WebIDL::ExceptionOr<void> {

View file

@ -5,16 +5,16 @@
*/
#include <LibJS/Runtime/VM.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Fetch/Headers.h>
#include <LibWeb/HTML/Window.h>
namespace Web::Fetch {
// https://fetch.spec.whatwg.org/#dom-headers
WebIDL::ExceptionOr<JS::NonnullGCPtr<Headers>> Headers::create_with_global_object(HTML::Window& window, Optional<HeadersInit> const& init)
WebIDL::ExceptionOr<JS::NonnullGCPtr<Headers>> Headers::construct_impl(JS::Realm& realm, Optional<HeadersInit> const& init)
{
// The new Headers(init) constructor steps are:
auto* headers = window.heap().allocate<Headers>(window.realm(), window);
auto* headers = realm.heap().allocate<Headers>(realm, realm);
// 1. Set thiss guard to "none".
headers->m_guard = Guard::None;
@ -26,11 +26,11 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Headers>> Headers::create_with_global_objec
return JS::NonnullGCPtr(*headers);
}
Headers::Headers(HTML::Window& window)
: PlatformObject(window.realm())
Headers::Headers(JS::Realm& realm)
: PlatformObject(realm)
, m_header_list(make_ref_counted<Infrastructure::HeaderList>())
{
set_prototype(&window.cached_web_prototype("Headers"));
set_prototype(&Bindings::cached_web_prototype(realm, "Headers"));
}
Headers::~Headers() = default;
@ -40,8 +40,8 @@ WebIDL::ExceptionOr<void> Headers::append(String const& name_string, String cons
{
// The append(name, value) method steps are to append (name, value) to this.
auto header = Infrastructure::Header {
.name = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(name_string.bytes())),
.value = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(value_string.bytes())),
.name = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(name_string.bytes())),
.value = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(value_string.bytes())),
};
TRY(append(move(header)));
return {};
@ -98,7 +98,7 @@ WebIDL::ExceptionOr<String> Headers::get(String const& name_string)
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header name" };
// 2. Return the result of getting name from thiss header list.
auto byte_buffer = TRY_OR_RETURN_OOM(global_object(), m_header_list->get(name));
auto byte_buffer = TRY_OR_RETURN_OOM(realm(), m_header_list->get(name));
// FIXME: Teach BindingsGenerator about Optional<String>
return byte_buffer.has_value() ? String { byte_buffer->span() } : String {};
}
@ -125,10 +125,10 @@ WebIDL::ExceptionOr<void> Headers::set(String const& name_string, String const&
auto value = value_string.bytes();
// 1. Normalize value.
auto normalized_value = TRY_OR_RETURN_OOM(global_object(), Infrastructure::normalize_header_value(value));
auto normalized_value = TRY_OR_RETURN_OOM(realm(), Infrastructure::normalize_header_value(value));
auto header = Infrastructure::Header {
.name = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(name)),
.name = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(name)),
.value = move(normalized_value),
};
@ -155,7 +155,7 @@ WebIDL::ExceptionOr<void> Headers::set(String const& name_string, String const&
return {};
// 7. Set (name, value) in thiss header list.
TRY_OR_RETURN_OOM(global_object(), m_header_list->set(move(header)));
TRY_OR_RETURN_OOM(realm(), m_header_list->set(move(header)));
// 8. If thiss guard is "request-no-cors", then remove privileged no-CORS request headers from this.
if (m_guard == Guard::RequestNoCORS)
@ -208,7 +208,7 @@ WebIDL::ExceptionOr<void> Headers::append(Infrastructure::Header header)
auto& [name, value] = header;
// 1. Normalize value.
value = TRY_OR_RETURN_OOM(global_object(), Infrastructure::normalize_header_value(value));
value = TRY_OR_RETURN_OOM(realm(), Infrastructure::normalize_header_value(value));
// 2. If name is not a header name or value is not a header value, then throw a TypeError.
if (!Infrastructure::is_header_name(name))
@ -227,21 +227,21 @@ WebIDL::ExceptionOr<void> Headers::append(Infrastructure::Header header)
// 5. Otherwise, if headerss guard is "request-no-cors":
if (m_guard == Guard::RequestNoCORS) {
// 1. Let temporaryValue be the result of getting name from headerss header list.
auto temporary_value = TRY_OR_RETURN_OOM(global_object(), m_header_list->get(name));
auto temporary_value = TRY_OR_RETURN_OOM(realm(), m_header_list->get(name));
// 2. If temporaryValue is null, then set temporaryValue to value.
if (!temporary_value.has_value()) {
temporary_value = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(value));
temporary_value = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(value));
}
// 3. Otherwise, set temporaryValue to temporaryValue, followed by 0x2C 0x20, followed by value.
else {
TRY_OR_RETURN_OOM(global_object(), temporary_value->try_append(0x2c));
TRY_OR_RETURN_OOM(global_object(), temporary_value->try_append(0x20));
TRY_OR_RETURN_OOM(global_object(), temporary_value->try_append(value));
TRY_OR_RETURN_OOM(realm(), temporary_value->try_append(0x2c));
TRY_OR_RETURN_OOM(realm(), temporary_value->try_append(0x20));
TRY_OR_RETURN_OOM(realm(), temporary_value->try_append(value));
}
auto temporary_header = Infrastructure::Header {
.name = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(name)),
.name = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(name)),
.value = temporary_value.release_value(),
};
@ -254,8 +254,8 @@ WebIDL::ExceptionOr<void> Headers::append(Infrastructure::Header header)
if (m_guard == Guard::Response && Infrastructure::is_forbidden_response_header_name(name))
return {};
// 7. Append (name, value) to headerss header list
TRY_OR_RETURN_OOM(global_object(), m_header_list->append(move(header)));
// 7. Append (name, value) to headerss header list.
TRY_OR_RETURN_OOM(realm(), m_header_list->append(move(header)));
// 8. If headerss guard is "request-no-cors", then remove privileged no-CORS request headers from headers.
if (m_guard == Guard::RequestNoCORS)
@ -278,8 +278,8 @@ WebIDL::ExceptionOr<void> Headers::fill(HeadersInit const& object)
// 2. Append (headers first item, headers second item) to headers.
auto header = Fetch::Infrastructure::Header {
.name = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(entry[0].bytes())),
.value = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(entry[1].bytes())),
.name = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(entry[0].bytes())),
.value = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(entry[1].bytes())),
};
TRY(append(move(header)));
}
@ -289,8 +289,8 @@ WebIDL::ExceptionOr<void> Headers::fill(HeadersInit const& object)
[this](OrderedHashMap<String, String> const& object) -> WebIDL::ExceptionOr<void> {
for (auto const& entry : object) {
auto header = Fetch::Infrastructure::Header {
.name = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(entry.key.bytes())),
.value = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(entry.value.bytes())),
.name = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(entry.key.bytes())),
.value = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(entry.value.bytes())),
};
TRY(append(move(header)));
}

View file

@ -31,7 +31,7 @@ public:
None,
};
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Headers>> create_with_global_object(HTML::Window& window, Optional<HeadersInit> const& init);
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Headers>> construct_impl(JS::Realm& realm, Optional<HeadersInit> const& init);
virtual ~Headers() override;
@ -58,7 +58,7 @@ public:
private:
friend class HeadersIterator;
explicit Headers(HTML::Window&);
explicit Headers(JS::Realm&);
void remove_privileged_no_cors_headers();

View file

@ -7,8 +7,8 @@
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibWeb/Bindings/HeadersIteratorPrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Fetch/HeadersIterator.h>
#include <LibWeb/HTML/Window.h>
namespace Web::Fetch {
@ -22,7 +22,7 @@ HeadersIterator::HeadersIterator(Headers const& headers, JS::Object::PropertyKin
, m_headers(headers)
, m_iteration_kind(iteration_kind)
{
set_prototype(&headers.global_object().ensure_web_prototype<Bindings::HeadersIteratorPrototype>("HeadersIterator"));
set_prototype(&Bindings::ensure_web_prototype<Bindings::HeadersIteratorPrototype>(headers.realm(), "HeadersIterator"));
}
HeadersIterator::~HeadersIterator() = default;

View file

@ -6,6 +6,7 @@
#include <AK/ScopeGuard.h>
#include <AK/URLParser.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/DOM/AbortSignal.h>
#include <LibWeb/Fetch/Enums.h>
#include <LibWeb/Fetch/Headers.h>
@ -15,7 +16,6 @@
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
#include <LibWeb/Fetch/Request.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/ReferrerPolicy/ReferrerPolicy.h>
namespace Web::Fetch {
@ -24,8 +24,7 @@ Request::Request(JS::Realm& realm, NonnullOwnPtr<Infrastructure::Request> reques
: PlatformObject(realm)
, m_request(move(request))
{
auto& window = verify_cast<HTML::Window>(realm.global_object());
set_prototype(&window.cached_web_prototype("Request"));
set_prototype(&Bindings::cached_web_prototype(realm, "Request"));
}
Request::~Request() = default;
@ -75,8 +74,6 @@ Optional<Infrastructure::Body&> Request::body_impl()
// https://fetch.spec.whatwg.org/#request-create
JS::NonnullGCPtr<Request> Request::create(NonnullOwnPtr<Infrastructure::Request> request, Headers::Guard guard, JS::Realm& realm)
{
auto& window = verify_cast<HTML::Window>(realm.global_object());
// Copy a NonnullRefPtr to the request's header list before request is being move()'d.
auto request_reader_list = request->header_list();
@ -85,27 +82,22 @@ JS::NonnullGCPtr<Request> Request::create(NonnullOwnPtr<Infrastructure::Request>
auto* request_object = realm.heap().allocate<Request>(realm, realm, move(request));
// 3. Set requestObjects headers to a new Headers object with realm, whose headers list is requests headers list and guard is guard.
request_object->m_headers = realm.heap().allocate<Headers>(realm, window);
request_object->m_headers = realm.heap().allocate<Headers>(realm, realm);
request_object->m_headers->set_header_list(move(request_reader_list));
request_object->m_headers->set_guard(guard);
// 4. Set requestObjects signal to a new AbortSignal object with realm.
request_object->m_signal = realm.heap().allocate<DOM::AbortSignal>(realm, window);
request_object->m_signal = realm.heap().allocate<DOM::AbortSignal>(realm, realm);
// 5. Return requestObject.
return JS::NonnullGCPtr { *request_object };
}
// https://fetch.spec.whatwg.org/#dom-request
WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> Request::create_with_global_object(HTML::Window& html_window, RequestInfo const& input, RequestInit const& init)
WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> Request::construct_impl(JS::Realm& realm, RequestInfo const& input, RequestInit const& init)
{
auto& realm = html_window.realm();
// Referred to as 'this' in the spec.
auto request_object = [&] {
auto request = adopt_own(*new Infrastructure::Request());
return JS::NonnullGCPtr { *realm.heap().allocate<Request>(realm, realm, move(request)) };
}();
auto request_object = JS::NonnullGCPtr { *realm.heap().allocate<Request>(realm, realm, make<Infrastructure::Request>()) };
// 1. Let request be null.
Infrastructure::Request const* input_request = nullptr;
@ -191,13 +183,13 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> Request::create_with_global_objec
// method
// requests method.
request.set_method(TRY_OR_RETURN_OOM(html_window, ByteBuffer::copy(request.method())));
request.set_method(TRY_OR_RETURN_OOM(realm, ByteBuffer::copy(request.method())));
// header list
// A copy of requests header list.
auto header_list_copy = make_ref_counted<Infrastructure::HeaderList>();
for (auto& header : *request.header_list())
TRY_OR_RETURN_OOM(html_window, header_list_copy->append(header));
TRY_OR_RETURN_OOM(realm, header_list_copy->append(header));
request.set_header_list(move(header_list_copy));
// unsafe-request flag
@ -382,7 +374,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> Request::create_with_global_objec
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Method must not be one of CONNECT, TRACE, or TRACK"sv };
// 3. Normalize method.
method = TRY_OR_RETURN_OOM(html_window, Infrastructure::normalize_method(method.bytes()));
method = TRY_OR_RETURN_OOM(realm, Infrastructure::normalize_method(method.bytes()));
// 4. Set requests method to method.
request.set_method(MUST(ByteBuffer::copy(method.bytes())));
@ -397,7 +389,8 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> Request::create_with_global_objec
// cannot exist with a null Infrastructure::Request.
// 28. Set thiss signal to a new AbortSignal object with thiss relevant Realm.
request_object->m_signal = realm.heap().allocate<DOM::AbortSignal>(HTML::relevant_realm(*request_object), html_window);
auto& this_relevant_realm = HTML::relevant_realm(*request_object);
request_object->m_signal = realm.heap().allocate<DOM::AbortSignal>(this_relevant_realm, this_relevant_realm);
// 29. If signal is not null, then make thiss signal follow signal.
if (input_signal != nullptr) {
@ -405,7 +398,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> Request::create_with_global_objec
}
// 30. Set thiss headers to a new Headers object with thiss relevant Realm, whose header list is requests header list and guard is "request".
request_object->m_headers = realm.heap().allocate<Headers>(realm, html_window);
request_object->m_headers = realm.heap().allocate<Headers>(realm, realm);
request_object->m_headers->set_header_list(request.header_list());
request_object->m_headers->set_guard(Headers::Guard::Request);

View file

@ -64,7 +64,7 @@ class Request final
public:
static JS::NonnullGCPtr<Request> create(NonnullOwnPtr<Infrastructure::Request>, Headers::Guard, JS::Realm&);
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> create_with_global_object(HTML::Window&, RequestInfo const& input, RequestInit const& init = {});
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> construct_impl(JS::Realm&, RequestInfo const& input, RequestInit const& init = {});
virtual ~Request() override;

View file

@ -5,6 +5,7 @@
*/
#include <AK/URLParser.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/Fetch/Enums.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
@ -12,7 +13,6 @@
#include <LibWeb/Fetch/Infrastructure/HTTP/Statuses.h>
#include <LibWeb/Fetch/Response.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/Infra/JSON.h>
namespace Web::Fetch {
@ -21,8 +21,7 @@ Response::Response(JS::Realm& realm, NonnullOwnPtr<Infrastructure::Response> res
: PlatformObject(realm)
, m_response(move(response))
{
auto& window = verify_cast<HTML::Window>(realm.global_object());
set_prototype(&window.cached_web_prototype("Response"));
set_prototype(&Bindings::cached_web_prototype(realm, "Response"));
}
Response::~Response() = default;
@ -67,8 +66,6 @@ Optional<Infrastructure::Body&> Response::body_impl()
// https://fetch.spec.whatwg.org/#response-create
JS::NonnullGCPtr<Response> Response::create(NonnullOwnPtr<Infrastructure::Response> response, Headers::Guard guard, JS::Realm& realm)
{
auto& window = verify_cast<HTML::Window>(realm.global_object());
// Copy a NonnullRefPtr to the response's header list before response is being move()'d.
auto response_reader_list = response->header_list();
@ -77,7 +74,7 @@ JS::NonnullGCPtr<Response> Response::create(NonnullOwnPtr<Infrastructure::Respon
auto* response_object = realm.heap().allocate<Response>(realm, realm, move(response));
// 3. Set responseObjects headers to a new Headers object with realm, whose headers list is responses headers list and guard is guard.
response_object->m_headers = realm.heap().allocate<Headers>(realm, window);
response_object->m_headers = realm.heap().allocate<Headers>(realm, realm);
response_object->m_headers->set_header_list(move(response_reader_list));
response_object->m_headers->set_guard(guard);
@ -88,10 +85,6 @@ JS::NonnullGCPtr<Response> Response::create(NonnullOwnPtr<Infrastructure::Respon
// https://fetch.spec.whatwg.org/#initialize-a-response
WebIDL::ExceptionOr<void> Response::initialize_response(ResponseInit const& init, Optional<Infrastructure::BodyWithType> const& body)
{
auto& vm = Bindings::main_thread_vm();
auto& realm = *vm.current_realm();
auto& window = verify_cast<HTML::Window>(realm.global_object());
// 1. If init["status"] is not in the range 200 to 599, inclusive, then throw a RangeError.
if (init.status < 200 || init.status > 599)
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::RangeError, "Status must be in range 200-599"sv };
@ -102,7 +95,7 @@ WebIDL::ExceptionOr<void> Response::initialize_response(ResponseInit const& init
m_response->set_status(init.status);
// 4. Set responses responses status message to init["statusText"].
m_response->set_status_message(TRY_OR_RETURN_OOM(window, ByteBuffer::copy(init.status_text.bytes())));
m_response->set_status_message(TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(init.status_text.bytes())));
// 5. If init["headers"] exists, then fill responses headers with init["headers"].
if (init.headers.has_value())
@ -120,10 +113,10 @@ WebIDL::ExceptionOr<void> Response::initialize_response(ResponseInit const& init
// 3. If bodys type is non-null and responses header list does not contain `Content-Type`, then append (`Content-Type`, bodys type) to responses header list.
if (body->type.has_value() && m_response->header_list()->contains("Content-Type"sv.bytes())) {
auto header = Infrastructure::Header {
.name = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy("Content-Type"sv.bytes())),
.value = TRY_OR_RETURN_OOM(global_object(), ByteBuffer::copy(body->type->span())),
.name = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy("Content-Type"sv.bytes())),
.value = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(body->type->span())),
};
TRY_OR_RETURN_OOM(global_object(), m_response->header_list()->append(move(header)));
TRY_OR_RETURN_OOM(realm(), m_response->header_list()->append(move(header)));
}
}
@ -131,22 +124,17 @@ WebIDL::ExceptionOr<void> Response::initialize_response(ResponseInit const& init
}
// https://fetch.spec.whatwg.org/#dom-response
WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::create_with_global_object(HTML::Window& window, Optional<BodyInit> const& body, ResponseInit const& init)
WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::construct_impl(JS::Realm& realm, Optional<BodyInit> const& body, ResponseInit const& init)
{
auto& realm = window.realm();
// Referred to as 'this' in the spec.
auto response_object = [&] {
auto response = adopt_own(*new Infrastructure::Response());
return JS::NonnullGCPtr { *realm.heap().allocate<Response>(realm, realm, move(response)) };
}();
auto response_object = JS::NonnullGCPtr { *realm.heap().allocate<Response>(realm, realm, make<Infrastructure::Response>()) };
// 1. Set thiss response to a new response.
// NOTE: This is done at the beginning as the 'this' value Response object
// cannot exist with a null Infrastructure::Response.
// 2. Set thiss headers to a new Headers object with thiss relevant Realm, whose header list is thiss responses header list and guard is "response".
response_object->m_headers = realm.heap().allocate<Headers>(realm, window);
response_object->m_headers = realm.heap().allocate<Headers>(realm, realm);
response_object->m_headers->set_header_list(response_object->response().header_list());
response_object->m_headers->set_guard(Headers::Guard::Response);
@ -176,9 +164,7 @@ JS::NonnullGCPtr<Response> Response::error()
// https://fetch.spec.whatwg.org/#dom-response-redirect
WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::redirect(String const& url, u16 status)
{
auto& vm = Bindings::main_thread_vm();
auto& realm = *vm.current_realm();
auto& window = verify_cast<HTML::Window>(realm.global_object());
auto& realm = HTML::current_settings_object().realm();
// 1. Let parsedURL be the result of parsing url with current settings objects API base URL.
auto api_base_url = HTML::current_settings_object().api_base_url();
@ -194,7 +180,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::redirect(String const&
// 4. Let responseObject be the result of creating a Response object, given a new response, "immutable", and thiss relevant Realm.
// FIXME: How can we reliably get 'this', i.e. the object the function was called on, in IDL-defined functions?
auto response_object = Response::create(make<Infrastructure::Response>(), Headers::Guard::Immutable, *vm.current_realm());
auto response_object = Response::create(make<Infrastructure::Response>(), Headers::Guard::Immutable, realm);
// 5. Set responseObjects responses status to status.
response_object->response().set_status(status);
@ -204,10 +190,10 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::redirect(String const&
// 7. Append (`Location`, value) to responseObjects responses header list.
auto header = Infrastructure::Header {
.name = TRY_OR_RETURN_OOM(window, ByteBuffer::copy("Location"sv.bytes())),
.value = TRY_OR_RETURN_OOM(window, ByteBuffer::copy(value.bytes())),
.name = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy("Location"sv.bytes())),
.value = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy(value.bytes())),
};
TRY_OR_RETURN_OOM(window, response_object->response().header_list()->append(move(header)));
TRY_OR_RETURN_OOM(realm, response_object->response().header_list()->append(move(header)));
// 8. Return responseObject.
return response_object;
@ -218,7 +204,6 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::json(JS::Value data, R
{
auto& vm = Bindings::main_thread_vm();
auto& realm = *vm.current_realm();
auto& window = verify_cast<HTML::Window>(realm.global_object());
// 1. Let bytes the result of running serialize a JavaScript value to JSON bytes on data.
auto bytes = TRY(Infra::serialize_javascript_value_to_json_bytes(vm, data));
@ -228,12 +213,12 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::json(JS::Value data, R
// 3. Let responseObject be the result of creating a Response object, given a new response, "response", and thiss relevant Realm.
// FIXME: How can we reliably get 'this', i.e. the object the function was called on, in IDL-defined functions?
auto response_object = Response::create(make<Infrastructure::Response>(), Headers::Guard::Response, *vm.current_realm());
auto response_object = Response::create(make<Infrastructure::Response>(), Headers::Guard::Response, realm);
// 4. Perform initialize a response given responseObject, init, and (body, "application/json").
auto body_with_type = Infrastructure::BodyWithType {
.body = move(body),
.type = TRY_OR_RETURN_OOM(window, ByteBuffer::copy("application/json"sv.bytes()))
.type = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy("application/json"sv.bytes()))
};
TRY(response_object->initialize_response(init, move(body_with_type)));

View file

@ -31,7 +31,7 @@ class Response final
public:
static JS::NonnullGCPtr<Response> create(NonnullOwnPtr<Infrastructure::Response>, Headers::Guard, JS::Realm&);
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> create_with_global_object(HTML::Window&, Optional<BodyInit> const& body = {}, ResponseInit const& init = {});
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> construct_impl(JS::Realm&, Optional<BodyInit> const& body = {}, ResponseInit const& init = {});
virtual ~Response() override;