A strict RFC 3986 URI implementation, with RFC 3987 IRI syntax checking. More...
Classes | |
| class | sourcemeta::core::URI |
| class | sourcemeta::core::URIParseError |
| class | sourcemeta::core::URIError |
A strict RFC 3986 URI implementation, with RFC 3987 IRI syntax checking.
This functionality is included as follows:
| class sourcemeta::core::URI |
A parsed URI that can be inspected, resolved, and recomposed
Public Member Functions | |
| URI ()=default | |
| Default constructor creates an empty URI. | |
| URI (const URI &)=default | |
| Copy constructor. | |
| URI (URI &&) noexcept=default | |
| Move constructor. | |
| template<typename T> | |
| URI (T &&input) | |
| URI (std::istream &input) | |
| auto | is_absolute () const noexcept -> bool |
| auto | is_urn () const -> bool |
| auto | is_tag () const -> bool |
| auto | is_mailto () const -> bool |
| auto | is_file () const -> bool |
| auto | is_http () const -> bool |
| auto | is_https () const -> bool |
| auto | is_fragment_only () const -> bool |
| auto | is_relative () const -> bool |
| auto | is_ipv4 () const -> bool |
| auto | is_ipv6 () const -> bool |
| auto | is_loopback () const -> bool |
| auto | is_localhost () const -> bool |
| auto | empty () const -> bool |
| auto | scheme () const -> std::optional< std::string_view > |
| auto | host () const -> std::optional< std::string_view > |
| auto | port () const -> std::optional< std::uint32_t > |
| auto | authority () const -> std::optional< std::string > |
| auto | path () const -> std::optional< std::string_view > |
| auto | path (const std::string &path) -> URI & |
| auto | path (std::string &&path) -> URI & |
| auto | append_path (std::string_view path) -> URI & |
| auto | append_path (const URI &reference) -> URI & |
| auto | append_path (URI &&reference) -> URI & |
| auto | extension (std::string &&extension) -> URI & |
| auto | fragment () const -> std::optional< std::string_view > |
| auto | fragment (const std::string_view fragment) -> URI & |
| auto | query () const -> std::optional< Query > |
| auto | query (const std::string_view query) -> URI & |
| auto | recompose () const -> std::string |
| auto | recompose_relative () const -> std::string |
| auto | recompose_without_fragment () const -> std::optional< std::string > |
| auto | canonicalize () -> URI & |
| auto | to_path () const -> std::filesystem::path |
| auto | resolve_from (const URI &base) -> URI & |
| auto | relative_to (const URI &base) -> URI & |
| auto | rebase (const URI &base, const URI &new_base) -> URI & |
| auto | rebase (const URI &base, URI &&new_base) -> URI & |
| auto | has_same_authority (const URI &other) const noexcept -> bool |
| auto | userinfo () const -> std::optional< std::string_view > |
| auto | userinfo (const std::string_view userinfo) -> URI & |
| auto | is_internationalized () const noexcept -> bool |
Static Public Member Functions | |
| static auto | from_fragment (const std::string_view fragment) -> URI |
| static auto | from_path (const std::filesystem::path &path) -> URI |
| static auto | from_iri (std::string_view input) -> URI |
| static auto | canonicalize (std::string_view input) -> std::string |
| static auto | escape (std::string_view input, bool maybe_encoded=false) -> std::string |
| template<typename Output> | |
| static auto | escape (const std::string_view input, Output &output, const bool maybe_encoded=false) -> void |
| template<typename Output> | |
| static auto | append_query_parameter (Output &sink, const std::string_view name, const std::string_view value, const char opener='?') -> void |
| static auto | unescape (std::string_view input) -> std::string |
| template<typename Output> | |
| static auto | unescape_form (const std::string_view input, Output &output) -> bool |
| static auto | normalize_path (std::string_view path) -> std::string |
| static auto | is_scheme (std::string_view input) noexcept -> bool |
| static auto | is_gen_delim (char character) noexcept -> bool |
| static auto | is_uri (std::string_view input) noexcept -> bool |
| static auto | is_uri_reference (std::string_view input) noexcept -> bool |
| static auto | is_iri (std::string_view input) noexcept -> bool |
| static auto | is_iri_reference (std::string_view input) noexcept -> bool |
| static auto | strip_path_prefix (std::string_view path, std::string_view prefix) -> std::optional< std::string > |
| static auto | rebase_path (std::string_view path, std::string_view old_prefix, std::string_view new_prefix) -> std::optional< std::string > |
|
inlineexplicit |
This constructor creates a URI from a string. For example:
| sourcemeta::core::URI::URI | ( | std::istream & | input | ) |
This constructor creates a URI from a C++ input stream. For example:
Append a path to the existing URI from a parsed reference. The reference must contain only a path. A scheme, authority, query, or fragment throws URIError. For example:
| auto sourcemeta::core::URI::append_path | ( | std::string_view | path | ) | -> URI & |
Append a path to the existing URI path or set a path if such component does not exist in the URI. The argument is treated as a path component to merge in. Authority prefixes and ? or # delimiters throw URIError. For example:
Append a path to the existing URI from a parsed reference, moving the path out of the reference rather than copying it. The reference must contain only a path. A scheme, authority, query, or fragment throws URIError. For example:
|
inlinestatic |
Append a percent-encoded name and value pair to a query, form body, or fragment under construction (RFC 3986 Section 2.1), joining it to any preceding pair. The caller writes the opening character of a fresh parameter list, the "?" of a query unless overridden through the opener, and the sink must not alias the name or value. For example:
|
nodiscard |
Get the recomposed authority component of the URI, if any, in the RFC 3986 form [ userinfo "@" ] host [ ":" port ], with IPv6 hosts wrapped in brackets. A URI with an empty authority, such as file:///path, returns a present but empty value, so a caller must not treat a present value as necessarily non-empty. For example:
| auto sourcemeta::core::URI::canonicalize | ( | ) | -> URI & |
Canonicalize a URI. For example:
|
static |
A convenient method to canonicalize and recompose a URI from a string. For example:
|
nodiscard |
Check if the URI corresponds to the empty URI. For example:
|
inlinestatic |
Percent-encode a string per RFC 3986, appending the result to a string like output sink rather than allocating a new string. Besides a std::string the sink can be a wiping string for secret material. The input can optionally be treated as possibly already encoded. The output must not alias the input. For example:
|
staticnodiscard |
Percent-encode a string per RFC 3986, escaping every octet outside the unreserved set. The input can optionally be treated as possibly already encoded, preserving valid escapes and decoding needlessly encoded unreserved octets so the result is stable under repeated application. For example:
| auto sourcemeta::core::URI::extension | ( | std::string && | extension | ) | -> URI & |
If the URI has a path, this method sets or replace the extension in the path. For example:
|
nodiscard |
| auto sourcemeta::core::URI::fragment | ( | const std::string_view | fragment | ) | -> URI & |
|
static |
Create a URI from a fragment. For example:
|
static |
Create a URI from a string that may be an Internationalized Resource Identifier (IRI) as defined by RFC 3987, accepting the non-ASCII characters that a plain URI does not permit. For example:
|
static |
|
nodiscardnoexcept |
Check whether two URIs share the same authority component. The authority is the user information, host, and port per RFC 3986 Section 3.2. The scheme is not part of the authority and is not compared. Comparison is byte-exact on the stored values, so call canonicalize() first if you want host case-insensitivity or default-port elision. For example:
|
nodiscard |
|
nodiscardnoexcept |
Check if the URI is absolute. For example:
|
nodiscard |
Check if the URI is a file URI. For example:
|
nodiscard |
Check if the URI only consists of a fragment. For example:
|
staticnodiscardnoexcept |
Check if the given character is a URI generic delimiter per RFC 3986 (":" / "/" / "?" / "#" / "[" / "]" / "@"). For example:
|
nodiscard |
Check if the URI has the HTTP scheme (RFC 9110 Section 4.2.1), accepting any scheme case (RFC 3986 Section 3.1). For example:
|
nodiscard |
Check if the URI has the https scheme (RFC 9110 Section 4.2.2), accepting any scheme case (RFC 3986 Section 3.1). For example:
|
nodiscardnoexcept |
Check whether this object holds an Internationalized Resource Identifier (IRI) as defined by RFC 3987, rather than a plain URI. For example:
|
nodiscard |
Check if the host is an IPv4 address. For example:
|
nodiscard |
Check if the host is an IPv6 address. For example:
|
staticnodiscardnoexcept |
Check if the given string is a valid absolute IRI (has a scheme) per RFC 3987 without constructing a full URI object. For example:
|
staticnodiscardnoexcept |
Check if the given string is a valid IRI reference per RFC 3987 (absolute or relative) without constructing a full URI object. For example:
|
nodiscard |
Check if the host is the special-use domain name localhost or a name falling within it, such as foo.localhost, in any case and with or without the trailing dot of the absolute form (RFC 6761 Section 6.3). A loopback IP literal is not a localhost name. For example:
|
nodiscard |
Check if the host is a loopback IP literal, any address in 127.0.0.0/8 (RFC 1122 Section 3.2.1.3), ::1 (RFC 4291 Section 2.5.3), or an IPv4-mapped or IPv4-compatible IPv6 address embedding a 127.0.0.0/8 address (RFC 4291 Section 2.5.5). The name localhost is deliberately not a loopback host, as it usually resolves to one but that is a different claim (RFC 8252 Section 8.3). For example:
|
nodiscard |
|
nodiscard |
Check if the URI is relative. For example:
|
staticnodiscardnoexcept |
Check if the given string is a valid URI scheme per RFC 3986 (ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )). For example:
|
nodiscard |
|
staticnodiscardnoexcept |
Check if the given string is a valid absolute URI (has a scheme) per RFC 3986 without constructing a full URI object. For example:
|
staticnodiscardnoexcept |
Check if the given string is a valid URI reference per RFC 3986 (absolute or relative) without constructing a full URI object. For example:
|
nodiscard |
|
staticnodiscard |
Remove the "." and ".." segments from a URI path per RFC 3986 Section 5.2.4, preserving leading ".." segments in a relative path. For example:
|
nodiscard |
Get the path part of the URI, if any. For example:
| auto sourcemeta::core::URI::path | ( | const std::string & | path | ) | -> URI & |
| auto sourcemeta::core::URI::path | ( | std::string && | path | ) | -> URI & |
Set the path part of the URI with move semantics. For example:
|
nodiscard |
Get the port part of the URI, if any. Parsing rejects a port that does not fit in 32 bits even though RFC 3986 leaves the production unbounded. For example:
|
nodiscard |
Get the query part of the URI as a navigable view, if any. For example:
| auto sourcemeta::core::URI::query | ( | const std::string_view | query | ) | -> URI & |
Set the query part of the URI. A leading ? in the input is stripped. Passing an empty string clears the query. For example:
Move a URI that lies under a base to the same position under a new base. A URI that is neither the base nor under it is left intact, and so is one that only shares a textual prefix without matching whole path segments. For example:
Move a URI that lies under a base to the same position under a new base, taking components out of new_base rather than copying them. A URI that is neither the base nor under it is left intact. For example:
|
staticnodiscard |
Replace a URI path prefix with a new prefix. For example:
|
nodiscard |
Recompose a URI as established by RFC 3986. For example:
|
nodiscard |
Recompose the path, query, and fragment of a URI as an RFC 3986 Section 4.2 relative reference. Scheme and authority are omitted. The result only resolves back to the original URI when used against a base that shares the same scheme and authority. For example:
|
nodiscard |
Recompose a URI as established by RFC 3986, but without including the fragment component. The result is an optional to handle the case where the input URI only consists of a fragment. For example:
Express a URI as a relative reference against a base URI, such that resolving the result against that base reproduces this URI:
That equation is the definition of a correct result, as RFC 3986 states how to resolve a reference but never how to compute one. It holds when both URIs are absolute and the target path carries no dot segments. Resolution strips those from a reference that keeps its scheme just as it does from a relative one, so a target carrying them is outside the guarantee whether or not a reference gets built. Within those bounds, a URI left intact because no reference expresses it satisfies the equation as well. For example:
Resolve a relative URI against a base URI as established by RFC 3986. For example:
|
nodiscard |
|
staticnodiscard |
Strip a URI path prefix and return the remaining suffix. For example:
|
nodiscard |
|
staticnodiscard |
Percent-decode every escape sequence in a string per RFC 3986, leaving malformed sequences untouched. For example:
|
inlinestaticnodiscard |
Decode an "application/x-www-form-urlencoded" component (RFC 6749 Appendix B and the HTML URL-encoded form syntax), appending the decoded bytes to the output. Besides a std::string the sink can be a wiping string for a secret such as a decoded client credential. Each "+" becomes a space and each "%" followed by two hexadecimal digits becomes its octet. A "%" that is not followed by two hexadecimal digits is rejected, returning false with the output restored to its original contents. The output must not alias the input. For example:
|
nodiscard |
Get the user information part of the URI, if any. For example:
As mentioned in RFC 3986, the format "user:password" is deprecated. Applications should not render as clear text any data after the first colon. See https://tools.ietf.org/html/rfc3986#section-3.2.1
| auto sourcemeta::core::URI::userinfo | ( | const std::string_view | userinfo | ) | -> URI & |
Set the user information part of the URI. Passing an empty string clears the user information. For example:
| class sourcemeta::core::URIParseError |
An error that represents a URI parsing failure
Public Member Functions | |
| URIParseError (const std::uint64_t column) | |
| Construct an error from the column at which parsing failed. | |
| auto | column () const noexcept -> std::uint64_t |
| Get the column number of the error. | |
| class sourcemeta::core::URIError |
An error that represents a general URI error event