]> git.lizzy.rs Git - rust.git/commitdiff
Fix some parsing errors in extra::url
authorKevin Ballard <kevin@sb.org>
Mon, 19 Aug 2013 09:27:57 +0000 (02:27 -0700)
committerKevin Ballard <kevin@sb.org>
Mon, 19 Aug 2013 09:27:57 +0000 (02:27 -0700)
Fixes issue #8612.

src/libextra/url.rs

index 3f23cbd02abab64abcea103b919f4d56fcf1b9b2..579e17e9f8f05712f5f141a2ee545bc15e75f8e2 100644 (file)
@@ -61,6 +61,7 @@ pub fn new(scheme: ~str,
 }
 
 impl UserInfo {
+    #[inline]
     pub fn new(user: ~str, pass: Option<~str>) -> UserInfo {
         UserInfo { user: user, pass: pass }
     }
@@ -460,11 +461,14 @@ enum State {
               }
               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 @@ enum State {
           }
           _ => ()
         }
-        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 @@ enum State {
       }
     }
 
-    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 @@ fn test_url_parse_host_slash() {
         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";