From 43973f2d0a32aedf4405f246b5f2806f61016384 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sat, 5 Oct 2024 16:33:30 +1300 Subject: [PATCH] LibURL: Define Origin methods depending on URL::Parser out of line This is also in an effort towards resolving a future circular dependency between Origin.h and URL.h --- Userland/Libraries/LibURL/CMakeLists.txt | 3 +- Userland/Libraries/LibURL/Origin.cpp | 50 +++++++++++++++++++ Userland/Libraries/LibURL/Origin.h | 36 ++----------- Userland/Libraries/LibWeb/DOM/Document.cpp | 1 + .../Libraries/LibWeb/HTML/NavigatorBeacon.cpp | 1 + 5 files changed, 58 insertions(+), 33 deletions(-) create mode 100644 Userland/Libraries/LibURL/Origin.cpp diff --git a/Userland/Libraries/LibURL/CMakeLists.txt b/Userland/Libraries/LibURL/CMakeLists.txt index 4cd8e745910..b40b60d5368 100644 --- a/Userland/Libraries/LibURL/CMakeLists.txt +++ b/Userland/Libraries/LibURL/CMakeLists.txt @@ -1,6 +1,7 @@ set(SOURCES - URL.cpp + Origin.cpp Parser.cpp + URL.cpp ) serenity_lib(LibURL url) diff --git a/Userland/Libraries/LibURL/Origin.cpp b/Userland/Libraries/LibURL/Origin.cpp new file mode 100644 index 00000000000..8608dad5073 --- /dev/null +++ b/Userland/Libraries/LibURL/Origin.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2024, Shannon Booth + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +namespace URL { + +// https://html.spec.whatwg.org/multipage/origin.html#ascii-serialisation-of-an-origin +ByteString Origin::serialize() const +{ + // 1. If origin is an opaque origin, then return "null" + if (is_opaque()) + return "null"; + + // 2. Otherwise, let result be origin's scheme. + StringBuilder result; + result.append(scheme()); + + // 3. Append "://" to result. + 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()); + + // 5. If origin's port is non-null, append a U+003A COLON character (:), and origin's port, serialized, to result. + if (port() != 0) { + result.append(':'); + result.append(ByteString::number(port())); + } + // 6. Return result + return result.to_byte_string(); +} + +} + +namespace AK { + +unsigned Traits::hash(URL::Origin const& origin) +{ + auto hash_without_host = pair_int_hash(origin.scheme().hash(), origin.port()); + if (origin.host().has()) + return hash_without_host; + return pair_int_hash(hash_without_host, URL::Parser::serialize_host(origin.host()).release_value_but_fixme_should_propagate_errors().hash()); +} + +} // namespace AK diff --git a/Userland/Libraries/LibURL/Origin.h b/Userland/Libraries/LibURL/Origin.h index 0e310c98742..5811fbe9c77 100644 --- a/Userland/Libraries/LibURL/Origin.h +++ b/Userland/Libraries/LibURL/Origin.h @@ -9,7 +9,6 @@ #include #include -#include namespace URL { @@ -72,30 +71,7 @@ public: } // https://html.spec.whatwg.org/multipage/origin.html#ascii-serialisation-of-an-origin - ByteString serialize() const - { - // 1. If origin is an opaque origin, then return "null" - if (is_opaque()) - return "null"; - - // 2. Otherwise, let result be origin's scheme. - StringBuilder result; - result.append(scheme()); - - // 3. Append "://" to result. - 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()); - - // 5. If origin's port is non-null, append a U+003A COLON character (:), and origin's port, serialized, to result. - if (port() != 0) { - result.append(':'); - result.append(ByteString::number(port())); - } - // 6. Return result - return result.to_byte_string(); - } + ByteString serialize() const; // https://html.spec.whatwg.org/multipage/origin.html#concept-origin-effective-domain Optional effective_domain() const @@ -121,14 +97,10 @@ private: } namespace AK { + template<> struct Traits : public DefaultTraits { - static unsigned hash(URL::Origin const& origin) - { - auto hash_without_host = pair_int_hash(origin.scheme().hash(), origin.port()); - if (origin.host().has()) - return hash_without_host; - return pair_int_hash(hash_without_host, URL::Parser::serialize_host(origin.host()).release_value_but_fixme_should_propagate_errors().hash()); - } + static unsigned hash(URL::Origin const& origin); }; + } // namespace AK diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 90c65742d0b..d2f42b8d18e 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include diff --git a/Userland/Libraries/LibWeb/HTML/NavigatorBeacon.cpp b/Userland/Libraries/LibWeb/HTML/NavigatorBeacon.cpp index dac0ab41fc1..a34fbfbb5d2 100644 --- a/Userland/Libraries/LibWeb/HTML/NavigatorBeacon.cpp +++ b/Userland/Libraries/LibWeb/HTML/NavigatorBeacon.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include #include