From 3fe1c7071d9585f53748d8d0ccb5a06c7b850287 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Sun, 5 Aug 2012 16:33:28 -0700 Subject: [PATCH] std: Some fixes to url parsing --- src/libstd/net_url.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/libstd/net_url.rs b/src/libstd/net_url.rs index ac9d4b389f9..dd97c1fdeb2 100644 --- a/src/libstd/net_url.rs +++ b/src/libstd/net_url.rs @@ -489,10 +489,17 @@ enum input { end = i; } + let end = end; // make end immutable so it can be captured + + let host_is_end_plus_one = || { + end+1 == len + && !['?', '#', '/'].contains(rawurl[end] as char) + }; + // finish up match st { start { - if end+1 == len { + if host_is_end_plus_one() { host = str::slice(rawurl, begin, end+1); } else { host = str::slice(rawurl, begin, end); @@ -516,7 +523,7 @@ enum input { } } - let rest = if end+1 == len { ~"" } + let rest = if host_is_end_plus_one() { ~"" } else { str::slice(rawurl, end, len) }; return result::ok((userinfo, host, port, rest)); } @@ -779,6 +786,15 @@ fn test_url_parse() { assert option::unwrap(copy u.fragment) == ~"something"; } + #[test] + fn test_url_parse_host_slash() { + let urlstr = ~"http://0.42.42.42/"; + let url = from_str(urlstr).get(); + #debug("url: %?", url); + assert url.host == ~"0.42.42.42"; + assert url.path == ~"/"; + } + #[test] fn test_no_scheme() { assert result::is_err(get_scheme(~"noschemehere.html"));