LibURL+LibWeb: Make URL serialization return a String

This can only ever fail from OOM, and will never by string containing
random byte sequences.
This commit is contained in:
Shannon Booth 2024-11-23 20:10:34 +13:00 committed by Andreas Kling
commit 8f6fe1de83
Notes: github-actions[bot] 2024-11-23 15:44:52 +00:00
15 changed files with 24 additions and 30 deletions

View file

@ -10,11 +10,11 @@
namespace URL {
// https://html.spec.whatwg.org/multipage/origin.html#ascii-serialisation-of-an-origin
ByteString Origin::serialize() const
String Origin::serialize() const
{
// 1. If origin is an opaque origin, then return "null"
if (is_opaque())
return "null";
return "null"_string;
// 2. Otherwise, let result be origin's scheme.
StringBuilder result;
@ -24,15 +24,16 @@ ByteString Origin::serialize() const
result.append("://"sv);
// 4. Append origin's host, serialized, to result.
result.append(Parser::serialize_host(host()).release_value_but_fixme_should_propagate_errors().to_byte_string());
result.append(MUST(Parser::serialize_host(host())));
// 5. If origin's port is non-null, append a U+003A COLON character (:), and origin's port, serialized, to result.
if (port().has_value()) {
result.append(':');
result.append(ByteString::number(*port()));
result.append(String::number(*port()));
}
// 6. Return result
return result.to_byte_string();
return result.to_string_without_validation();
}
}

View file

@ -71,7 +71,7 @@ public:
}
// https://html.spec.whatwg.org/multipage/origin.html#ascii-serialisation-of-an-origin
ByteString serialize() const;
String serialize() const;
// https://html.spec.whatwg.org/multipage/origin.html#concept-origin-effective-domain
Optional<Host> effective_domain() const

View file

@ -213,12 +213,10 @@ WebIDL::ExceptionOr<void> DOMURL::set_href(String const& href)
}
// https://url.spec.whatwg.org/#dom-url-origin
WebIDL::ExceptionOr<String> DOMURL::origin() const
String DOMURL::origin() const
{
auto& vm = realm().vm();
// The origin getter steps are to return the serialization of thiss URLs origin. [HTML]
return TRY_OR_THROW_OOM(vm, String::from_byte_string(m_url.origin().serialize()));
return m_url.origin().serialize();
}
// https://url.spec.whatwg.org/#dom-url-protocol

View file

@ -35,7 +35,7 @@ public:
WebIDL::ExceptionOr<String> href() const;
WebIDL::ExceptionOr<void> set_href(String const&);
WebIDL::ExceptionOr<String> origin() const;
String origin() const;
WebIDL::ExceptionOr<String> protocol() const;
WebIDL::ExceptionOr<void> set_protocol(String const&);

View file

@ -194,7 +194,7 @@ String Request::serialize_origin() const
return "null"_string;
// 2. Return requests origin, serialized.
return MUST(String::from_byte_string(m_origin.get<URL::Origin>().serialize()));
return m_origin.get<URL::Origin>().serialize();
}
// https://fetch.spec.whatwg.org/#byte-serializing-a-request-origin

View file

@ -43,7 +43,7 @@ ErrorOr<String> generate_new_blob_url()
// 6. If serialized is "null", set it to an implementation-defined value.
if (serialized == "null"sv)
serialized = "ladybird"sv;
serialized = "ladybird"_string;
// 7. Append serialized to result.
TRY(result.try_append(serialized));

View file

@ -449,7 +449,7 @@ void EventSource::dispatch_the_event()
// the value of the event type buffer.
MessageEventInit init {};
init.data = JS::PrimitiveString::create(vm(), data_buffer);
init.origin = MUST(String::from_byte_string(m_url.origin().serialize()));
init.origin = m_url.origin().serialize();
init.last_event_id = last_event_id;
auto type = m_event_type.is_empty() ? HTML::EventNames::message : m_event_type;

View file

@ -52,7 +52,7 @@ String HTMLHyperlinkElementUtils::origin() const
return String {};
// 3. Return the serialization of this element's url's origin.
return MUST(String::from_byte_string(m_url->origin().serialize()));
return m_url->origin().serialize();
}
// https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-protocol

View file

