diff options
author | Linus Groh <mail@linusgroh.de> | 2023-03-02 22:26:12 +0000 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-03-03 09:25:34 +0000 |
commit | 7f9ddcf4202b8870be5c1ec865632137d02b06f9 (patch) | |
tree | b2c76d1cf0de86f3937d0ecdef6404e94daf5ab3 /Userland | |
parent | f561940e548f874a093cec484349d38f7acb76dd (diff) | |
download | serenity-7f9ddcf4202b8870be5c1ec865632137d02b06f9.zip |
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.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibWeb/Bindings/FetchMethod.cpp | 81 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Fetch/Body.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Fetch/BodyInit.cpp | 8 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Fetch/BodyInit.h | 6 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Fetch/Headers.cpp | 41 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Fetch/Headers.h | 22 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Fetch/Headers.idl | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Fetch/Request.cpp | 45 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Fetch/Request.h | 18 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Fetch/Request.idl | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Fetch/Response.cpp | 18 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Fetch/Response.h | 12 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Fetch/Response.idl | 2 |
13 files changed, 131 insertions, 130 deletions
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 <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()) 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 <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(); } 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 <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 {}; 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 <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); 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<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 this’s 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 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<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 this’s 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 « 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<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))); 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 <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); - - using ForEachCallback = Function<JS::ThrowCompletionOr<void>(DeprecatedString const&, DeprecatedString const&)>; + 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>(String const&, String const&)>; JS::ThrowCompletionOr<void> 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<sequence<ByteString>> or record<ByteString, ByteString>) 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 <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 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<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 request’s 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 this’s 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 this’s 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 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<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 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<String> 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<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 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<String> { // 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<String> 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 <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; 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 <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 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<String> 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 <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; 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 <Fetch/BodyInit.idl> #import <Fetch/Headers.idl> -[Exposed=(Window,Worker)] +[Exposed=(Window,Worker), UseNewAKString] interface Response { constructor(optional BodyInit? body = null, optional ResponseInit init = {}); |