The scheme is the top-level hierarchical element which defines the syntax and semantics of the rest of the URL. The scheme identifier is always followed by a colon when it appears in a URL.
Here are some examples of URLs with the schemes https
and file
:
https://www.example.com/path/to/file.txt?page=2
file:///usr/local/bin/
A scheme must start with a letter, and may contain only letters, digits, plus and minus signs, and periods:
Table 1.7. Scheme BNF
scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) absolute-URI = scheme ":" hier-part [ "?" query ] URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ] |
The functions for inspecting the scheme in a url_view
are as follows:
Table 1.8. Scheme Observers
Function |
Description |
---|---|
Return true if a scheme is present |
|
Return the scheme as a string |
|
Return the scheme as a known-scheme enumeration constant |
Note | |
---|---|
None of these functions throw exceptions. If the URL has no scheme, |
The function scheme
can be used to obtain the scheme from a url_view
:
Code |
Output |
---|---|
urls::string_view s = "mailto:name@email.com"; urls::url_view u = urls::parse_uri( s ).value(); std::cout << u.scheme() << "\n"; |
mailto |
If the URL has no scheme, this function returns an empty string. To check
whether a URL contains a scheme the function url_view::has_scheme
might be used.
Code |
Output |
---|---|
urls::url_view u = urls::parse_uri( s ).value(); if (u.has_scheme()) { std::cout << u.scheme() << "\n"; } |
mailto |
The library defines an enumeration of values for some well-known scheme identifiers.
urls::string_view s = "file://host/path/to/file"; urls::url_view u = urls::parse_uri( s ).value(); if (u.scheme_id() == urls::scheme::file) { // handle file }
These may be used instead of their corresponding strings:
Table 1.9. Scheme IDs
ID |
Description |
---|---|
Indicates no scheme present |
|
A valid but unknown scheme |
|
File Transfer Protocol ("FTP") |
|
File URI Scheme |
|
Hypertext Transfer Protocol |
|
Secure Hypertext Transfer Protocol |
|
WebSocket Protocol |
|
Secure WebSocket Protocol |
A number of schemes are used to define the semantics of URLs. For instance,
the term web address is often used informally to describe URLs with the
http
scheme, whose semantics
are defined by rfc3986.
One of the conventions of the HTTP scheme is that when the port 80 is implicitly
assumed when it is not provided. Such conventions are not part of the URL
protocol.
Schemes are also different from protocols. Although the scheme http
is used to interact with resources
via the HTTP protocol, the scheme file
has no corresponding protocol.
Some noteworthy IANA-registered schemes are
Table 1.10. Scheme IDs
Scheme |
Resource |
---|---|
SMTP/MIME messages |
|
Inline data |
|
WebDAV |
|
Domain Name System |
|
File systems |
|
FTP resources |
|
GIT repository |
|
HTTP resources |
|
HTTP secured using SSL/TLS |
|
Secure WebSocket Protocol |
|
Identify files by content |
|
Network File System |
|
POP3 |
|
Amazon S3 |
|
SMS messages |
|
Subversion (SVN) repository |
|
Telephone number |
|
Streaming protocols over UDP |
|
Uniform Resource Names |
|
WebSocket Protocol |
|
Secure WebSocket Protocol |
Many other valid but unofficial schemes are common:
Table 1.11. Scheme IDs
Scheme |
Resource |
---|---|
Digital Object Identifier |
|
Javascript Code |
|
Open Database Connectivity |
|
Slack Client |