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)];
fn url(-scheme: ~str, -user: option<userinfo>, -host: ~str, -port: option<~str>,
-path: ~str, -query: query, -fragment: option<~str>) -> url {
fn url(-scheme: ~str, -user: option<userinfo>, -host: ~str,
-port: option<~str>, -path: ~str, -query: query,
-fragment: option<~str>) -> url {
{ scheme: scheme, user: user, host: host, port: port,
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
* sequences.
* Encodes a URI by replacing reserved characters with percent encoded
* character sequences.
*
* 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
* character sequences.
* Encodes a URI component by replacing reserved characters with percent
* encoded character sequences.
*
* 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
* 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| {
let m = str_hash();
let mut key = ~"";
@ -282,7 +284,8 @@ fn split_char_first(s: ~str, c: char) -> (~str, ~str) {
if index+match_ == len {
return (str::slice(s, 0, index), ~"");
} 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 {
digit, // all digits
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 mut st : state = start;
@ -415,7 +418,7 @@ fn get_authority(rawurl: ~str) ->
}
}
// now state machine
// now process states
alt c {
':' {
colon_count += 1;
@ -647,7 +650,8 @@ fn to_str(url: url) -> ~str {
str::concat(~[~"?", query_to_str(url.query)])
};
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 {
~""
};
@ -721,27 +725,33 @@ mod tests {
assert p == option::some(~"8000");
// invalid authorities;
assert result::is_err(get_authority(~"//user:pass@rust-lang:something"));
assert result::is_err(get_authority(~"//user@rust-lang:something:/path"));
assert result::is_err(get_authority(
~"//user:pass@rust-lang:something"));
assert result::is_err(get_authority(
~"//user@rust-lang:something:/path"));
assert result::is_err(get_authority(
~"//2001:0db8:85a3:0042:0000:8a2e:0370:7334:800a"));
assert result::is_err(get_authority(
~"//2001:0db8:85a3:0042:0000:8a2e:0370:7334:8000:00"));
// 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 == ~"";
let (_, h, _, _) = result::unwrap(get_authority(~"rust-lang.org"));
let (_, h, _, _) = result::unwrap(
get_authority(~"rust-lang.org"));
assert h == ~"";
}
#[test]
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 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 r == ~"#fragment";
let (p, r) = result::unwrap(get_path(~"/gen/:addr=?q=v", false));
@ -761,7 +771,8 @@ mod tests {
let u = result::unwrap(up);
assert u.scheme == ~"http";
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.path == ~"/doc";
assert u.query.find(|kv| kv.first() == ~"s").get().second() == ~"v";