JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Static Public Member Functions | Friends | List of all members
juce::URL Class Reference

Represents a URL and has a bunch of useful functions to manipulate it. More...

#include "juce_URL.h"

Classes

class  DownloadTask
 Represents a download task. More...
 
struct  DownloadTaskListener
 Used to receive callbacks for download progress. More...
 
class  DownloadTaskOptions
 Holds options that can be specified when starting a new download with downloadToFile(). More...
 
class  InputStreamOptions
 Class used to create a set of options to pass to the createInputStream() method. More...
 

Public Types

enum class  ParameterHandling { inAddress , inPostData }
 

Public Member Functions

 URL ()
 Creates an empty URL.
 
 URL (const String &url)
 Creates a URL from a string.
 
 URL (File localFile)
 Creates URL referring to a local file on your disk using the file:// scheme.
 
bool operator== (const URL &) const
 Compares two URLs.
 
bool operator!= (const URL &) const
 
String toString (bool includeGetParameters) const
 Returns a string version of the URL.
 
bool isEmpty () const noexcept
 Returns true if the URL is an empty string.
 
bool isWellFormed () const
 True if it seems to be valid.
 
String getDomain () const
 Returns just the domain part of the URL.
 
String getSubPath (bool includeGetParameters=false) const
 Returns the path part of the URL.
 
String getQueryString () const
 If any parameters are set, returns these URL-encoded, including the "?" prefix.
 
String getAnchorString () const
 If any anchor is set, returns URL-encoded anchor, including the "#" prefix.
 
String getScheme () const
 Returns the scheme of the URL.
 
bool isLocalFile () const
 Returns true if this URL refers to a local file.
 
File getLocalFile () const
 Returns the file path of the local file to which this URL refers to.
 
String getFileName () const
 Returns the file name.
 
int getPort () const
 Attempts to read a port number from the URL.
 
URL withNewDomainAndPath (const String &newFullPath) const
 Returns a new version of this URL with a different domain and path.
 
URL withNewSubPath (const String &newPath) const
 Returns a new version of this URL with a different sub-path.
 
URL getParentURL () const
 Attempts to return a URL which is the parent folder containing this URL.
 
URL getChildURL (const String &subPath) const
 Returns a new URL that refers to a sub-path relative to this one.
 
URL withParameter (const String &parameterName, const String &parameterValue) const
 Returns a copy of this URL, with a GET or POST parameter added to the end.
 
URL withParameters (const StringPairArray &parametersToAdd) const
 Returns a copy of this URL, with a set of GET or POST parameters added.
 
URL withAnchor (const String &anchor) const
 Returns a copy of this URL, with an anchor added to the end of the URL.
 
URL withFileToUpload (const String &parameterName, const File &fileToUpload, const String &mimeType) const
 Returns a copy of this URL, with a file-upload type parameter added to it.
 
URL withDataToUpload (const String &parameterName, const String &filename, const MemoryBlock &fileContentToUpload, const String &mimeType) const
 Returns a copy of this URL, with a file-upload type parameter added to it.
 
const StringArraygetParameterNames () const noexcept
 Returns an array of the names of all the URL's parameters.
 
const StringArraygetParameterValues () const noexcept
 Returns an array of the values of all the URL's parameters.
 
URL withPOSTData (const String &postData) const
 Returns a copy of this URL, with a block of data to send as the POST data.
 
URL withPOSTData (const MemoryBlock &postData) const
 Returns a copy of this URL, with a block of data to send as the POST data.
 
String getPostData () const
 Returns the data that was set using withPOSTData().
 
const MemoryBlockgetPostDataAsMemoryBlock () const noexcept
 Returns the data that was set using withPOSTData() as a MemoryBlock.
 
bool launchInDefaultBrowser () const
 Tries to launch the system's default browser to open the URL.
 
std::unique_ptr< InputStreamcreateInputStream (const InputStreamOptions &options) const
 Attempts to open a stream that can read from this URL.
 
std::unique_ptr< OutputStreamcreateOutputStream () const
 Attempts to open an output stream to a URL for writing.
 
std::unique_ptr< DownloadTaskdownloadToFile (const File &targetLocation, String extraHeaders=String(), DownloadTaskListener *listener=nullptr, bool usePostCommand=false)
 This function is replaced by a new overload accepting a DownloadTaskOptions argument.
 
