|
Ultralight C++ API 2.0.0
|
#include <Ultralight/URL.h>
Parsed web address.
URL represents a parsed web address conforming to the WHATWG specification. You'll use it to inspect address components and resolve relative paths when loading pages into a View.
The object converts to String automatically, so you can pass it directly to View::LoadURL().
Construct a URL from a string and verify that parsing succeeded before loading the address:
Testing a URL in a conditional evaluates to true when parsing succeeds and false when parsing fails.
The library stores addresses in canonical form– str() returns the lowercased scheme and host rather than the raw input text.
Inspect individual parts of an address with component accessors:
Accessors return an empty String when a component is absent.
Call query_parameter() with a key name to read a single parameter– it returns the form-decoded value, or an empty String when the key isn't present.
Resolve a relative path against an existing base URL using Resolve():
Resolve() resolves references using the same rules a web browser applies to page links.
Append key-value pairs to an address using AppendQueryParameter():
You can add parameters individually or replace the entire query at once:
Convert a local file path to a file: URL with FromFilePath():
Both methods use the host OS's native path format, converting conventions such as Windows drive letters and backslashes automatically.
Static Public Member Functions | |
| static URL | FromFilePath (const String &file_path) |
| Create a file: URL from a native file-system path. | |
| static String | EncodeComponent (const String &component) |
| Percent-encode a string for safe embedding inside a URL component. | |
| static String | DecodeComponent (const String &encoded) |
| Decode a percent-encoded URL component. | |
Public Member Functions | |
| URL () | |
| Create an empty, invalid URL. | |
| URL (const String &url_string) | |
| Parse a string as an absolute URL (WHATWG-standard parsing). | |
| URL (const URL &base, const String &relative) | |
| Resolve a (possibly relative) reference against a base URL. | |
| URL (const URL &other) | |
| Copy constructor. | |
| URL (URL &&other) | |
| Move constructor. | |
| ~URL () | |
| Destructor. | |
| URL & | operator= (const URL &other) |
| Assignment operator. | |
| URL & | operator= (URL &&other) |
| Move assignment operator. | |
| bool | is_valid () const |
| Whether or not this URL parsed successfully. | |
| operator bool () const | |
| Whether or not this URL parsed successfully (same as is_valid()). | |
| bool | empty () const |
| Whether or not this URL is empty (equivalent to !is_valid()). | |
| String | str () const |
| Get the canonical serialization of the whole URL ("" if invalid). | |
| operator String () const | |
| Implicit conversion to the canonical serialization. | |
| URL | Resolve (const String &relative) const |
| Resolve a relative or absolute reference against this URL (same as the two-argument constructor). | |
| String | origin () const |
| Get the origin serialization, like JavaScript's URL.origin. | |
| String | scheme () const |
| Get the scheme, lowercased, without the trailing colon (eg, "https"; "" if invalid). | |
| String | host () const |
| Get the host, without the port (eg, "example.com"; "" if invalid or hostless). | |
| String | host_with_port () const |
| Get the host with the explicit port appended, if one is present (eg, "example.com:8080"). | |
| bool | has_port () const |
| Whether or not the URL carries an explicit port. | |
| unsigned short | port () const |
| Get the explicit port number (0 when has_port() is false). | |
| String | username () const |
| Get the username portion of the URL's credentials, percent-decoded ("" if none). | |
| String | password () const |
| Get the password portion of the URL's credentials, percent-decoded ("" if none). | |
| String | path () const |
| Get the path (eg, "/a/b"; "" if invalid). | |
| String | query () const |
| Get the query string, without the leading '? | |
| String | fragment () const |
| Get the fragment, without the leading '#' ("" if none). | |
| bool | set_scheme (const String &scheme) |
| Set the scheme. | |
| void | set_host (const String &host) |
| Set the host (without port). | |
| void | set_port (unsigned short port) |
| Set an explicit port. | |
| void | clear_port () |
| Remove the explicit port, if any. | |
| void | set_username (const String &username) |
| Set the username portion of the URL's credentials (percent-encoded as needed). | |
| void | set_password (const String &password) |
| Set the password portion of the URL's credentials (percent-encoded as needed). | |
| void | set_path (const String &path) |
| Set the path. | |
| void | set_query (const String &query) |
| Set the raw query string (without the leading '? | |
| void | set_fragment (const String &fragment) |
| Set the fragment (without the leading '#'). | |
| bool | has_query_parameter (const String &key) const |
| Whether or not the query contains a parameter with the given key (form-decoded match). | |
| String | query_parameter (const String &key) const |
| Get the first query-parameter value for the given key, form-decoded ("" if absent). | |
| void | AppendQueryParameter (const String &key, const String &value) |
| Append a key-value pair to the query, form-encoding both. | |
| String | ToFilePath () const |
| Get the native file-system path for a file: URL. | |
| bool | operator== (const URL &other) const |
| Equality over the canonical serialization. | |
| bool | operator!= (const URL &other) const |
| Inequality over the canonical serialization. | |
| bool | operator< (const URL &other) const |
| Lexicographic ordering over the canonical serialization (for ordered containers). | |
| bool | operator== (const String &other) const |
| Compare the canonical serialization against a raw string. | |
| bool | operator!= (const String &other) const |
| Inequality against a raw string (see the equality operator taking a String). | |
Friends | |
| bool | operator== (const String &lhs, const URL &rhs) |
| Equality with the string on the left-hand side. | |
| bool | operator!= (const String &lhs, const URL &rhs) |
| Inequality with the string on the left-hand side. | |
| URL | ( | ) |
Create an empty, invalid URL.
|
explicit |
Parse a string as an absolute URL (WHATWG-standard parsing).
Input without a scheme (eg, "example.com") does not parse; the result is invalid. To resolve a relative reference, use the two-argument constructor taking a base URL.
| url_string | The string to parse. |
| URL | ( | const URL & | base, |
| const String & | relative ) |
| URL | ( | const URL & | other | ) |
Copy constructor.
| URL | ( | URL && | other | ) |
Move constructor.
| ~URL | ( | ) |
Destructor.
Append a key-value pair to the query, form-encoding both.
Spaces become + and reserved characters are percent-encoded.
| key | The parameter name (unencoded). |
| value | The parameter value (unencoded). |
| void clear_port | ( | ) |
Remove the explicit port, if any.
Decode a percent-encoded URL component.
This matches JavaScript's decodeURIComponent for well-formed input: XX escapes (case-insensitive hex) are decoded as UTF-8 bytes.
Unlike its JavaScript counterpart this function never fails. A malformed escape (truncated, or with non-hex digits) is passed through unchanged.
| encoded | The text to decode. |
| bool empty | ( | ) | const |
Whether or not this URL is empty (equivalent to !is_valid()).
Percent-encode a string for safe embedding inside a URL component.
This matches JavaScript's encodeURIComponent: every character except letters, digits, and - _ . ! ~ * ' ( ) is replaced by the percent-encoding of its UTF-8 bytes.
| component | The text to encode. |
| String fragment | ( | ) | const |
Get the fragment, without the leading '#' ("" if none).
Create a file: URL from a native file-system path.
Platform path conventions are handled for you (eg, Windows drive letters and backslashes). Use ToFilePath() for the reverse conversion.
| file_path | An absolute native file-system path. |
| bool has_port | ( | ) | const |
Whether or not the URL carries an explicit port.
A default port (eg, 443 for https) is elided during canonicalization and does not count as explicit.
| bool has_query_parameter | ( | const String & | key | ) | const |
Whether or not the query contains a parameter with the given key (form-decoded match).
| String host | ( | ) | const |
Get the host, without the port (eg, "example.com"; "" if invalid or hostless).
| String host_with_port | ( | ) | const |
Get the host with the explicit port appended, if one is present (eg, "example.com:8080").
| bool is_valid | ( | ) | const |
Whether or not this URL parsed successfully.
|
inlineexplicit |
Whether or not this URL parsed successfully (same as is_valid()).
| operator String | ( | ) | const |
Implicit conversion to the canonical serialization.
| bool operator!= | ( | const String & | other | ) | const |
Inequality against a raw string (see the equality operator taking a String).
| bool operator!= | ( | const URL & | other | ) | const |
Inequality over the canonical serialization.
| bool operator< | ( | const URL & | other | ) | const |
Lexicographic ordering over the canonical serialization (for ordered containers).
| bool operator== | ( | const String & | other | ) | const |
Compare the canonical serialization against a raw string.
| bool operator== | ( | const URL & | other | ) | const |
Equality over the canonical serialization.
| String origin | ( | ) | const |
Get the origin serialization, like JavaScript's URL.origin.
For hierarchical schemes this is "scheme://host" plus the port when one is explicitly present (eg, "https://example.com:8080").
| String password | ( | ) | const |
Get the password portion of the URL's credentials, percent-decoded ("" if none).
| String path | ( | ) | const |
Get the path (eg, "/a/b"; "" if invalid).
| unsigned short port | ( | ) | const |
Get the explicit port number (0 when has_port() is false).
| String query | ( | ) | const |
Get the query string, without the leading '?
' ("" if none).
Values inside the query are stored encoded. Use query_parameter() to read a decoded value.
Get the first query-parameter value for the given key, form-decoded ("" if absent).
| String scheme | ( | ) | const |
Get the scheme, lowercased, without the trailing colon (eg, "https"; "" if invalid).
| void set_fragment | ( | const String & | fragment | ) |
Set the fragment (without the leading '#').
Pass an empty string to remove the fragment entirely.
| void set_host | ( | const String & | host | ) |
| void set_password | ( | const String & | password | ) |
Set the password portion of the URL's credentials (percent-encoded as needed).
Pass an empty string to remove it.
| void set_path | ( | const String & | path | ) |
Set the path.
The path is encoded as needed. On http(s) URLs an empty path becomes "/".
| void set_port | ( | unsigned short | port | ) |
Set an explicit port.
A scheme-default port (eg, 443 for https) is elided from the serialization.
| void set_query | ( | const String & | query | ) |
Set the raw query string (without the leading '?
').
Pass an empty string to remove the query entirely.
The value is used as-is, so you are responsible for encoding it. To build a query from arbitrary text safely, use AppendQueryParameter() instead.
| bool set_scheme | ( | const String & | scheme | ) |
Set the scheme.
The new scheme is validated and canonicalized (lowercased).
| scheme | The new scheme, with or without the trailing colon. |
| void set_username | ( | const String & | username | ) |
Set the username portion of the URL's credentials (percent-encoded as needed).
Pass an empty string to remove it.
| String ToFilePath | ( | ) | const |
Get the native file-system path for a file: URL.
| String username | ( | ) | const |
Get the username portion of the URL's credentials, percent-decoded ("" if none).
Inequality with the string on the left-hand side.
Equality with the string on the left-hand side.