Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
url.hh File Reference
#include <filesystem>
#include <ranges>
#include <span>
#include "nix/util/error.hh"
#include "nix/util/canon-path.hh"
#include "nix/util/split.hh"
#include "nix/util/util.hh"
#include "nix/util/variant-wrapper.hh"

Go to the source code of this file.

Classes

struct  nix::ParsedURL
struct  nix::ParsedURL::Authority
struct  nix::ParsedUrlScheme
struct  nix::VerbatimURL

Functions

std::ostream & nix::operator<< (std::ostream &os, const ParsedURL &url)
 nix::MakeError (BadURL, Error)
std::string nix::percentDecode (std::string_view in)
std::string nix::percentEncode (std::string_view s, std::string_view keep="")
std::string nix::renderUrlPathNoPctEncoding (std::span< const std::string > urlPath)
std::string nix::encodeUrlPath (std::span< const std::string > urlPath)
StringMap nix::decodeQuery (std::string_view query, bool lenient=false)
std::string nix::encodeQuery (const StringMap &query)
ParsedURL nix::parseURL (std::string_view url, bool lenient=false)
ParsedURL nix::parseURLRelative (std::string_view url, const ParsedURL &base)
ParsedUrlScheme nix::parseUrlScheme (std::string_view scheme)
ParsedURL nix::fixGitURL (std::string url)
bool nix::isValidSchemeName (std::string_view scheme)
std::vector< std::string > nix::pathToUrlPath (const std::filesystem::path &path)
std::filesystem::path nix::urlPathToPath (std::span< const std::string > urlPath)
std::ostream & nix::operator<< (std::ostream &os, const VerbatimURL &url)

Function Documentation

◆ decodeQuery()

StringMap nix::decodeQuery ( std::string_view query,
bool lenient = false )
Parameters
lenient
See also
parseURL

◆ encodeUrlPath()

std::string nix::encodeUrlPath ( std::span< const std::string > urlPath)

Percent encode path. %2F for "interior slashes" is the most important.

◆ fixGitURL()

ParsedURL nix::fixGitURL ( std::string url)

Detects scp-style uris (e.g. git@github.com:NixOS/nix) and fixes them by removing the : and assuming a scheme of ssh://. Also drops git+ from the scheme (e.g. git+https:// to https://) and changes absolute paths into file:// URLs.

See also
https://git-scm.com/docs/git-clone#_git_urls

ssh://[<user>@]<host>[:<port>]/<path-to-git-repo> git://<host>[:<port>]/<path-to-git-repo> http[s]://<host>[:<port>]/<path-to-git-repo> ftp[s]://<host>[:<port>]/<path-to-git-repo>

An alternative scp-like syntax may also be used with the ssh protocol: [<user>@]<host>:/<path-to-git-repo> This syntax is only recognized if there are no slashes before the first colon.

For local repositories, also supported by Git natively, the following syntaxes may be used: /path/to/repo.git/ file:///path/to/repo.git/

Note
file:/path/to/repo is recognised by libfetchers, but not git so this functions accepts it too. Technically this conflicts with the SCP-like syntax where file is the hostname, but it's special-cased.

◆ isValidSchemeName()

bool nix::isValidSchemeName ( std::string_view scheme)

Whether a string is valid as RFC 3986 scheme name. Colon : is part of the URI; not the scheme name, and therefore rejected. See https://www.rfc-editor.org/rfc/rfc3986#section-3.1

Does not check whether the scheme is understood, as that's context-dependent.

◆ parseURL()

ParsedURL nix::parseURL ( std::string_view url,
bool lenient = false )

Parse a URL into a ParsedURL.

Parameters
lenientAlso allow some long-supported Nix URIs that are not quite compliant with RFC3986. Here are the deviations:
  • Fragments can contain unescaped (not URL encoded) '^', '"' or space literals.
  • Queries may contain unescaped '"' or spaces.
Note
IPv6 ZoneId literals (RFC4007) are represented in URIs according to RFC6874.
Exceptions
BadURL

The WHATWG specification of the URL constructor in Java Script is also a useful reference: https://url.spec.whatwg.org/#concept-basic-url-parser. Note, however, that it includes various scheme-specific normalizations / extra steps that we do not implement.

◆ parseURLRelative()

ParsedURL nix::parseURLRelative ( std::string_view url,
const ParsedURL & base )

Like parseURL, but also accepts relative URLs, which are resolved against the given base URL.

This is specified in IETF RFC 3986, section 5

Exceptions
BadURL

Behavior should also match the new URL(url, base) JavaScript constructor, except for extra steps specific to the HTTP scheme. See parseURL for link to the relevant WHATWG standard.

◆ parseUrlScheme()

ParsedUrlScheme nix::parseUrlScheme ( std::string_view scheme)

Parse a URL scheme of the form '(applicationScheme+)?transportScheme' into a tuple '(applicationScheme, transportScheme)'

parseUrlScheme("http") == ParsedUrlScheme{ {}, "http"} parseUrlScheme("tarball+http") == ParsedUrlScheme{ {"tarball"}, "http"}

◆ pathToUrlPath()

std::vector< std::string > nix::pathToUrlPath ( const std::filesystem::path & path)

Convert a filesystem path to a URL path vector.

On Windows, converts backslashes to forward slashes and prepends a / before the drive letter (e.g., C:\foo\bar becomes /C:/foo/bar).

◆ renderUrlPathNoPctEncoding()

std::string nix::renderUrlPathNoPctEncoding ( std::span< const std::string > urlPath)

Render URL path segments to a string by joining with /. Does not percent-encode the segments.

◆ urlPathToPath()

std::filesystem::path nix::urlPathToPath ( std::span< const std::string > urlPath)

Convert a URL path vector to a native filesystem path.

On Windows, strips the leading / before the drive letter and converts to native format (e.g., /C:/foo/bar becomes C:\foo\bar).