Skip to content

Fix some parsing errors in extra::url #8616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 33 additions & 28 deletions src/libextra/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl Url {
}

impl UserInfo {
#[inline]
pub fn new(user: ~str, pass: Option<~str>) -> UserInfo {
UserInfo { user: user, pass: pass }
}
Expand Down Expand Up @@ -460,11 +461,14 @@ fn get_authority(rawurl: &str) ->
}
InHost => {
pos = i;
// can't be sure whether this is an ipv6 address or a port
if input == Unreserved {
return Err(~"Illegal characters in authority.");
// must be port
host = rawurl.slice(begin, i).to_owned();
st = InPort;
} else {
// can't be sure whether this is an ipv6 address or a port
st = Ip6Port;
}
st = Ip6Port;
}
Ip6Port => {
if input == Unreserved {
Expand Down Expand Up @@ -514,25 +518,12 @@ fn get_authority(rawurl: &str) ->
}
_ => ()
}
end = i;
}

let end = end; // make end immutable so it can be captured

let host_is_end_plus_one: &fn() -> bool = || {
let xs = ['?', '#', '/'];
end+1 == len
&& !xs.iter().any(|x| *x == (rawurl[end] as char))
};

// finish up
match st {
Start => {
if host_is_end_plus_one() {
host = rawurl.slice(begin, end+1).to_owned();
} else {
host = rawurl.slice(begin, end).to_owned();
}
host = rawurl.slice(begin, end).to_owned();
}
PassHostPort | Ip6Port => {
if input != Digit {
Expand All @@ -552,8 +543,7 @@ fn get_authority(rawurl: &str) ->
}
}

let rest = if host_is_end_plus_one() { ~"" }
else { rawurl.slice(end, len).to_owned() };
let rest = rawurl.slice(end, len).to_owned();
return Ok((userinfo, host, port, rest));
}

Expand Down Expand Up @@ -806,18 +796,17 @@ mod tests {

#[test]
fn test_url_parse() {
let url = ~"http://user:pass@rust-lang.org/doc?s=v#something";
let url = ~"http://user:pass@rust-lang.org:8080/doc?s=v#something";

let up = from_str(url);
let u = up.unwrap();
assert!(u.scheme == ~"http");
let userinfo = u.user.get_ref();
assert!(userinfo.user == ~"user");
assert!(userinfo.pass.get_ref() == &~"pass");
assert!(u.host == ~"rust-lang.org");
assert!(u.path == ~"/doc");
assert!(u.query == ~[(~"s", ~"v")]);
assert!(u.fragment.get_ref() == &~"something");
assert_eq!(&u.scheme, &~"http");
assert_eq!(&u.user, &Some(UserInfo::new(~"user", Some(~"pass"))));
assert_eq!(&u.host, &~"rust-lang.org");
assert_eq!(&u.port, &Some(~"8080"));
assert_eq!(&u.path, &~"/doc");
assert_eq!(&u.query, &~[(~"s", ~"v")]);
assert_eq!(&u.fragment, &Some(~"something"));
}

#[test]
Expand All @@ -828,6 +817,22 @@ mod tests {
assert!(url.path == ~"/");
}

#[test]
fn test_url_host_with_port() {
let urlstr = ~"scheme://host:1234";
let url = from_str(urlstr).unwrap();
assert_eq!(&url.scheme, &~"scheme");
assert_eq!(&url.host, &~"host");
assert_eq!(&url.port, &Some(~"1234"));
assert_eq!(&url.path, &~""); // is empty path really correct? Other tests think so
let urlstr = ~"scheme://host:1234/";
let url = from_str(urlstr).unwrap();
assert_eq!(&url.scheme, &~"scheme");
assert_eq!(&url.host, &~"host");
assert_eq!(&url.port, &Some(~"1234"));
assert_eq!(&url.path, &~"/");
}

#[test]
fn test_url_with_underscores() {
let urlstr = ~"http://dotcom.com/file_name.html";
Expand Down