A class used to encapsulate an URL.
URL objects are created either from an input string, which is then parsed into URL components, or by setting one or more URL components individually.
#include <network/YiUrl.h>
Public Types | |
enum | Parts { Parts::Scheme = 0x1, Parts::Username = 0x2, Parts::Password = 0x4, Parts::Host = 0x8, Parts::Port = 0x10, Parts::Path = 0x20, Parts::Query = 0x40, Parts::Fragment = 0x80, Parts::Authentication = Username | Password, Parts::Authority = Authentication | Host | Port, Parts::AuthorityAndPath = Authority | Path, Parts::Everything = Scheme | AuthorityAndPath | Query | Fragment, Parts::NoScheme = Everything & (~Scheme), Parts::NoAuthentication = Everything & (~Authentication) } |
enum | Encoding { Encoding::Encoded, Encoding::Decoded } |
Static Public Member Functions | |
static const CYIUrl & | EmptyUrl () |
static CYIString | Encode (const CYIString &input) |
static CYIString | Decode (const CYIString &input) |
|
strong |
|
strong |
CYIUrl::CYIUrl | ( | ) |
Creates a blank URL.
|
explicit |
Creates an URL from the string url. The string will be parsed into URL components.
The encoding parameter can be used to specify the encoding of the provided url string. The various delimiters used in URLs are expected to be unencoded. By default, the provided string is expected to be encoded.
void CYIUrl::Clear | ( | ) |
Clears this URL and resets it to a 'blank' URL. Note that blank URLs have no scheme.
Decodes the string input using URL percent encoding.
All character sequences 'XX', where XX is two hexadecimal digits, are transformed to their single-byte representation. 'Orphan' percent characters (such as in '100% coverage') are left as-is, as are percent sequences with invalid data (such as 'AX').
Example: The string 'The%20100%%20quick%20brown%20fox' gets encoded to 'The 100% quick brown fox'.
|
static |
Returns an instance of an empty URL. Useful for functions that return references.
Encodes the string input using URL percent encoding.
The following characters are left as is: a-z, A-Z, 0-9, '-', '.', '_' and '~'. All other characters, including UTF-8 characters and controls characters, are encoded as follows XX, where XX is two uppercase hexadecimal digits. UTF-8 characters with codepoints above 127 are encoded as multiple bytes represented in XX format.
Example: The string 'The quick brown fox' gets encoded to 'The%20quick%20brown%20fox'.
CYIString CYIUrl::GetAuthentication | ( | Encoding | encoding = Encoding::Decoded | ) | const |
Returns the authentication portion of this URL. The authentication portion is composted of the username and, password sections.
Example: For the URL 'http://myuser:mypassword@www.example.com:123/my/path?key=value#bookmark', this function would return 'myuser:mypassword'.
The encoding parameter can be used to specify the encoding of the returned value. By default, the value is returned in decoded form.
CYIString CYIUrl::GetAuthority | ( | Encoding | encoding = Encoding::Decoded | ) | const |
Returns the authority portion of this URL. The authority portion is composted of the username, password, host and port sections.
Example: For the URL 'http://myuser:mypassword@www.example.com:123/my/path?key=value#bookmark', this function would return 'myuser:mypas:123'. swor d@www .exa mple. com
The encoding parameter can be used to specify the encoding of the returned value. By default, the value is returned in decoded form.
CYIString CYIUrl::GetFragment | ( | Encoding | encoding = Encoding::Decoded | ) | const |
Returns the fragment portion of this URL.
Example: For the URL 'http://myuser:mypassword@www.example.com:123/my/path?key=value#bookmark', this function would return 'bookmark'.
The encoding parameter can be used to specify the encoding of the returned value. By default, the value is returned in decoded form.
CYIString CYIUrl::GetHost | ( | Encoding | encoding = Encoding::Decoded | ) | const |
Returns the host portion of this URL.
Example: For the URL 'http://myuser:mypassword@www.example.com:123/my/path?key=value#bookmark', this function would return 'www.example.com'.
The encoding parameter can be used to specify the encoding of the returned value. By default, the value is returned in decoded form.
CYIString CYIUrl::GetLocalPath | ( | Encoding | encoding = Encoding::Encoded | ) | const |
Returns the path portion of this URL formatted to be used with the local filesystem.
Returns empty string if the scheme portion of this URL is not 'file'.
CYIString CYIUrl::GetPassword | ( | Encoding | encoding = Encoding::Decoded | ) | const |
Returns the password portion of this URL.
Example: For the URL 'http://myuser:mypassword@www.example.com:123/my/path?key=value#bookmark', this function would return 'mypassword'.
The encoding parameter can be used to specify the encoding of the returned value. By default, the value is returned in decoded form.
CYIString CYIUrl::GetPath | ( | Encoding | encoding = Encoding::Decoded | ) | const |
Returns the path portion of this URL.
Example: For the URL 'http://myuser:mypassword@www.example.com:123/my/path?key=value#bookmark', this function would return 'my/path'.
The encoding parameter can be used to specify the encoding of the returned value. By default, the value is returned in decoded form.
int32_t CYIUrl::GetPort | ( | ) | const |
Returns the port portion of this URL as an integer. Returns -1 if no port is set, or if the set port string cannot be parsed as a (valid) port.
Example: For the URL 'http://myuser:mypassword@www.example.com:123/my/path?key=value#bookmark', this function would return 123.
const CYIString& CYIUrl::GetPortString | ( | ) | const |
Returns the port portion of this URL as a string.
Example: For the URL 'http://myuser:mypassword@www.example.com:123/my/path?key=value#bookmark', this function would return '123'.
CYIString CYIUrl::GetQuery | ( | Encoding | encoding = Encoding::Decoded | ) | const |
Returns the query portion of this URL.
Example: For the URL 'http://myuser:mypassword@www.example.com:123/my/path?key=value#bookmark', this function would return 'key=value'.
The encoding parameter can be used to specify the encoding of the returned value. By default, the value is returned in decoded form.
CYIUrlQuery CYIUrl::GetQueryBuilder | ( | ) | const |
Returns a CYIUrlQuery query object initialized from this URL's query.
const CYIString& CYIUrl::GetScheme | ( | ) | const |
Returns the scheme portion of this URL.
Example: For the URL 'http://myuser:mypassword@www.example.com:123/my/path?key=value#bookmark', this function would return 'http'.
CYIString CYIUrl::GetUsername | ( | Encoding | encoding = Encoding::Decoded | ) | const |
Returns the username portion of this URL.
Example: For the URL 'http://myuser:mypassword@www.example.com:123/my/path?key=value#bookmark', this function would return 'myuser'.
The encoding parameter can be used to specify the encoding of the returned value. By default, the value is returned in decoded form.
bool CYIUrl::IsEmpty | ( | ) | const |
Returns true if this URL has no non-empty sections.
bool CYIUrl::operator!= | ( | const CYIUrl & | other | ) | const |
bool CYIUrl::operator< | ( | const CYIUrl & | other | ) | const |
bool CYIUrl::operator== | ( | const CYIUrl & | other | ) | const |
void CYIUrl::SetAuthentication | ( | const CYIString & | authentication, |
Encoding | encoding = Encoding::Decoded |
||
) |
Sets the authentication portion of this URL. The authentication is composed of the username and password, though both sections are optional.
Example: Given the URL 'http://myuser:mypassword@www.example.com:123/my/path?key=value#bookmark', calling this function with 'bob:smith' will result in the URL 'ftp://bob:smith@www.example.com:123/my/path?key=value#bookmark'.
The encoding parameter can be used to specify the encoding of the provided authentication string. The ':' delimiter is expected to be unencoded. By default, the authentication is assumed to be decoded.
void CYIUrl::SetAuthority | ( | const CYIString & | authority, |
Encoding | encoding = Encoding::Decoded |
||
) |
Sets the authority portion of this URL. The authority is composed of the username, password, host and port, though all sections are optional.
Example: Given the URL 'http://myuser:mypassword@www.example.com:123/my/path?key=value#bookmark', calling this function with 'bob:smith:42' will result in the URL ' @mys erver .comftp://bob:smith@myserver.com:42/my/path?key=value#bookmark'.
The encoding parameter can be used to specify the encoding of the provided authority string. The ':' and '@' delimiters are expected to be unencoded. By default, the authority is assumed to be decoded.
void CYIUrl::SetFragment | ( | const CYIString & | fragment, |
CYIUrl::Encoding | encoding = Encoding::Decoded |
||
) |
Sets the fragment portion of this URL.
Example: Given the URL 'http://myuser:mypassword@www.example.com:123/my/path?key=value#bookmark', calling this function with 'place' will result in the URL 'ftp://myuser:mypassword@www.example.com:123/my/path?fruit=banana#place'.
The encoding parameter can be used to specify the encoding of the provided fragment string. By default, the fragment is assumed to be decoded.
void CYIUrl::SetHost | ( | const CYIString & | host, |
Encoding | encoding = Encoding::Decoded |
||
) |
Sets the host portion of this URL.
Example: Given the URL 'http://myuser:mypassword@www.example.com:123/my/path?key=value#bookmark', calling this function with 'myserver.com' will result in the URL 'ftp://myuser:mypassword@myserver.com:123/my/path?key=value#bookmark'.
The encoding parameter can be used to specify the encoding of the provided host string. By default, the host is assumed to be decoded.
void CYIUrl::SetPassword | ( | const CYIString & | password, |
Encoding | encoding = Encoding::Decoded |
||
) |
Sets the password portion of this URL.
Example: Given the URL 'http://myuser:mypassword@www.example.com:123/my/path?key=value#bookmark', calling this function with 'smith' will result in the URL 'ftp://myuser:smith@www.example.com:123/my/path?key=value#bookmark'.
The encoding parameter can be used to specify the encoding of the provided password string. By default, the password is assumed to be decoded.
void CYIUrl::SetPath | ( | const CYIString & | path, |
CYIUrl::Encoding | encoding = Encoding::Decoded |
||
) |
Sets the path portion of this URL.
Example: Given the URL 'http://myuser:mypassword@www.example.com:123/my/path?key=value#bookmark', calling this function with 'differentPath' will result in the URL 'ftp://myuser:mypassword@www.example.com:123/differentPath?key=value#bookmark'.
The encoding parameter can be used to specify the encoding of the provided path string. By default, the path is assumed to be decoded.
void CYIUrl::SetPort | ( | int32_t | port | ) |
Sets the port portion of this URL. Negative values are interpreted as 'no port'.
Example: Given the URL 'http://myuser:mypassword@www.example.com:123/my/path?key=value#bookmark', calling this function with '42' will result in the URL 'ftp://myuser:mypassword@www.example.com:42/my/path?key=value#bookmark'.
void CYIUrl::SetPort | ( | const CYIString & | port | ) |
Sets the port portion of this URL.
Example: Given the URL 'http://myuser:mypassword@www.example.com:123/my/path?key=value#bookmark', calling this function with '42' will result in the URL 'ftp://myuser:mypassword@www.example.com:42/my/path?key=value#bookmark'.
void CYIUrl::SetQuery | ( | const CYIString & | query, |
CYIUrl::Encoding | encoding = Encoding::Decoded |
||
) |
Sets the query portion of this URL.
Example: Given the URL 'http://myuser:mypassword@www.example.com:123/my/path?key=value#bookmark', calling this function with 'fruit=banana' will result in the URL 'ftp://myuser:mypassword@www.example.com:123/my/path?fruit=banana#bookmark'.
The encoding parameter can be used to specify the encoding of the query string. The parameter delimiters, '&' and '=', are expected to be unencoded. By default, the query string is assumed to be decoded.
void CYIUrl::SetQuery | ( | const CYIUrlQuery & | query | ) |
Sets the query portion of this URL from the data contained in a CYIUrlQuery object.
void CYIUrl::SetScheme | ( | const CYIString & | scheme | ) |
Sets the scheme portion of this URL. The scheme must not be postfixed with ':' or '://'.
Example: Given the URL 'http://myuser:mypassword@www.example.com:123/my/path?key=value#bookmark', calling this function with 'ftp' will result in the URL 'ftp://myuser:mypassword@www.example.com:123/my/path?key=value#bookmark'.
void CYIUrl::SetString | ( | const CYIString & | url, |
Encoding | encoding = Encoding::Encoded |
||
) |
Sets the string representation of this URL. The string will be parsed into the individual string components.
The encoding parameter can be used to specify the encoding of the provided url string. The various delimiters used in URLs are expected to be unencoded. By default, the provided string is expected to be encoded.
void CYIUrl::SetUsername | ( | const CYIString & | username, |
Encoding | encoding = Encoding::Decoded |
||
) |
Sets the username portion of this URL.
Example: Given the URL 'http://myuser:mypassword@www.example.com:123/my/path?key=value#bookmark', calling this function with 'bob' will result in the URL 'ftp://bob:mypassword@www.example.com:123/my/path?key=value#bookmark'.
The encoding parameter can be used to specify the encoding of the provided username string. By default, the username is assumed to be decoded.
CYIString CYIUrl::ToString | ( | Parts | parts = Parts::Everything , |
Encoding | encoding = Encoding::Encoded |
||
) | const |
Returns a string generated from the components of this URL.
The parts parameter can be used to specify which parts of the URL to include in the generated string. Note that some combinations don't really make sense (e.g. 'scheme + port'). By default, all parts of the URL are used to generate the returned string.
Example: For the URL 'http://myuser:mypassword@www.example.com:123/my/path?key=value#bookmark', this function, when called with EVERYTHING, would return 'http://myuser:mypassword@www.example.com:123/my/path?key=value#bookmark'. When called with HOST, it would return 'www.example.com'. When called with NO_SCHEME, it would return 'myuser:mypas:123/my/path?key=value::bookmark'. swor d@www .exa mple. com
The encoding parameter can be used to specify the encoding of the returned value. By default, the value is returned in encoded form.