diff options
Diffstat (limited to 'src/libstd/net_url.rs')
| -rw-r--r-- | src/libstd/net_url.rs | 82 |
1 files changed, 36 insertions, 46 deletions
diff --git a/src/libstd/net_url.rs b/src/libstd/net_url.rs index 74c603e29e9..40c9f96f5e8 100644 --- a/src/libstd/net_url.rs +++ b/src/libstd/net_url.rs @@ -1,6 +1,5 @@ //! Types/fns concerning URLs (see RFC 3986) -#[forbid(deprecated_mode)]; -#[forbid(deprecated_pattern)]; +// tjc: forbid deprecated modes again after a snapshot use core::cmp::Eq; use map::HashMap; @@ -11,15 +10,6 @@ use result::{Err, Ok}; use to_str::ToStr; use to_bytes::IterBytes; -export Url, Query; -export from_str, to_str; -export get_scheme; -export query_to_str; - -export encode, decode; -export encode_component, decode_component; -export encode_form_urlencoded, decode_form_urlencoded; - struct Url { scheme: ~str, user: Option<UserInfo>, @@ -35,9 +25,9 @@ type UserInfo = { pass: Option<~str> }; -type Query = ~[(~str, ~str)]; +pub type Query = ~[(~str, ~str)]; -fn Url(+scheme: ~str, +user: Option<UserInfo>, +host: ~str, +pub fn Url(scheme: ~str, +user: Option<UserInfo>, +host: ~str, +port: Option<~str>, +path: ~str, +query: Query, +fragment: Option<~str>) -> Url { Url { scheme: move scheme, user: move user, host: move host, @@ -45,7 +35,7 @@ fn Url(+scheme: ~str, +user: Option<UserInfo>, +host: ~str, fragment: move fragment } } -fn UserInfo(+user: ~str, +pass: Option<~str>) -> UserInfo { +fn UserInfo(user: ~str, +pass: Option<~str>) -> UserInfo { {user: move user, pass: move pass} } @@ -94,7 +84,7 @@ fn encode_inner(s: &str, full_url: bool) -> ~str { * * This function is compliant with RFC 3986. */ -fn encode(s: &str) -> ~str { +pub fn encode(s: &str) -> ~str { encode_inner(s, true) } @@ -104,7 +94,7 @@ fn encode(s: &str) -> ~str { * * This function is compliant with RFC 3986. */ -fn encode_component(s: &str) -> ~str { +pub fn encode_component(s: &str) -> ~str { encode_inner(s, false) } @@ -151,14 +141,14 @@ fn decode_inner(s: &str, full_url: bool) -> ~str { * * This will only decode escape sequences generated by encode_uri. */ -fn decode(s: &str) -> ~str { +pub fn decode(s: &str) -> ~str { decode_inner(s, true) } /** * Decode a string encoded with percent encoding. */ -fn decode_component(s: &str) -> ~str { +pub fn decode_component(s: &str) -> ~str { decode_inner(s, false) } @@ -184,7 +174,7 @@ fn encode_plus(s: &str) -> ~str { /** * Encode a hashmap to the 'application/x-www-form-urlencoded' media type. */ -fn encode_form_urlencoded(m: HashMap<~str, @DVec<@~str>>) -> ~str { +pub fn encode_form_urlencoded(m: HashMap<~str, @DVec<@~str>>) -> ~str { let mut out = ~""; let mut first = true; @@ -210,7 +200,7 @@ 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]) -> +pub fn decode_form_urlencoded(s: ~[u8]) -> map::HashMap<~str, @dvec::DVec<@~str>> { do io::with_bytes_reader(s) |rdr| { let m = HashMap(); @@ -307,7 +297,7 @@ fn userinfo_from_str(uinfo: &str) -> UserInfo { return UserInfo(user, pass); } -fn userinfo_to_str(+userinfo: UserInfo) -> ~str { +fn userinfo_to_str(userinfo: UserInfo) -> ~str { if option::is_some(&userinfo.pass) { return str::concat(~[copy userinfo.user, ~":", option::unwrap(copy userinfo.pass), @@ -329,13 +319,13 @@ fn query_from_str(rawquery: &str) -> Query { if str::len(rawquery) != 0 { for str::split_char(rawquery, '&').each |p| { let (k, v) = split_char_first(*p, '='); - vec::push(query, (decode_component(k), decode_component(v))); + query.push((decode_component(k), decode_component(v))); }; } return query; } -fn query_to_str(+query: Query) -> ~str { +pub fn query_to_str(query: Query) -> ~str { let mut strvec = ~[]; for query.each |kv| { let (k, v) = copy *kv; @@ -345,7 +335,7 @@ fn query_to_str(+query: Query) -> ~str { } // returns the scheme and the rest of the url, or a parsing error -fn get_scheme(rawurl: &str) -> result::Result<(~str, ~str), @~str> { +pub fn get_scheme(rawurl: &str) -> result::Result<(~str, ~str), @~str> { for str::each_chari(rawurl) |i,c| { match c { 'A' .. 'Z' | 'a' .. 'z' => loop, @@ -524,7 +514,7 @@ fn get_authority(rawurl: &str) -> let host_is_end_plus_one: &fn() -> bool = || { end+1 == len - && !['?', '#', '/'].contains(rawurl[end] as char) + && !['?', '#', '/'].contains(&(rawurl[end] as char)) }; // finish up @@ -624,33 +614,33 @@ fn get_query_fragment(rawurl: &str) -> * */ -fn from_str(rawurl: &str) -> result::Result<Url, ~str> { +pub fn from_str(rawurl: &str) -> result::Result<Url, ~str> { // scheme let mut schm = get_scheme(rawurl); - if result::is_err(schm) { - return result::Err(copy *result::get_err(schm)); + if result::is_err(&schm) { + return result::Err(copy *result::get_err(&schm)); } let (scheme, rest) = result::unwrap(schm); // authority let mut auth = get_authority(rest); - if result::is_err(auth) { - return result::Err(copy *result::get_err(auth)); + if result::is_err(&auth) { + return result::Err(copy *result::get_err(&auth)); } let (userinfo, host, port, rest) = result::unwrap(auth); // path let has_authority = if host == ~"" { false } else { true }; let mut pth = get_path(rest, has_authority); - if result::is_err(pth) { - return result::Err(copy *result::get_err(pth)); + if result::is_err(&pth) { + return result::Err(copy *result::get_err(&pth)); } let (path, rest) = result::unwrap(pth); // query and fragment let mut qry = get_query_fragment(rest); - if result::is_err(qry) { - return result::Err(copy *result::get_err(qry)); + if result::is_err(&qry) { + return result::Err(copy *result::get_err(&qry)); } let (query, fragment) = result::unwrap(qry); @@ -661,7 +651,7 @@ fn from_str(rawurl: &str) -> result::Result<Url, ~str> { impl Url : FromStr { static fn from_str(s: &str) -> Option<Url> { match from_str(s) { - Ok(url) => Some(url), + Ok(move url) => Some(url), Err(_) => None } } @@ -682,7 +672,7 @@ impl Url : FromStr { * result in just "http://somehost.com". * */ -fn to_str(+url: Url) -> ~str { +pub fn to_str(url: Url) -> ~str { let user = if url.user.is_some() { userinfo_to_str(option::unwrap(copy url.user)) } else { @@ -714,7 +704,7 @@ fn to_str(+url: Url) -> ~str { } impl Url: to_str::ToStr { - fn to_str() -> ~str { + pub fn to_str() -> ~str { to_str(self) } } @@ -736,7 +726,7 @@ impl Url : Eq { } impl Url: IterBytes { - pure fn iter_bytes(lsb0: bool, f: to_bytes::Cb) { + pure fn iter_bytes(+lsb0: bool, f: to_bytes::Cb) { unsafe { self.to_str() }.iter_bytes(lsb0, f) } } @@ -796,13 +786,13 @@ mod tests { assert p == option::Some(~"8000"); // invalid authorities; - assert result::is_err(get_authority( + 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")); - assert result::is_err(get_authority( + assert result::is_err(&get_authority( ~"//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")); // these parse as empty, because they don't start with '//' @@ -830,7 +820,7 @@ mod tests { assert r == ~"?q=v"; //failure cases - assert result::is_err(get_path(~"something?q", true)); + assert result::is_err(&get_path(~"something?q", true)); } @@ -877,13 +867,13 @@ mod tests { #[test] fn test_no_scheme() { - assert result::is_err(get_scheme(~"noschemehere.html")); + assert result::is_err(&get_scheme(~"noschemehere.html")); } #[test] fn test_invalid_scheme_errors() { - assert result::is_err(from_str(~"99://something")); - assert result::is_err(from_str(~"://something")); + assert result::is_err(&from_str(~"99://something")); + assert result::is_err(&from_str(~"://something")); } #[test] |
