mod_servlet
C++Servlets
|
Represents a Uniform Resource Identifier (URI) reference. More...
#include <uri.h>
Public Types | |
typedef std::string | string_type |
The URI string_type. | |
typedef std::experimental::string_view | string_view |
The URI string_view. | |
typedef string_view::const_iterator | const_iterator |
The URI const_iterator type. | |
Public Member Functions | |
URI () | |
Default constructor. More... | |
URI (const string_type &uri_str) | |
Constructs a URI by parsing the given string. More... | |
URI (string_type &&uri_str) | |
Constructs a URI by parsing the given string. More... | |
URI (string_view uri_str) | |
Constructs a URI by parsing the given string. More... | |
URI (const char *uri_str) | |
Constructs a URI by parsing the given string. More... | |
template<class InputIter > | |
URI (InputIter first, InputIter last) | |
Constructs a URI by parsing the given string. More... | |
URI (string_view scheme, string_view userInfo, string_view host, uint16_t port, string_view path, string_view query, string_view fragment) | |
Constructs a hierarchical URI from the given components. More... | |
URI (string_view scheme, string_view host, uint16_t port, string_view path, string_view query) | |
Constructs a hierarchical URI from the given components. More... | |
URI (const URI &other) | |
Copy constructor. More... | |
URI (URI &&other) noexcept | |
Move constructor. More... | |
~URI () noexcept | |
Destructor. | |
URI & | operator= (const URI &other) |
Copy assignment operator. More... | |
URI & | operator= (URI &&other) |
Move assignment operator. More... | |
URI & | operator= (const string_type &uri_str) |
String assignment operator. More... | |
URI & | operator= (string_type &&uri_str) |
Move string assignment operator. More... | |
URI & | operator= (string_view uri_str) |
String view assignment operator. More... | |
URI & | operator= (const char *uri_str) |
C-string assignment operator. More... | |
void | swap (URI &other) noexcept |
Swaps one uri object with another. More... | |
const_iterator | begin () const noexcept |
Returns an iterator at the first element in the underlying URI view. More... | |
const_iterator | end () const noexcept |
Returns an iterator at the last+1 element in the underlying URI view. More... | |
string_view | scheme () const noexcept |
Returns the scheme component of this URI. More... | |
void | set_scheme (string_view scheme) |
Sets scheme component to this URI. More... | |
string_view | user_info () const noexcept |
Returns the userInfo component of this URI. More... | |
void | set_user_info (string_view user_info) |
Sets user_info component to this URI. More... | |
string_view | host () const noexcept |
Returns the host component of this URI. More... | |
void | set_host (string_view host) |
Sets host component to this URI. More... | |
string_view | port_view () const noexcept |
Returns the port view component of this URI. More... | |
uint16_t | port () const noexcept |
Returns the port component of this URI. More... | |
void | set_port (string_view port) |
Sets port component to this URI. More... | |
void | set_port (uint16_t port) |
Sets port component to this URI. More... | |
string_view | path () const noexcept |
Returns the path view component of this URI. More... | |
void | set_path (string_view path) |
Sets path component to this URI. More... | |
string_view | query () const noexcept |
Returns the query view component of this URI. More... | |
void | set_query (string_view query) |
Sets query component to this URI. More... | |
void | add_to_query (string_view name, string_view value) |
Adds name-value pair to the current query component of this URI. More... | |
void | parse_query (std::function< void(std::string &&, std::string &&)> consumer) |
Parses this URI's query into name-value pairs and feeds them to a given consumer. More... | |
string_view | fragment () const noexcept |
Returns the fragment view component of this URI. More... | |
void | set_fragment (string_view fragment) |
Sets fragment component to this URI. More... | |
bool | has_authority () const noexcept |
Tests whether this URI has a valid authority. More... | |
string_view | authority () const noexcept |
Returns the raw authority component of this URI. More... | |
string_view | uri_view () const noexcept |
Returns the string view representation of this URI. More... | |
const std::string & | string () const noexcept |
Returns the URI as a std::string object. More... | |
std::string && | string_move () |
Returns the URI as a std::string&& object. More... | |
std::string | to_ASCII_string () const |
Returns the content of this URI as a US-ASCII string. More... | |
bool | empty () const noexcept |
Tells whether or not this URI is empty. More... | |
bool | is_absolute () const noexcept |
Tells whether or not this URI is absolute. More... | |
bool | is_opaque () const noexcept |
Tells whether or not this URI is opaque. More... | |
void | normalize_path () |
Normalizes this URI's path. More... | |
void | normalize () |
Normalizes this URI. More... | |
URI | create_normalized () const |
Create normalized copy of this URI. More... | |
URI | create_with_normalized_path () const |
Create copy of this URI with normalized path. More... | |
URI | relativize (const URI &uri) const |
Relativizes the given URI against this URI. More... | |
URI | resolve (const URI &uri) const |
Resolves the given URI against this URI. More... | |
int | compare (const URI &other) const noexcept |
Compares this URI against another. More... | |
Static Public Member Functions | |
static void | parse_query (string_view query, std::function< void(std::string &&, std::string &&)> consumer) |
Parses a given query string into name-value pairs and feeds them to a given consumer. More... | |
static uint16_t | get_default_port (string_view scheme) |
Returns default port for given scheme or 0 if the default port could not be found. More... | |
static std::string | decode (string_view str) |
Decode all the persent encoded characters in the string. More... | |
Represents a Uniform Resource Identifier (URI) reference.
This class conforms to a URI as defined by RFC 3986, RFC 3987 and RFC 2732, including scoped IDs. It provides member functions for normalizing, comparing and resolving URIs.
This class provides constructors for creating URI instances from their components or by parsing their string forms, methods for accessing the various components of an instance, and methods for normalizing, resolving, and relativizing URI instances. Instances of this class are mutable.
At the highest level a URI reference (hereinafter simply "URI") in string form has the syntax
[scheme
:
]scheme-specific-part[#
fragment]
where square brackets [...] delineate optional components and the characters :
and #
stand for themselves.
An absolute URI specifies a scheme; a URI that is not absolute is said to be relative. URIs are also classified according to whether they are opaque or hierarchical.
An opaque URI is an absolute URI whose scheme-specific part does not begin with a slash character ('/'
). Opaque URIs are not subject to further parsing. Some examples of opaque URIs are:
java- net@ java. sun. com
news:comp.lang.java
A hierarchical URI is either an absolute URI whose scheme-specific part begins with a slash character, or a relative URI, that is, a URI that does not specify a scheme. Some examples of hierarchical URIs are:
http://java.sun.com/j2se/1.3/
docs/guide/collections/designfaq.html#28
../../../demo/jfc/SwingSet2/src/SwingSet2.java
file:///~/calendar
A hierarchical URI is subject to further parsing according to the syntax
[scheme
:
][//
authority][path][?
query][#
fragment]
where the characters :
, /
, ?
, and #
stand for themselves. The scheme-specific part of a hierarchical URI consists of the characters between the scheme and fragment components.
The authority component of a hierarchical URI is, if specified, either server-based or registry-based. A server-based authority parses according to the familiar syntax
[user-info
@
]host[:
port]
where the characters @
and :
stand for themselves. Nearly all URI schemes currently in use are server-based. An authority component that does not parse in this way is considered to be registry-based.
The path component of a hierarchical URI is itself said to be absolute if it begins with a slash character ('/'
); otherwise it is relative. The path of a hierarchical URI that is either absolute or specifies an authority is always absolute.
All told, then, a URI instance has the following nine components:
Component Type scheme string_view
scheme-specific-part string_view
authority string_view
user-info string_view
host string_view
port uint16_t
path string_view
query string_view
fragment string_view
In a given instance any particular component is either undefined or defined with a distinct value. Undefined string components are represented by empty view
, while undefined integer components are represented by 0
.
Whether a particular component is or is not defined in an instance depends upon the type of the URI being represented. An absolute URI has a scheme component. An opaque URI has a scheme, a scheme-specific part, and possibly a fragment, but has no other components. A hierarchical URI always has a path (though it may be empty) and a scheme-specific-part (which at least contains the path), and may have any of the other components. If the authority component is present and is server-based then the host component will be defined and the user-information and port components may be defined.
The key operations supported by this class are those of normalization, resolution, and relativization.
Normalization is the process of removing unnecessary "."
and ".."
segments from the path component of a hierarchical URI. Each "."
segment is simply removed. A ".."
segment is removed only if it is preceded by a non-".."
segment. Normalization has no effect upon opaque URIs.
Resolution is the process of resolving one URI against another, base URI. The resulting URI is constructed from components of both URIs in the manner specified by RFC 2396, taking components from the base URI for those not specified in the original. For hierarchical URIs, the path of the original is resolved against the path of the base and then normalized. The result, for example, of resolving
docs/guide/collections/designfaq.html#28
(1)
against the base URI http://java.sun.com/j2se/1.3/
is the result URI
http://java.sun.com/j2se/1.3/docs/guide/collections/designfaq.html#28
Resolving the relative URI
../../../demo/jfc/SwingSet2/src/SwingSet2.java
(2)
against this result yields, in turn,
http://java.sun.com/j2se/1.3/demo/jfc/SwingSet2/src/SwingSet2.java
Resolution of both absolute and relative URIs, and of both absolute and relative paths in the case of hierarchical URIs, is supported. Resolving the URI file:///~calendar
against any other URI simply yields the original URI, since it is absolute. Resolving the relative URI (2) above against the relative base URI (1) yields the normalized, but still relative, URI
demo/jfc/SwingSet2/src/SwingSet2.java
Relativization, finally, is the inverse of resolution: For any two normalized URIs u and v,
u
.relativize(
u.resolve(
v)) ==
vand
u.resolve(
u.relativize(
v)) ==
v.
This operation is often useful when constructing a document containing URIs that must be made relative to the base URI of the document wherever possible. For example, relativizing the URI
against the base URI
yields the relative URI docs/guide/index.html
.
A URI is a uniform resource identifier while a URL is a uniform resource locator. Hence every URL is a URI, abstractly speaking, but not every URI is a URL. This is because there is another subcategory of URIs, uniform resource names (URNs), which name resources but do not specify how to locate them. The mailto
, news
, and isbn
URIs shown above are examples of URNs.
An instance of this class represents a URI reference in the syntactic sense defined by RFC 2396. A URI may be either absolute or relative. A URI string is parsed according to the generic syntax without regard to the scheme, if any, that it specifies. No lookup of the host, if any, is performed, and no scheme-dependent stream handler is constructed. Equality, hashing, and comparison are defined strictly in terms of the character content of the instance. In other words, a URI instance is little more than a structured string that supports the syntactic, scheme-independent operations of comparison, normalization, resolution, and relativization.
|
inline |
Default constructor.
Creates empty URI.
|
inlineexplicit |
Constructs a URI by parsing the given string.
This constructor parses the given string as specified by the grammar in RFC 2396, Appendix A, except for the following deviations:
An empty authority component is permitted as long as it is followed by a non-empty path, a query component, or a fragment component. This allows the parsing of URIs such as "file:///foo/bar"
, which seems to be the intent of RFC 2396 although the grammar does not permit it. If the authority component is empty then the user-information, host, and port components are undefined.
Empty relative paths are permitted; this seems to be the intent of RFC 2396 although the grammar does not permit it. The primary consequence of this deviation is that a standalone fragment such as "#foo"
parses as a relative URI with an empty path and the given fragment, and can be usefully resolved against a base URI.
IPv4 addresses are not validated.
IPv6 addresses are permitted for the host component. An IPv6 address must be enclosed in square brackets ('['
and ']'
) as specified by RFC 2732. Other than this constraint IPv6 addresses are not validated.
Characters in the other category are permitted wherever RFC 2396 permits escaped octets, that is, in the user-information, path, query, and fragment components, as well as in the authority component if the authority is registry-based. This allows URIs to contain Unicode characters beyond those in the US-ASCII character set.
uri_str | The string to be parsed into a URI |
uri_syntax_error | If the given string violates RFC 2396, as augmented by the above deviations |
|
inlineexplicit |
Constructs a URI by parsing the given string.
URI string is parsed as described in URI(const string_type&)
uri_str | The string to be parsed into a URI |
uri_syntax_error | If the given string violates RFC 2396, as augmented by the above deviations |
|
inlineexplicit |
Constructs a URI by parsing the given string.
URI string is parsed as described in URI(const string_type&)
uri_str | The string to be parsed into a URI |
uri_syntax_error | If the given string violates RFC 2396, as augmented by the above deviations |
|
inlineexplicit |
Constructs a URI by parsing the given string.
URI string is parsed as described in URI(const string_type&)
uri_str | The string to be parsed into a URI |
uri_syntax_error | If the given string violates RFC 2396, as augmented by the above deviations |
|
inline |
Constructs a URI by parsing the given string.
URI string is constructed from first, last iterators and the string is parsed further as described in URI(const string_type&)
InputIter | input iterator type from which URI string will be created |
uri_syntax_error | If the given string violates RFC 2396, as augmented by the above deviations |
servlet::URI::URI | ( | string_view | scheme, |
string_view | userInfo, | ||
string_view | host, | ||
uint16_t | port, | ||
string_view | path, | ||
string_view | query, | ||
string_view | fragment | ||
) |
Constructs a hierarchical URI from the given components.
If a scheme is given then the path, if also given, must either be empty or begin with a slash character ('/'
). If the path begins not with slash, than slash will be prepended to it to facilitate relative paths. Otherwise initial component of the new URI may be left undefined by passing empty view for the corresponding parameter or, in the case of the port
parameter, by passing 0
or negative value.
This constructor first builds a URI string from the given components according to the rules specified in RFC 2396, section 5.2, step 7:
Initially, the result string is empty.
If a scheme is given then it is appended to the result, followed by a colon character (':'
).
If user information, a host, or a port are given then the string "//"
is appended.
If user information is given then it is appended, followed by a commercial-at character ('@'
).
If a host is given then it is appended.
If a port number is given then a colon character (':'
) is appended, followed by the port number in decimal.
If a path is given then it is appended.
If a query is given then a question-mark character ('?'
) is appended, followed by the query.
Finally, if a fragment is given then a hash character ('#'
) is appended, followed by the fragment.
No further parsing for the resulting URI string is attempted.
scheme | Scheme name |
userInfo | User name and authorization information |
host | Host name |
port | Port number |
path | Path |
query | Query |
fragment | Fragment |
|
inline |
Constructs a hierarchical URI from the given components.
This constructor follows the same rules as URI(string_view scheme, string_view userInfo, string_view host, uint16_t port, string_view path, string_view query, string_view fragment) but with empty userInfo and fragment.
scheme | Scheme name |
host | Host name |
port | Port number |
path | Path |
query | Query |
void servlet::URI::add_to_query | ( | string_view | name, |
string_view | value | ||
) |
Adds name-value pair to the current query component of this URI.
If query component already exists, this method will append "&name=value"
string, otherwise new query component will be created as "name=value"
No parsing or validation is performed in this method.
|
noexcept |
Returns the raw authority component of this URI.
The authority component of a URI, if defined, only contains the commercial-at character ('@'
) and characters in the unreserved, punct, escaped, and other categories. If the authority is server-based then it is further constrained to have valid user-information, host, and port components.
null
if the authority is undefined
|
inlinenoexcept |
|
noexcept |
Compares this URI against another.
Before comparing the URIs are normalized. If this is not a desired behaviour comparison operators should be used to compare URIs without normalization.
other | The URI to compare to this. |
0
if the URIs are considered equal, negative value if this is less than other and and positive value if this is greater than other. URI servlet::URI::create_normalized | ( | ) | const |
URI servlet::URI::create_with_normalized_path | ( | ) | const |
Create copy of this URI with normalized path.
Copy of this URI is created and normalize_path method is called on the copy
|
static |
Decode all the persent encoded characters in the string.
str | Strign view to decode. |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
static |
Returns default port for given scheme or 0
if the default port could not be found.
scheme | The scheme for which to find the default port |
0
if the default port is not found.
|
noexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
noexcept |
Tells whether or not this URI is opaque.
A URI is opaque if, and only if, it is absolute and its scheme-specific part does not begin with a slash character ('/'). An opaque URI has a scheme, a scheme-specific part, and possibly a fragment; all other components are undefined.
true
if, and only if, this URI is opaque void servlet::URI::normalize | ( | ) |
Normalizes this URI.
The normalization includes:
All alphabetic characters in the scheme component are converted to lower-case
All persent encoded characters are decoded
Path component is normalized as described in normalize_path
void servlet::URI::normalize_path | ( | ) |
Normalizes this URI's path.
The path is normalized in a manner consistent with RFC 2396, section 5.2, step 6, sub-steps c through f; that is:
All "."
segments are removed.
If a ".."
segment is preceded by a non-".."
segment then both of these segments are removed. This step is repeated until it is no longer applicable.
A normalized path will begin with one or more ".."
segments if there were insufficient non-".."
segments preceding them to allow their removal. Otherwise, a normalized path will not contain any "."
or ".."
segments.
Copy assignment operator.
other | URI to copy from |
Move assignment operator.
other | URI to move from |
|
inline |
String assignment operator.
Assigns the string representation of URI to this URI object. The string is parsed following the rules to string constructor URI(const string_type&)
uri_str | The string to be parsed into a URI |
uri_syntax_error | If the given string violates RFC 2396, as augmented by the above deviations |
|
inline |
Move string assignment operator.
Assigns the string representation of URI to this URI object. The string is parsed following the rules to string constructor URI(const string_type&)
uri_str | The string to be parsed into a URI |
uri_syntax_error | If the given string violates RFC 2396, as augmented by the above deviations |
|
inline |
String view assignment operator.
Assigns the string representation of URI to this URI object. The string is parsed following the rules to string constructor URI(const string_type&)
uri_str | The string to be parsed into a URI |
uri_syntax_error | If the given string violates RFC 2396, as augmented by the above deviations |
|
inline |
C-string assignment operator.
Assigns the string representation of URI to this URI object. The string is parsed following the rules to string constructor URI(const string_type&)
uri_str | The string to be parsed into a URI |
uri_syntax_error | If the given string violates RFC 2396, as augmented by the above deviations |
|
inline |
Parses this URI's query into name-value pairs and feeds them to a given consumer.
This function is equivalent of calling:
consumer | std::function to feed name-value pairs to. |
|
static |
Parses a given query string into name-value pairs and feeds them to a given consumer.
Before adding the name-value pairs to the consumer the are decodes from all the persent encoded characters.
The consumer here is used for genericity not to restrict type of map object passed to the method. For example:
query | query string view |
consumer | std::function to feed name-value pairs to. |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
Relativizes the given URI against this URI.
The relativization of the given URI against this URI is computed as follows:
If either this URI or the given URI are opaque, or if the scheme and authority components of the two URIs are not identical, or if the path of this URI is not a prefix of the path of the given URI, then the given URI is returned.
Otherwise a new relative hierarchical URI is constructed with query and fragment components taken from the given URI and with a path component computed by removing this URI's path from the beginning of the given URI's path.
Resolves the given URI against this URI.
If the given URI is already absolute, or if this URI is opaque, then the given URI is returned.
<If the given URI's fragment component is defined, its path component is empty, and its scheme, authority, and query components are undefined, then a URI with the given fragment but with all other components equal to those of this URI is returned. This allows a URI representing a standalone fragment reference, such as "#foo"
, to be usefully resolved against a base URI.
Otherwise this method constructs a new hierarchical URI in a manner consistent with RFC 2396, section 5.2; that is:
A new URI is constructed with this URI's scheme and the given URI's query and fragment components.
If the given URI has an authority component then the new URI's authority and path are taken from the given URI.
Otherwise the new URI's authority component is copied from this URI, and its path is computed as follows:
If the given URI's path is absolute then the new URI's path is taken from the given URI.
Otherwise the given URI's path is relative, and so the new URI's path is computed by resolving the path of the given URI against the path of this URI. This is done by concatenating all but the last segment of this URI's path, if any, with the given URI's path and then normalizing the result as if by invoking the normalize method.
The result of this method is absolute if, and only if, either this URI is absolute or the given URI is absolute.
|
inlinenoexcept |
void servlet::URI::set_fragment | ( | string_view | fragment | ) |
Sets fragment component to this URI.
New fragment component replaces the old one. If new fragment component is empty, the current fragment component will be deleted and considered to be undefined
No parsing or validation is performed in this method.
fragment | New fragment component to this URI |
void servlet::URI::set_host | ( | string_view | host | ) |
void servlet::URI::set_path | ( | string_view | path | ) |
Sets path component to this URI.
New path component replaces the old one. If new path component is empty, the current path component will be deleted and considered to be undefined
No parsing or validation is performed in this method.
path | New path component to this URI |
uri_syntax_error | If the given path component is not empty and does not begin with slash character "/" |
void servlet::URI::set_port | ( | string_view | port | ) |
Sets port component to this URI.
New port component replaces the old one. If new port component is empty, the current port component will be deleted and considered to be undefined
This method validates port, by reading uint16_t representation. No forther parsing is performed in this method.
port | New port component to this URI |
void servlet::URI::set_port | ( | uint16_t | port | ) |
void servlet::URI::set_query | ( | string_view | query | ) |
void servlet::URI::set_scheme | ( | string_view | scheme | ) |
void servlet::URI::set_user_info | ( | string_view | user_info | ) |
Sets user_info component to this URI.
New user_info component replaces the old one. If new user_info component is empty, the current user_info component will be deleted and considered to be undefined
No parsing or validation is performed in this method.
user_info | New user_info component to this URI |
|
inlinenoexcept |
|
inline |
|
noexcept |
Swaps one uri object with another.
other | The other uri object. |
std::string servlet::URI::to_ASCII_string | ( | ) | const |
Returns the content of this URI as a US-ASCII string.
If this URI does not contain any characters in the other category then an invocation of this method will return the same value as an invocation of the string method. Otherwise this method works as if by invoking that method and then encoding the result.
|
inlinenoexcept |
|
inlinenoexcept |