Skip to content

Commit

Permalink
Auto merge of #406 - valenting:empty-pass, r=SimonSapin
Browse files Browse the repository at this point in the history
Properly handle empty username or password

Imports test changes from:
web-platform-tests/wpt@e001240

Related URL spec changes: whatwg/url#186

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-url/406)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo committed Oct 31, 2017
2 parents 47b56e8 + de4cc76 commit 222c34c
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 7 deletions.
15 changes: 13 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,14 +701,23 @@ impl<'a> Parser<'a> {
};

let mut username_end = None;
let mut has_password = false;
let mut has_username = false;
while userinfo_char_count > 0 {
let (c, utf8_c) = input.next_utf8().unwrap();
userinfo_char_count -= 1;
if c == ':' && username_end.is_none() {
// Start parsing password
username_end = Some(to_u32(self.serialization.len())?);
self.serialization.push(':');
// We don't add a colon if the password is empty
if userinfo_char_count > 0 {
self.serialization.push(':');
has_password = true;
}
} else {
if !has_password {
has_username = true;
}
self.check_url_code_point(c, &input);
self.serialization.extend(utf8_percent_encode(utf8_c, USERINFO_ENCODE_SET));
}
Expand All @@ -717,7 +726,9 @@ impl<'a> Parser<'a> {
Some(i) => i,
None => to_u32(self.serialization.len())?,
};
self.serialization.push('@');
if has_username || has_password {
self.serialization.push('@');
}
Ok((username_end, remaining))
}

Expand Down
2 changes: 1 addition & 1 deletion tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ fn test_serialization() {
("http://example.com/", "http://example.com/"),
("http://addslash.com", "http://addslash.com/"),
("http://@emptyuser.com/", "http://emptyuser.com/"),
("http://:@emptypass.com/", "http://:@emptypass.com/"),
("http://:@emptypass.com/", "http://emptypass.com/"),
("http://[email protected]/", "http://[email protected]/"),
("http://user:[email protected]/", "http://user:[email protected]/"),
("http://slashquery.com/path/?q=something", "http://slashquery.com/path/?q=something"),
Expand Down
128 changes: 124 additions & 4 deletions tests/urltestdata.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,66 @@
"search": "?b",
"hash": "#c"
},
{
"input": "https://test:@test",
"base": "about:blank",
"href": "https://test@test/",
"origin": "https://test",
"protocol": "https:",
"username": "test",
"password": "",
"host": "test",
"hostname": "test",
"port": "",
"pathname": "/",
"search": "",
"hash": ""
},
{
"input": "https://:@test",
"base": "about:blank",
"href": "https://test/",
"origin": "https://test",
"protocol": "https:",
"username": "",
"password": "",
"host": "test",
"hostname": "test",
"port": "",
"pathname": "/",
"search": "",
"hash": ""
},
{
"input": "non-special://test:@test/x",
"base": "about:blank",
"href": "non-special://test@test/x",
"origin": "null",
"protocol": "non-special:",
"username": "test",
"password": "",
"host": "test",
"hostname": "test",
"port": "",
"pathname": "/x",
"search": "",
"hash": ""
},
{
"input": "non-special://:@test/x",
"base": "about:blank",
"href": "non-special://test/x",
"origin": "null",
"protocol": "non-special:",
"username": "",
"password": "",
"host": "test",
"hostname": "test",
"port": "",
"pathname": "/x",
"search": "",
"hash": ""
},
{
"input": "http:foo.com",
"base": "http://example.org/foo/bar",
Expand Down Expand Up @@ -3096,7 +3156,7 @@
{
"input": "http:a:@www.example.com",
"base": "about:blank",
"href": "http://a:@www.example.com/",
"href": "http://[email protected]/",
"origin": "http://www.example.com",
"protocol": "http:",
"username": "a",
Expand All @@ -3111,7 +3171,7 @@
{
"input": "http:/a:@www.example.com",
"base": "about:blank",
"href": "http://a:@www.example.com/",
"href": "http://[email protected]/",
"origin": "http://www.example.com",
"protocol": "http:",
"username": "a",
Expand All @@ -3126,7 +3186,7 @@
{
"input": "http://a:@www.example.com",
"base": "about:blank",
"href": "http://a:@www.example.com/",
"href": "http://[email protected]/",
"origin": "http://www.example.com",
"protocol": "http:",
"username": "a",
Expand Down Expand Up @@ -3171,7 +3231,67 @@
{
"input": "http://:@www.example.com",
"base": "about:blank",
"href": "http://:@www.example.com/",
"href": "http://www.example.com/",
"origin": "http://www.example.com",
"protocol": "http:",
"username": "",
"password": "",
"host": "www.example.com",
"hostname": "www.example.com",
"port": "",
"pathname": "/",
"search": "",
"hash": ""
},
{
"input": "http:a:@www.example.com",
"base": "about:blank",
"href": "http://[email protected]/",
"origin": "http://www.example.com",
"protocol": "http:",
"username": "a",
"password": "",
"host": "www.example.com",
"hostname": "www.example.com",
"port": "",
"pathname": "/",
"search": "",
"hash": ""
},
{
"input": "http:/a:@www.example.com",
"base": "about:blank",
"href": "http://[email protected]/",
"origin": "http://www.example.com",
"protocol": "http:",
"username": "a",
"password": "",
"host": "www.example.com",
"hostname": "www.example.com",
"port": "",
"pathname": "/",
"search": "",
"hash": ""
},
{
"input": "http://a:@www.example.com",
"base": "about:blank",
"href": "http://[email protected]/",
"origin": "http://www.example.com",
"protocol": "http:",
"username": "a",
"password": "",
"host": "www.example.com",
"hostname": "www.example.com",
"port": "",
"pathname": "/",
"search": "",
"hash": ""
},
{
"input": "http://:@www.example.com",
"base": "about:blank",
"href": "http://www.example.com/",
"origin": "http://www.example.com",
"protocol": "http:",
"username": "",
Expand Down

0 comments on commit 222c34c

Please sign in to comment.