From 2e64e0b8367b015b5c5c69705b74b78c59230f23 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 27 Nov 2024 16:18:42 +0000 Subject: [PATCH] LibURL: Migrate Origin scheme from ByteString to String --- Libraries/LibIPC/Decoder.cpp | 2 +- Libraries/LibIPC/Encoder.cpp | 2 +- Libraries/LibURL/Origin.cpp | 4 ++-- Libraries/LibURL/Origin.h | 9 +++------ Libraries/LibURL/URL.cpp | 4 ++-- Libraries/LibWeb/SecureContexts/AbstractOperations.cpp | 2 +- 6 files changed, 10 insertions(+), 13 deletions(-) diff --git a/Libraries/LibIPC/Decoder.cpp b/Libraries/LibIPC/Decoder.cpp index e32d7f7f762..3de7417fced 100644 --- a/Libraries/LibIPC/Decoder.cpp +++ b/Libraries/LibIPC/Decoder.cpp @@ -104,7 +104,7 @@ ErrorOr decode(Decoder& decoder) if (is_opaque) return URL::Origin {}; - auto scheme = TRY(decoder.decode()); + auto scheme = TRY(decoder.decode>()); auto host = TRY(decoder.decode()); auto port = TRY(decoder.decode>()); diff --git a/Libraries/LibIPC/Encoder.cpp b/Libraries/LibIPC/Encoder.cpp index 5ebae06f557..a83b9904cc3 100644 --- a/Libraries/LibIPC/Encoder.cpp +++ b/Libraries/LibIPC/Encoder.cpp @@ -123,7 +123,7 @@ ErrorOr encode(Encoder& encoder, URL::Origin const& origin) TRY(encoder.encode(true)); } else { TRY(encoder.encode(false)); - TRY(encoder.encode(origin.scheme())); + TRY(encoder.encode(origin.scheme())); TRY(encoder.encode(origin.host())); TRY(encoder.encode(origin.port())); } diff --git a/Libraries/LibURL/Origin.cpp b/Libraries/LibURL/Origin.cpp index c808b32b560..0042d7a7515 100644 --- a/Libraries/LibURL/Origin.cpp +++ b/Libraries/LibURL/Origin.cpp @@ -18,7 +18,7 @@ String Origin::serialize() const // 2. Otherwise, let result be origin's scheme. StringBuilder result; - result.append(scheme()); + result.append(scheme().value_or(String {})); // 3. Append "://" to result. result.append("://"sv); @@ -45,7 +45,7 @@ unsigned Traits::hash(URL::Origin const& origin) if (origin.is_opaque()) return 0; - unsigned hash = origin.scheme().hash(); + unsigned hash = origin.scheme().value_or(String {}).hash(); if (origin.port().has_value()) hash = pair_int_hash(hash, *origin.port()); diff --git a/Libraries/LibURL/Origin.h b/Libraries/LibURL/Origin.h index 6cba035293c..64ebe68604b 100644 --- a/Libraries/LibURL/Origin.h +++ b/Libraries/LibURL/Origin.h @@ -15,7 +15,7 @@ namespace URL { class Origin { public: Origin() = default; - Origin(Optional const& scheme, Host const& host, Optional port) + Origin(Optional const& scheme, Host const& host, Optional port) : m_state(State { .scheme = scheme, .host = host, @@ -27,10 +27,7 @@ public: // https://html.spec.whatwg.org/multipage/origin.html#concept-origin-opaque bool is_opaque() const { return !m_state.has_value(); } - StringView scheme() const - { - return m_state->scheme.map([](auto& str) { return str.view(); }).value_or(StringView {}); - } + Optional const& scheme() const { return m_state->scheme; } Host const& host() const { return m_state->host; } Optional port() const { return m_state->port; } @@ -97,7 +94,7 @@ public: private: struct State { - Optional scheme; + Optional scheme; Host host; Optional port; }; diff --git a/Libraries/LibURL/URL.cpp b/Libraries/LibURL/URL.cpp index 80d76ee57ac..b1035bc391d 100644 --- a/Libraries/LibURL/URL.cpp +++ b/Libraries/LibURL/URL.cpp @@ -392,7 +392,7 @@ Origin URL::origin() const // -> "wss" if (scheme().is_one_of("ftp"sv, "http"sv, "https"sv, "ws"sv, "wss"sv)) { // Return the tuple origin (url’s scheme, url’s host, url’s port, null). - return Origin(scheme().to_byte_string(), host().value(), port()); + return Origin(scheme(), host().value(), port()); } // -> "file" @@ -400,7 +400,7 @@ Origin URL::origin() const if (scheme() == "file"sv || scheme() == "resource"sv) { // Unfortunate as it is, this is left as an exercise to the reader. When in doubt, return a new opaque origin. // Note: We must return an origin with the `file://' protocol for `file://' iframes to work from `file://' pages. - return Origin(scheme().to_byte_string(), String {}, {}); + return Origin(scheme(), String {}, {}); } // -> Otherwise diff --git a/Libraries/LibWeb/SecureContexts/AbstractOperations.cpp b/Libraries/LibWeb/SecureContexts/AbstractOperations.cpp index 2293323dc51..9855e5f964c 100644 --- a/Libraries/LibWeb/SecureContexts/AbstractOperations.cpp +++ b/Libraries/LibWeb/SecureContexts/AbstractOperations.cpp @@ -24,7 +24,7 @@ Trustworthiness is_origin_potentially_trustworthy(URL::Origin const& origin) // 3. If origin’s scheme is either "https" or "wss", return "Potentially Trustworthy". // Note: This is meant to be analog to the a priori authenticated URL concept in [MIX]. - if (origin.scheme().is_one_of("https"sv, "wss"sv)) + if (auto& scheme = origin.scheme(); scheme.has_value() && scheme->is_one_of("https"sv, "wss"sv)) return Trustworthiness::PotentiallyTrustworthy; // 4. If origin’s host matches one of the CIDR notations 127.0.0.0/8 or ::1/128 [RFC4632], return "Potentially Trustworthy".