1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-25 10:41:16 +02:00

ParsedURL: Remove url field

This prevents a 'url' field that is out of sync with the other
fields. You can use to_string() to get the full URL.
This commit is contained in:
Eelco Dolstra 2025-01-07 14:45:58 +01:00
parent a0901e5588
commit f705ce7f9a
8 changed files with 25 additions and 43 deletions

View file

@ -66,7 +66,7 @@ Input Input::fromURL(
} }
} }
throw Error("input '%s' is unsupported", url.url); throw Error("input '%s' is unsupported", url);
} }
Input Input::fromAttrs(const Settings & settings, Attrs && attrs) Input Input::fromAttrs(const Settings & settings, Attrs && attrs)

View file

@ -50,7 +50,7 @@ struct GitArchiveInputScheme : InputScheme
else if (std::regex_match(path[2], refRegex)) else if (std::regex_match(path[2], refRegex))
ref = path[2]; ref = path[2];
else else
throw BadURL("in URL '%s', '%s' is not a commit hash or branch/tag name", url.url, path[2]); throw BadURL("in URL '%s', '%s' is not a commit hash or branch/tag name", url, path[2]);
} else if (size > 3) { } else if (size > 3) {
std::string rs; std::string rs;
for (auto i = std::next(path.begin(), 2); i != path.end(); i++) { for (auto i = std::next(path.begin(), 2); i != path.end(); i++) {
@ -63,34 +63,34 @@ struct GitArchiveInputScheme : InputScheme
if (std::regex_match(rs, refRegex)) { if (std::regex_match(rs, refRegex)) {
ref = rs; ref = rs;
} else { } else {
throw BadURL("in URL '%s', '%s' is not a branch/tag name", url.url, rs); throw BadURL("in URL '%s', '%s' is not a branch/tag name", url, rs);
} }
} else if (size < 2) } else if (size < 2)
throw BadURL("URL '%s' is invalid", url.url); throw BadURL("URL '%s' is invalid", url);
for (auto &[name, value] : url.query) { for (auto &[name, value] : url.query) {
if (name == "rev") { if (name == "rev") {
if (rev) if (rev)
throw BadURL("URL '%s' contains multiple commit hashes", url.url); throw BadURL("URL '%s' contains multiple commit hashes", url);
rev = Hash::parseAny(value, HashAlgorithm::SHA1); rev = Hash::parseAny(value, HashAlgorithm::SHA1);
} }
else if (name == "ref") { else if (name == "ref") {
if (!std::regex_match(value, refRegex)) if (!std::regex_match(value, refRegex))
throw BadURL("URL '%s' contains an invalid branch/tag name", url.url); throw BadURL("URL '%s' contains an invalid branch/tag name", url);
if (ref) if (ref)
throw BadURL("URL '%s' contains multiple branch/tag names", url.url); throw BadURL("URL '%s' contains multiple branch/tag names", url);
ref = value; ref = value;
} }
else if (name == "host") { else if (name == "host") {
if (!std::regex_match(value, hostRegex)) if (!std::regex_match(value, hostRegex))
throw BadURL("URL '%s' contains an invalid instance host", url.url); throw BadURL("URL '%s' contains an invalid instance host", url);
host_url = value; host_url = value;
} }
// FIXME: barf on unsupported attributes // FIXME: barf on unsupported attributes
} }
if (ref && rev) if (ref && rev)
throw BadURL("URL '%s' contains both a commit hash and a branch/tag name %s %s", url.url, *ref, rev->gitRev()); throw BadURL("URL '%s' contains both a commit hash and a branch/tag name %s %s", url, *ref, rev->gitRev());
Input input{settings}; Input input{settings};
input.attrs.insert_or_assign("type", std::string { schemeName() }); input.attrs.insert_or_assign("type", std::string { schemeName() });

View file

@ -26,16 +26,16 @@ struct IndirectInputScheme : InputScheme
else if (std::regex_match(path[1], refRegex)) else if (std::regex_match(path[1], refRegex))
ref = path[1]; ref = path[1];
else else
throw BadURL("in flake URL '%s', '%s' is not a commit hash or branch/tag name", url.url, path[1]); throw BadURL("in flake URL '%s', '%s' is not a commit hash or branch/tag name", url, path[1]);
} else if (path.size() == 3) { } else if (path.size() == 3) {
if (!std::regex_match(path[1], refRegex)) if (!std::regex_match(path[1], refRegex))
throw BadURL("in flake URL '%s', '%s' is not a branch/tag name", url.url, path[1]); throw BadURL("in flake URL '%s', '%s' is not a branch/tag name", url, path[1]);
ref = path[1]; ref = path[1];
if (!std::regex_match(path[2], revRegex)) if (!std::regex_match(path[2], revRegex))
throw BadURL("in flake URL '%s', '%s' is not a commit hash", url.url, path[2]); throw BadURL("in flake URL '%s', '%s' is not a commit hash", url, path[2]);
rev = Hash::parseAny(path[2], HashAlgorithm::SHA1); rev = Hash::parseAny(path[2], HashAlgorithm::SHA1);
} else } else
throw BadURL("GitHub URL '%s' is invalid", url.url); throw BadURL("GitHub URL '%s' is invalid", url);
std::string id = path[0]; std::string id = path[0];
if (!std::regex_match(id, flakeRegex)) if (!std::regex_match(id, flakeRegex))

