From 7f9ddcf4202b8870be5c1ec865632137d02b06f9 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Thu, 2 Mar 2023 22:26:12 +0000 Subject: 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. --- Userland/Libraries/LibWeb/Bindings/FetchMethod.cpp | 81 ++++++++++------------ Userland/Libraries/LibWeb/Fetch/Body.cpp | 4 +- Userland/Libraries/LibWeb/Fetch/BodyInit.cpp | 8 +-- Userland/Libraries/LibWeb/Fetch/BodyInit.h | 6 +- Userland/Libraries/LibWeb/Fetch/Headers.cpp | 41 ++++++----- Userland/Libraries/LibWeb/Fetch/Headers.h | 22 +++--- Userland/Libraries/LibWeb/Fetch/Headers.idl | 2 +- Userland/Libraries/LibWeb/Fetch/Request.cpp | 45 +++++++----- Userland/Libraries/LibWeb/Fetch/Request.h | 18 ++--- Userland/Libraries/LibWeb/Fetch/Request.idl | 2 +- Userland/Libraries/LibWeb/Fetch/Response.cpp | 18 +++-- Userland/Libraries/LibWeb/Fetch/Response.h | 12 ++-- Userland/Libraries/LibWeb/Fetch/Response.idl | 2 +- 13 files changed, 131 insertions(+), 130 deletions(-) (limited to 'Userland') diff --git a/Userland/Libraries/LibWeb/Bindings/FetchMethod.cpp b/Userland/Libraries/LibWeb/Bindings/FetchMethod.cpp index 40a9b6de9b..ec57e8007e 100644 --- a/Userland/Libraries/LibWeb/Bindings/FetchMethod.cpp +++ b/Userland/Libraries/LibWeb/Bindings/FetchMethod.cpp @@ -24,7 +24,7 @@ // ```idl // #import // #import -// [Exposed=Window] +// [Exposed=Window, UseNewAKString] // interface Dummy { // static Promise fetch(RequestInfo input, optional RequestInit init = {}); // }; @@ -55,7 +55,7 @@ JS::ThrowCompletionOr fetch(JS::VM& vm) if (vm.argument_count() < 1) return vm.throw_completion(JS::ErrorType::BadArgCountOne, "fetch"); auto arg0 = vm.argument(0); - auto arg0_to_variant = [&vm, &realm](JS::Value arg0) -> JS::ThrowCompletionOr, DeprecatedString>> { + auto arg0_to_variant = [&vm, &realm](JS::Value arg0) -> JS::ThrowCompletionOr, String>> { // These might be unused. (void)vm; (void)realm; @@ -66,9 +66,9 @@ JS::ThrowCompletionOr fetch(JS::VM& vm) return JS::make_handle(static_cast(arg0_object)); } } - return TRY(arg0.to_deprecated_string(vm)); + return TRY(arg0.to_string(vm)); }; - Variant, DeprecatedString> input = TRY(arg0_to_variant(arg0)); + Variant, String> input = TRY(arg0_to_variant(arg0)); auto arg1 = vm.argument(1); if (!arg1.is_nullish() && !arg1.is_object()) return vm.throw_completion(JS::ErrorType::NotAnObjectOfType, "RequestInit"); @@ -77,7 +77,7 @@ JS::ThrowCompletionOr 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, JS::Handle, JS::Handle, JS::Handle, DeprecatedString>> { + auto body_property_value_to_variant = [&vm, &realm](JS::Value body_property_value) -> JS::ThrowCompletionOr, JS::Handle, JS::Handle, JS::Handle, String>> { // These might be unused. (void)vm; (void)realm; @@ -94,9 +94,9 @@ JS::ThrowCompletionOr fetch(JS::VM& vm) if (is(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, JS::Handle, JS::Handle, JS::Handle, DeprecatedString>> body_value; + Optional, JS::Handle, JS::Handle, JS::Handle, 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 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>, OrderedHashMap>> { + auto headers_property_value_to_variant = [&vm, &realm](JS::Value headers_property_value) -> JS::ThrowCompletionOr>, OrderedHashMap>> { // These might be unused. (void)vm; (void)realm; @@ -170,7 +170,7 @@ JS::ThrowCompletionOr 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> headers_value; + Vector> headers_value; for (;;) { auto* next1 = TRY(JS::iterator_step(vm, iterator1)); if (!next1) @@ -182,17 +182,15 @@ JS::ThrowCompletionOr fetch(JS::VM& vm) if (!iterator_method1) return vm.throw_completion(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 sequence_item1; + Vector 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 fetch(JS::VM& vm) } return headers_value; } - OrderedHashMap record_union_type; + OrderedHashMap 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 fetch(JS::VM& vm) } return vm.throw_completion("No union types matched"sv); }; - Optional>, OrderedHashMap>> headers_value; + Optional>, OrderedHashMap>> 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 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 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 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 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 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 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()) diff --git a/Userland/Libraries/LibWeb/Fetch/Body.cpp b/Userland/Libraries/LibWeb/Fetch/Body.cpp index 3edc293bf0..8290cf954b 100644 --- a/Userland/Libraries/LibWeb/Fetch/Body.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Body.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Linus Groh + * Copyright (c) 2022-2023, Linus Groh * * SPDX-License-Identifier: BSD-2-Clause */ @@ -135,7 +135,7 @@ WebIDL::ExceptionOr 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(); } diff --git a/Userland/Libraries/LibWeb/Fetch/BodyInit.cpp b/Userland/Libraries/LibWeb/Fetch/BodyInit.cpp index fa60c917bb..8b94dd38bc 100644 --- a/Userland/Libraries/LibWeb/Fetch/BodyInit.cpp +++ b/Userland/Libraries/LibWeb/Fetch/BodyInit.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Linus Groh + * Copyright (c) 2022-2023, Linus Groh * Copyright (c) 2022, Kenneth Myhra * * SPDX-License-Identifier: BSD-2-Clause @@ -96,10 +96,10 @@ WebIDL::ExceptionOr 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 { - // NOTE: AK::DeprecatedString is always UTF-8. + [&](String const& scalar_value_string) -> WebIDL::ExceptionOr { + // 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 {}; diff --git a/Userland/Libraries/LibWeb/Fetch/BodyInit.h b/Userland/Libraries/LibWeb/Fetch/BodyInit.h index 27d1648584..f22deb9c7b 100644 --- a/Userland/Libraries/LibWeb/Fetch/BodyInit.h +++ b/Userland/Libraries/LibWeb/Fetch/BodyInit.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Linus Groh + * Copyright (c) 2022-2023, Linus Groh * * SPDX-License-Identifier: BSD-2-Clause */ @@ -14,9 +14,9 @@ namespace Web::Fetch { // https://fetch.spec.whatwg.org/#bodyinit -using BodyInit = Variant, JS::Handle, JS::Handle, JS::Handle, DeprecatedString>; +using BodyInit = Variant, JS::Handle, JS::Handle, JS::Handle, String>; -using BodyInitOrReadableBytes = Variant, JS::Handle, JS::Handle, JS::Handle, DeprecatedString, ReadonlyBytes>; +using BodyInitOrReadableBytes = Variant, JS::Handle, JS::Handle, JS::Handle, String, ReadonlyBytes>; WebIDL::ExceptionOr safely_extract_body(JS::Realm&, BodyInitOrReadableBytes const&); WebIDL::ExceptionOr extract_body(JS::Realm&, BodyInitOrReadableBytes const&, bool keepalive = false); diff --git a/Userland/Libraries/LibWeb/Fetch/Headers.cpp b/Userland/Libraries/LibWeb/Fetch/Headers.cpp index 7aae86b938..04129a906c 100644 --- a/Userland/Libraries/LibWeb/Fetch/Headers.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Headers.cpp @@ -52,7 +52,7 @@ void Headers::visit_edges(JS::Cell::Visitor& visitor) } // https://fetch.spec.whatwg.org/#dom-headers-append -WebIDL::ExceptionOr Headers::append(DeprecatedString const& name_string, DeprecatedString const& value_string) +WebIDL::ExceptionOr Headers::append(String const& name_string, String const& value_string) { auto& vm = this->vm(); @@ -66,15 +66,15 @@ WebIDL::ExceptionOr Headers::append(DeprecatedString const& name_string, D } // https://fetch.spec.whatwg.org/#dom-headers-delete -WebIDL::ExceptionOr Headers::delete_(DeprecatedString const& name_string) +WebIDL::ExceptionOr 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 Headers::delete_(DeprecatedString const& name_string) } // https://fetch.spec.whatwg.org/#dom-headers-get -WebIDL::ExceptionOr Headers::get(DeprecatedString const& name_string) +WebIDL::ExceptionOr> 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 Headers::get(DeprecatedString const& name_ return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header name"sv }; // 2. Return the result of getting name from this’s header list. - auto byte_buffer = TRY_OR_THROW_OOM(vm(), m_header_list->get(name)); - // FIXME: Teach BindingsGenerator about Optional - 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 {}; } // https://fetch.spec.whatwg.org/#dom-headers-getsetcookie -WebIDL::ExceptionOr> Headers::get_set_cookie() +WebIDL::ExceptionOr> Headers::get_set_cookie() { // The getSetCookie() method steps are: auto& vm = this->vm(); - auto values = Vector {}; + auto values = Vector {}; // 1. If this’s header list does not contain `Set-Cookie`, then return « ». if (!m_header_list->contains("Set-Cookie"sv.bytes())) @@ -127,13 +127,13 @@ WebIDL::ExceptionOr> 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 Headers::has(DeprecatedString const& name_string) +WebIDL::ExceptionOr Headers::has(String const& name_string) { // The has(name) method steps are: auto name = name_string.bytes(); @@ -147,7 +147,7 @@ WebIDL::ExceptionOr Headers::has(DeprecatedString const& name_string) } // https://fetch.spec.whatwg.org/#dom-headers-set -WebIDL::ExceptionOr Headers::set(DeprecatedString const& name_string, DeprecatedString const& value_string) +WebIDL::ExceptionOr Headers::set(String const& name_string, String const& value_string) { auto& realm = this->realm(); auto& vm = realm.vm(); @@ -185,12 +185,11 @@ WebIDL::ExceptionOr Headers::set(DeprecatedString const& name_string, Depr // https://webidl.spec.whatwg.org/#es-iterable, Step 4 JS::ThrowCompletionOr 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 this’s header list. auto value_pairs_to_iterate_over = [&]() -> JS::ThrowCompletionOr> { - auto headers_or_error = m_header_list->sort_and_combine(); - if (headers_or_error.is_error()) - return vm().throw_completion(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 Headers::for_each(ForEachCallback callback) auto const& pair = pairs[i]; // 2. Invoke idlCallback with « pair’s value, pair’s 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 idlObject’s 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 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> const& object) -> WebIDL::ExceptionOr { + [&](Vector> const& object) -> WebIDL::ExceptionOr { 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 const& object) -> WebIDL::ExceptionOr { + [&](OrderedHashMap const& object) -> WebIDL::ExceptionOr { 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))); diff --git a/Userland/Libraries/LibWeb/Fetch/Headers.h b/Userland/Libraries/LibWeb/Fetch/Headers.h index c366b5fcbf..559fe0c3e8 100644 --- a/Userland/Libraries/LibWeb/Fetch/Headers.h +++ b/Userland/Libraries/LibWeb/Fetch/Headers.h @@ -6,8 +6,8 @@ #pragma once -#include #include +#include #include #include #include @@ -18,7 +18,7 @@ namespace Web::Fetch { -using HeadersInit = Variant>, OrderedHashMap>; +using HeadersInit = Variant>, OrderedHashMap>; // 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 fill(HeadersInit const&); + WebIDL::ExceptionOr append(Infrastructure::Header); // JS API functions - WebIDL::ExceptionOr append(Infrastructure::Header); - WebIDL::ExceptionOr append(DeprecatedString const& name, DeprecatedString const& value); - WebIDL::ExceptionOr delete_(DeprecatedString const& name); - WebIDL::ExceptionOr get(DeprecatedString const& name); - WebIDL::ExceptionOr> get_set_cookie(); - WebIDL::ExceptionOr has(DeprecatedString const& name); - WebIDL::ExceptionOr set(DeprecatedString const& name, DeprecatedString const& value); - - using ForEachCallback = Function(DeprecatedString const&, DeprecatedString const&)>; + WebIDL::ExceptionOr append(String const& name, String const& value); + WebIDL::ExceptionOr delete_(String const& name); + WebIDL::ExceptionOr> get(String const& name); + WebIDL::ExceptionOr> get_set_cookie(); + WebIDL::ExceptionOr has(String const& name); + WebIDL::ExceptionOr set(String const& name, String const& value); + + using ForEachCallback = Function(String const&, String const&)>; JS::ThrowCompletionOr for_each(ForEachCallback); private: diff --git a/Userland/Libraries/LibWeb/Fetch/Headers.idl b/Userland/Libraries/LibWeb/Fetch/Headers.idl index a495339dd5..43dedffcad 100644 --- a/Userland/Libraries/LibWeb/Fetch/Headers.idl +++ b/Userland/Libraries/LibWeb/Fetch/Headers.idl @@ -1,6 +1,6 @@ typedef (sequence> or record) HeadersInit; -[Exposed=(Window,Worker)] +[Exposed=(Window,Worker), UseNewAKString] interface Headers { constructor(optional HeadersInit init); diff --git a/Userland/Libraries/LibWeb/Fetch/Request.cpp b/Userland/Libraries/LibWeb/Fetch/Request.cpp index a746461137..81d9d218a9 100644 --- a/Userland/Libraries/LibWeb/Fetch/Request.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Request.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Linus Groh + * Copyright (c) 2022-2023, Linus Groh * * SPDX-License-Identifier: BSD-2-Clause */ @@ -119,9 +119,9 @@ WebIDL::ExceptionOr> Request::construct_impl(JS::Realm DOM::AbortSignal const* input_signal = nullptr; // 5. If input is a string, then: - if (input.has()) { + if (input.has()) { // 1. Let parsedURL be the result of parsing input with baseURL. - auto parsed_url = URLParser::parse(input.get(), &base_url); + auto parsed_url = URLParser::parse(input.get(), &base_url); // 2. If parsedURL is failure, then throw a TypeError. if (!parsed_url.is_valid()) @@ -355,7 +355,7 @@ WebIDL::ExceptionOr> Request::construct_impl(JS::Realm // 23. If init["integrity"] exists, then set request’s 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 request’s keepalive to it. if (init.keepalive.has_value()) @@ -373,7 +373,7 @@ WebIDL::ExceptionOr> 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 request’s method to method. request->set_method(MUST(ByteBuffer::copy(method.bytes()))); @@ -424,7 +424,7 @@ WebIDL::ExceptionOr> Request::construct_impl(JS::Realm // 4. If headers is a Headers object, then for each header of its header list, append header to this’s headers. if (auto* header_list = headers.get_pointer>()) { 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 this’s headers with headers. else { @@ -457,7 +457,7 @@ WebIDL::ExceptionOr> Request::construct_impl(JS::Realm // 4. If type is non-null and this’s headers’s header list does not contain `Content-Type`, then append (`Content-Type`, type) to this’s 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> Request::construct_impl(JS::Realm } // https://fetch.spec.whatwg.org/#dom-request-method -DeprecatedString Request::method() const +WebIDL::ExceptionOr Request::method() const { + auto& vm = this->vm(); + // The method getter steps are to return this’s request’s 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 Request::url() const { + auto& vm = this->vm(); + // The url getter steps are to return this’s request’s 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 Request::referrer() const { + auto& vm = this->vm(); return m_request->referrer().visit( - [&](Infrastructure::Request::Referrer const& referrer) { + [&](Infrastructure::Request::Referrer const& referrer) -> WebIDL::ExceptionOr { switch (referrer) { // 1. If this’s request’s referrer is "no-referrer", then return the empty string. case Infrastructure::Request::Referrer::NoReferrer: - return DeprecatedString::empty(); + return String {}; // 2. If this’s request’s 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 { // 3. Return this’s request’s 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 Request::integrity() const { + auto& vm = this->vm(); + // The integrity getter steps are to return this’s request’s 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 diff --git a/Userland/Libraries/LibWeb/Fetch/Request.h b/Userland/Libraries/LibWeb/Fetch/Request.h index 55c49611a4..ee9d0709e0 100644 --- a/Userland/Libraries/LibWeb/Fetch/Request.h +++ b/Userland/Libraries/LibWeb/Fetch/Request.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Linus Groh + * Copyright (c) 2022-2023, Linus Groh * * SPDX-License-Identifier: BSD-2-Clause */ @@ -19,20 +19,20 @@ namespace Web::Fetch { // https://fetch.spec.whatwg.org/#requestinfo -using RequestInfo = Variant, DeprecatedString>; +using RequestInfo = Variant, String>; // https://fetch.spec.whatwg.org/#requestinit struct RequestInit { - Optional method; + Optional method; Optional headers; Optional> body; - Optional referrer; + Optional referrer; Optional referrer_policy; Optional mode; Optional credentials; Optional cache; Optional redirect; - Optional integrity; + Optional integrity; Optional keepalive; Optional> signal; Optional duplex; @@ -78,17 +78,17 @@ public: [[nodiscard]] JS::NonnullGCPtr request() const { return m_request; } // JS API functions - [[nodiscard]] DeprecatedString method() const; - [[nodiscard]] DeprecatedString url() const; + [[nodiscard]] WebIDL::ExceptionOr method() const; + [[nodiscard]] WebIDL::ExceptionOr url() const; [[nodiscard]] JS::NonnullGCPtr headers() const; [[nodiscard]] Bindings::RequestDestination destination() const; - [[nodiscard]] DeprecatedString referrer() const; + [[nodiscard]] WebIDL::ExceptionOr 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 integrity() const; [[nodiscard]] bool keepalive() const; [[nodiscard]] bool is_reload_navigation() const; [[nodiscard]] bool is_history_navigation() const; diff --git a/Userland/Libraries/LibWeb/Fetch/Request.idl b/Userland/Libraries/LibWeb/Fetch/Request.idl index 232d18b394..aae8bc4299 100644 --- a/Userland/Libraries/LibWeb/Fetch/Request.idl +++ b/Userland/Libraries/LibWeb/Fetch/Request.idl @@ -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 = {}); diff --git a/Userland/Libraries/LibWeb/Fetch/Response.cpp b/Userland/Libraries/LibWeb/Fetch/Response.cpp index 5be3f25759..54e7cf0e79 100644 --- a/Userland/Libraries/LibWeb/Fetch/Response.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Response.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Linus Groh + * Copyright (c) 2022-2023, Linus Groh * * SPDX-License-Identifier: BSD-2-Clause */ @@ -168,7 +168,7 @@ JS::NonnullGCPtr Response::error(JS::VM& vm) } // https://fetch.spec.whatwg.org/#dom-response-redirect -WebIDL::ExceptionOr> Response::redirect(JS::VM& vm, DeprecatedString const& url, u16 status) +WebIDL::ExceptionOr> 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 Response::url() const { + auto& vm = this->vm(); + // The url getter steps are to return the empty string if this’s response’s URL is null; otherwise this’s response’s 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 Response::status_text() const { + auto& vm = this->vm(); + // The statusText getter steps are to return this’s response’s 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 diff --git a/Userland/Libraries/LibWeb/Fetch/Response.h b/Userland/Libraries/LibWeb/Fetch/Response.h index 516a680c6c..4ae12d7624 100644 --- a/Userland/Libraries/LibWeb/Fetch/Response.h +++ b/Userland/Libraries/LibWeb/Fetch/Response.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Linus Groh + * Copyright (c) 2022-2023, Linus Groh * * 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 headers; }; @@ -46,14 +46,14 @@ public: // JS API functions [[nodiscard]] static JS::NonnullGCPtr error(JS::VM&); - [[nodiscard]] static WebIDL::ExceptionOr> redirect(JS::VM&, DeprecatedString const& url, u16 status); - [[nodiscard]] static WebIDL::ExceptionOr> json(JS::VM&, JS::Value data, ResponseInit const& init = {}); + static WebIDL::ExceptionOr> redirect(JS::VM&, String const& url, u16 status); + static WebIDL::ExceptionOr> json(JS::VM&, JS::Value data, ResponseInit const& init = {}); [[nodiscard]] Bindings::ResponseType type() const; - [[nodiscard]] DeprecatedString url() const; + [[nodiscard]] WebIDL::ExceptionOr url() const; [[nodiscard]] bool redirected() const; [[nodiscard]] u16 status() const; [[nodiscard]] bool ok() const; - [[nodiscard]] DeprecatedString status_text() const; + [[nodiscard]] WebIDL::ExceptionOr status_text() const; [[nodiscard]] JS::NonnullGCPtr headers() const; [[nodiscard]] WebIDL::ExceptionOr> clone() const; diff --git a/Userland/Libraries/LibWeb/Fetch/Response.idl b/Userland/Libraries/LibWeb/Fetch/Response.idl index 467f7f18bb..e73be7df90 100644 --- a/Userland/Libraries/LibWeb/Fetch/Response.idl +++ b/Userland/Libraries/LibWeb/Fetch/Response.idl @@ -2,7 +2,7 @@ #import #import -[Exposed=(Window,Worker)] +[Exposed=(Window,Worker), UseNewAKString] interface Response { constructor(optional BodyInit? body = null, optional ResponseInit init = {}); -- cgit v1.2.3