From 0e20a6fdc5c703b10609a84e344288500fae6871 Mon Sep 17 00:00:00 2001 From: hogliux Date: Tue, 1 Nov 2022 16:56:18 +0100 Subject: [PATCH] URL: Add support for anchors in URLs --- modules/juce_core/network/juce_URL.cpp | 33 ++++++++++++++++++++++++-- modules/juce_core/network/juce_URL.h | 12 +++++++++- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/modules/juce_core/network/juce_URL.cpp b/modules/juce_core/network/juce_URL.cpp index ce9c503057..f213307746 100644 --- a/modules/juce_core/network/juce_URL.cpp +++ b/modules/juce_core/network/juce_URL.cpp @@ -178,7 +178,15 @@ URL::URL (File localFile) void URL::init() { - auto i = url.indexOfChar ('?'); + auto i = url.indexOfChar ('#'); + + if (i >= 0) + { + anchor = removeEscapeChars (url.substring (i + 1)); + url = url.upToFirstOccurrenceOf ("#", false, false); + } + + i = url.indexOfChar ('?'); if (i >= 0) { @@ -346,8 +354,21 @@ String URL::getSubPath (bool includeGetParameters) const String URL::getQueryString() const { + String result; + if (parameterNames.size() > 0) - return "?" + URLHelpers::getMangledParameters (*this); + result += "?" + URLHelpers::getMangledParameters (*this); + + if (anchor.isNotEmpty()) + result += getAnchorString(); + + return result; +} + +String URL::getAnchorString() const +{ + if (anchor.isNotEmpty()) + return "#" + URL::addEscapeChars (anchor, true); return {}; } @@ -861,6 +882,14 @@ URL URL::withParameters (const StringPairArray& parametersToAdd) const return u; } +URL URL::withAnchor (const String& anchorToAdd) const +{ + auto u = *this; + + u.anchor = anchorToAdd; + return u; +} + URL URL::withPOSTData (const String& newPostData) const { return withPOSTData (MemoryBlock (newPostData.toRawUTF8(), newPostData.getNumBytesAsUTF8())); diff --git a/modules/juce_core/network/juce_URL.h b/modules/juce_core/network/juce_URL.h index 1653ebfe3c..6a399448c9 100644 --- a/modules/juce_core/network/juce_URL.h +++ b/modules/juce_core/network/juce_URL.h @@ -100,6 +100,11 @@ public: */ String getQueryString() const; + /** If any anchor is set, returns URL-encoded anchor, including the "#" + prefix. + */ + String getAnchorString() const; + /** Returns the scheme of the URL. e.g. for "http://www.xyz.com/foobar", this will return "http" (it won't @@ -187,7 +192,7 @@ public: @see getParameterNames, getParameterValues */ [[nodiscard]] URL withParameter (const String& parameterName, - const String& parameterValue) const; + const String& parameterValue) const; /** Returns a copy of this URL, with a set of GET or POST parameters added. @@ -197,6 +202,10 @@ public: */ [[nodiscard]] URL withParameters (const StringPairArray& parametersToAdd) const; + /** Returns a copy of this URL, with an anchor added to the end of the URL. + */ + [[nodiscard]] URL withAnchor (const String& anchor) const; + /** Returns a copy of this URL, with a file-upload type parameter added to it. When performing a POST where one of your parameters is a binary file, this @@ -723,6 +732,7 @@ private: String url; MemoryBlock postData; StringArray parameterNames, parameterValues; + String anchor; ReferenceCountedArray filesToUpload;