1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:17:45 +00:00

LibWeb/Fetch: Port JS interfaces to new String

Since BodyInit and Headers are tightly coupled to both Request and
Response, I chose to do all of them at once instead of introducing a
bunch of temporary conversion glue code.
This commit is contained in:
Linus Groh 2023-03-02 22:26:12 +00:00
parent f561940e54
commit 7f9ddcf420
13 changed files with 130 additions and 129 deletions

View file

@ -24,7 +24,7 @@
// ```idl
// #import <Fetch/Request.idl>
// #import <Fetch/Response.idl>
// [Exposed=Window]
// [Exposed=Window, UseNewAKString]
// interface Dummy {
// static Promise<Response> fetch(RequestInfo input, optional RequestInit init = {});
// };
@ -55,7 +55,7 @@ JS::ThrowCompletionOr<JS::Value> fetch(JS::VM& vm)
if (vm.argument_count() < 1)
return vm.throw_completion<JS::TypeError>(JS::ErrorType::BadArgCountOne, "fetch");
auto arg0 = vm.argument(0);
auto arg0_to_variant = [&vm, &realm](JS::Value arg0) -> JS::ThrowCompletionOr<Variant<JS::Handle<Request>, DeprecatedString>> {
auto arg0_to_variant = [&vm, &realm](JS::Value arg0) -> JS::ThrowCompletionOr<Variant<JS::Handle<Request>, String>> {
// These might be unused.
(void)vm;
(void)realm;
@ -66,9 +66,9 @@ JS::ThrowCompletionOr<JS::Value> fetch(JS::VM& vm)
return JS::make_handle(static_cast<Request&>(arg0_object));
}
}
return TRY(arg0.to_deprecated_string(vm));
return TRY(arg0.to_string(vm));
};
Variant<JS::Handle<Request>, DeprecatedString> input = TRY(arg0_to_variant(arg0));
Variant<JS::Handle<Request>, String> input = TRY(arg0_to_variant(arg0));
auto arg1 = vm.argument(1);
if (!arg1.is_nullish() && !arg1.is_object())
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "RequestInit");
@ -77,7 +77,7 @@ JS::ThrowCompletionOr<JS::Value> fetch(JS::VM& vm)
if (arg1.is_object())
body_property_value = TRY(arg1.as_object().get("body"));
if (!body_property_value.is_undefined()) {
auto body_property_value_to_variant = [&vm, &realm](JS::Value body_property_value) -> JS::ThrowCompletionOr<Variant<JS::Handle<ReadableStream>, JS::Handle<Blob>, JS::Handle<JS::Object>, JS::Handle<URLSearchParams>, DeprecatedString>> {
auto body_property_value_to_variant = [&vm, &realm](JS::Value body_property_value) -> JS::ThrowCompletionOr<Variant<JS::Handle<ReadableStream>, JS::Handle<Blob>, JS::Handle<JS::Object>, JS::Handle<URLSearchParams>, String>> {
// These might be unused.
(void)vm;
(void)realm;
@ -94,9 +94,9 @@ JS::ThrowCompletionOr<JS::Value> fetch(JS::VM& vm)
if (is<JS::ArrayBuffer>(body_property_value_object))
return JS::make_handle(body_property_value_object);
}
return TRY(body_property_value.to_deprecated_string(vm));
return TRY(body_property_value.to_string(vm));
};
Optional<Variant<JS::Handle<ReadableStream>, JS::Handle<Blob>, JS::Handle<JS::Object>, JS::Handle<URLSearchParams>, DeprecatedString>> body_value;
Optional<Variant<JS::Handle<ReadableStream>, JS::Handle<Blob>, JS::Handle<JS::Object>, JS::Handle<URLSearchParams>, String>> body_value;
if (!body_property_value.is_nullish())
body_value = TRY(body_property_value_to_variant(body_property_value));
init.body = body_value;
@ -161,7 +161,7 @@ JS::ThrowCompletionOr<JS::Value> fetch(JS::VM& vm)
if (arg1.is_object())
headers_property_value = TRY(arg1.as_object().get("headers"));
if (!headers_property_value.is_undefined()) {
auto headers_property_value_to_variant = [&vm, &realm](JS::Value headers_property_value) -> JS::ThrowCompletionOr<Variant<Vector<Vector<DeprecatedString>>, OrderedHashMap<DeprecatedString, DeprecatedString>>> {
auto headers_property_value_to_variant = [&vm, &realm](JS::Value headers_property_value) -> JS::ThrowCompletionOr<Variant<Vector<Vector<String>>, OrderedHashMap<String, String>>> {
// These might be unused.
(void)vm;
(void)realm;
@ -170,7 +170,7 @@ JS::ThrowCompletionOr<JS::Value> fetch(JS::VM& vm)
auto* method = TRY(headers_property_value.get_method(vm, *vm.well_known_symbol_iterator()));
if (method) {
auto iterator1 = TRY(JS::get_iterator(vm, headers_property_value, JS::IteratorHint::Sync, method));
Vector<Vector<DeprecatedString>> headers_value;
Vector<Vector<String>> headers_value;
for (;;) {
auto* next1 = TRY(JS::iterator_step(vm, iterator1));
if (!next1)
@ -182,17 +182,15 @@ JS::ThrowCompletionOr<JS::Value> fetch(JS::VM& vm)
if (!iterator_method1)
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotIterable, TRY_OR_THROW_OOM(vm, next_item1.to_string_without_side_effects()));
auto iterator2 = TRY(JS::get_iterator(vm, next_item1, JS::IteratorHint::Sync, iterator_method1));
Vector<DeprecatedString> sequence_item1;
Vector<String> sequence_item1;
for (;;) {
auto* next2 = TRY(JS::iterator_step(vm, iterator2));
if (!next2)
break;
auto next_item2 = TRY(JS::iterator_value(vm, *next2));
DeprecatedString sequence_item2;
if (next_item2.is_null() && false) {
sequence_item2 = DeprecatedString::empty();
} else {
sequence_item2 = TRY(next_item2.to_deprecated_string(vm));
String sequence_item2;
if (!false || !next_item2.is_null()) {
sequence_item2 = TRY(next_item2.to_string(vm));
}
sequence_item1.append(sequence_item2);
}
@ -200,25 +198,21 @@ JS::ThrowCompletionOr<JS::Value> fetch(JS::VM& vm)
}
return headers_value;
}
OrderedHashMap<DeprecatedString, DeprecatedString> record_union_type;
OrderedHashMap<String, String> record_union_type;
auto record_keys1 = TRY(headers_property_value_object.internal_own_property_keys());
for (auto& key1 : record_keys1) {
auto property_key1 = MUST(JS::PropertyKey::from_value(vm, key1));
auto descriptor1 = TRY(headers_property_value_object.internal_get_own_property(property_key1));
if (!descriptor1.has_value() || !descriptor1->enumerable.has_value() || !descriptor1->enumerable.value())
continue;
DeprecatedString typed_key1;
if (key1.is_null() && false) {
typed_key1 = DeprecatedString::empty();
} else {
typed_key1 = TRY(key1.to_deprecated_string(vm));
String typed_key1;
if (!false || !key1.is_null()) {
typed_key1 = TRY(key1.to_string(vm));
}
auto value1 = TRY(headers_property_value_object.get(property_key1));
DeprecatedString typed_value1;
if (value1.is_null() && false) {
typed_value1 = DeprecatedString::empty();
} else {
typed_value1 = TRY(value1.to_deprecated_string(vm));
String typed_value1;
if (!false || !value1.is_null()) {
typed_value1 = TRY(value1.to_string(vm));
}
record_union_type.set(typed_key1, typed_value1);
}
@ -226,7 +220,7 @@ JS::ThrowCompletionOr<JS::Value> fetch(JS::VM& vm)
}
return vm.throw_completion<JS::TypeError>("No union types matched"sv);
};
Optional<Variant<Vector<Vector<DeprecatedString>>, OrderedHashMap<DeprecatedString, DeprecatedString>>> headers_value;
Optional<Variant<Vector<Vector<String>>, OrderedHashMap<String, String>>> headers_value;
if (!headers_property_value.is_nullish())
headers_value = TRY(headers_property_value_to_variant(headers_property_value));
init.headers = headers_value;
@ -235,14 +229,13 @@ JS::ThrowCompletionOr<JS::Value> fetch(JS::VM& vm)
if (arg1.is_object())
integrity_property_value = TRY(arg1.as_object().get("integrity"));
if (!integrity_property_value.is_undefined()) {
DeprecatedString integrity_value;
Optional<String> integrity_value;
if (!integrity_property_value.is_undefined()) {
if (integrity_property_value.is_null() && false)
integrity_value = DeprecatedString::empty();
else
integrity_value = TRY(integrity_property_value.to_deprecated_string(vm));
if (!false || !integrity_property_value.is_null())
integrity_value = TRY(integrity_property_value.to_string(vm));
}
init.integrity = integrity_value;
if (integrity_value.has_value())
init.integrity = integrity_value.release_value();
}
auto keepalive_property_value = JS::js_undefined();
if (arg1.is_object())
@ -257,14 +250,13 @@ JS::ThrowCompletionOr<JS::Value> fetch(JS::VM& vm)
if (arg1.is_object())
method_property_value = TRY(arg1.as_object().get("method"));
if (!method_property_value.is_undefined()) {
DeprecatedString method_value;
Optional<String> method_value;
if (!method_property_value.is_undefined()) {
if (method_property_value.is_null() && false)
method_value = DeprecatedString::empty();
else
method_value = TRY(method_property_value.to_deprecated_string(vm));
if (!false || !method_property_value.is_null())
method_value = TRY(method_property_value.to_string(vm));
}
init.method = method_value;
if (method_value.has_value())
init.method = method_value.release_value();
}
auto mode_property_value = JS::js_undefined();
if (arg1.is_object())
@ -308,14 +300,13 @@ JS::ThrowCompletionOr<JS::Value> fetch(JS::VM& vm)
if (arg1.is_object())
referrer_property_value = TRY(arg1.as_object().get("referrer"));
if (!referrer_property_value.is_undefined()) {
DeprecatedString referrer_value;
Optional<String> referrer_value;
if (!referrer_property_value.is_undefined()) {
if (referrer_property_value.is_null() && false)
referrer_value = DeprecatedString::empty();
else
referrer_value = TRY(referrer_property_value.to_deprecated_string(vm));
if (!false || !referrer_property_value.is_null())
referrer_value = TRY(referrer_property_value.to_string(vm));
}
init.referrer = referrer_value;
if (referrer_value.has_value())
init.referrer = referrer_value.release_value();
}
auto referrer_policy_property_value = JS::js_undefined();
if (arg1.is_object())

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -135,7 +135,7 @@ WebIDL::ExceptionOr<JS::Value> package_data(JS::Realm& realm, ByteBuffer bytes,
return Infra::parse_json_bytes_to_javascript_value(vm, bytes);
case PackageDataType::Text:
// Return the result of running UTF-8 decode on bytes.
return JS::PrimitiveString::create(vm, DeprecatedString::copy(bytes));
return JS::PrimitiveString::create(vm, TRY_OR_THROW_OOM(vm, String::from_utf8(bytes)));
default:
VERIFY_NOT_REACHED();
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022, Kenneth Myhra <kennethmyhra@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
@ -96,10 +96,10 @@ WebIDL::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm,
type = TRY_OR_THROW_OOM(vm, ByteBuffer::copy("application/x-www-form-urlencoded;charset=UTF-8"sv.bytes()));
return {};
},
[&](DeprecatedString const& scalar_value_string) -> WebIDL::ExceptionOr<void> {
// NOTE: AK::DeprecatedString is always UTF-8.
[&](String const& scalar_value_string) -> WebIDL::ExceptionOr<void> {
// NOTE: AK::String is always UTF-8.
// Set source to the UTF-8 encoding of object.
source = scalar_value_string.to_byte_buffer();
source = TRY_OR_THROW_OOM(vm, ByteBuffer::copy(scalar_value_string.bytes()));
// Set type to `text/plain;charset=UTF-8`.
type = TRY_OR_THROW_OOM(vm, ByteBuffer::copy("text/plain;charset=UTF-8"sv.bytes()));
return {};

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -14,9 +14,9 @@
namespace Web::Fetch {
// https://fetch.spec.whatwg.org/#bodyinit
using BodyInit = Variant<JS::Handle<Streams::ReadableStream>, JS::Handle<FileAPI::Blob>, JS::Handle<JS::Object>, JS::Handle<URL::URLSearchParams>, DeprecatedString>;
using BodyInit = Variant<JS::Handle<Streams::ReadableStream>, JS::Handle<FileAPI::Blob>, JS::Handle<JS::Object>, JS::Handle<URL::URLSearchParams>, String>;
using BodyInitOrReadableBytes = Variant<JS::Handle<Streams::ReadableStream>, JS::Handle<FileAPI::Blob>, JS::Handle<JS::Object>, JS::Handle<URL::URLSearchParams>, DeprecatedString, ReadonlyBytes>;
using BodyInitOrReadableBytes = Variant<JS::Handle<Streams::ReadableStream>, JS::Handle<FileAPI::Blob>, JS::Handle<JS::Object>, JS::Handle<URL::URLSearchParams>, String, ReadonlyBytes>;
WebIDL::ExceptionOr<Infrastructure::BodyWithType> safely_extract_body(JS::Realm&, BodyInitOrReadableBytes const&);
WebIDL::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm&, BodyInitOrReadableBytes const&, bool keepalive = false);

View file

@ -52,7 +52,7 @@ void Headers::visit_edges(JS::Cell::Visitor& visitor)
}
// https://fetch.spec.whatwg.org/#dom-headers-append
WebIDL::ExceptionOr<void> Headers::append(DeprecatedString const& name_string, DeprecatedString const& value_string)
WebIDL::ExceptionOr<void> Headers::append(String const& name_string, String const& value_string)
{
auto& vm = this->vm();
@ -66,15 +66,15 @@ WebIDL::ExceptionOr<void> Headers::append(DeprecatedString const& name_string, D
}
// https://fetch.spec.whatwg.org/#dom-headers-delete
WebIDL::ExceptionOr<void> Headers::delete_(DeprecatedString const& name_string)
WebIDL::ExceptionOr<void> Headers::delete_(String const& name_string)
{
// The delete(name) method steps are:
auto& realm = this->realm();
auto& vm = this->vm();
auto name = name_string.bytes();
// 1. If validating (name, ``) for headers returns false, then return.
// NOTE: Passing a dummy header value ought not to have any negative repercussions.
auto header = TRY_OR_THROW_OOM(realm.vm(), Infrastructure::Header::from_string_pair(name, ""sv));
auto header = TRY_OR_THROW_OOM(vm, Infrastructure::Header::from_string_pair(name, ""sv));
if (!TRY(validate(header)))
return {};
@ -97,9 +97,10 @@ WebIDL::ExceptionOr<void> Headers::delete_(DeprecatedString const& name_string)
}
// https://fetch.spec.whatwg.org/#dom-headers-get
WebIDL::ExceptionOr<DeprecatedString> Headers::get(DeprecatedString const& name_string)
WebIDL::ExceptionOr<Optional<String>> Headers::get(String const& name_string)
{
// The get(name) method steps are:
auto& vm = this->vm();
auto name = name_string.bytes();
// 1. If name is not a header name, then throw a TypeError.
@ -107,17 +108,16 @@ WebIDL::ExceptionOr<DeprecatedString> Headers::get(DeprecatedString const& name_
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header name"sv };
// 2. Return the result of getting name from thiss header list.
auto byte_buffer = TRY_OR_THROW_OOM(vm(), m_header_list->get(name));
// FIXME: Teach BindingsGenerator about Optional<DeprecatedString>
return byte_buffer.has_value() ? DeprecatedString { byte_buffer->span() } : DeprecatedString {};
auto byte_buffer = TRY_OR_THROW_OOM(vm, m_header_list->get(name));
return byte_buffer.has_value() ? TRY_OR_THROW_OOM(vm, String::from_utf8(*byte_buffer)) : Optional<String> {};
}
// https://fetch.spec.whatwg.org/#dom-headers-getsetcookie
WebIDL::ExceptionOr<Vector<DeprecatedString>> Headers::get_set_cookie()
WebIDL::ExceptionOr<Vector<String>> Headers::get_set_cookie()
{
// The getSetCookie() method steps are:
auto& vm = this->vm();
auto values = Vector<DeprecatedString> {};
auto values = Vector<String> {};
// 1. If thiss header list does not contain `Set-Cookie`, then return « ».
if (!m_header_list->contains("Set-Cookie"sv.bytes()))
@ -127,13 +127,13 @@ WebIDL::ExceptionOr<Vector<DeprecatedString>> Headers::get_set_cookie()
// `Set-Cookie`, in order.
for (auto const& header : *m_header_list) {
if (StringView { header.name }.equals_ignoring_case("Set-Cookie"sv))
TRY_OR_THROW_OOM(vm, values.try_append(DeprecatedString { header.value.bytes() }));
TRY_OR_THROW_OOM(vm, values.try_append(TRY_OR_THROW_OOM(vm, String::from_utf8(header.value))));
}
return values;
}
// https://fetch.spec.whatwg.org/#dom-headers-has
WebIDL::ExceptionOr<bool> Headers::has(DeprecatedString const& name_string)
WebIDL::ExceptionOr<bool> Headers::has(String const& name_string)
{
// The has(name) method steps are:
auto name = name_string.bytes();
@ -147,7 +147,7 @@ WebIDL::ExceptionOr<bool> Headers::has(DeprecatedString const& name_string)
}
// https://fetch.spec.whatwg.org/#dom-headers-set
WebIDL::ExceptionOr<void> Headers::set(DeprecatedString const& name_string, DeprecatedString const& value_string)
WebIDL::ExceptionOr<void> Headers::set(String const& name_string, String const& value_string)
{
auto& realm = this->realm();
auto& vm = realm.vm();
@ -185,12 +185,11 @@ WebIDL::ExceptionOr<void> Headers::set(DeprecatedString const& name_string, Depr
// https://webidl.spec.whatwg.org/#es-iterable, Step 4
JS::ThrowCompletionOr<void> Headers::for_each(ForEachCallback callback)
{
auto& vm = this->vm();
// The value pairs to iterate over are the return value of running sort and combine with thiss header list.
auto value_pairs_to_iterate_over = [&]() -> JS::ThrowCompletionOr<Vector<Fetch::Infrastructure::Header>> {
auto headers_or_error = m_header_list->sort_and_combine();
if (headers_or_error.is_error())
return vm().throw_completion<JS::InternalError>(JS::ErrorType::NotEnoughMemoryToAllocate);
return headers_or_error.release_value();
return TRY_OR_THROW_OOM(vm, m_header_list->sort_and_combine());
};
// 1-5. Are done in the generated wrapper code.
@ -207,7 +206,7 @@ JS::ThrowCompletionOr<void> Headers::for_each(ForEachCallback callback)
auto const& pair = pairs[i];
// 2. Invoke idlCallback with « pairs value, pairs key, idlObject » and with thisArg as the callback this value.
TRY(callback(StringView { pair.name }, StringView { pair.value }));
TRY(callback(TRY_OR_THROW_OOM(vm, String::from_utf8(pair.name)), TRY_OR_THROW_OOM(vm, String::from_utf8(pair.value))));
// 3. Set pairs to idlObjects current list of value pairs to iterate over. (It might have changed.)
pairs = TRY(value_pairs_to_iterate_over());
@ -309,20 +308,20 @@ WebIDL::ExceptionOr<void> Headers::fill(HeadersInit const& object)
// To fill a Headers object headers with a given object object, run these steps:
return object.visit(
// 1. If object is a sequence, then for each header of object:
[&](Vector<Vector<DeprecatedString>> const& object) -> WebIDL::ExceptionOr<void> {
[&](Vector<Vector<String>> const& object) -> WebIDL::ExceptionOr<void> {
for (auto const& entry : object) {
// 1. If header's size is not 2, then throw a TypeError.
if (entry.size() != 2)
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Array must contain header key/value pair"sv };
// 2. Append (header[0], header[1]) to headers.
auto header = TRY_OR_THROW_OOM(vm, Infrastructure::Header::from_string_pair(entry[0], entry[1].bytes()));
auto header = TRY_OR_THROW_OOM(vm, Infrastructure::Header::from_string_pair(entry[0], entry[1]));
TRY(append(move(header)));
}
return {};
},
// 2. Otherwise, object is a record, then for each key → value of object, append (key, value) to headers.
[&](OrderedHashMap<DeprecatedString, DeprecatedString> const& object) -> WebIDL::ExceptionOr<void> {
[&](OrderedHashMap<String, String> const& object) -> WebIDL::ExceptionOr<void> {
for (auto const& entry : object) {
auto header = TRY_OR_THROW_OOM(vm, Infrastructure::Header::from_string_pair(entry.key, entry.value));
TRY(append(move(header)));

View file

@ -6,8 +6,8 @@
#pragma once
#include <AK/DeprecatedString.h>
#include <AK/HashMap.h>
#include <AK/String.h>
#include <AK/Variant.h>
#include <AK/Vector.h>
#include <LibJS/Forward.h>
@ -18,7 +18,7 @@
namespace Web::Fetch {
using HeadersInit = Variant<Vector<Vector<DeprecatedString>>, OrderedHashMap<DeprecatedString, DeprecatedString>>;
using HeadersInit = Variant<Vector<Vector<String>>, OrderedHashMap<String, String>>;
// https://fetch.spec.whatwg.org/#headers-class
class Headers final : public Bindings::PlatformObject {
@ -44,17 +44,17 @@ public:
void set_guard(Guard guard) { m_guard = guard; }
WebIDL::ExceptionOr<void> fill(HeadersInit const&);
WebIDL::ExceptionOr<void> append(Infrastructure::Header);
// JS API functions
WebIDL::ExceptionOr<void> append(Infrastructure::Header);
WebIDL::ExceptionOr<void> append(DeprecatedString const& name, DeprecatedString const& value);
WebIDL::ExceptionOr<void> delete_(DeprecatedString const& name);
WebIDL::ExceptionOr<DeprecatedString> get(DeprecatedString const& name);
WebIDL::ExceptionOr<Vector<DeprecatedString>> get_set_cookie();
WebIDL::ExceptionOr<bool> has(DeprecatedString const& name);
WebIDL::ExceptionOr<void> set(DeprecatedString const& name, DeprecatedString const& value);
WebIDL::ExceptionOr<void> append(String const& name, String const& value);
WebIDL::ExceptionOr<void> delete_(String const& name);
WebIDL::ExceptionOr<Optional<String>> get(String const& name);
WebIDL::ExceptionOr<Vector<String>> get_set_cookie();
WebIDL::ExceptionOr<bool> has(String const& name);
WebIDL::ExceptionOr<void> set(String const& name, String const& value);
using ForEachCallback = Function<JS::ThrowCompletionOr<void>(DeprecatedString const&, DeprecatedString const&)>;
using ForEachCallback = Function<JS::ThrowCompletionOr<void>(String const&, String const&)>;
JS::ThrowCompletionOr<void> for_each(ForEachCallback);
private:

View file

@ -1,6 +1,6 @@
typedef (sequence<sequence<ByteString>> or record<ByteString, ByteString>) HeadersInit;
[Exposed=(Window,Worker)]
[Exposed=(Window,Worker), UseNewAKString]
interface Headers {
constructor(optional HeadersInit init);

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -119,9 +119,9 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> Request::construct_impl(JS::Realm
DOM::AbortSignal const* input_signal = nullptr;
// 5. If input is a string, then:
if (input.has<DeprecatedString>()) {
if (input.has<String>()) {
// 1. Let parsedURL be the result of parsing input with baseURL.
auto parsed_url = URLParser::parse(input.get<DeprecatedString>(), &base_url);
auto parsed_url = URLParser::parse(input.get<String>(), &base_url);
// 2. If parsedURL is failure, then throw a TypeError.
if (!parsed_url.is_valid())
@ -355,7 +355,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> Request::construct_impl(JS::Realm
// 23. If init["integrity"] exists, then set requests integrity metadata to it.
if (init.integrity.has_value())
request->set_integrity_metadata(*init.integrity);
request->set_integrity_metadata(init.integrity->to_deprecated_string());
// 24. If init["keepalive"] exists, then set requests keepalive to it.
if (init.keepalive.has_value())
@ -373,7 +373,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> Request::construct_impl(JS::Realm
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Method must not be one of CONNECT, TRACE, or TRACK"sv };
// 3. Normalize method.
method = TRY_OR_THROW_OOM(vm, Infrastructure::normalize_method(method.bytes()));
method = TRY_OR_THROW_OOM(vm, String::from_utf8(TRY_OR_THROW_OOM(vm, Infrastructure::normalize_method(method.bytes()))));
// 4. Set requests method to method.
request->set_method(MUST(ByteBuffer::copy(method.bytes())));
@ -424,7 +424,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> Request::construct_impl(JS::Realm
// 4. If headers is a Headers object, then for each header of its header list, append header to thiss headers.
if (auto* header_list = headers.get_pointer<JS::NonnullGCPtr<Infrastructure::HeaderList>>()) {
for (auto& header : *header_list->ptr())
TRY(request_object->headers()->append(DeprecatedString::copy(header.name), DeprecatedString::copy(header.value)));
TRY(request_object->headers()->append(TRY_OR_THROW_OOM(vm, Infrastructure::Header::from_string_pair(header.name, header.value))));
}
// 5. Otherwise, fill thiss headers with headers.
else {
@ -457,7 +457,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> Request::construct_impl(JS::Realm
// 4. If type is non-null and thiss headerss header list does not contain `Content-Type`, then append (`Content-Type`, type) to thiss headers.
if (type.has_value() && !request_object->headers()->header_list()->contains("Content-Type"sv.bytes()))
TRY(request_object->headers()->append("Content-Type"sv, DeprecatedString::copy(type->span())));
TRY(request_object->headers()->append(TRY_OR_THROW_OOM(vm, Infrastructure::Header::from_string_pair("Content-Type"sv, type->span()))));
}
// 37. Let inputOrInitBody be initBody if it is non-null; otherwise inputBody.
@ -500,17 +500,21 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Request>> Request::construct_impl(JS::Realm
}
// https://fetch.spec.whatwg.org/#dom-request-method
DeprecatedString Request::method() const
WebIDL::ExceptionOr<String> Request::method() const
{
auto& vm = this->vm();
// The method getter steps are to return thiss requests method.
return DeprecatedString::copy(m_request->method());
return TRY_OR_THROW_OOM(vm, String::from_utf8(m_request->method()));
}
// https://fetch.spec.whatwg.org/#dom-request-url
DeprecatedString Request::url() const
WebIDL::ExceptionOr<String> Request::url() const
{
auto& vm = this->vm();
// The url getter steps are to return thiss requests URL, serialized.
return m_request->url().serialize();
return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_request->url().serialize()));
}
// https://fetch.spec.whatwg.org/#dom-request-headers
@ -528,24 +532,25 @@ Bindings::RequestDestination Request::destination() const
}
// https://fetch.spec.whatwg.org/#dom-request-referrer
DeprecatedString Request::referrer() const
WebIDL::ExceptionOr<String> Request::referrer() const
{
auto& vm = this->vm();
return m_request->referrer().visit(
[&](Infrastructure::Request::Referrer const& referrer) {
[&](Infrastructure::Request::Referrer const& referrer) -> WebIDL::ExceptionOr<String> {
switch (referrer) {
// 1. If thiss requests referrer is "no-referrer", then return the empty string.
case Infrastructure::Request::Referrer::NoReferrer:
return DeprecatedString::empty();
return String {};
// 2. If thiss requests referrer is "client", then return "about:client".
case Infrastructure::Request::Referrer::Client:
return DeprecatedString { "about:client"sv };
return TRY_OR_THROW_OOM(vm, "about:client"_string);
default:
VERIFY_NOT_REACHED();
}
},
[&](AK::URL const& url) {
[&](AK::URL const& url) -> WebIDL::ExceptionOr<String> {
// 3. Return thiss requests referrer, serialized.
return url.serialize();
return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(url.serialize()));
});
}
@ -585,10 +590,12 @@ Bindings::RequestRedirect Request::redirect() const
}
// https://fetch.spec.whatwg.org/#dom-request-integrity
DeprecatedString Request::integrity() const
WebIDL::ExceptionOr<String> Request::integrity() const
{
auto& vm = this->vm();
// The integrity getter steps are to return thiss requests integrity metadata.
return m_request->integrity_metadata();
return TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_request->integrity_metadata()));
}
// https://fetch.spec.whatwg.org/#dom-request-keepalive

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -19,20 +19,20 @@
namespace Web::Fetch {
// https://fetch.spec.whatwg.org/#requestinfo
using RequestInfo = Variant<JS::Handle<Request>, DeprecatedString>;
using RequestInfo = Variant<JS::Handle<Request>, String>;
// https://fetch.spec.whatwg.org/#requestinit
struct RequestInit {
Optional<DeprecatedString> method;
Optional<String> method;
Optional<HeadersInit> headers;
Optional<Optional<BodyInit>> body;
Optional<DeprecatedString> referrer;
Optional<String> referrer;
Optional<Bindings::ReferrerPolicy> referrer_policy;
Optional<Bindings::RequestMode> mode;
Optional<Bindings::RequestCredentials> credentials;
Optional<Bindings::RequestCache> cache;
Optional<Bindings::RequestRedirect> redirect;
Optional<DeprecatedString> integrity;
Optional<String> integrity;
Optional<bool> keepalive;
Optional<JS::GCPtr<DOM::AbortSignal>> signal;
Optional<Bindings::RequestDuplex> duplex;
@ -78,17 +78,17 @@ public:
[[nodiscard]] JS::NonnullGCPtr<Infrastructure::Request> request() const { return m_request; }
// JS API functions
[[nodiscard]] DeprecatedString method() const;
[[nodiscard]] DeprecatedString url() const;
[[nodiscard]] WebIDL::ExceptionOr<String> method() const;
[[nodiscard]] WebIDL::ExceptionOr<String> url() const;
[[nodiscard]] JS::NonnullGCPtr<Headers> headers() const;
[[nodiscard]] Bindings::RequestDestination destination() const;
[[nodiscard]] DeprecatedString referrer() const;
[[nodiscard]] WebIDL::ExceptionOr<String> referrer() const;
[[nodiscard]] Bindings::ReferrerPolicy referrer_policy() const;
[[nodiscard]] Bindings::RequestMode mode() const;
[[nodiscard]] Bindings::RequestCredentials credentials() const;
[[nodiscard]] Bindings::RequestCache cache() const;
[[nodiscard]] Bindings::RequestRedirect redirect() const;
[[nodiscard]] DeprecatedString integrity() const;
[[nodiscard]] WebIDL::ExceptionOr<String> integrity() const;
[[nodiscard]] bool keepalive() const;
[[nodiscard]] bool is_reload_navigation() const;
[[nodiscard]] bool is_history_navigation() const;

View file

@ -6,7 +6,7 @@
typedef (Request or USVString) RequestInfo;
// https://fetch.spec.whatwg.org/#request
[Exposed=(Window,Worker)]
[Exposed=(Window,Worker), UseNewAKString]
interface Request {
constructor(RequestInfo input, optional RequestInit init = {});

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -168,7 +168,7 @@ JS::NonnullGCPtr<Response> Response::error(JS::VM& vm)
}
// https://fetch.spec.whatwg.org/#dom-response-redirect
WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::redirect(JS::VM& vm, DeprecatedString const& url, u16 status)
WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::redirect(JS::VM& vm, String const& url, u16 status)
{
auto& realm = *vm.current_realm();
@ -236,12 +236,14 @@ Bindings::ResponseType Response::type() const
}
// https://fetch.spec.whatwg.org/#dom-response-url
DeprecatedString Response::url() const
WebIDL::ExceptionOr<String> Response::url() const
{
auto& vm = this->vm();
// The url getter steps are to return the empty string if thiss responses URL is null; otherwise thiss responses URL, serialized with exclude fragment set to true.
return !m_response->url().has_value()
? DeprecatedString::empty()
: m_response->url()->serialize(AK::URL::ExcludeFragment::Yes);
? String {}
: TRY_OR_THROW_OOM(vm, String::from_deprecated_string(m_response->url()->serialize(AK::URL::ExcludeFragment::Yes)));
}
// https://fetch.spec.whatwg.org/#dom-response-redirected
@ -266,10 +268,12 @@ bool Response::ok() const
}
// https://fetch.spec.whatwg.org/#dom-response-statustext
DeprecatedString Response::status_text() const
WebIDL::ExceptionOr<String> Response::status_text() const
{
auto& vm = this->vm();
// The statusText getter steps are to return thiss responses status message.
return DeprecatedString::copy(m_response->status_message());
return TRY_OR_THROW_OOM(vm, String::from_utf8(m_response->status_message()));
}
// https://fetch.spec.whatwg.org/#dom-response-headers

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -21,7 +21,7 @@ namespace Web::Fetch {
// https://fetch.spec.whatwg.org/#responseinit
struct ResponseInit {
u16 status;
DeprecatedString status_text;
String status_text;
Optional<HeadersInit> headers;
};
@ -46,14 +46,14 @@ public:
// JS API functions
[[nodiscard]] static JS::NonnullGCPtr<Response> error(JS::VM&);
[[nodiscard]] static WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> redirect(JS::VM&, DeprecatedString const& url, u16 status);
[[nodiscard]] static WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> json(JS::VM&, JS::Value data, ResponseInit const& init = {});
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> redirect(JS::VM&, String const& url, u16 status);
static WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> json(JS::VM&, JS::Value data, ResponseInit const& init = {});
[[nodiscard]] Bindings::ResponseType type() const;
[[nodiscard]] DeprecatedString url() const;
[[nodiscard]] WebIDL::ExceptionOr<String> url() const;
[[nodiscard]] bool redirected() const;
[[nodiscard]] u16 status() const;
[[nodiscard]] bool ok() const;
[[nodiscard]] DeprecatedString status_text() const;
[[nodiscard]] WebIDL::ExceptionOr<String> status_text() const;
[[nodiscard]] JS::NonnullGCPtr<Headers> headers() const;
[[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> clone() const;

View file

@ -2,7 +2,7 @@
#import <Fetch/BodyInit.idl>
#import <Fetch/Headers.idl>
[Exposed=(Window,Worker)]
[Exposed=(Window,Worker), UseNewAKString]
interface Response {
constructor(optional BodyInit? body = null, optional ResponseInit init = {});