std::unique_ptr< DownloadTaskdownloadToFile (const File &targetLocation, const DownloadTaskOptions &options)
 Download the URL to a file.
 
bool readEntireBinaryStream (MemoryBlock &destData, bool usePostCommand=false) const
 Tries to download the entire contents of this URL into a binary data block.
 
String readEntireTextStream (bool usePostCommand=false) const
 Tries to download the entire contents of this URL as a string.
 
std::unique_ptr< XmlElementreadEntireXmlStream (bool usePostCommand=false) const
 Tries to download the entire contents of this URL and parse it as XML.
 

Static Public Member Functions

static bool isProbablyAWebsiteURL (const String &possibleURL)
 Takes a guess as to whether a string might be a valid website address.
 
static bool isProbablyAnEmailAddress (const String &possibleEmailAddress)
 Takes a guess as to whether a string might be a valid email address.
 
static String addEscapeChars (const String &stringToAddEscapeCharsTo, bool isParameter, bool roundBracketsAreLegal=true)
 Adds escape sequences to a string to encode any characters that aren't legal in a URL.
 
static String removeEscapeChars (const String &stringToRemoveEscapeCharsFrom)
 Replaces any escape character sequences in a string with their original character codes.
 
static URL createWithoutParsing (const String &url)
 Returns a URL without attempting to remove any embedded parameters from the string.
 

Friends

class WebInputStream
 

Detailed Description

Represents a URL and has a bunch of useful functions to manipulate it.

This class can be used to launch URLs in browsers, and also to create InputStreams that can read from remote HTTP or FTP sources.

@tags{Core}

Definition at line 37 of file juce_URL.h.

Member Enumeration Documentation

◆ ParameterHandling

enum class juce::URL::ParameterHandling
strong

Definition at line 310 of file juce_URL.h.

Constructor & Destructor Documentation

◆ URL() [1/3]

juce::URL::URL ( )

Creates an empty URL.

Definition at line 138 of file juce_URL.cpp.

◆ URL() [2/3]

juce::URL::URL ( const String url)

Creates a URL from a string.

This will parse any embedded parameters after a '?' character and store them in the list (see getParameterNames etc). If you don't want this to happen, you can use createWithoutParsing().

Definition at line 140 of file juce_URL.cpp.

◆ URL() [3/3]

juce::URL::URL ( File  localFile)
explicit

Creates URL referring to a local file on your disk using the file:// scheme.

Definition at line 145 of file juce_URL.cpp.

Member Function Documentation

◆ addEscapeChars()

String juce::URL::addEscapeChars ( const String stringToAddEscapeCharsTo,
bool  isParameter,
bool  roundBracketsAreLegal = true 
)
static

Adds escape sequences to a string to encode any characters that aren't legal in a URL.

E.g. any spaces will be replaced with "%20".

This is the opposite of removeEscapeChars().

