std::net::url cleanups for lint check

This commit is contained in:
Daniel Patterson 2012-07-31 23:14:50 -04:00 committed by Brian Anderson
parent b57f6b73ba
commit 21187206c3

View file

@ -30,8 +30,9 @@ type userinfo = {
type query = ~[(~str, ~str)]; type query = ~[(~str, ~str)];
fn url(-scheme: ~str, -user: option<userinfo>, -host: ~str, -port: option<~str>, fn url(-scheme: ~str, -user: option<userinfo>, -host: ~str,
-path: ~str, -query: query, -fragment: option<~str>) -> url { -port: option<~str>, -path: ~str, -query: query,
-fragment: option<~str>) -> url {
{ scheme: scheme, user: user, host: host, port: port, { scheme: scheme, user: user, host: host, port: port,
path: path, query: query, fragment: fragment } path: path, query: query, fragment: fragment }
} }
@ -80,8 +81,8 @@ fn encode_inner(s: ~str, full_url: bool) -> ~str {
} }
/** /**
* Encodes a URI by replacing reserved characters with percent encoded character * Encodes a URI by replacing reserved characters with percent encoded
* sequences. * character sequences.
* *
* This function is compliant with RFC 3986. * This function is compliant with RFC 3986.
*/ */
@ -90,8 +91,8 @@ fn encode(s: ~str) -> ~str {
} }
/** /**
* Encodes a URI component by replacing reserved characters with percent encoded * Encodes a URI component by replacing reserved characters with percent
* character sequences. * encoded character sequences.
* *
* This function is compliant with RFC 3986. * This function is compliant with RFC 3986.
*/ */
@ -201,7 +202,8 @@ fn encode_form_urlencoded(m: hashmap<~str, @dvec<@~str>>) -> ~str {
* Decode a string encoded with the 'application/x-www-form-urlencoded' media * Decode a string encoded with the 'application/x-www-form-urlencoded' media
* type into a hashmap. * type into a hashmap.
*/ */
fn decode_form_urlencoded(s: ~[u8]) -> hashmap<~str, @dvec<@~str>> { fn decode_form_urlencoded(s: ~[u8]) ->
map::hashmap<~str, @dvec::dvec<@~str>> {
do io::with_bytes_reader(s) |rdr| { do io::with_bytes_reader(s) |rdr| {
let m = str_hash(); let m = str_hash();
let mut key = ~""; let mut key = ~"";
@ -282,7 +284,8 @@ fn split_char_first(s: ~str, c: char) -> (~str, ~str) {
if index+match_ == len { if index+match_ == len {
return (str::slice(s, 0, index), ~""); return (str::slice(s, 0, index), ~"");
} else { } else {
return (str::slice(s, 0, index), str::slice(s, index + match_, str::len(s))); return (str::slice(s, 0, index),
str::slice(s, index + match_, str::len(s)));
} }
} }
@ -373,7 +376,7 @@ fn get_authority(rawurl: ~str) ->
enum input { enum input {
digit, // all digits digit, // all digits
hex, // digits and letters a-f hex, // digits and letters a-f
unreserved // all other legal characters in usernames, passwords, hosts unreserved // all other legal characters
} }
let len = str::len(rawurl); let len = str::len(rawurl);
let mut st : state = start; let mut st : state = start;
@ -415,7 +418,7 @@ fn get_authority(rawurl: ~str) ->
} }
} }
// now state machine // now process states
alt c { alt c {
':' { ':' {
colon_count += 1; colon_count += 1;
@ -647,7 +650,8 @@ fn to_str(url: url) -> ~str {
str::concat(~[~"?", query_to_str(url.query)]) str::concat(~[~"?", query_to_str(url.query)])
}; };
let fragment = if option::is_some(url.fragment) { let fragment = if option::is_some(url.fragment) {
str::concat(~[~"#", encode_component(option::unwrap(copy url.fragment))]) str::concat(~[~"#", encode_component(
option::unwrap(copy url.fragment))])
} else { } else {
~"" ~""
}; };
@ -721,27 +725,33 @@ mod tests {
assert p == option::some(~"8000"); assert p == option::some(~"8000");
// invalid authorities; // invalid authorities;
assert result::is_err(get_authority(~"//user:pass@rust-lang:something")); assert result::is_err(get_authority(
assert result::is_err(get_authority(~"//user@rust-lang:something:/path")); ~"//user:pass@rust-lang:something"));
assert result::is_err(get_authority(
~"//user@rust-lang:something:/path"));
assert result::is_err(get_authority( assert result::is_err(get_authority(
~"//2001:0db8:85a3:0042:0000:8a2e:0370:7334:800a")); ~"//2001:0db8:85a3:0042:0000:8a2e:0370:7334:800a"));
assert result::is_err(get_authority( assert result::is_err(get_authority(
~"//2001:0db8:85a3:0042:0000:8a2e:0370:7334:8000:00")); ~"//2001:0db8:85a3:0042:0000:8a2e:0370:7334:8000:00"));
// these parse as empty, because they don't start with '//' // these parse as empty, because they don't start with '//'
let (_, h, _, _) = result::unwrap(get_authority(~"user:pass@rust-lang")); let (_, h, _, _) = result::unwrap(
get_authority(~"user:pass@rust-lang"));
assert h == ~""; assert h == ~"";
let (_, h, _, _) = result::unwrap(get_authority(~"rust-lang.org")); let (_, h, _, _) = result::unwrap(
get_authority(~"rust-lang.org"));
assert h == ~""; assert h == ~"";
} }
#[test] #[test]
fn test_get_path() { fn test_get_path() {
let (p, r) = result::unwrap(get_path(~"/something+%20orother", true)); let (p, r) = result::unwrap(get_path(
~"/something+%20orother", true));
assert p == ~"/something+ orother"; assert p == ~"/something+ orother";
assert r == ~""; assert r == ~"";
let (p, r) = result::unwrap(get_path(~"test@email.com#fragment", false)); let (p, r) = result::unwrap(get_path(
~"test@email.com#fragment", false));
assert p == ~"test@email.com"; assert p == ~"test@email.com";
assert r == ~"#fragment"; assert r == ~"#fragment";
let (p, r) = result::unwrap(get_path(~"/gen/:addr=?q=v", false)); let (p, r) = result::unwrap(get_path(~"/gen/:addr=?q=v", false));
@ -761,7 +771,8 @@ mod tests {
let u = result::unwrap(up); let u = result::unwrap(up);
assert u.scheme == ~"http"; assert u.scheme == ~"http";
assert option::unwrap(copy u.user).user == ~"user"; assert option::unwrap(copy u.user).user == ~"user";
assert option::unwrap(copy option::unwrap(copy u.user).pass) == ~"pass"; assert option::unwrap(copy option::unwrap(copy u.user).pass)
== ~"pass";
assert u.host == ~"rust-lang.org"; assert u.host == ~"rust-lang.org";
assert u.path == ~"/doc"; assert u.path == ~"/doc";
assert u.query.find(|kv| kv.first() == ~"s").get().second() == ~"v"; assert u.query.find(|kv| kv.first() == ~"s").get().second() == ~"v";