Skip to content

Commit 87048a3

Browse files
committed
refactor
- make clearer where percent encoding is used. - add more generalised tests for user.name kind of user names.
1 parent 1afa7a5 commit 87048a3

File tree

3 files changed

+36
-43
lines changed

3 files changed

+36
-43
lines changed

gix-url/src/lib.rs

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -328,39 +328,6 @@ impl Url {
328328
}
329329
}
330330

331-
/// Characters that must be percent-encoded in the userinfo component of a URL.
332-
///
333-
/// According to RFC 3986, userinfo can contain:
334-
/// - unreserved characters: `A-Z a-z 0-9 - . _ ~`
335-
/// - percent-encoded characters
336-
/// - sub-delims: `! $ & ' ( ) * + , ; =`
337-
/// - `:`
338-
///
339-
/// This encode set encodes everything else, particularly `@` (userinfo delimiter),
340-
/// `/` `?` `#` (path/query/fragment delimiters), and various other special characters.
341-
const USERINFO_ENCODE_SET: &percent_encoding::AsciiSet = &percent_encoding::CONTROLS
342-
.add(b' ')
343-
.add(b'"')
344-
.add(b'#')
345-
.add(b'%')
346-
.add(b'/')
347-
.add(b'<')
348-
.add(b'>')
349-
.add(b'?')
350-
.add(b'@')
351-
.add(b'[')
352-
.add(b'\\')
353-
.add(b']')
354-
.add(b'^')
355-
.add(b'`')
356-
.add(b'{')
357-
.add(b'|')
358-
.add(b'}');
359-
360-
fn percent_encode(s: &str) -> Cow<'_, str> {
361-
percent_encoding::utf8_percent_encode(s, USERINFO_ENCODE_SET).into()
362-
}
363-
364331
/// Serialization
365332
impl Url {
366333
/// Write this URL losslessly to `out`, ready to be parsed again.
@@ -379,6 +346,38 @@ impl Url {
379346
}
380347

381348
fn write_canonical_form_to(&self, out: &mut dyn std::io::Write) -> std::io::Result<()> {
349+
fn percent_encode(s: &str) -> Cow<'_, str> {
350+
/// Characters that must be percent-encoded in the userinfo component of a URL.
351+
///
352+
/// According to RFC 3986, userinfo can contain:
353+
/// - unreserved characters: `A-Z a-z 0-9 - . _ ~`
354+
/// - percent-encoded characters
355+
/// - sub-delims: `! $ & ' ( ) * + , ; =`
356+
/// - `:`
357+
///
358+
/// This encode-set encodes everything else, particularly `@` (userinfo delimiter),
359+
/// `/` `?` `#` (path/query/fragment delimiters), and various other special characters.
360+
const USERINFO_ENCODE_SET: &percent_encoding::AsciiSet = &percent_encoding::CONTROLS
361+
.add(b' ')
362+
.add(b'"')
363+
.add(b'#')
364+
.add(b'%')
365+
.add(b'/')
366+
.add(b'<')
367+
.add(b'>')
368+
.add(b'?')
369+
.add(b'@')
370+
.add(b'[')
371+
.add(b'\\')
372+
.add(b']')
373+
.add(b'^')
374+
.add(b'`')
375+
.add(b'{')
376+
.add(b'|')
377+
.add(b'}');
378+
percent_encoding::utf8_percent_encode(s, USERINFO_ENCODE_SET).into()
379+
}
380+
382381
out.write_all(self.scheme.as_str().as_bytes())?;
383382
out.write_all(b"://")?;
384383
match (&self.user, &self.host) {

gix-url/tests/fixtures/make_baseline.sh

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ tests_windows=()
1515
for path in "repo" "re:po" "re/po"; do
1616
# normal urls
1717
for protocol in "ssh+git" "git+ssh" "git" "ssh"; do
18-
for host in "host" "user@host" "user_name@host" "user@[::1]" "user@::1"; do
18+
for host in "host" "user@host" "user_name@host" "user.name@host" "user@[::1]" "user@::1"; do
1919
for port_separator in "" ":"; do
2020
tests+=("$protocol://$host$port_separator/$path")
2121

@@ -52,12 +52,6 @@ done
5252
tests_windows+=("file://c:/repo")
5353
tests_windows+=("c:repo")
5454

55-
# Test URLs with dots in usernames (gitbutler issue #11419)
56-
# Dots should not be percent-encoded in userinfo
57-
tests+=("ssh://user.name@host/repo")
58-
tests+=("git://user.name@host/repo")
59-
tests+=("user.name@host:repo")
60-
6155
tests_unix+=("${tests[@]}")
6256
tests_windows+=("${tests[@]}")
6357

gix-url/tests/url/baseline.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ fn run() {
8484
}
8585

8686
assert!(
87-
failure_count_reserialization <= 63,
87+
failure_count_reserialization <= 72,
8888
"the number of reserialization errors should ideally get better, not worse - if this panic is not due to regressions but to new passing test cases, you can set this check to {failure_count_reserialization}"
8989
);
9090
assert_eq!(failure_count_roundtrips, 0, "there should be no roundtrip errors");
@@ -185,8 +185,8 @@ mod baseline {
185185

186186
pub fn max_num_failures(&self) -> usize {
187187
match self {
188-
Kind::Unix => 195,
189-
Kind::Windows => 195 + 6,
188+
Kind::Unix => 222,
189+
Kind::Windows => 222 + 6,
190190
}
191191
}
192192

0 commit comments

Comments
 (0)