@ -133,15 +133,13 @@ WebIDL::ExceptionOr<void> Location::set_href(String const& new_href)
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-location-origin
WebIDL::ExceptionOr<String> Location::origin() const
{
auto& vm = this->vm();
// 1. If this's relevant Document is non-null and its origin is not same origin-domain with the entry settings object's origin, then throw a "SecurityError" DOMException.
auto const relevant_document = this->relevant_document();
if (relevant_document && !relevant_document->origin().is_same_origin_domain(entry_settings_object().origin()))
return WebIDL::SecurityError::create(realm(), "Location's relevant document is not same origin-domain with the entry settings object's origin"_string);
// 2. Return the serialization of this's url's origin.
return TRY_OR_THROW_OOM(vm, String::from_byte_string(url().origin().serialize()));
return url().origin().serialize();
}
// https://html.spec.whatwg.org/multipage/history.html#dom-location-protocol

View file

@ -1077,7 +1077,7 @@ WebIDL::ExceptionOr<void> Window::window_post_message_steps(JS::Value message, W
// with the origin attribute initialized to origin and the source attribute initialized to source, and then return.
if (deserialize_record_or_error.is_exception()) {
MessageEventInit message_event_init {};
message_event_init.origin = MUST(String::from_byte_string(origin));
message_event_init.origin = origin;
message_event_init.source = GC::make_root(source);
auto message_error_event = MessageEvent::create(target_realm, EventNames::messageerror, message_event_init);
@ -1103,7 +1103,7 @@ WebIDL::ExceptionOr<void> Window::window_post_message_steps(JS::Value message, W
// the source attribute initialized to source, the data attribute initialized to messageClone, and the ports attribute
// initialized to newPorts.
MessageEventInit message_event_init {};
message_event_init.origin = MUST(String::from_byte_string(origin));
message_event_init.origin = origin;
message_event_init.source = GC::make_root(source);
message_event_init.data = message_clone;
message_event_init.ports = move(new_ports);

View file

@ -83,12 +83,10 @@ void WindowOrWorkerGlobalScopeMixin::finalize()
}
// https://html.spec.whatwg.org/multipage/webappapis.html#dom-origin
WebIDL::ExceptionOr<String> WindowOrWorkerGlobalScopeMixin::origin() const
String WindowOrWorkerGlobalScopeMixin::origin() const
{
auto& vm = this_impl().vm();
// The origin getter steps are to return this's relevant settings object's origin, serialized.
return TRY_OR_THROW_OOM(vm, String::from_byte_string(relevant_settings_object(this_impl()).origin().serialize()));
return relevant_settings_object(this_impl()).origin().serialize();
}
// https://html.spec.whatwg.org/multipage/webappapis.html#dom-issecurecontext

View file

@ -33,7 +33,7 @@ public:
virtual Bindings::PlatformObject const& this_impl() const = 0;
// JS API functions
WebIDL::ExceptionOr<String> origin() const;
String origin() const;
bool is_secure_context() const;
bool cross_origin_isolated() const;
GC::Ref<WebIDL::Promise> create_image_bitmap(ImageBitmapSource image, Optional<ImageBitmapOptions> options = {}) const;

View file

@ -22,11 +22,10 @@ WebIDL::ExceptionOr<String> WorkerLocation::href() const
}
// https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-origin
WebIDL::ExceptionOr<String> WorkerLocation::origin() const
String WorkerLocation::origin() const
{
auto& vm = realm().vm();
// The origin getter steps are to return the serialization of this's WorkerGlobalScope object's url's origin.
return TRY_OR_THROW_OOM(vm, String::from_byte_string(m_global_scope->url().origin().serialize()));
return m_global_scope->url().origin().serialize();
}
// https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-protocol

View file

@ -19,7 +19,7 @@ public:
virtual ~WorkerLocation() override;
WebIDL::ExceptionOr<String> href() const;
WebIDL::ExceptionOr<String> origin() const;
String origin() const;
WebIDL::ExceptionOr<String> protocol() const;
WebIDL::ExceptionOr<String> host() const;
WebIDL::ExceptionOr<String> hostname() const;

View file

@ -124,7 +124,7 @@ ErrorOr<void> WebSocket::establish_web_socket_connection(URL::URL& url_record, V
auto* window_or_worker = dynamic_cast<HTML::WindowOrWorkerGlobalScopeMixin*>(&client.global_object());
VERIFY(window_or_worker);
auto origin_string = MUST(window_or_worker->origin()).to_byte_string();
auto origin_string = window_or_worker->origin().to_byte_string();
Vector<ByteString> protcol_byte_strings;
for (auto const& protocol : protocols)