diff --git a/src/libextra/url.rs b/src/libextra/url.rs index 3f23cbd02ab..579e17e9f8f 100644 --- a/src/libextra/url.rs +++ b/src/libextra/url.rs @@ -61,6 +61,7 @@ impl Url { } impl UserInfo { + #[inline] pub fn new(user: ~str, pass: Option<~str>) -> UserInfo { UserInfo { user: user, pass: pass } } @@ -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 { @@ -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 { @@ -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)); } @@ -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] @@ -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";