View file

@ -14,7 +14,7 @@ struct PathInputScheme : InputScheme
if (url.scheme != "path") return {}; if (url.scheme != "path") return {};
if (url.authority && *url.authority != "") if (url.authority && *url.authority != "")
throw Error("path URL '%s' should not have an authority ('%s')", url.url, *url.authority); throw Error("path URL '%s' should not have an authority ('%s')", url, *url.authority);
Input input{settings}; Input input{settings};
input.attrs.insert_or_assign("type", "path"); input.attrs.insert_or_assign("type", "path");
@ -27,10 +27,10 @@ struct PathInputScheme : InputScheme
if (auto n = string2Int<uint64_t>(value)) if (auto n = string2Int<uint64_t>(value))
input.attrs.insert_or_assign(name, *n); input.attrs.insert_or_assign(name, *n);
else else
throw Error("path URL '%s' has invalid parameter '%s'", url.to_string(), name); throw Error("path URL '%s' has invalid parameter '%s'", url, name);
} }
else else
throw Error("path URL '%s' has unsupported parameter '%s'", url.to_string(), name); throw Error("path URL '%s' has unsupported parameter '%s'", url, name);
return input; return input;
} }

View file

@ -162,7 +162,6 @@ std::pair<FlakeRef, std::string> parsePathFlakeRefWithFragment(
auto base = std::string("git+file://") + flakeRoot; auto base = std::string("git+file://") + flakeRoot;
auto parsedURL = ParsedURL{ auto parsedURL = ParsedURL{
.url = base, // FIXME
.base = base, .base = base,
.scheme = "git+file", .scheme = "git+file",
.authority = "", .authority = "",
@ -220,7 +219,6 @@ static std::optional<std::pair<FlakeRef, std::string>> parseFlakeIdRef(
if (std::regex_match(url, match, flakeRegex)) { if (std::regex_match(url, match, flakeRegex)) {
auto parsedURL = ParsedURL{ auto parsedURL = ParsedURL{
.url = url,
.base = "flake:" + match.str(1), .base = "flake:" + match.str(1),
.scheme = "flake", .scheme = "flake",
.authority = "", .authority = "",

View file

@ -20,23 +20,11 @@ namespace nix {
} }
std::ostream& operator<<(std::ostream& os, const ParsedURL& p) {
return os << "\n"
<< "url: " << p.url << "\n"
<< "base: " << p.base << "\n"
<< "scheme: " << p.scheme << "\n"
<< "authority: " << p.authority.value() << "\n"
<< "path: " << p.path << "\n"
<< "query: " << print_map(p.query) << "\n"
<< "fragment: " << p.fragment << "\n";
}
TEST(parseURL, parsesSimpleHttpUrl) { TEST(parseURL, parsesSimpleHttpUrl) {
auto s = "http://www.example.org/file.tar.gz"; auto s = "http://www.example.org/file.tar.gz";
auto parsed = parseURL(s); auto parsed = parseURL(s);
ParsedURL expected { ParsedURL expected {
.url = "http://www.example.org/file.tar.gz",
.base = "http://www.example.org/file.tar.gz", .base = "http://www.example.org/file.tar.gz",
.scheme = "http", .scheme = "http",
.authority = "www.example.org", .authority = "www.example.org",
@ -53,7 +41,6 @@ namespace nix {
auto parsed = parseURL(s); auto parsed = parseURL(s);
ParsedURL expected { ParsedURL expected {
.url = "https://www.example.org/file.tar.gz",
.base = "https://www.example.org/file.tar.gz", .base = "https://www.example.org/file.tar.gz",
.scheme = "https", .scheme = "https",
.authority = "www.example.org", .authority = "www.example.org",
@ -70,7 +57,6 @@ namespace nix {
auto parsed = parseURL(s); auto parsed = parseURL(s);
ParsedURL expected { ParsedURL expected {
.url = "https://www.example.org/file.tar.gz",
.base = "https://www.example.org/file.tar.gz", .base = "https://www.example.org/file.tar.gz",
.scheme = "https", .scheme = "https",
.authority = "www.example.org", .authority = "www.example.org",
@ -87,7 +73,6 @@ namespace nix {
auto parsed = parseURL(s); auto parsed = parseURL(s);
ParsedURL expected { ParsedURL expected {
.url = "http://www.example.org/file.tar.gz",
.base = "http://www.example.org/file.tar.gz", .base = "http://www.example.org/file.tar.gz",
.scheme = "http", .scheme = "http",
.authority = "www.example.org", .authority = "www.example.org",
@ -104,7 +89,6 @@ namespace nix {
auto parsed = parseURL(s); auto parsed = parseURL(s);
ParsedURL expected { ParsedURL expected {
.url = "file+https://www.example.org/video.mp4",
.base = "https://www.example.org/video.mp4", .base = "https://www.example.org/video.mp4",
.scheme = "file+https", .scheme = "file+https",
.authority = "www.example.org", .authority = "www.example.org",
@ -126,7 +110,6 @@ namespace nix {
auto parsed = parseURL(s); auto parsed = parseURL(s);
ParsedURL expected { ParsedURL expected {
.url = "http://127.0.0.1:8080/file.tar.gz",
.base = "https://127.0.0.1:8080/file.tar.gz", .base = "https://127.0.0.1:8080/file.tar.gz",
.scheme = "http", .scheme = "http",
.authority = "127.0.0.1:8080", .authority = "127.0.0.1:8080",
@ -143,7 +126,6 @@ namespace nix {
auto parsed = parseURL(s); auto parsed = parseURL(s);
ParsedURL expected { ParsedURL expected {
.url = "http://[fe80::818c:da4d:8975:415c\%enp0s25]:8080",
.base = "http://[fe80::818c:da4d:8975:415c\%enp0s25]:8080", .base = "http://[fe80::818c:da4d:8975:415c\%enp0s25]:8080",
.scheme = "http", .scheme = "http",
.authority = "[fe80::818c:da4d:8975:415c\%enp0s25]:8080", .authority = "[fe80::818c:da4d:8975:415c\%enp0s25]:8080",
@ -161,7 +143,6 @@ namespace nix {
auto parsed = parseURL(s); auto parsed = parseURL(s);
ParsedURL expected { ParsedURL expected {
.url = "http://[2a02:8071:8192:c100:311d:192d:81ac:11ea]:8080",
.base = "http://[2a02:8071:8192:c100:311d:192d:81ac:11ea]:8080", .base = "http://[2a02:8071:8192:c100:311d:192d:81ac:11ea]:8080",
.scheme = "http", .scheme = "http",
.authority = "[2a02:8071:8192:c100:311d:192d:81ac:11ea]:8080", .authority = "[2a02:8071:8192:c100:311d:192d:81ac:11ea]:8080",
@ -185,7 +166,6 @@ namespace nix {
auto parsed = parseURL(s); auto parsed = parseURL(s);
ParsedURL expected { ParsedURL expected {
.url = "http://user:pass@www.example.org/file.tar.gz",
.base = "http://user:pass@www.example.org/file.tar.gz", .base = "http://user:pass@www.example.org/file.tar.gz",
.scheme = "http", .scheme = "http",
.authority = "user:pass@www.example.org:8080", .authority = "user:pass@www.example.org:8080",
@ -203,7 +183,6 @@ namespace nix {
auto parsed = parseURL(s); auto parsed = parseURL(s);
ParsedURL expected { ParsedURL expected {
.url = "",
.base = "", .base = "",
.scheme = "file", .scheme = "file",
.authority = "", .authority = "",
@ -228,7 +207,6 @@ namespace nix {
auto parsed = parseURL(s); auto parsed = parseURL(s);
ParsedURL expected { ParsedURL expected {
.url = "ftp://ftp.nixos.org/downloads/nixos.iso",
.base = "ftp://ftp.nixos.org/downloads/nixos.iso", .base = "ftp://ftp.nixos.org/downloads/nixos.iso",
.scheme = "ftp", .scheme = "ftp",
.authority = "ftp.nixos.org", .authority = "ftp.nixos.org",

View file

@ -40,7 +40,6 @@ ParsedURL parseURL(const std::string & url)
path = "/"; path = "/";
return ParsedURL{ return ParsedURL{
.url = url,
.base = base, .base = base,
.scheme = scheme, .scheme = scheme,
.authority = authority, .authority = authority,
@ -136,6 +135,12 @@ std::string ParsedURL::to_string() const
+ (fragment.empty() ? "" : "#" + percentEncode(fragment)); + (fragment.empty() ? "" : "#" + percentEncode(fragment));
} }
std::ostream & operator << (std::ostream & os, const ParsedURL & url)
{
os << url.to_string();
return os;
}
bool ParsedURL::operator ==(const ParsedURL & other) const noexcept bool ParsedURL::operator ==(const ParsedURL & other) const noexcept
{ {
return return

View file

@ -7,9 +7,8 @@ namespace nix {
struct ParsedURL struct ParsedURL
{ {
std::string url;
/// URL without query/fragment /// URL without query/fragment
std::string base; std::string base; // FIXME: remove
std::string scheme; std::string scheme;
std::optional<std::string> authority; std::optional<std::string> authority;
std::string path; std::string path;
@ -26,6 +25,8 @@ struct ParsedURL
ParsedURL canonicalise(); ParsedURL canonicalise();
}; };
std::ostream & operator << (std::ostream & os, const ParsedURL & url);
MakeError(BadURL, Error); MakeError(BadURL, Error);
std::string percentDecode(std::string_view in); std::string percentDecode(std::string_view in);