Parameters
stringToAddEscapeCharsTothe string to escape.
isParameterif true then the string is going to be used as a parameter, so it also encodes '$' and ',' (which would otherwise be legal in a URL.
roundBracketsAreLegaltechnically round brackets are ok in URLs, however, some servers (like AWS) also want round brackets to be escaped.
See also
removeEscapeChars

Definition at line 966 of file juce_URL.cpp.

◆ createInputStream()

std::unique_ptr< InputStream > juce::URL::createInputStream ( const InputStreamOptions options) const

Attempts to open a stream that can read from this URL.

Note that this method will block until the first byte of data has been received or an error has occurred.

Note that on some platforms (Android, for example) it's not permitted to do any network action from the message thread, so you must only call it from a background thread.

Unless the URL represents a local file, this method returns an instance of a WebInputStream. You can use dynamic_cast to cast the return value to a WebInputStream which allows you more fine-grained control of the transfer process.

If the URL represents a local file, then this method simply returns a FileInputStream.

Parameters
optionsa set of options that will be used when opening the stream.
Returns
a valid input stream, or nullptr if there was an error trying to open it.

Definition at line 735 of file juce_URL.cpp.

◆ createOutputStream()

std::unique_ptr< OutputStream > juce::URL::createOutputStream ( ) const

Attempts to open an output stream to a URL for writing.

This method can only be used for certain scheme types such as local files and content:// URIs on Android.

Definition at line 812 of file juce_URL.cpp.

◆ createWithoutParsing()

URL juce::URL::createWithoutParsing ( const String url)
static

Returns a URL without attempting to remove any embedded parameters from the string.

This may be necessary if you need to create a request that involves both POST parameters and parameters which are embedded in the URL address itself.

Definition at line 219 of file juce_URL.cpp.

◆ downloadToFile() [1/2]

std::unique_ptr< URL::DownloadTask > juce::URL::downloadToFile ( const File targetLocation,
const DownloadTaskOptions options 
)

Download the URL to a file.

This method attempts to download the URL to a given file location.

Using this method to download files on mobile is less flexible but more reliable than using createInputStream or WebInputStreams as it will attempt to download the file using a native OS background network task. Such tasks automatically deal with network re-connections and continuing your download while your app is suspended.

Definition at line 610 of file juce_Network_linux.cpp.

◆ downloadToFile() [2/2]

std::unique_ptr< URL::DownloadTask > juce::URL::downloadToFile ( const File targetLocation,
String  extraHeaders = String(),
DownloadTaskListener listener = nullptr,
bool  usePostCommand = false 
)

This function is replaced by a new overload accepting a DownloadTaskOptions argument.

Definition at line 1029 of file juce_URL.cpp.

◆ getAnchorString()

String juce::URL::getAnchorString ( ) const

If any anchor is set, returns URL-encoded anchor, including the "#" prefix.

Definition at line 368 of file juce_URL.cpp.

◆ getChildURL()

URL juce::URL::getChildURL ( const String subPath) const

Returns a new URL that refers to a sub-path relative to this one.

e.g. if the URL is "http://www.xyz.com/foo" and you call this with "bar", it'll return "http://www.xyz.com/foo/bar".

Note that there's no way for this method to know whether the original URL is a file or directory, so it's up to you to make sure it's a directory. It also won't attempt to be smart about the content of the childPath string, so if this string is an absolute URL, it'll still just get bolted onto the end of the path.

See also
File::getChildFile

Definition at line 466 of file juce_URL.cpp.

◆ getDomain()

String juce::URL::getDomain ( ) const

Returns just the domain part of the URL.

e.g. for "http://www.xyz.com/foobar", this will return "www.xyz.com".

Definition at line 338 of file juce_URL.cpp.

◆ getFileName()

String juce::URL::getFileName ( ) const

Returns the file name.

For all but Android's content:// scheme, it will simply return the last segment of the URL, e.g. for "http://www.xyz.com/foo/bar.txt", this will return "bar.txt".

For Android's content:// scheme, it will attempt to resolve the filename located under the URL.

Definition at line 392 of file juce_URL.cpp.

◆ getLocalFile()

File juce::URL::getLocalFile ( ) const

Returns the file path of the local file to which this URL refers to.

If the URL does not represent a local file URL (i.e. the URL's scheme is not 'file') then this method will assert.

This method also supports converting Android's content:// URLs to local file paths.

See also
isLocalFile

Definition at line 387 of file juce_URL.cpp.

◆ getParameterNames()

const StringArray & juce::URL::getParameterNames ( ) const
noexcept

Returns an array of the names of all the URL's parameters.

e.g. for the url "www.fish.com?type=haddock&amount=some+fish", this array would contain two items: "type" and "amount".

You can call getParameterValues() to get the corresponding value of each parameter. Note that the list can contain multiple parameters with the same name.

See also
getParameterValues, withParameter

Definition at line 249 of file juce_URL.h.

◆ getParameterValues()

const StringArray & juce::URL::getParameterValues ( ) const
noexcept

Returns an array of the values of all the URL's parameters.

e.g. for the url "www.fish.com?type=haddock&amount=some+fish", this array would contain two items: "haddock" and "some fish".

The values returned will have been cleaned up to remove any escape characters.

You can call getParameterNames() to get the corresponding name of each parameter. Note that the list can contain multiple parameters with the same name.

See also
getParameterNames, withParameter

Definition at line 263 of file juce_URL.h.

◆ getParentURL()

URL juce::URL::getParentURL ( ) const

Attempts to return a URL which is the parent folder containing this URL.

If there isn't a parent, this method will just return a copy of this URL.

Definition at line 459 of file juce_URL.cpp.

◆ getPort()

int juce::URL::getPort ( ) const

Attempts to read a port number from the URL.

Returns
the port number, or 0 if none is explicitly specified.

Definition at line 432 of file juce_URL.cpp.

◆ getPostData()

String juce::URL::getPostData ( ) const

Returns the data that was set using withPOSTData().

Definition at line 286 of file juce_URL.h.

◆ getPostDataAsMemoryBlock()

const MemoryBlock & juce::URL::getPostDataAsMemoryBlock ( ) const
noexcept

Returns the data that was set using withPOSTData() as a MemoryBlock.

Definition at line 289 of file juce_URL.h.

◆ getQueryString()

String juce::URL::getQueryString ( ) const

If any parameters are set, returns these URL-encoded, including the "?" prefix.

Definition at line 355 of file juce_URL.cpp.

◆ getScheme()

String juce::URL::getScheme ( ) const

Returns the scheme of the URL.

e.g. for "http://www.xyz.com/foobar", this will return "http" (it won't include the colon).

Definition at line 376 of file juce_URL.cpp.

◆ getSubPath()

String juce::URL::getSubPath ( bool  includeGetParameters = false) const

Returns the path part of the URL.

e.g. for "http://www.xyz.com/foo/bar?x=1", this will return "foo/bar".

Parameters
includeGetParametersif this 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.
See also
getQueryString

Definition at line 343 of file juce_URL.cpp.

◆ isEmpty()

bool juce::URL::isEmpty ( ) const
noexcept

Returns true if the URL is an empty string.

Definition at line 327 of file juce_URL.cpp.

◆ isLocalFile()

bool juce::URL::isLocalFile ( ) const

Returns true if this URL refers to a local file.

Definition at line 382 of file juce_URL.cpp.

◆ isProbablyAnEmailAddress()

bool juce::URL::isProbablyAnEmailAddress ( const String possibleEmailAddress)
static

Takes a guess as to whether a string might be a valid email address.

This isn't foolproof!

Definition at line 553 of file juce_URL.cpp.

◆ isProbablyAWebsiteURL()

bool juce::URL::isProbablyAWebsiteURL ( const String possibleURL)
static

Takes a guess as to whether a string might be a valid website address.

This isn't foolproof!

Definition at line 538 of file juce_URL.cpp.

◆ isWellFormed()

bool juce::URL::isWellFormed ( ) const

True if it seems to be valid.

Definition at line 332 of file juce_URL.cpp.

◆ launchInDefaultBrowser()

bool juce::URL::launchInDefaultBrowser ( ) const

Tries to launch the system's default browser to open the URL.

Returns
true if this seems to have worked.

Definition at line 993 of file juce_URL.cpp.

◆ operator!=()

bool juce::URL::operator!= ( const URL other) const

Definition at line 233 of file juce_URL.cpp.

◆ operator==()

bool juce::URL::operator== ( const URL other) const

Compares two URLs.

All aspects of the URLs must be identical for them to match, including any parameters, upload files, etc.

Definition at line 224 of file juce_URL.cpp.

◆ readEntireBinaryStream()

bool juce::URL::readEntireBinaryStream ( MemoryBlock destData,
bool  usePostCommand = false 
) const

Tries to download the entire contents of this URL into a binary data block.

If it succeeds, this will return true and append the data it read onto the end of the memory block.

Note that on some platforms (Android, for example) it's not permitted to do any network action from the message thread, so you must only call it from a background thread.

Parameters
destDatathe memory block to append the new data to.
usePostCommandwhether to use a POST command to get the data (uses a GET command if this is false).
See also
readEntireTextStream, readEntireXmlStream

Definition at line 833 of file juce_URL.cpp.

◆ readEntireTextStream()

String juce::URL::readEntireTextStream ( bool  usePostCommand = false) const

Tries to download the entire contents of this URL as a string.

If it fails, this will return an empty string, otherwise it will return the contents of the downloaded file. If you need to distinguish between a read operation that fails and one that returns an empty string, you'll need to use a different method, such as readEntireBinaryStream().

Note that on some platforms (Android, for example) it's not permitted to do any network action from the message thread, so you must only call it from a background thread.

Parameters
usePostCommandwhether to use a POST command to get the data (uses a GET command if this is false).
See also
readEntireBinaryStream, readEntireXmlStream

Definition at line 847 of file juce_URL.cpp.

◆ readEntireXmlStream()

std::unique_ptr< XmlElement > juce::URL::readEntireXmlStream ( bool  usePostCommand = false) const

Tries to download the entire contents of this URL and parse it as XML.

If it fails, or if the text that it reads can't be parsed as XML, this will return nullptr.

Note that on some platforms (Android, for example) it's not permitted to do any network action from the message thread, so you must only call it from a background thread.

Parameters
usePostCommandwhether to use a POST command to get the data (uses a GET command if this is false).
See also
readEntireBinaryStream, readEntireTextStream

Definition at line 858 of file juce_URL.cpp.

◆ removeEscapeChars()

String juce::URL::removeEscapeChars ( const String stringToRemoveEscapeCharsFrom)
static

Replaces any escape character sequences in a string with their original character codes.

E.g. any instances of "%20" will be replaced by a space.

This is the opposite of addEscapeChars().

See also
addEscapeChars

Definition at line 937 of file juce_URL.cpp.

◆ toString()

String juce::URL::toString ( bool  includeGetParameters) const

Returns a string version of the URL.

Parameters
includeGetParametersif this 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.
See also
getQueryString

Definition at line 319 of file juce_URL.cpp.

◆ withAnchor()

URL juce::URL::withAnchor ( const String anchor) const

Returns a copy of this URL, with an anchor added to the end of the URL.

Definition at line 883 of file juce_URL.cpp.

◆ withDataToUpload()

URL juce::URL::withDataToUpload ( const String parameterName,
const String filename,
const MemoryBlock fileContentToUpload,
const String mimeType 
) 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 lets you specify the file content.

Note that the filename parameter should not be a full path, it's just the last part of the filename.

See also
withFileToUpload

Definition at line 929 of file juce_URL.cpp.

◆ withFileToUpload()

URL juce::URL::withFileToUpload ( const String parameterName,
const File fileToUpload,
const String mimeType 
) 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 lets you specify the file.

Note that the filename is stored, but the file itself won't actually be read until this URL is later used to create a network input stream. If you want to upload data from memory, use withDataToUpload().

See also
withDataToUpload

Definition at line 922 of file juce_URL.cpp.

◆ withNewDomainAndPath()

URL juce::URL::withNewDomainAndPath ( const String newFullPath) 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 "abc.com/zzz", it'll return "http://abc.com/zzz?x=1".

See also
withNewSubPath

Definition at line 439 of file juce_URL.cpp.

◆ withNewSubPath()

URL juce::URL::withNewSubPath ( const String newPath) const

Returns a new version of this URL with a different sub-path.

e.g. if the URL is "http://www.xyz.com/foo?x=1" and you call this with "bar", it'll return "http://www.xyz.com/bar?x=1".

See also
withNewDomainAndPath

Definition at line 446 of file juce_URL.cpp.

◆ withParameter()

URL juce::URL::withParameter ( const String parameterName,
const String parameterValue 
) const

Returns a copy of this URL, with a GET or POST parameter added to the end.

Any control characters in the value will be URL-encoded.

e.g. calling "withParameter ("amount", "some fish") for the url "www.fish.com" would produce a new url whose <tt>toString (true)</tt> method would return "www.fish.com?amount=some+fish".

See also
getParameterNames, getParameterValues

Definition at line 864 of file juce_URL.cpp.

◆ withParameters()

URL juce::URL::withParameters ( const StringPairArray parametersToAdd) const

Returns a copy of this URL, with a set of GET or POST parameters added.

This is a convenience method, equivalent to calling withParameter for each value.

See also
withParameter

Definition at line 872 of file juce_URL.cpp.

◆ withPOSTData() [1/2]

URL juce::URL::withPOSTData ( const MemoryBlock postData) const

Returns a copy of this URL, with a block of data to send as the POST data.

If the URL already contains some POST data, this will replace it, rather than being appended to it.

If no HTTP command is set when calling createInputStream() to read from this URL and some data has been set, it will do a POST request.

Definition at line 896 of file juce_URL.cpp.

◆ withPOSTData() [2/2]

URL juce::URL::withPOSTData ( const String postData) const

Returns a copy of this URL, with a block of data to send as the POST data.

If the URL already contains some POST data, this will replace it, rather than being appended to it.

If no HTTP command is set when calling createInputStream() to read from this URL and some data has been set, it will do a POST request.

Definition at line 891 of file juce_URL.cpp.

Friends And Related Symbol Documentation

◆ WebInputStream

friend class WebInputStream
friend

Definition at line 718 of file juce_URL.h.


The documentation for this class was generated from the following files: