Skip to content

Commit 211bf4a

Browse files
author
bors-servo
authored
Auto merge of #411 - valenting:no-host-username, r=SimonSapin
Do not allow username,password,port for URLs without a host or file URLs Imports tests from web-platform-tests/wpt@0e6a90f and tracks URL spec change at whatwg/url#224 <!-- 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/411) <!-- Reviewable:end -->
2 parents c6284a2 + f9d7971 commit 211bf4a

File tree

5 files changed

+49
-22
lines changed

5 files changed

+49
-22
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
name = "url"
44
# When updating version, also modify html_root_url in the lib.rs
5-
version = "1.6.0"
5+
version = "1.6.1"
66
authors = ["The rust-url developers"]
77

88
description = "URL library for Rust, based on the WHATWG URL Standard"

src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ assert_eq!(css_url.as_str(), "http://servo.github.io/rust-url/main.css");
104104
# run().unwrap();
105105
*/
106106

107-
#![doc(html_root_url = "https://docs.rs/url/1.6.0")]
107+
#![doc(html_root_url = "https://docs.rs/url/1.6.1")]
108108

109109
#[cfg(feature="rustc-serialize")] extern crate rustc_serialize;
110110
#[macro_use] extern crate matches;
@@ -1427,7 +1427,8 @@ impl Url {
14271427
/// # run().unwrap();
14281428
/// ```
14291429
pub fn set_port(&mut self, mut port: Option<u16>) -> Result<(), ()> {
1430-
if !self.has_host() || self.scheme() == "file" {
1430+
// has_host implies !cannot_be_a_base
1431+
if !self.has_host() || self.host() == Some(Host::Domain("")) || self.scheme() == "file" {
14311432
return Err(())
14321433
}
14331434
if port.is_some() && port == parser::default_port(self.scheme()) {
@@ -1695,7 +1696,8 @@ impl Url {
16951696
/// # run().unwrap();
16961697
/// ```
16971698
pub fn set_password(&mut self, password: Option<&str>) -> Result<(), ()> {
1698-
if !self.has_host() {
1699+
// has_host implies !cannot_be_a_base
1700+
if !self.has_host() || self.host() == Some(Host::Domain("")) || self.scheme() == "file" {
16991701
return Err(())
17001702
}
17011703
if let Some(password) = password {
@@ -1776,7 +1778,8 @@ impl Url {
17761778
/// # run().unwrap();
17771779
/// ```
17781780
pub fn set_username(&mut self, username: &str) -> Result<(), ()> {
1779-
if !self.has_host() {
1781+
// has_host implies !cannot_be_a_base
1782+
if !self.has_host() || self.host() == Some(Host::Domain("")) || self.scheme() == "file" {
17801783
return Err(())
17811784
}
17821785
let username_start = self.scheme_end + 3;

src/quirks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ pub fn set_port(url: &mut Url, new_port: &str) -> Result<(), ()> {
152152
{
153153
// has_host implies !cannot_be_a_base
154154
let scheme = url.scheme();
155-
if !url.has_host() || scheme == "file" {
155+
if !url.has_host() || url.host() == Some(Host::Domain("")) || scheme == "file" {
156156
return Err(())
157157
}
158158
result = Parser::parse_port(Input::new(new_port), || default_port(scheme), Context::Setter)

tests/setters_tests.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,22 @@
258258
"username": "%c3%89t%C3%A9"
259259
}
260260
},
261+
{
262+
"href": "sc:///",
263+
"new_value": "x",
264+
"expected": {
265+
"href": "sc:///",
266+
"username": ""
267+
}
268+
},
269+
{
270+
"href": "file://test/",
271+
"new_value": "test",
272+
"expected": {
273+
"href": "file://test/",
274+
"username": ""
275+
}
276+
},
261277
{
262278
"href": "javascript://x/",
263279
"new_value": "wario",
@@ -345,6 +361,22 @@
345361
"password": "%c3%89t%C3%A9"
346362
}
347363
},
364+
{
365+
"href": "sc:///",
366+
"new_value": "x",
367+
"expected": {
368+
"href": "sc:///",
369+
"password": ""
370+
}
371+
},
372+
{
373+
"href": "file://test/",
374+
"new_value": "test",
375+
"expected": {
376+
"href": "file://test/",
377+
"password": ""
378+
}
379+
},
348380
{
349381
"href": "javascript://x/",
350382
"new_value": "bowser",
@@ -1214,6 +1246,14 @@
12141246
"port": ""
12151247
}
12161248
},
1249+
{
1250+
"href": "sc:///",
1251+
"new_value": "12",
1252+
"expected": {
1253+
"href": "sc:///",
1254+
"port": ""
1255+
}
1256+
},
12171257
{
12181258
"href": "sc://x/",
12191259
"new_value": "12",

tests/unit.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -295,22 +295,6 @@ fn host_and_port_display() {
295295
)
296296
}
297297

298-
#[test]
299-
/// https://github.com/servo/rust-url/issues/25
300-
fn issue_25() {
301-
let filename = if cfg!(windows) { r"C:\run\pg.sock" } else { "/run/pg.sock" };
302-
let mut url = Url::from_file_path(filename).unwrap();
303-
url.check_invariants().unwrap();
304-
url.set_scheme("postgres").unwrap();
305-
url.check_invariants().unwrap();
306-
url.set_host(Some("")).unwrap();
307-
url.check_invariants().unwrap();
308-
url.set_username("me").unwrap();
309-
url.check_invariants().unwrap();
310-
let expected = format!("postgres://me@/{}run/pg.sock", if cfg!(windows) { "C:/" } else { "" });
311-
assert_eq!(url.as_str(), expected);
312-
}
313-
314298
#[test]
315299
/// https://github.com/servo/rust-url/issues/61
316300
fn issue_61() {

0 commit comments

Comments
 (0)