From ee14a5ea5543fff47758f3793cb1b15bd2ee39dc Mon Sep 17 00:00:00 2001 From: attila Date: Sun, 17 Sep 2023 00:16:22 +0200 Subject: [PATCH] URL: Add getOrigin() --- modules/juce_core/network/juce_URL.cpp | 12 ++++++++++++ modules/juce_core/network/juce_URL.h | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/modules/juce_core/network/juce_URL.cpp b/modules/juce_core/network/juce_URL.cpp index db505ed0e7..cd7275c852 100644 --- a/modules/juce_core/network/juce_URL.cpp +++ b/modules/juce_core/network/juce_URL.cpp @@ -448,6 +448,18 @@ int URL::getPort() const return colonPos > 0 ? url.substring (colonPos + 1).getIntValue() : 0; } +String URL::getOrigin() const +{ + const auto schemeAndDomain = getScheme() + "://" + getDomain(); + + const auto colonPos = url.indexOfChar (URLHelpers::findStartOfNetLocation (url), ':'); + + if (colonPos > 0) + return schemeAndDomain + ":" + String { getPort() }; + + return schemeAndDomain; +} + URL URL::withNewDomainAndPath (const String& newURL) const { URL u (*this); diff --git a/modules/juce_core/network/juce_URL.h b/modules/juce_core/network/juce_URL.h index bb9a4f21c2..421a054a28 100644 --- a/modules/juce_core/network/juce_URL.h +++ b/modules/juce_core/network/juce_URL.h @@ -154,6 +154,13 @@ public: */ int getPort() const; + /** Returns the origin of a resource reachable on this URL. + + In the context of cross-origin resource sharing (CORS) a script downloaded from this URL + would only be allowed to reach resources from the returned origin. + */ + String getOrigin() const; + /** Returns a new version of this URL with a different domain and path. e.g. if the URL is "http://www.xyz.com/foo?x=1" and you call this with