Boost.URL Logo

PrevUpHomeNext

Scheme

Notation

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 ]

Member Functions

The functions for inspecting the scheme in a url_view are as follows:

Table 1.8. Scheme Observers

Function

Description

has_scheme

Return true if a scheme is present

scheme

Return the scheme as a string

scheme_id

Return the scheme as a known-scheme enumeration constant


[Note] Note

None of these functions throw exceptions. If the URL has no scheme, scheme returns an empty string. If the function scheme_id identifies a valid but unknown scheme, the value scheme::unknown is returned.

Observers

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
Common schemes

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

scheme::none

Indicates no scheme present

scheme::unknown

A valid but unknown scheme

scheme::ftp

File Transfer Protocol ("FTP")

scheme::file

File URI Scheme

scheme::http

Hypertext Transfer Protocol

scheme::https

Secure Hypertext Transfer Protocol

scheme::ws

WebSocket Protocol

scheme::wss

Secure WebSocket Protocol


Use Cases

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

cid

SMTP/MIME messages

data

Inline data

dav

WebDAV

dns

Domain Name System

file

File systems

ftp

FTP resources

git

GIT repository

http

HTTP resources

https

HTTP secured using SSL/TLS

mailto

Secure WebSocket Protocol

magnet

Identify files by content

nfs

Network File System

pop

POP3

s3

Amazon S3

sms

SMS messages

svn

Subversion (SVN) repository

tel

Telephone number

udp

Streaming protocols over UDP

urn

Uniform Resource Names

ws

WebSocket Protocol

wss

Secure WebSocket Protocol


Many other valid but unofficial schemes are common:

Table 1.11. Scheme IDs

Scheme

Resource

doi

Digital Object Identifier

javascript

Javascript Code

odbc

Open Database Connectivity

slack

Slack Client



PrevUpHomeNext