2020-03-30 17:04:18 +03:00
|
|
|
|
#pragma once
|
2023-04-01 06:18:41 +03:00
|
|
|
|
///@file
|
2020-03-30 17:04:18 +03:00
|
|
|
|
|
2020-05-12 00:52:15 +03:00
|
|
|
|
#include "error.hh"
|
2020-03-30 17:04:18 +03:00
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
struct ParsedURL
|
|
|
|
|
{
|
|
|
|
|
std::string url;
|
2023-03-27 04:12:25 +03:00
|
|
|
|
/// URL without query/fragment
|
|
|
|
|
std::string base;
|
2020-03-30 17:04:18 +03:00
|
|
|
|
std::string scheme;
|
|
|
|
|
std::optional<std::string> authority;
|
|
|
|
|
std::string path;
|
|
|
|
|
std::map<std::string, std::string> query;
|
|
|
|
|
std::string fragment;
|
|
|
|
|
|
|
|
|
|
std::string to_string() const;
|
|
|
|
|
|
|
|
|
|
bool operator ==(const ParsedURL & other) const;
|
2023-11-14 17:00:21 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove `.` and `..` path elements.
|
|
|
|
|
*/
|
|
|
|
|
ParsedURL canonicalise();
|
2020-03-30 17:04:18 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
MakeError(BadURL, Error);
|
|
|
|
|
|
|
|
|
|
std::string percentDecode(std::string_view in);
|
2023-02-07 17:44:37 +02:00
|
|
|
|
std::string percentEncode(std::string_view s, std::string_view keep="");
|
2020-03-30 17:04:18 +03:00
|
|
|
|
|
|
|
|
|
std::map<std::string, std::string> decodeQuery(const std::string & query);
|
|
|
|
|
|
|
|
|
|
ParsedURL parseURL(const std::string & url);
|
|
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
|
/**
|
2020-10-16 02:35:24 +03:00
|
|
|
|
* Although that’s not really standardized anywhere, an number of tools
|
|
|
|
|
* use a scheme of the form 'x+y' in urls, where y is the “transport layer”
|
|
|
|
|
* scheme, and x is the “application layer” scheme.
|
|
|
|
|
*
|
|
|
|
|
* For example git uses `git+https` to designate remotes using a Git
|
|
|
|
|
* protocol over http.
|
|
|
|
|
*/
|
|
|
|
|
struct ParsedUrlScheme {
|
|
|
|
|
std::optional<std::string_view> application;
|
|
|
|
|
std::string_view transport;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ParsedUrlScheme parseUrlScheme(std::string_view scheme);
|
|
|
|
|
|
2023-09-28 17:52:28 +03:00
|
|
|
|
/* Detects scp-style uris (e.g. git@github.com:NixOS/nix) and fixes
|
|
|
|
|
them by removing the `:` and assuming a scheme of `ssh://`. Also
|
|
|
|
|
changes absolute paths into file:// URLs. */
|
|
|
|
|
std::string fixGitURL(const std::string & url);
|
|
|
|
|
|
2023-12-06 16:14:41 +02:00
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
bool isValidSchemeName(std::string_view scheme);
|
|
|
|
|
|
2020-03-30 17:04:18 +03:00
|
|
|
|
}
|