The class url
is a container used to store and produce URLs. The URL
parsing functions can be used to create a new container from a url_view
:
string_view s = "https://www.example.com"; url_view u = parse_uri(s).value(); url v(u);
All url_view
observers are also available for a url
:
std::cout << v << "\n" "scheme: " << v.scheme() << "\n" "has authority: " << v.has_authority() << "\n" "authority: " << v.encoded_authority() << "\n" "path: " << v.encoded_path() << "\n";
The interface of url_view
decomposes the URL into
its individual parts and allows for inspection of the various parts as well
as returning metadata about the URL itself.
The decoded variant of the modifier functions will encode any characters that would be invalid for the specified component.
Code |
Output |
---|---|
v.set_host("www.my example.com"); std::cout << v << "\n"; |
http://www.my%20example.com |
v.set_host("my website.com"); v.set_path("my file.txt"); v.set_query("id=42&name=John Doe"); std::cout << v << "\n"; |
https://my%20website.com/my%20file.txt?id=42&name=John%20Doe |
The encoded variant of the modifier functions require that the encoded strings are valid for the specified component. The functions will throw if the input string contains any character invalid for the specified component.
Code |
Output |
---|---|
v.set_scheme("http"); std::cout << v << "\n"; |
http://www.example.com |
For each observer function in url_view
, an instance of url
provides a corresponding set
function to define the value of the specified component.
Component |
Decoded |
Encoded |
---|---|---|
authority |
||
fragment |
||
host |
||
password |
||
path |
||
path_absolute |
||
port |
||
query |
||
scheme |
||
user |
||
userinfo |