From c380d2cfdc1709407b77b1854517c47cbac3a137 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Thu, 13 Oct 2022 18:25:00 +0200 Subject: LibWeb: Move url_origin() to URL/URL.{cpp,h} --- Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp | 27 ++---------------- Userland/Libraries/LibWeb/URL/URL.cpp | 33 ++++++++++++++++++++++ Userland/Libraries/LibWeb/URL/URL.h | 2 ++ 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp b/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp index c713467031..05c070d5d2 100644 --- a/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp +++ b/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp @@ -25,6 +25,7 @@ #include #include #include +#include namespace Web::HTML { @@ -39,30 +40,6 @@ static bool url_matches_about_blank(AK::URL const& url) && url.host().is_null(); } -// https://url.spec.whatwg.org/#concept-url-origin -static HTML::Origin url_origin(AK::URL const& url) -{ - // FIXME: Move this whole function somewhere better. - - if (url.scheme() == "blob"sv) { - // FIXME: Implement - return HTML::Origin {}; - } - - if (url.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 HTML::Origin(url.scheme(), url.host(), url.port().value_or(0)); - } - - if (url.scheme() == "file"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 HTML::Origin(url.scheme(), String(), 0); - } - - return HTML::Origin {}; -} - // https://html.spec.whatwg.org/multipage/browsers.html#determining-the-origin HTML::Origin determine_the_origin(BrowsingContext const& browsing_context, Optional url, SandboxingFlagSet sandbox_flags, Optional invocation_origin) { @@ -88,7 +65,7 @@ HTML::Origin determine_the_origin(BrowsingContext const& browsing_context, Optio } // 5. Return url's origin. - return url_origin(*url); + return URL::url_origin(*url); } // https://html.spec.whatwg.org/multipage/browsers.html#creating-a-new-top-level-browsing-context diff --git a/Userland/Libraries/LibWeb/URL/URL.cpp b/Userland/Libraries/LibWeb/URL/URL.cpp index 8f742edb63..62f9210f26 100644 --- a/Userland/Libraries/LibWeb/URL/URL.cpp +++ b/Userland/Libraries/LibWeb/URL/URL.cpp @@ -307,4 +307,37 @@ void URL::set_hash(String const& hash) m_url = move(result_url); } +// https://url.spec.whatwg.org/#concept-url-origin +HTML::Origin url_origin(AK::URL const& url) +{ + // FIXME: We should probably have an extended version of AK::URL for LibWeb instead of standalone functions like this. + + // The origin of a URL url is the origin returned by running these steps, switching on url’s scheme: + // "blob" + if (url.scheme() == "blob"sv) { + // FIXME: Support 'blob://' URLs + return HTML::Origin {}; + } + + // "ftp" + // "http" + // "https" + // "ws" + // "wss" + if (url.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 HTML::Origin(url.scheme(), url.host(), url.port().value_or(0)); + } + + // "file" + if (url.scheme() == "file"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 HTML::Origin(url.scheme(), String(), 0); + } + + // Return a new opaque origin. + return HTML::Origin {}; +} + } diff --git a/Userland/Libraries/LibWeb/URL/URL.h b/Userland/Libraries/LibWeb/URL/URL.h index 015480d841..44d5add4fd 100644 --- a/Userland/Libraries/LibWeb/URL/URL.h +++ b/Userland/Libraries/LibWeb/URL/URL.h @@ -71,4 +71,6 @@ private: JS::NonnullGCPtr m_query; }; +HTML::Origin url_origin(AK::URL const&); + } -- cgit v1.2.3