1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 21:07:36 +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

@ -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 = {});