From 60f58be769710d82ab41f4bb3e3472da10540a8e Mon Sep 17 00:00:00 2001 From: James Hurst Date: Mon, 15 Apr 2019 12:24:16 +0100 Subject: [PATCH] Added a method to return query parameters as a URL encoded string, and to optionally include these in getSubPath --- modules/juce_core/network/juce_URL.cpp | 22 +++++++++++++++++----- modules/juce_core/network/juce_URL.h | 11 ++++++++++- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/modules/juce_core/network/juce_URL.cpp b/modules/juce_core/network/juce_URL.cpp index a130bae3f1..f68180f162 100644 --- a/modules/juce_core/network/juce_URL.cpp +++ b/modules/juce_core/network/juce_URL.cpp @@ -325,8 +325,8 @@ void URL::addParameter (const String& name, const String& value) String URL::toString (bool includeGetParameters) const { - if (includeGetParameters && parameterNames.size() > 0) - return url + "?" + URLHelpers::getMangledParameters (*this); + if (includeGetParameters) + return url + getQueryString(); return url; } @@ -347,12 +347,24 @@ String URL::getDomain() const return getDomainInternal (false); } -String URL::getSubPath() const +String URL::getSubPath (bool includeGetParameters) const { auto startOfPath = URLHelpers::findStartOfPath (url); + auto subPath = startOfPath <= 0 ? String() + : url.substring (startOfPath); - return startOfPath <= 0 ? String() - : url.substring (startOfPath); + if (includeGetParameters) + subPath += getQueryString(); + + return subPath; +} + +String URL::getQueryString() const +{ + if (parameterNames.size() > 0) + return "?" + URLHelpers::getMangledParameters (*this); + + return {}; } String URL::getScheme() const diff --git a/modules/juce_core/network/juce_URL.h b/modules/juce_core/network/juce_URL.h index 81724f972b..abfc456ca1 100644 --- a/modules/juce_core/network/juce_URL.h +++ b/modules/juce_core/network/juce_URL.h @@ -90,8 +90,17 @@ public: /** Returns the path part of the URL. E.g. for "http://www.xyz.com/foo/bar?x=1", this will return "foo/bar". + + If includeGetParameters is true and any parameters have been set with the + withParameter() method, then the string will have these appended on the + end and url-encoded. + */ + String getSubPath (bool includeGetParamters = false) const; + + /** If any parameters are set, returns these URL encoded, including the "?" + * prefix. */ - String getSubPath() const; + String getQueryString() const; /** Returns the scheme of the URL. E.g. for "http://www.xyz.com/foobar", this will return "http". (It won't