Skip to content

Commit f705ce7

Browse files
committed
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.
1 parent a0901e5 commit f705ce7

File tree

8 files changed

+25
-43
lines changed

8 files changed

+25
-43
lines changed

src/libfetchers/fetchers.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Input Input::fromURL(
6666
}
6767
}
6868

69-
throw Error("input '%s' is unsupported", url.url);
69+
throw Error("input '%s' is unsupported", url);
7070
}
7171

7272
Input Input::fromAttrs(const Settings & settings, Attrs && attrs)

src/libfetchers/github.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct GitArchiveInputScheme : InputScheme
5050
else if (std::regex_match(path[2], refRegex))
5151
ref = path[2];
5252
else
53-
throw BadURL("in URL '%s', '%s' is not a commit hash or branch/tag name", url.url, path[2]);
53+
throw BadURL("in URL '%s', '%s' is not a commit hash or branch/tag name", url, path[2]);
5454
} else if (size > 3) {
5555
std::string rs;
5656
for (auto i = std::next(path.begin(), 2); i != path.end(); i++) {
@@ -63,34 +63,34 @@ struct GitArchiveInputScheme : InputScheme
6363
if (std::regex_match(rs, refRegex)) {
6464
ref = rs;
6565
} else {
66-
throw BadURL("in URL '%s', '%s' is not a branch/tag name", url.url, rs);
66+
throw BadURL("in URL '%s', '%s' is not a branch/tag name", url, rs);
6767
}
6868
} else if (size < 2)
69-
throw BadURL("URL '%s' is invalid", url.url);
69+
throw BadURL("URL '%s' is invalid", url);
7070

7171
for (auto &[name, value] : url.query) {
7272
if (name == "rev") {
7373
if (rev)
74-
throw BadURL("URL '%s' contains multiple commit hashes", url.url);
74+
throw BadURL("URL '%s' contains multiple commit hashes", url);
7575
rev = Hash::parseAny(value, HashAlgorithm::SHA1);
7676
}
7777
else if (name == "ref") {
7878
if (!std::regex_match(value, refRegex))
79-
throw BadURL("URL '%s' contains an invalid branch/tag name", url.url);
79+
throw BadURL("URL '%s' contains an invalid branch/tag name", url);
8080
if (ref)
81-
throw BadURL("URL '%s' contains multiple branch/tag names", url.url);
81+
throw BadURL("URL '%s' contains multiple branch/tag names", url);
8282
ref = value;
8383
}
8484
else if (name == "host") {
8585
if (!std::regex_match(value, hostRegex))
86-
throw BadURL("URL '%s' contains an invalid instance host", url.url);
86+
throw BadURL("URL '%s' contains an invalid instance host", url);
8787
host_url = value;
8888
}
8989
// FIXME: barf on unsupported attributes
9090
}
9191

9292
if (ref && rev)
93-
throw BadURL("URL '%s' contains both a commit hash and a branch/tag name %s %s", url.url, *ref, rev->gitRev());
93+
throw BadURL("URL '%s' contains both a commit hash and a branch/tag name %s %s", url, *ref, rev->gitRev());
9494

9595
Input input{settings};
9696
input.attrs.insert_or_assign("type", std::string { schemeName() });

src/libfetchers/indirect.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ struct IndirectInputScheme : InputScheme
2626
else if (std::regex_match(path[1], refRegex))
2727
ref = path[1];
2828
else
29-
throw BadURL("in flake URL '%s', '%s' is not a commit hash or branch/tag name", url.url, path[1]);
29+
throw BadURL("in flake URL '%s', '%s' is not a commit hash or branch/tag name", url, path[1]);
3030
} else if (path.size() == 3) {
3131
if (!std::regex_match(path[1], refRegex))
32-
throw BadURL("in flake URL '%s', '%s' is not a branch/tag name", url.url, path[1]);
32+
throw BadURL("in flake URL '%s', '%s' is not a branch/tag name", url, path[1]);
3333
ref = path[1];
3434
if (!std::regex_match(path[2], revRegex))
35-
throw BadURL("in flake URL '%s', '%s' is not a commit hash", url.url, path[2]);
35+
throw BadURL("in flake URL '%s', '%s' is not a commit hash", url, path[2]);
3636
rev = Hash::parseAny(path[2], HashAlgorithm::SHA1);
3737
} else
38-
throw BadURL("GitHub URL '%s' is invalid", url.url);
38+
throw BadURL("GitHub URL '%s' is invalid", url);
3939

