Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
nix::ParsedURL Struct Reference

#include <url.hh>

Classes

struct  Authority

Public Member Functions

std::string renderAuthorityAndPath () const
std::string to_string () const
std::string renderPath (bool encode=false) const
auto operator<=> (const ParsedURL &other) const noexcept=default
ParsedURL canonicalise ()
auto pathSegments (bool skipEmpty) const &

Public Attributes

std::string scheme
std::optional< Authority > authority
std::vector< std::string > path
StringMap query
std::string fragment

Detailed Description

Represents a parsed RFC3986 URL.

Note
All fields are already percent decoded.

Member Function Documentation

◆ canonicalise()

ParsedURL nix::ParsedURL::canonicalise ( )

Remove . and .. path segments.

◆ pathSegments()

auto nix::ParsedURL::pathSegments ( bool skipEmpty) const &
inline

Get a range of path segments (the substrings separated by '/' characters).

Parameters
skipEmptySkip all empty path segments

◆ renderAuthorityAndPath()

std::string nix::ParsedURL::renderAuthorityAndPath ( ) const

Render just the middle part of a URL, without the // which indicates whether the authority is present.

Note
This is kind of an ad-hoc operation, but it ends up coming up with some frequency, probably due to the current design of StoreReference in nix-store.

◆ renderPath()

std::string nix::ParsedURL::renderPath ( bool encode = false) const

Render the path to a string.

Parameters
encodeWhether to percent encode path segments.

Member Data Documentation

◆ authority

std::optional<Authority> nix::ParsedURL::authority

Optional parsed authority component of the URL.

IMPORTANT: An empty authority (i.e. one with an empty host string) and a missing authority (std::nullopt) are drastically different cases. This is especially important for "file:///path/to/file" URLs defined by RFC8089. The presence of the authority is indicated by // following the <scheme>: part of the URL.

◆ path

std::vector<std::string> nix::ParsedURL::path
Note
Unlike Unix paths, URLs provide a way to escape path separators, in the form of the %2F encoding of /. That means that if one percent-decodes the path into a single string, that decoding will be lossy, because / and %2F both become /. The right thing to do is instead split up the path on /, and then percent decode each part.

For an example, the path

foo/bar%2Fbaz/quux

is parsed as

{"foo, "bar/baz", "quux"}

We're doing splitting and joining that assumes the separator (/ in this case) only goes between elements.

That means the parsed representation will begin with an empty element to make an initial /, and will end with an ementy element to make a trailing /. That means that elements of this vector mostly, but not always, correspond to segments of the path.

Examples:

Note that both trailing and leading slashes are, in general, semantically significant.

For trailing slashes, the main example affecting many schemes is that ../baz resolves against a base URL different depending on the presence/absence of a trailing slash:

See parseURLRelative for more details.

For leading slashes, there are some requirements to be aware of.

  • When there is an authority, the path must start with a leading slash. Otherwise the path will not be separated from the authority, and will not round trip though the parser:

    {.scheme="https", .authority.host = "foo", .path={"bad"}}

    will render to https://foobar. but that would parse back as as

    {.scheme="https", .authority.host = "foobar", .path={}}
  • When there is no authority, the path must not begin with two slashes. Otherwise, there will be another parser round trip issue:

    {.scheme="https", .path={"", "", "bad"}}

    will render to https://bad. but that would parse back as as

    {.scheme="https", .authority.host = "bad", .path={}}

These invariants will be checked in to_string and renderAuthorityAndPath.


The documentation for this struct was generated from the following files:
  • libutil/include/nix/util/url.hh
  • libutil/url.cc