Skip to content

Commit

Permalink
Auto merge of servo#244 - servo:sethostnone, r=SimonSapin
Browse files Browse the repository at this point in the history
Prevent panic when calling `url::Url::set_host` with `None`.

Fixes servo#243.

<!-- 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/244)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo authored Dec 17, 2016
2 parents 37fe8a7 + 2c9e076 commit ae7204f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1087,12 +1087,18 @@ impl Url {
}

if let Some(host) = host {
if host == "" && SchemeType::from(self.scheme()).is_special() {
return Err(ParseError::EmptyHost);
}
self.set_host_internal(try!(Host::parse(host)), None)
} else if self.has_host() {
if SchemeType::from(self.scheme()).is_special() {
return Err(ParseError::EmptyHost)
}
debug_assert!(self.byte_at(self.scheme_end) == b':');
debug_assert!(self.byte_at(self.path_start) == b'/');
let new_path_start = self.scheme_end + 1;
self.serialization.drain(self.path_start as usize..new_path_start as usize);
self.serialization.drain(new_path_start as usize..self.path_start as usize);
let offset = self.path_start - new_path_start;
self.path_start = new_path_start;
self.username_end = new_path_start;
Expand Down
16 changes: 16 additions & 0 deletions tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,19 @@ fn append_empty_segment_then_mutate() {
url.assert_invariants();
assert_eq!(url.to_string(), "http://localhost:6767/foo/bar?a=b");
}

#[test]
/// https://github.com/servo/rust-url/issues/243
fn test_set_host() {
let mut url = Url::parse("https://example.net/hello").unwrap();
url.set_host(Some("foo.com")).unwrap();
assert_eq!(url.as_str(), "https://foo.com/hello");
assert!(url.set_host(None).is_err());
assert_eq!(url.as_str(), "https://foo.com/hello");
assert!(url.set_host(Some("")).is_err());
assert_eq!(url.as_str(), "https://foo.com/hello");

let mut url = Url::parse("foobar://example.net/hello").unwrap();
url.set_host(None).unwrap();
assert_eq!(url.as_str(), "foobar:/hello");
}

0 comments on commit ae7204f

Please sign in to comment.