4040
std::string id = path[0];
4141
if (!std::regex_match(id, flakeRegex))

src/libfetchers/path.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct PathInputScheme : InputScheme
1414
if (url.scheme != "path") return {};
1515

1616
if (url.authority && *url.authority != "")
17-
throw Error("path URL '%s' should not have an authority ('%s')", url.url, *url.authority);
17+
throw Error("path URL '%s' should not have an authority ('%s')", url, *url.authority);
1818

1919
Input input{settings};
2020
input.attrs.insert_or_assign("type", "path");
@@ -27,10 +27,10 @@ struct PathInputScheme : InputScheme
2727
if (auto n = string2Int<uint64_t>(value))
2828
input.attrs.insert_or_assign(name, *n);
2929
else
30-
throw Error("path URL '%s' has invalid parameter '%s'", url.to_string(), name);
30+
throw Error("path URL '%s' has invalid parameter '%s'", url, name);
3131
}
3232
else
33-
throw Error("path URL '%s' has unsupported parameter '%s'", url.to_string(), name);
33+
throw Error("path URL '%s' has unsupported parameter '%s'", url, name);
3434

3535
return input;
3636
}

src/libflake/flake/flakeref.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ std::pair<FlakeRef, std::string> parsePathFlakeRefWithFragment(
162162
auto base = std::string("git+file://") + flakeRoot;
163163

164164
auto parsedURL = ParsedURL{
165-
.url = base, // FIXME
166165
.base = base,
167166
.scheme = "git+file",
168167
.authority = "",
@@ -220,7 +219,6 @@ static std::optional<std::pair<FlakeRef, std::string>> parseFlakeIdRef(
220219

221220
if (std::regex_match(url, match, flakeRegex)) {
222221
auto parsedURL = ParsedURL{
223-
.url = url,
224222
.base = "flake:" + match.str(1),
225223
.scheme = "flake",
226224
.authority = "",

src/libutil-tests/url.cc

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,11 @@ namespace nix {
2020
}
2121

2222

23-
std::ostream& operator<<(std::ostream& os, const ParsedURL& p) {
24-
return os << "\n"
25-
<< "url: " << p.url << "\n"
26-
<< "base: " << p.base << "\n"
27-
<< "scheme: " << p.scheme << "\n"
28-
<< "authority: " << p.authority.value() << "\n"
29-
<< "path: " << p.path << "\n"
30-
<< "query: " << print_map(p.query) << "\n"
31-
<< "fragment: " << p.fragment << "\n";
32-
}
33-
3423
TEST(parseURL, parsesSimpleHttpUrl) {
3524
auto s = "http://www.example.org/file.tar.gz";
3625
auto parsed = parseURL(s);
3726

3827
ParsedURL expected {
39-
.url = "http://www.example.org/file.tar.gz",
4028
.base = "http://www.example.org/file.tar.gz",
4129
.scheme = "http",
4230
.authority = "www.example.org",
@@ -53,7 +41,6 @@ namespace nix {
5341
auto parsed = parseURL(s);
5442

5543
ParsedURL expected {
56-
.url = "https://www.example.org/file.tar.gz",
5744
.base = "https://www.example.org/file.tar.gz",
5845
.scheme = "https",
5946
.authority = "www.example.org",
@@ -70,7 +57,6 @@ namespace nix {
7057
auto parsed = parseURL(s);
7158

7259
ParsedURL expected {
73-
.url = "https://www.example.org/file.tar.gz",
7460
.base = "https://www.example.org/file.tar.gz",
7561
.scheme = "https",
7662
.authority = "www.example.org",
@@ -87,7 +73,6 @@ namespace nix {
8773
auto parsed = parseURL(s);
8874

8975
ParsedURL expected {
90-
.url = "http://www.example.org/file.tar.gz",
9176
.base = "http://www.example.org/file.tar.gz",
9277
.scheme = "http",
9378
.authority = "www.example.org",
@@ -104,7 +89,6 @@ namespace nix {
10489
auto parsed = parseURL(s);
10590

10691
ParsedURL expected {
107-
.url = "file+https://www.example.org/video.mp4",
10892
.base = "https://www.example.org/video.mp4",
10993
.scheme = "file+https",
11094
.authority = "www.example.org",
@@ -126,7 +110,6 @@ namespace nix {
126110
auto parsed = parseURL(s);
127111

128112
ParsedURL expected {
129-
.url = "http://127.0.0.1:8080/file.tar.gz",
130113
.base = "https://127.0.0.1:8080/file.tar.gz",
131114
.scheme = "http",
132115
.authority = "127.0.0.1:8080",
@@ -143,7 +126,6 @@ namespace nix {
143126
auto parsed = parseURL(s);
144127

145128
ParsedURL expected {
146-
.url = "http://[fe80::818c:da4d:8975:415c\%enp0s25]:8080",
147129
.base = "http://[fe80::818c:da4d:8975:415c\%enp0s25]:8080",
148130
.scheme = "http",
149131
.authority = "[fe80::818c:da4d:8975:415c\%enp0s25]:8080",
@@ -161,7 +143,6 @@ namespace nix {
161143
auto parsed = parseURL(s);
162144

163145
ParsedURL expected {
164-
.url = "http://[2a02:8071:8192:c100:311d:192d:81ac:11ea]:8080",
165146
.base = "http://[2a02:8071:8192:c100:311d:192d:81ac:11ea]:8080",
166147
.scheme = "http",
167148
.authority = "[2a02:8071:8192:c100:311d:192d:81ac:11ea]:8080",
@@ -185,7 +166,6 @@ namespace nix {
185166
auto parsed = parseURL(s);
186167

187168
ParsedURL expected {
188-
.url = "http://user:[email protected]/file.tar.gz",
189169
.base = "http://user:[email protected]/file.tar.gz",
190170
.scheme = "http",
191171
.authority = "user:[email protected]:8080",
@@ -203,7 +183,6 @@ namespace nix {
203183
auto parsed = parseURL(s);
204184

205185
ParsedURL expected {
206-
.url = "",
207186
.base = "",
208187
.scheme = "file",
209188
.authority = "",
@@ -228,7 +207,6 @@ namespace nix {
228207
auto parsed = parseURL(s);
229208

230209
ParsedURL expected {
231-
.url = "ftp://ftp.nixos.org/downloads/nixos.iso",
232210
.base = "ftp://ftp.nixos.org/downloads/nixos.iso",
233211
.scheme = "ftp",
234212
.authority = "ftp.nixos.org",

src/libutil/url.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ ParsedURL parseURL(const std::string & url)
4040
path = "/";
4141

4242
return ParsedURL{
43-
.url = url,
4443
.base = base,
4544
.scheme = scheme,
4645
.authority = authority,
@@ -136,6 +135,12 @@ std::string ParsedURL::to_string() const
136135
+ (fragment.empty() ? "" : "#" + percentEncode(fragment));
137136
}
138137

138+
std::ostream & operator << (std::ostream & os, const ParsedURL & url)
139+
{
140+
os << url.to_string();
141+
return os;
142+
}
143+
139144
bool ParsedURL::operator ==(const ParsedURL & other) const noexcept
140145
{
141146
return

src/libutil/url.hh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ namespace nix {
77

88
struct ParsedURL
99
{
10-
std::string url;
1110
/// URL without query/fragment
12-
std::string base;
11+
std::string base; // FIXME: remove
1312
std::string scheme;
1413
std::optional<std::string> authority;
1514
std::string path;
@@ -26,6 +25,8 @@ struct ParsedURL
2625
ParsedURL canonicalise();
2726
};
2827

28+
std::ostream & operator << (std::ostream & os, const ParsedURL & url);
29+
2930
MakeError(BadURL, Error);
3031

3132
std::string percentDecode(std::string_view in);

0 commit comments

Comments
 (0)