Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext

Parsing

Algorithms which parse URLs return a view which references the underlying character buffer without taking ownership, avoiding memory allocations and copies. The following example parses a string literal containing a URI:

string_view s = "https://user:pass@example.com:443/path/to/my%2dfile.txt?id=42&name=John%20Doe+Jingleheimer%2DSchmidt#page%20anchor";

The function returns a result which holds a url_view if the string is a valid URL. Otherwise it holds an error_code. It is impossible to construct a url_view which refers to an invalid URL.

[Warning] Warning

The caller is responsible for ensuring that the lifetime of the character buffer extends until it is no longer referenced by the view. These are the same semantics as that of std::string_view.

For convenience, a URL view can be constructed directly from the character buffer in a string_view. In this case, it parses the string according to the URI-reference grammar, throwing an exception upon failure. The following two statements are equivalent:

result<url_view> r = parse_uri( s );

In this library, free functions which parse things are named with the word "parse" followed by the name of the grammar used to match the string. There are several varieties of URLs, and depending on the use-case a particular grammar may be needed. In the target of an HTTP GET request for example, the scheme and fragment are omitted. This corresponds to the origin-form production rule described in rfc7230. The function parse_origin_form is suited for this purpose. All the URL parsing functions are listed here:

Table 1.5. Parsing Functions

Function

Grammar

Example

Notes

parse_absolute_uri

absolute-URI

http://www.boost.org/index.html?field=value

No fragment

parse_origin_form

origin-form

/index.html?field=value

Used in HTTP

parse_relative_ref

relative-ref

//www.boost.org/index.html?field=value#downloads

parse_uri

URI

http://www.boost.org/index.html?field=value#downloads

parse_uri_reference

URI-reference

http://www.boost.org/index.html

Any URI or relative-ref


The URL is stored in its serialized form. Therefore, it can always be easily output, sent, or embedded as part of a protocol:

url u = parse_uri_reference( "https://www.example.com/path/to/file.txt" ).value();

assert(u.encoded_path() == "/path/to/file.txt");

A url is an allocating container which owns its character buffer. Upon construction from url_view, it allocates dynamic storage to hold a copy of the string.

result< url > rv = parse_uri_reference( "https://www.example.com/path/to/file.txt" );

static_assert( std::is_convertible< result< url_view >, result< url > >::value, "" );

A static_url is a container which owns its character buffer for a URL whose maximum size is known. Upon construction from url_view, it does not perform any dynamic memory allocations.

result< static_url<1024> > rv = parse_uri_reference( "https://www.example.com/path/to/file.txt" );

static_assert( std::is_convertible< result< static_url<1024> >, result< url > >::value, "" );
Result Type

In many places, functions in the library have a return type which uses the result alias template. This class allows the parsing algorithms to report errors without referring to exceptions.

The functions result::has_value and result::has_error can be used to check if the result contains an error.

result< url > ru = parse_uri_reference( "https://www.example.com/path/to/file.txt" );
if ( ru.has_value() )
{
    url u = *ru;
    assert(u.encoded_path() == "/path/to/file.txt");
}

This ensures result::value does not throw an error. In contexts where it is acceptable to throw errors, result::value can be used directly.

Check the reference for result for a synopsis of the type. For complete information please consult the full result documentation in Boost.System.


PrevUpHomeNext