2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-07-09 13:08:07 -05:00
|
|
|
//! Types/fns concerning URLs (see RFC 3986)
|
2013-01-31 19:12:29 -06:00
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::cmp::Eq;
|
|
|
|
use core::io::{Reader, ReaderUtil};
|
|
|
|
use core::io;
|
2013-04-03 08:28:36 -05:00
|
|
|
use core::hashmap::HashMap;
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::str;
|
|
|
|
use core::to_bytes;
|
|
|
|
use core::uint;
|
2012-07-24 22:21:32 -05:00
|
|
|
|
2013-04-03 12:03:41 -05:00
|
|
|
#[deriving(Clone, Eq)]
|
2012-09-02 22:59:22 -05:00
|
|
|
struct Url {
|
2012-07-09 13:08:07 -05:00
|
|
|
scheme: ~str,
|
2012-08-30 13:01:39 -05:00
|
|
|
user: Option<UserInfo>,
|
2012-07-09 13:08:07 -05:00
|
|
|
host: ~str,
|
2012-08-20 14:23:37 -05:00
|
|
|
port: Option<~str>,
|
2012-07-09 13:08:07 -05:00
|
|
|
path: ~str,
|
2012-08-30 13:01:39 -05:00
|
|
|
query: Query,
|
2012-08-20 14:23:37 -05:00
|
|
|
fragment: Option<~str>
|
2012-09-02 22:59:22 -05:00
|
|
|
}
|
2012-07-09 13:08:07 -05:00
|
|
|
|
2013-04-03 12:03:41 -05:00
|
|
|
#[deriving(Clone, Eq)]
|
2012-12-21 09:47:32 -06:00
|
|
|
struct UserInfo {
|
2012-07-09 13:08:07 -05:00
|
|
|
user: ~str,
|
2012-08-20 14:23:37 -05:00
|
|
|
pass: Option<~str>
|
2012-12-21 09:47:32 -06:00
|
|
|
}
|
2012-07-09 13:08:07 -05:00
|
|
|
|
2012-10-03 18:43:56 -05:00
|
|
|
pub type Query = ~[(~str, ~str)];
|
2012-07-09 13:08:07 -05:00
|
|
|
|
2012-12-21 09:47:32 -06:00
|
|
|
pub impl Url {
|
2013-03-21 23:34:30 -05:00
|
|
|
fn new(
|
2012-12-21 09:47:32 -06:00
|
|
|
scheme: ~str,
|
|
|
|
user: Option<UserInfo>,
|
|
|
|
host: ~str,
|
|
|
|
port: Option<~str>,
|
|
|
|
path: ~str,
|
|
|
|
query: Query,
|
|
|
|
fragment: Option<~str>
|
|
|
|
) -> Url {
|
|
|
|
Url {
|
|
|
|
scheme: scheme,
|
|
|
|
user: user,
|
|
|
|
host: host,
|
|
|
|
port: port,
|
|
|
|
path: path,
|
|
|
|
query: query,
|
|
|
|
fragment: fragment,
|
|
|
|
}
|
|
|
|
}
|
2012-07-09 13:08:07 -05:00
|
|
|
}
|
|
|
|
|
2012-12-21 09:47:32 -06:00
|
|
|
pub impl UserInfo {
|
2013-03-21 23:34:30 -05:00
|
|
|
fn new(user: ~str, pass: Option<~str>) -> UserInfo {
|
2012-12-21 09:47:32 -06:00
|
|
|
UserInfo { user: user, pass: pass }
|
|
|
|
}
|
2012-07-09 13:08:07 -05:00
|
|
|
}
|
|
|
|
|
2012-09-03 14:33:51 -05:00
|
|
|
fn encode_inner(s: &str, full_url: bool) -> ~str {
|
2012-07-24 22:21:32 -05:00
|
|
|
do io::with_str_reader(s) |rdr| {
|
|
|
|
let mut out = ~"";
|
|
|
|
|
|
|
|
while !rdr.eof() {
|
|
|
|
let ch = rdr.read_byte() as char;
|
2012-08-01 17:04:33 -05:00
|
|
|
match ch {
|
2012-07-24 22:21:32 -05:00
|
|
|
// unreserved:
|
2012-09-01 20:38:05 -05:00
|
|
|
'A' .. 'Z' |
|
|
|
|
'a' .. 'z' |
|
|
|
|
'0' .. '9' |
|
2012-08-03 21:59:04 -05:00
|
|
|
'-' | '.' | '_' | '~' => {
|
2012-09-21 20:36:32 -05:00
|
|
|
str::push_char(&mut out, ch);
|
2012-07-24 22:21:32 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {
|
|
|
|
if full_url {
|
2012-08-01 17:04:33 -05:00
|
|
|
match ch {
|
2012-07-24 22:21:32 -05:00
|
|
|
// gen-delims:
|
|
|
|
':' | '/' | '?' | '#' | '[' | ']' | '@' |
|
|
|
|
|
|
|
|
// sub-delims:
|
|
|
|
'!' | '$' | '&' | '"' | '(' | ')' | '*' |
|
2012-08-03 21:59:04 -05:00
|
|
|
'+' | ',' | ';' | '=' => {
|
2012-09-21 20:36:32 -05:00
|
|
|
str::push_char(&mut out, ch);
|
2012-07-24 22:21:32 -05:00
|
|
|
}
|
|
|
|
|
2012-10-12 14:32:36 -05:00
|
|
|
_ => out += fmt!("%%%X", ch as uint)
|
2012-07-24 22:21:32 -05:00
|
|
|
}
|
|
|
|
} else {
|
2012-10-12 14:32:36 -05:00
|
|
|
out += fmt!("%%%X", ch as uint);
|
2012-07-24 22:21:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-31 22:14:50 -05:00
|
|
|
/**
|
|
|
|
* Encodes a URI by replacing reserved characters with percent encoded
|
|
|
|
* character sequences.
|
2012-07-24 22:21:32 -05:00
|
|
|
*
|
|
|
|
* This function is compliant with RFC 3986.
|
|
|
|
*/
|
2013-03-21 23:34:30 -05:00
|
|
|
pub fn encode(s: &str) -> ~str {
|
2013-04-09 00:31:42 -05:00
|
|
|
encode_inner(s, true)
|
2012-07-24 22:21:32 -05:00
|
|
|
}
|
|
|
|
|
2012-07-31 22:14:50 -05:00
|
|
|
/**
|
|
|
|
* Encodes a URI component by replacing reserved characters with percent
|
|
|
|
* encoded character sequences.
|
2012-07-24 22:21:32 -05:00
|
|
|
*
|
|
|
|
* This function is compliant with RFC 3986.
|
|
|
|
*/
|
2012-10-11 16:12:50 -05:00
|
|
|
|
2013-03-21 23:34:30 -05:00
|
|
|
pub fn encode_component(s: &str) -> ~str {
|
2013-04-09 00:31:42 -05:00
|
|
|
encode_inner(s, false)
|
2012-07-24 22:21:32 -05:00
|
|
|
}
|
|
|
|
|
2012-09-02 23:43:20 -05:00
|
|
|
fn decode_inner(s: &str, full_url: bool) -> ~str {
|
2012-07-24 22:21:32 -05:00
|
|
|
do io::with_str_reader(s) |rdr| {
|
|
|
|
let mut out = ~"";
|
|
|
|
|
|
|
|
while !rdr.eof() {
|
2012-08-01 17:04:33 -05:00
|
|
|
match rdr.read_char() {
|
2012-08-03 21:59:04 -05:00
|
|
|
'%' => {
|
2012-07-24 22:21:32 -05:00
|
|
|
let bytes = rdr.read_bytes(2u);
|
2012-09-14 11:55:33 -05:00
|
|
|
let ch = uint::parse_bytes(bytes, 16u).get() as char;
|
2012-07-24 22:21:32 -05:00
|
|
|
|
|
|
|
if full_url {
|
|
|
|
// Only decode some characters:
|
2012-08-01 17:04:33 -05:00
|
|
|
match ch {
|
2012-07-24 22:21:32 -05:00
|
|
|
// gen-delims:
|
|
|
|
':' | '/' | '?' | '#' | '[' | ']' | '@' |
|
|
|
|
|
|
|
|
// sub-delims:
|
|
|
|
'!' | '$' | '&' | '"' | '(' | ')' | '*' |
|
2012-08-03 21:59:04 -05:00
|
|
|
'+' | ',' | ';' | '=' => {
|
2012-09-21 20:36:32 -05:00
|
|
|
str::push_char(&mut out, '%');
|
|
|
|
str::push_char(&mut out, bytes[0u] as char);
|
|
|
|
str::push_char(&mut out, bytes[1u] as char);
|
2012-07-24 22:21:32 -05:00
|
|
|
}
|
|
|
|
|
2012-09-21 20:36:32 -05:00
|
|
|
ch => str::push_char(&mut out, ch)
|
2012-07-24 22:21:32 -05:00
|
|
|
}
|
|
|
|
} else {
|
2012-09-21 20:36:32 -05:00
|
|
|
str::push_char(&mut out, ch);
|
2012-07-24 22:21:32 -05:00
|
|
|
}
|
|
|
|
}
|
2012-09-21 20:36:32 -05:00
|
|
|
ch => str::push_char(&mut out, ch)
|
2012-07-24 22:21:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-25 18:22:59 -05:00
|
|
|
/**
|
|
|
|
* Decode a string encoded with percent encoding.
|
2012-07-31 22:14:50 -05:00
|
|
|
*
|
2012-12-21 09:47:32 -06:00
|
|
|
* This will only decode escape sequences generated by encode.
|
2012-07-24 22:21:32 -05:00
|
|
|
*/
|
2013-03-21 23:34:30 -05:00
|
|
|
pub fn decode(s: &str) -> ~str {
|
2013-04-09 00:31:42 -05:00
|
|
|
decode_inner(s, true)
|
2012-07-24 22:21:32 -05:00
|
|
|
}
|
|
|
|
|
2012-07-31 22:14:50 -05:00
|
|
|
/**
|
2012-07-25 18:22:59 -05:00
|
|
|
* Decode a string encoded with percent encoding.
|
2012-07-24 22:21:32 -05:00
|
|
|
*/
|
2013-03-21 23:34:30 -05:00
|
|
|
pub fn decode_component(s: &str) -> ~str {
|
2013-04-09 00:31:42 -05:00
|
|
|
decode_inner(s, false)
|
2012-07-24 22:21:32 -05:00
|
|
|
}
|
|
|
|
|
2012-09-02 23:43:20 -05:00
|
|
|
fn encode_plus(s: &str) -> ~str {
|
2012-07-24 22:21:32 -05:00
|
|
|
do io::with_str_reader(s) |rdr| {
|
|
|
|
let mut out = ~"";
|
|
|
|
|
|
|
|
while !rdr.eof() {
|
|
|
|
let ch = rdr.read_byte() as char;
|
2012-08-01 17:04:33 -05:00
|
|
|
match ch {
|
2012-09-01 20:38:05 -05:00
|
|
|
'A' .. 'Z' | 'a' .. 'z' | '0' .. '9' | '_' | '.' | '-' => {
|
2012-09-21 20:36:32 -05:00
|
|
|
str::push_char(&mut out, ch);
|
2012-07-24 22:21:32 -05:00
|
|
|
}
|
2012-09-21 20:36:32 -05:00
|
|
|
' ' => str::push_char(&mut out, '+'),
|
2012-10-12 14:32:36 -05:00
|
|
|
_ => out += fmt!("%%%X", ch as uint)
|
2012-07-24 22:21:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-31 22:14:50 -05:00
|
|
|
/**
|
2012-07-25 18:22:59 -05:00
|
|
|
* Encode a hashmap to the 'application/x-www-form-urlencoded' media type.
|
2012-07-24 22:21:32 -05:00
|
|
|
*/
|
2013-04-03 08:28:36 -05:00
|
|
|
pub fn encode_form_urlencoded(m: &HashMap<~str, ~[~str]>) -> ~str {
|
2012-07-24 22:21:32 -05:00
|
|
|
let mut out = ~"";
|
|
|
|
let mut first = true;
|
|
|
|
|
2013-04-06 10:22:36 -05:00
|
|
|
for m.each |key, values| {
|
2012-12-21 09:47:32 -06:00
|
|
|
let key = encode_plus(*key);
|
2012-07-24 22:21:32 -05:00
|
|
|
|
2013-02-07 20:03:13 -06:00
|
|
|
for values.each |value| {
|
2012-07-24 22:21:32 -05:00
|
|
|
if first {
|
|
|
|
first = false;
|
|
|
|
} else {
|
2012-09-21 20:36:32 -05:00
|
|
|
str::push_char(&mut out, '&');
|
2012-07-24 22:21:32 -05:00
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
2012-12-21 09:47:32 -06:00
|
|
|
out += fmt!("%s=%s", key, encode_plus(*value));
|
2012-07-24 22:21:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out
|
|
|
|
}
|
|
|
|
|
2012-07-31 22:14:50 -05:00
|
|
|
/**
|
2012-07-25 18:22:59 -05:00
|
|
|
* Decode a string encoded with the 'application/x-www-form-urlencoded' media
|
2012-07-24 22:21:32 -05:00
|
|
|
* type into a hashmap.
|
|
|
|
*/
|
2013-04-03 08:28:36 -05:00
|
|
|
pub fn decode_form_urlencoded(s: &[u8]) -> HashMap<~str, ~[~str]> {
|
2012-07-24 22:21:32 -05:00
|
|
|
do io::with_bytes_reader(s) |rdr| {
|
2013-04-03 08:28:36 -05:00
|
|
|
let mut m = HashMap::new();
|
2012-07-24 22:21:32 -05:00
|
|
|
let mut key = ~"";
|
|
|
|
let mut value = ~"";
|
|
|
|
let mut parsing_key = true;
|
|
|
|
|
|
|
|
while !rdr.eof() {
|
2012-08-01 17:04:33 -05:00
|
|
|
match rdr.read_char() {
|
2012-12-21 09:47:32 -06:00
|
|
|
'&' | ';' => {
|
|
|
|
if key != ~"" && value != ~"" {
|
|
|
|
let mut values = match m.pop(&key) {
|
2013-02-15 01:30:30 -06:00
|
|
|
Some(values) => values,
|
2012-12-21 09:47:32 -06:00
|
|
|
None => ~[],
|
|
|
|
};
|
|
|
|
|
|
|
|
values.push(value);
|
2012-07-24 22:21:32 -05:00
|
|
|
m.insert(key, values);
|
2012-12-21 09:47:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
parsing_key = true;
|
|
|
|
key = ~"";
|
|
|
|
value = ~"";
|
2012-07-24 22:21:32 -05:00
|
|
|
}
|
2012-12-21 09:47:32 -06:00
|
|
|
'=' => parsing_key = false,
|
|
|
|
ch => {
|
|
|
|
let ch = match ch {
|
|
|
|
'%' => {
|
|
|
|
let bytes = rdr.read_bytes(2u);
|
|
|
|
uint::parse_bytes(bytes, 16u).get() as char
|
|
|
|
}
|
|
|
|
'+' => ' ',
|
|
|
|
ch => ch
|
|
|
|
};
|
2012-07-24 22:21:32 -05:00
|
|
|
|
2012-12-21 09:47:32 -06:00
|
|
|
if parsing_key {
|
|
|
|
str::push_char(&mut key, ch)
|
|
|
|
} else {
|
|
|
|
str::push_char(&mut value, ch)
|
|
|
|
}
|
2012-07-24 22:21:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if key != ~"" && value != ~"" {
|
2012-12-21 09:47:32 -06:00
|
|
|
let mut values = match m.pop(&key) {
|
2013-02-15 01:30:30 -06:00
|
|
|
Some(values) => values,
|
2012-12-21 09:47:32 -06:00
|
|
|
None => ~[],
|
2012-07-24 22:21:32 -05:00
|
|
|
};
|
2012-12-21 09:47:32 -06:00
|
|
|
|
|
|
|
values.push(value);
|
|
|
|
m.insert(key, values);
|
2012-07-24 22:21:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-21 23:34:30 -05:00
|
|
|
fn split_char_first(s: &str, c: char) -> (~str, ~str) {
|
2012-07-25 21:40:31 -05:00
|
|
|
let len = str::len(s);
|
|
|
|
let mut index = len;
|
2012-08-01 17:04:33 -05:00
|
|
|
let mut mat = 0;
|
2013-04-09 00:31:42 -05:00
|
|
|
do io::with_str_reader(s) |rdr| {
|
|
|
|
let mut ch;
|
|
|
|
while !rdr.eof() {
|
|
|
|
ch = rdr.read_byte() as char;
|
|
|
|
if ch == c {
|
|
|
|
// found a match, adjust markers
|
|
|
|
index = rdr.tell()-1;
|
|
|
|
mat = 1;
|
|
|
|
break;
|
2012-07-25 21:40:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-01 17:04:33 -05:00
|
|
|
if index+mat == len {
|
2013-03-21 06:36:21 -05:00
|
|
|
return (str::slice(s, 0, index).to_owned(), ~"");
|
2012-07-09 13:08:07 -05:00
|
|
|
} else {
|
2013-03-21 06:36:21 -05:00
|
|
|
return (str::slice(s, 0, index).to_owned(),
|
|
|
|
str::slice(s, index + mat, str::len(s)).to_owned());
|
2012-07-09 13:08:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 23:34:30 -05:00
|
|
|
fn userinfo_from_str(uinfo: &str) -> UserInfo {
|
2012-07-09 13:08:07 -05:00
|
|
|
let (user, p) = split_char_first(uinfo, ':');
|
|
|
|
let pass = if str::len(p) == 0 {
|
2012-12-21 09:47:32 -06:00
|
|
|
None
|
2012-07-09 13:08:07 -05:00
|
|
|
} else {
|
2012-12-21 09:47:32 -06:00
|
|
|
Some(p)
|
2012-07-09 13:08:07 -05:00
|
|
|
};
|
2012-12-21 09:47:32 -06:00
|
|
|
return UserInfo::new(user, pass);
|
2012-07-09 13:08:07 -05:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:34:30 -05:00
|
|
|
fn userinfo_to_str(userinfo: &UserInfo) -> ~str {
|
2012-12-21 09:47:32 -06:00
|
|
|
match userinfo.pass {
|
|
|
|
Some(ref pass) => fmt!("%s:%s@", userinfo.user, *pass),
|
|
|
|
None => fmt!("%s@", userinfo.user),
|
2012-07-09 13:08:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 23:34:30 -05:00
|
|
|
fn query_from_str(rawquery: &str) -> Query {
|
2012-08-30 13:01:39 -05:00
|
|
|
let mut query: Query = ~[];
|
2012-07-09 13:08:07 -05:00
|
|
|
if str::len(rawquery) != 0 {
|
2013-03-24 01:51:18 -05:00
|
|
|
for str::each_split_char(rawquery, '&') |p| {
|
|
|
|
let (k, v) = split_char_first(p, '=');
|
2013-04-09 00:31:42 -05:00
|
|
|
query.push((decode_component(k), decode_component(v)));
|
2012-07-09 13:08:07 -05:00
|
|
|
};
|
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return query;
|
2012-07-09 13:08:07 -05:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:34:30 -05:00
|
|
|
pub fn query_to_str(query: &Query) -> ~str {
|
2013-04-09 00:31:42 -05:00
|
|
|
let mut strvec = ~[];
|
|
|
|
for query.each |kv| {
|
|
|
|
match kv {
|
|
|
|
&(ref k, ref v) => {
|
|
|
|
strvec.push(fmt!("%s=%s",
|
|
|
|
encode_component(*k),
|
|
|
|
encode_component(*v))
|
|
|
|
);
|
2012-12-21 09:47:32 -06:00
|
|
|
}
|
2012-10-11 16:12:50 -05:00
|
|
|
}
|
2012-12-21 09:47:32 -06:00
|
|
|
}
|
2013-05-19 00:07:44 -05:00
|
|
|
return str::connect(strvec, "&");
|
2012-07-09 13:08:07 -05:00
|
|
|
}
|
|
|
|
|
2012-07-31 21:44:37 -05:00
|
|
|
// returns the scheme and the rest of the url, or a parsing error
|
2013-03-21 23:34:30 -05:00
|
|
|
pub fn get_scheme(rawurl: &str) -> Result<(~str, ~str), ~str> {
|
2012-07-09 13:08:07 -05:00
|
|
|
for str::each_chari(rawurl) |i,c| {
|
2012-08-01 17:04:33 -05:00
|
|
|
match c {
|
2012-09-07 17:32:04 -05:00
|
|
|
'A' .. 'Z' | 'a' .. 'z' => loop,
|
2012-09-01 20:38:05 -05:00
|
|
|
'0' .. '9' | '+' | '-' | '.' => {
|
2012-07-25 21:40:31 -05:00
|
|
|
if i == 0 {
|
2012-12-21 09:47:32 -06:00
|
|
|
return Err(~"url: Scheme must begin with a letter.");
|
2012-07-25 21:40:31 -05:00
|
|
|
}
|
2012-09-07 17:32:04 -05:00
|
|
|
loop;
|
2012-07-25 21:40:31 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
':' => {
|
2012-07-25 21:40:31 -05:00
|
|
|
if i == 0 {
|
2012-12-21 09:47:32 -06:00
|
|
|
return Err(~"url: Scheme cannot be empty.");
|
2012-07-25 21:40:31 -05:00
|
|
|
} else {
|
2013-03-21 06:36:21 -05:00
|
|
|
return Ok((rawurl.slice(0,i).to_owned(),
|
|
|
|
rawurl.slice(i+1,str::len(rawurl)).to_owned()));
|
2012-07-25 21:40:31 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {
|
2012-12-21 09:47:32 -06:00
|
|
|
return Err(~"url: Invalid character in scheme.");
|
2012-07-25 21:40:31 -05:00
|
|
|
}
|
2012-07-09 13:08:07 -05:00
|
|
|
}
|
|
|
|
};
|
2012-12-21 09:47:32 -06:00
|
|
|
return Err(~"url: Scheme must be terminated with a colon.");
|
2012-07-09 13:08:07 -05:00
|
|
|
}
|
|
|
|
|
2013-04-03 12:03:41 -05:00
|
|
|
#[deriving(Clone, Eq)]
|
2012-08-30 13:01:39 -05:00
|
|
|
enum Input {
|
|
|
|
Digit, // all digits
|
|
|
|
Hex, // digits and letters a-f
|
|
|
|
Unreserved // all other legal characters
|
2012-08-27 18:26:35 -05:00
|
|
|
}
|
|
|
|
|
2012-07-31 21:44:37 -05:00
|
|
|
// returns userinfo, host, port, and unparsed part, or an error
|
2013-03-21 23:34:30 -05:00
|
|
|
fn get_authority(rawurl: &str) ->
|
2012-12-21 09:47:32 -06:00
|
|
|
Result<(Option<UserInfo>, ~str, Option<~str>, ~str), ~str> {
|
2013-05-19 00:07:44 -05:00
|
|
|
if !str::starts_with(rawurl, "//") {
|
2012-07-31 21:44:37 -05:00
|
|
|
// there is no authority.
|
2012-12-21 09:47:32 -06:00
|
|
|
return Ok((None, ~"", None, rawurl.to_str()));
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
|
|
|
|
2012-08-30 13:01:39 -05:00
|
|
|
enum State {
|
|
|
|
Start, // starting state
|
|
|
|
PassHostPort, // could be in user or port
|
|
|
|
Ip6Port, // either in ipv6 host or port
|
|
|
|
Ip6Host, // are in an ipv6 host
|
|
|
|
InHost, // are in a host - may be ipv6, but don't know yet
|
|
|
|
InPort // are in port
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
2012-08-27 18:26:35 -05:00
|
|
|
|
2012-12-21 09:47:32 -06:00
|
|
|
let len = rawurl.len();
|
|
|
|
let mut st = Start;
|
|
|
|
let mut in = Digit; // most restricted, start here.
|
2012-07-31 21:44:37 -05:00
|
|
|
|
2012-12-21 09:47:32 -06:00
|
|
|
let mut userinfo = None;
|
|
|
|
let mut host = ~"";
|
|
|
|
let mut port = None;
|
2012-07-31 21:44:37 -05:00
|
|
|
|
|
|
|
let mut colon_count = 0;
|
2012-12-21 09:47:32 -06:00
|
|
|
let mut pos = 0, begin = 2, end = len;
|
2012-07-31 21:44:37 -05:00
|
|
|
|
2012-08-01 16:19:43 -05:00
|
|
|
for str::each_chari(rawurl) |i,c| {
|
2012-09-07 17:32:04 -05:00
|
|
|
if i < 2 { loop; } // ignore the leading //
|
2012-07-31 21:44:37 -05:00
|
|
|
|
|
|
|
// deal with input class first
|
2012-08-01 17:04:33 -05:00
|
|
|
match c {
|
2012-09-01 20:38:05 -05:00
|
|
|
'0' .. '9' => (),
|
|
|
|
'A' .. 'F' | 'a' .. 'f' => {
|
2012-08-30 13:01:39 -05:00
|
|
|
if in == Digit {
|
|
|
|
in = Hex;
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
|
|
|
}
|
2012-09-01 20:38:05 -05:00
|
|
|
'G' .. 'Z' | 'g' .. 'z' | '-' | '.' | '_' | '~' | '%' |
|
2012-08-03 21:59:04 -05:00
|
|
|
'&' |'\'' | '(' | ')' | '+' | '!' | '*' | ',' | ';' | '=' => {
|
2012-08-30 13:01:39 -05:00
|
|
|
in = Unreserved;
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
':' | '@' | '?' | '#' | '/' => {
|
2012-07-31 21:44:37 -05:00
|
|
|
// separators, don't change anything
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {
|
2012-12-21 09:47:32 -06:00
|
|
|
return Err(~"Illegal character in authority");
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-31 22:14:50 -05:00
|
|
|
// now process states
|
2012-08-01 17:04:33 -05:00
|
|
|
match c {
|
2012-08-03 21:59:04 -05:00
|
|
|
':' => {
|
2012-07-31 21:44:37 -05:00
|
|
|
colon_count += 1;
|
2012-08-01 17:04:33 -05:00
|
|
|
match st {
|
2012-08-30 13:01:39 -05:00
|
|
|
Start => {
|
2012-07-31 21:44:37 -05:00
|
|
|
pos = i;
|
2012-08-30 13:01:39 -05:00
|
|
|
st = PassHostPort;
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
2012-08-30 13:01:39 -05:00
|
|
|
PassHostPort => {
|
2012-07-31 21:44:37 -05:00
|
|
|
// multiple colons means ipv6 address.
|
2012-08-30 13:01:39 -05:00
|
|
|
if in == Unreserved {
|
2012-12-21 09:47:32 -06:00
|
|
|
return Err(
|
|
|
|
~"Illegal characters in IPv6 address.");
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
2012-08-30 13:01:39 -05:00
|
|
|
st = Ip6Host;
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
2012-08-30 13:01:39 -05:00
|
|
|
InHost => {
|
2012-07-31 21:44:37 -05:00
|
|
|
pos = i;
|
|
|
|
// can't be sure whether this is an ipv6 address or a port
|
2012-08-30 13:01:39 -05:00
|
|
|
if in == Unreserved {
|
2012-12-21 09:47:32 -06:00
|
|
|
return Err(~"Illegal characters in authority.");
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
2012-08-30 13:01:39 -05:00
|
|
|
st = Ip6Port;
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
2012-08-30 13:01:39 -05:00
|
|
|
Ip6Port => {
|
|
|
|
if in == Unreserved {
|
2012-12-21 09:47:32 -06:00
|
|
|
return Err(~"Illegal characters in authority.");
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
2012-08-30 13:01:39 -05:00
|
|
|
st = Ip6Host;
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
2012-08-30 13:01:39 -05:00
|
|
|
Ip6Host => {
|
2012-07-31 21:44:37 -05:00
|
|
|
if colon_count > 7 {
|
2013-03-21 06:36:21 -05:00
|
|
|
host = str::slice(rawurl, begin, i).to_owned();
|
2012-07-31 21:44:37 -05:00
|
|
|
pos = i;
|
2012-08-30 13:01:39 -05:00
|
|
|
st = InPort;
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {
|
2012-12-21 09:47:32 -06:00
|
|
|
return Err(~"Invalid ':' in authority.");
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-30 13:01:39 -05:00
|
|
|
in = Digit; // reset input class
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:04 -05:00
|
|
|
'@' => {
|
2012-08-30 13:01:39 -05:00
|
|
|
in = Digit; // reset input class
|
2012-07-31 21:44:37 -05:00
|
|
|
colon_count = 0; // reset count
|
2012-08-01 17:04:33 -05:00
|
|
|
match st {
|
2012-08-30 13:01:39 -05:00
|
|
|
Start => {
|
2013-03-21 06:36:21 -05:00
|
|
|
let user = str::slice(rawurl, begin, i).to_owned();
|
2012-12-21 09:47:32 -06:00
|
|
|
userinfo = Some(UserInfo::new(user, None));
|
2012-08-30 13:01:39 -05:00
|
|
|
st = InHost;
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
2012-08-30 13:01:39 -05:00
|
|
|
PassHostPort => {
|
2013-03-21 06:36:21 -05:00
|
|
|
let user = str::slice(rawurl, begin, pos).to_owned();
|
|
|
|
let pass = str::slice(rawurl, pos+1, i).to_owned();
|
2012-12-21 09:47:32 -06:00
|
|
|
userinfo = Some(UserInfo::new(user, Some(pass)));
|
2012-08-30 13:01:39 -05:00
|
|
|
st = InHost;
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {
|
2012-12-21 09:47:32 -06:00
|
|
|
return Err(~"Invalid '@' in authority.");
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
begin = i+1;
|
|
|
|
}
|
2012-07-31 22:14:50 -05:00
|
|
|
|
2012-08-03 21:59:04 -05:00
|
|
|
'?' | '#' | '/' => {
|
2012-08-01 16:19:43 -05:00
|
|
|
end = i;
|
2012-07-31 21:44:37 -05:00
|
|
|
break;
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => ()
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
2012-08-01 16:19:43 -05:00
|
|
|
end = i;
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
|
|
|
|
2012-08-05 18:33:28 -05:00
|
|
|
let end = end; // make end immutable so it can be captured
|
|
|
|
|
2013-03-21 23:34:30 -05:00
|
|
|
let host_is_end_plus_one: &fn() -> bool = || {
|
2012-08-05 18:33:28 -05:00
|
|
|
end+1 == len
|
2012-09-25 19:39:22 -05:00
|
|
|
&& !['?', '#', '/'].contains(&(rawurl[end] as char))
|
2012-08-05 18:33:28 -05:00
|
|
|
};
|
|
|
|
|
2012-07-31 21:44:37 -05:00
|
|
|
// finish up
|
2012-08-01 17:04:33 -05:00
|
|
|
match st {
|
2012-08-30 13:01:39 -05:00
|
|
|
Start => {
|
2012-08-05 18:33:28 -05:00
|
|
|
if host_is_end_plus_one() {
|
2013-03-21 06:36:21 -05:00
|
|
|
host = str::slice(rawurl, begin, end+1).to_owned();
|
2012-07-31 21:44:37 -05:00
|
|
|
} else {
|
2013-03-21 06:36:21 -05:00
|
|
|
host = str::slice(rawurl, begin, end).to_owned();
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-30 13:01:39 -05:00
|
|
|
PassHostPort | Ip6Port => {
|
|
|
|
if in != Digit {
|
2012-12-21 09:47:32 -06:00
|
|
|
return Err(~"Non-digit characters in port.");
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
2013-03-21 06:36:21 -05:00
|
|
|
host = str::slice(rawurl, begin, pos).to_owned();
|
|
|
|
port = Some(str::slice(rawurl, pos+1, end).to_owned());
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
2012-08-30 13:01:39 -05:00
|
|
|
Ip6Host | InHost => {
|
2013-03-21 06:36:21 -05:00
|
|
|
host = str::slice(rawurl, begin, end).to_owned();
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
2012-08-30 13:01:39 -05:00
|
|
|
InPort => {
|
|
|
|
if in != Digit {
|
2012-12-21 09:47:32 -06:00
|
|
|
return Err(~"Non-digit characters in port.");
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
2013-03-21 06:36:21 -05:00
|
|
|
port = Some(str::slice(rawurl, pos+1, end).to_owned());
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-05 18:33:28 -05:00
|
|
|
let rest = if host_is_end_plus_one() { ~"" }
|
2013-03-21 06:36:21 -05:00
|
|
|
else { str::slice(rawurl, end, len).to_owned() };
|
2012-12-21 09:47:32 -06:00
|
|
|
return Ok((userinfo, host, port, rest));
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// returns the path and unparsed part of url, or an error
|
2013-03-21 23:34:30 -05:00
|
|
|
fn get_path(rawurl: &str, authority: bool) ->
|
2012-12-21 09:47:32 -06:00
|
|
|
Result<(~str, ~str), ~str> {
|
2012-07-31 21:44:37 -05:00
|
|
|
let len = str::len(rawurl);
|
|
|
|
let mut end = len;
|
|
|
|
for str::each_chari(rawurl) |i,c| {
|
2012-08-01 17:04:33 -05:00
|
|
|
match c {
|
2012-09-01 20:38:05 -05:00
|
|
|
'A' .. 'Z' | 'a' .. 'z' | '0' .. '9' | '&' |'\'' | '(' | ')' | '.'
|
2012-08-06 15:12:49 -05:00
|
|
|
| '@' | ':' | '%' | '/' | '+' | '!' | '*' | ',' | ';' | '='
|
2012-08-06 17:17:08 -05:00
|
|
|
| '_' | '-' => {
|
2012-09-07 17:32:04 -05:00
|
|
|
loop;
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
'?' | '#' => {
|
2012-07-31 21:44:37 -05:00
|
|
|
end = i;
|
|
|
|
break;
|
|
|
|
}
|
2012-12-21 09:47:32 -06:00
|
|
|
_ => return Err(~"Invalid character in path.")
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if authority {
|
2013-05-19 00:07:44 -05:00
|
|
|
if end != 0 && !str::starts_with(rawurl, "/") {
|
2012-12-21 09:47:32 -06:00
|
|
|
return Err(~"Non-empty path must begin with\
|
2012-07-31 21:44:37 -05:00
|
|
|
'/' in presence of authority.");
|
|
|
|
}
|
|
|
|
}
|
2012-07-31 22:14:50 -05:00
|
|
|
|
2013-03-21 06:36:21 -05:00
|
|
|
return Ok((decode_component(str::slice(rawurl, 0, end).to_owned()),
|
|
|
|
str::slice(rawurl, end, len).to_owned()));
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// returns the parsed query and the fragment, if present
|
2013-03-21 23:34:30 -05:00
|
|
|
fn get_query_fragment(rawurl: &str) ->
|
2012-12-21 09:47:32 -06:00
|
|
|
Result<(Query, Option<~str>), ~str> {
|
2013-05-19 00:07:44 -05:00
|
|
|
if !str::starts_with(rawurl, "?") {
|
|
|
|
if str::starts_with(rawurl, "#") {
|
2013-03-21 06:36:21 -05:00
|
|
|
let f = decode_component(str::slice(rawurl,
|
2012-07-31 22:14:50 -05:00
|
|
|
1,
|
2013-03-20 13:13:13 -05:00
|
|
|
str::len(rawurl)).to_owned());
|
2012-12-21 09:47:32 -06:00
|
|
|
return Ok((~[], Some(f)));
|
2012-07-31 21:44:37 -05:00
|
|
|
} else {
|
2012-12-21 09:47:32 -06:00
|
|
|
return Ok((~[], None));
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
|
|
|
}
|
2013-03-21 06:36:21 -05:00
|
|
|
let (q, r) = split_char_first(str::slice(rawurl, 1,
|
2013-03-20 13:13:13 -05:00
|
|
|
str::len(rawurl)).to_owned(), '#');
|
2012-07-31 22:14:50 -05:00
|
|
|
let f = if str::len(r) != 0 {
|
2012-12-21 09:47:32 -06:00
|
|
|
Some(decode_component(r)) } else { None };
|
|
|
|
return Ok((query_from_str(q), f));
|
2012-07-31 21:44:37 -05:00
|
|
|
}
|
|
|
|
|
2012-07-09 13:08:07 -05:00
|
|
|
/**
|
|
|
|
* Parse a `str` to a `url`
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* `rawurl` - a string representing a full url, including scheme.
|
|
|
|
*
|
|
|
|
* # Returns
|
|
|
|
*
|
|
|
|
* a `url` that contains the parsed representation of the url.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-03-21 23:34:30 -05:00
|
|
|
pub fn from_str(rawurl: &str) -> Result<Url, ~str> {
|
2012-07-31 21:44:37 -05:00
|
|
|
// scheme
|
2012-12-21 09:47:32 -06:00
|
|
|
let (scheme, rest) = match get_scheme(rawurl) {
|
|
|
|
Ok(val) => val,
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
};
|
2012-07-31 21:44:37 -05:00
|
|
|
|
|
|
|
// authority
|
2012-12-21 09:47:32 -06:00
|
|
|
let (userinfo, host, port, rest) = match get_authority(rest) {
|
|
|
|
Ok(val) => val,
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
};
|
2012-07-31 21:44:37 -05:00
|
|
|
|
|
|
|
// path
|
|
|
|
let has_authority = if host == ~"" { false } else { true };
|
2012-12-21 09:47:32 -06:00
|
|
|
let (path, rest) = match get_path(rest, has_authority) {
|
|
|
|
Ok(val) => val,
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
};
|
2012-07-31 21:44:37 -05:00
|
|
|
|
|
|
|
// query and fragment
|
2012-12-21 09:47:32 -06:00
|
|
|
let (query, fragment) = match get_query_fragment(rest) {
|
|
|
|
Ok(val) => val,
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
};
|
2012-07-09 13:08:07 -05:00
|
|
|
|
2012-12-21 09:47:32 -06:00
|
|
|
Ok(Url::new(scheme, userinfo, host, port, path, query, fragment))
|
2012-07-09 13:08:07 -05:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl FromStr for Url {
|
2013-03-21 23:34:30 -05:00
|
|
|
fn from_str(s: &str) -> Option<Url> {
|
2012-09-03 19:01:34 -05:00
|
|
|
match from_str(s) {
|
2013-02-15 01:30:30 -06:00
|
|
|
Ok(url) => Some(url),
|
2012-09-03 19:01:34 -05:00
|
|
|
Err(_) => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-09 13:08:07 -05:00
|
|
|
/**
|
|
|
|
* Format a `url` as a string
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* `url` - a url.
|
|
|
|
*
|
|
|
|
* # Returns
|
|
|
|
*
|
|
|
|
* a `str` that contains the formatted url. Note that this will usually
|
|
|
|
* be an inverse of `from_str` but might strip out unneeded separators.
|
|
|
|
* for example, "http://somehost.com?", when parsed and formatted, will
|
|
|
|
* result in just "http://somehost.com".
|
|
|
|
*
|
|
|
|
*/
|
2013-03-21 23:34:30 -05:00
|
|
|
pub fn to_str(url: &Url) -> ~str {
|
2012-12-21 09:47:32 -06:00
|
|
|
let user = match url.user {
|
|
|
|
Some(ref user) => userinfo_to_str(user),
|
|
|
|
None => ~"",
|
2012-07-09 13:08:07 -05:00
|
|
|
};
|
2012-12-21 09:47:32 -06:00
|
|
|
|
|
|
|
let authority = if url.host.is_empty() {
|
2012-07-31 23:40:38 -05:00
|
|
|
~""
|
2012-12-21 09:47:32 -06:00
|
|
|
} else {
|
|
|
|
fmt!("//%s%s", user, url.host)
|
2012-07-31 23:40:38 -05:00
|
|
|
};
|
2012-12-21 09:47:32 -06:00
|
|
|
|
|
|
|
let query = if url.query.is_empty() {
|
2012-07-09 13:08:07 -05:00
|
|
|
~""
|
|
|
|
} else {
|
2012-12-21 09:47:32 -06:00
|
|
|
fmt!("?%s", query_to_str(&url.query))
|
2012-07-09 13:08:07 -05:00
|
|
|
};
|
2012-12-21 09:47:32 -06:00
|
|
|
|
|
|
|
let fragment = match url.fragment {
|
|
|
|
Some(ref fragment) => fmt!("#%s", encode_component(*fragment)),
|
|
|
|
None => ~"",
|
2012-07-09 13:08:07 -05:00
|
|
|
};
|
|
|
|
|
2012-12-21 09:47:32 -06:00
|
|
|
fmt!("%s:%s%s%s%s", url.scheme, authority, url.path, query, fragment)
|
2012-07-09 13:08:07 -05:00
|
|
|
}
|
|
|
|
|
2013-05-02 06:20:22 -05:00
|
|
|
impl ToStr for Url {
|
2013-03-21 23:34:30 -05:00
|
|
|
pub fn to_str(&self) -> ~str {
|
2013-02-03 22:47:26 -06:00
|
|
|
to_str(self)
|
2012-07-28 18:05:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-02 17:33:27 -05:00
|
|
|
impl IterBytes for Url {
|
|
|
|
fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool {
|
|
|
|
self.to_str().iter_bytes(lsb0, f)
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 13:36:04 -06:00
|
|
|
|
2013-02-25 13:11:21 -06:00
|
|
|
// Put a few tests outside of the 'test' module so they can test the internal
|
|
|
|
// functions and those functions don't need 'pub'
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_split_char_first() {
|
2013-05-23 11:39:00 -05:00
|
|
|
let (u,v) = split_char_first("hello, sweet world", ',');
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(u, ~"hello");
|
|
|
|
assert_eq!(v, ~" sweet world");
|
2013-02-25 13:11:21 -06:00
|
|
|
|
2013-05-23 11:39:00 -05:00
|
|
|
let (u,v) = split_char_first("hello sweet world", ',');
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(u, ~"hello sweet world");
|
|
|
|
assert_eq!(v, ~"");
|
2013-02-25 13:11:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_authority() {
|
|
|
|
let (u, h, p, r) = get_authority(
|
|
|
|
"//user:pass@rust-lang.org/something").unwrap();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(u, Some(UserInfo::new(~"user", Some(~"pass"))));
|
|
|
|
assert_eq!(h, ~"rust-lang.org");
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(p.is_none());
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(r, ~"/something");
|
2013-02-25 13:11:21 -06:00
|
|
|
|
|
|
|
let (u, h, p, r) = get_authority(
|
|
|
|
"//rust-lang.org:8000?something").unwrap();
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(u.is_none());
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(h, ~"rust-lang.org");
|
|
|
|
assert_eq!(p, Some(~"8000"));
|
|
|
|
assert_eq!(r, ~"?something");
|
2013-02-25 13:11:21 -06:00
|
|
|
|
|
|
|
let (u, h, p, r) = get_authority(
|
|
|
|
"//rust-lang.org#blah").unwrap();
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(u.is_none());
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(h, ~"rust-lang.org");
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(p.is_none());
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(r, ~"#blah");
|
2013-02-25 13:11:21 -06:00
|
|
|
|
|
|
|
// ipv6 tests
|
|
|
|
let (_, h, _, _) = get_authority(
|
|
|
|
"//2001:0db8:85a3:0042:0000:8a2e:0370:7334#blah").unwrap();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(h, ~"2001:0db8:85a3:0042:0000:8a2e:0370:7334");
|
2013-02-25 13:11:21 -06:00
|
|
|
|
|
|
|
let (_, h, p, _) = get_authority(
|
|
|
|
"//2001:0db8:85a3:0042:0000:8a2e:0370:7334:8000#blah").unwrap();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(h, ~"2001:0db8:85a3:0042:0000:8a2e:0370:7334");
|
|
|
|
assert_eq!(p, Some(~"8000"));
|
2013-02-25 13:11:21 -06:00
|
|
|
|
|
|
|
let (u, h, p, _) = get_authority(
|
|
|
|
"//us:p@2001:0db8:85a3:0042:0000:8a2e:0370:7334:8000#blah"
|
|
|
|
).unwrap();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(u, Some(UserInfo::new(~"us", Some(~"p"))));
|
|
|
|
assert_eq!(h, ~"2001:0db8:85a3:0042:0000:8a2e:0370:7334");
|
|
|
|
assert_eq!(p, Some(~"8000"));
|
2013-02-25 13:11:21 -06:00
|
|
|
|
|
|
|
// invalid authorities;
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(get_authority("//user:pass@rust-lang:something").is_err());
|
|
|
|
assert!(get_authority("//user@rust-lang:something:/path").is_err());
|
|
|
|
assert!(get_authority(
|
2013-03-06 15:58:02 -06:00
|
|
|
"//2001:0db8:85a3:0042:0000:8a2e:0370:7334:800a").is_err());
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(get_authority(
|
2013-03-06 15:58:02 -06:00
|
|
|
"//2001:0db8:85a3:0042:0000:8a2e:0370:7334:8000:00").is_err());
|
2013-02-25 13:11:21 -06:00
|
|
|
|
|
|
|
// these parse as empty, because they don't start with '//'
|
2013-05-23 11:39:00 -05:00
|
|
|
let (_, h, _, _) = get_authority("user:pass@rust-lang").unwrap();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(h, ~"");
|
2013-05-23 11:39:00 -05:00
|
|
|
let (_, h, _, _) = get_authority("rust-lang.org").unwrap();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(h, ~"");
|
2013-02-25 13:11:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_path() {
|
|
|
|
let (p, r) = get_path("/something+%20orother", true).unwrap();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(p, ~"/something+ orother");
|
|
|
|
assert_eq!(r, ~"");
|
2013-02-25 13:11:21 -06:00
|
|
|
let (p, r) = get_path("test@email.com#fragment", false).unwrap();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(p, ~"test@email.com");
|
|
|
|
assert_eq!(r, ~"#fragment");
|
2013-05-23 11:39:00 -05:00
|
|
|
let (p, r) = get_path("/gen/:addr=?q=v", false).unwrap();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(p, ~"/gen/:addr=");
|
|
|
|
assert_eq!(r, ~"?q=v");
|
2013-02-25 13:11:21 -06:00
|
|
|
|
|
|
|
//failure cases
|
2013-05-23 11:39:00 -05:00
|
|
|
assert!(get_path("something?q", true).is_err());
|
2013-02-25 13:11:21 -06:00
|
|
|
}
|
|
|
|
|
2012-07-09 13:08:07 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2013-01-08 21:37:25 -06:00
|
|
|
|
|
|
|
use net_url::*;
|
2012-12-28 14:46:08 -06:00
|
|
|
|
2013-04-03 08:28:36 -05:00
|
|
|
use core::hashmap::HashMap;
|
2012-12-28 14:46:08 -06:00
|
|
|
|
2012-07-25 18:22:59 -05:00
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_url_parse() {
|
2012-07-25 18:22:59 -05:00
|
|
|
let url = ~"http://user:pass@rust-lang.org/doc?s=v#something";
|
2012-07-31 22:14:50 -05:00
|
|
|
|
2012-07-31 21:44:37 -05:00
|
|
|
let up = from_str(url);
|
2012-12-21 09:47:32 -06:00
|
|
|
let u = up.unwrap();
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(u.scheme == ~"http");
|
2012-12-21 09:47:32 -06:00
|
|
|
let userinfo = u.user.get_ref();
|
2013-03-28 20:39:09 -05:00
|
|
|
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");
|
2012-07-25 18:22:59 -05:00
|
|
|
}
|
2012-07-25 21:40:31 -05:00
|
|
|
|
2012-08-05 18:33:28 -05:00
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_url_parse_host_slash() {
|
2012-08-05 18:33:28 -05:00
|
|
|
let urlstr = ~"http://0.42.42.42/";
|
2012-12-21 09:47:32 -06:00
|
|
|
let url = from_str(urlstr).unwrap();
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(url.host == ~"0.42.42.42");
|
|
|
|
assert!(url.path == ~"/");
|
2012-08-05 18:33:28 -05:00
|
|
|
}
|
|
|
|
|
2012-08-06 15:12:49 -05:00
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_url_with_underscores() {
|
2012-08-06 15:12:49 -05:00
|
|
|
let urlstr = ~"http://dotcom.com/file_name.html";
|
2012-12-21 09:47:32 -06:00
|
|
|
let url = from_str(urlstr).unwrap();
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(url.path == ~"/file_name.html");
|
2012-08-06 15:12:49 -05:00
|
|
|
}
|
|
|
|
|
2012-08-06 17:17:08 -05:00
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_url_with_dashes() {
|
2012-08-06 17:17:08 -05:00
|
|
|
let urlstr = ~"http://dotcom.com/file-name.html";
|
2012-12-21 09:47:32 -06:00
|
|
|
let url = from_str(urlstr).unwrap();
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(url.path == ~"/file-name.html");
|
2012-08-06 17:17:08 -05:00
|
|
|
}
|
|
|
|
|
2012-08-05 15:48:26 -05:00
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_no_scheme() {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(get_scheme("noschemehere.html").is_err());
|
2012-08-05 15:48:26 -05:00
|
|
|
}
|
|
|
|
|
2012-07-25 21:40:31 -05:00
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_invalid_scheme_errors() {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(from_str("99://something").is_err());
|
|
|
|
assert!(from_str("://something").is_err());
|
2012-07-25 21:40:31 -05:00
|
|
|
}
|
|
|
|
|
2012-07-09 13:08:07 -05:00
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_full_url_parse_and_format() {
|
2012-07-09 13:08:07 -05:00
|
|
|
let url = ~"http://user:pass@rust-lang.org/doc?s=v#something";
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(from_str(url).unwrap().to_str(), url);
|
2012-07-09 13:08:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_userless_url_parse_and_format() {
|
2012-07-09 13:08:07 -05:00
|
|
|
let url = ~"http://rust-lang.org/doc?s=v#something";
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(from_str(url).unwrap().to_str(), url);
|
2012-07-09 13:08:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_queryless_url_parse_and_format() {
|
2012-07-09 13:08:07 -05:00
|
|
|
let url = ~"http://user:pass@rust-lang.org/doc#something";
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(from_str(url).unwrap().to_str(), url);
|
2012-07-09 13:08:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_empty_query_url_parse_and_format() {
|
2012-07-09 13:08:07 -05:00
|
|
|
let url = ~"http://user:pass@rust-lang.org/doc?#something";
|
|
|
|
let should_be = ~"http://user:pass@rust-lang.org/doc#something";
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(from_str(url).unwrap().to_str(), should_be);
|
2012-07-09 13:08:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_fragmentless_url_parse_and_format() {
|
2012-07-09 13:08:07 -05:00
|
|
|
let url = ~"http://user:pass@rust-lang.org/doc?q=v";
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(from_str(url).unwrap().to_str(), url);
|
2012-07-09 13:08:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_minimal_url_parse_and_format() {
|
2012-07-09 13:08:07 -05:00
|
|
|
let url = ~"http://rust-lang.org/doc";
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(from_str(url).unwrap().to_str(), url);
|
2012-07-09 13:08:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_scheme_host_only_url_parse_and_format() {
|
2012-07-09 13:08:07 -05:00
|
|
|
let url = ~"http://rust-lang.org";
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(from_str(url).unwrap().to_str(), url);
|
2012-07-09 13:08:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_pathless_url_parse_and_format() {
|
2012-07-09 13:08:07 -05:00
|
|
|
let url = ~"http://user:pass@rust-lang.org?q=v#something";
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(from_str(url).unwrap().to_str(), url);
|
2012-07-09 13:08:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_scheme_host_fragment_only_url_parse_and_format() {
|
2012-07-09 13:08:07 -05:00
|
|
|
let url = ~"http://rust-lang.org#something";
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(from_str(url).unwrap().to_str(), url);
|
2012-07-09 13:08:07 -05:00
|
|
|
}
|
|
|
|
|
2012-07-24 22:21:32 -05:00
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_url_component_encoding() {
|
2012-07-24 22:21:32 -05:00
|
|
|
let url = ~"http://rust-lang.org/doc%20uments?ba%25d%20=%23%26%2B";
|
2012-12-21 09:47:32 -06:00
|
|
|
let u = from_str(url).unwrap();
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(u.path == ~"/doc uments");
|
|
|
|
assert!(u.query == ~[(~"ba%d ", ~"#&+")]);
|
2012-07-24 22:21:32 -05:00
|
|
|
}
|
|
|
|
|
2012-07-31 23:40:38 -05:00
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_url_without_authority() {
|
2012-07-31 23:40:38 -05:00
|
|
|
let url = ~"mailto:test@email.com";
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(from_str(url).unwrap().to_str(), url);
|
2012-07-31 23:40:38 -05:00
|
|
|
}
|
|
|
|
|
2012-07-24 22:21:32 -05:00
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_encode() {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(encode(""), ~"");
|
|
|
|
assert_eq!(encode("http://example.com"), ~"http://example.com");
|
|
|
|
assert_eq!(encode("foo bar% baz"), ~"foo%20bar%25%20baz");
|
|
|
|
assert_eq!(encode(" "), ~"%20");
|
|
|
|
assert_eq!(encode("!"), ~"!");
|
|
|
|
assert_eq!(encode("\""), ~"\"");
|
|
|
|
assert_eq!(encode("#"), ~"#");
|
|
|
|
assert_eq!(encode("$"), ~"$");
|
|
|
|
assert_eq!(encode("%"), ~"%25");
|
|
|
|
assert_eq!(encode("&"), ~"&");
|
|
|
|
assert_eq!(encode("'"), ~"%27");
|
|
|
|
assert_eq!(encode("("), ~"(");
|
|
|
|
assert_eq!(encode(")"), ~")");
|
|
|
|
assert_eq!(encode("*"), ~"*");
|
|
|
|
assert_eq!(encode("+"), ~"+");
|
|
|
|
assert_eq!(encode(","), ~",");
|
|
|
|
assert_eq!(encode("/"), ~"/");
|
|
|
|
assert_eq!(encode(":"), ~":");
|
|
|
|
assert_eq!(encode(";"), ~";");
|
|
|
|
assert_eq!(encode("="), ~"=");
|
|
|
|
assert_eq!(encode("?"), ~"?");
|
|
|
|
assert_eq!(encode("@"), ~"@");
|
|
|
|
assert_eq!(encode("["), ~"[");
|
|
|
|
assert_eq!(encode("]"), ~"]");
|
2012-07-24 22:21:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_encode_component() {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(encode_component(""), ~"");
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(encode_component("http://example.com") ==
|
2013-03-06 15:58:02 -06:00
|
|
|
~"http%3A%2F%2Fexample.com");
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(encode_component("foo bar% baz") ==
|
2013-03-06 21:09:17 -06:00
|
|
|
~"foo%20bar%25%20baz");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(encode_component(" "), ~"%20");
|
|
|
|
assert_eq!(encode_component("!"), ~"%21");
|
|
|
|
assert_eq!(encode_component("#"), ~"%23");
|
|
|
|
assert_eq!(encode_component("$"), ~"%24");
|
|
|
|
assert_eq!(encode_component("%"), ~"%25");
|
|
|
|
assert_eq!(encode_component("&"), ~"%26");
|
|
|
|
assert_eq!(encode_component("'"), ~"%27");
|
|
|
|
assert_eq!(encode_component("("), ~"%28");
|
|
|
|
assert_eq!(encode_component(")"), ~"%29");
|
|
|
|
assert_eq!(encode_component("*"), ~"%2A");
|
|
|
|
assert_eq!(encode_component("+"), ~"%2B");
|
|
|
|
assert_eq!(encode_component(","), ~"%2C");
|
|
|
|
assert_eq!(encode_component("/"), ~"%2F");
|
|
|
|
assert_eq!(encode_component(":"), ~"%3A");
|
|
|
|
assert_eq!(encode_component(";"), ~"%3B");
|
|
|
|
assert_eq!(encode_component("="), ~"%3D");
|
|
|
|
assert_eq!(encode_component("?"), ~"%3F");
|
|
|
|
assert_eq!(encode_component("@"), ~"%40");
|
|
|
|
assert_eq!(encode_component("["), ~"%5B");
|
|
|
|
assert_eq!(encode_component("]"), ~"%5D");
|
2012-07-24 22:21:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_decode() {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(decode(""), ~"");
|
|
|
|
assert_eq!(decode("abc/def 123"), ~"abc/def 123");
|
|
|
|
assert_eq!(decode("abc%2Fdef%20123"), ~"abc%2Fdef 123");
|
|
|
|
assert_eq!(decode("%20"), ~" ");
|
|
|
|
assert_eq!(decode("%21"), ~"%21");
|
|
|
|
assert_eq!(decode("%22"), ~"%22");
|
|
|
|
assert_eq!(decode("%23"), ~"%23");
|
|
|
|
assert_eq!(decode("%24"), ~"%24");
|
|
|
|
assert_eq!(decode("%25"), ~"%");
|
|
|
|
assert_eq!(decode("%26"), ~"%26");
|
|
|
|
assert_eq!(decode("%27"), ~"'");
|
|
|
|
assert_eq!(decode("%28"), ~"%28");
|
|
|
|
assert_eq!(decode("%29"), ~"%29");
|
|
|
|
assert_eq!(decode("%2A"), ~"%2A");
|
|
|
|
assert_eq!(decode("%2B"), ~"%2B");
|
|
|
|
assert_eq!(decode("%2C"), ~"%2C");
|
|
|
|
assert_eq!(decode("%2F"), ~"%2F");
|
|
|
|
assert_eq!(decode("%3A"), ~"%3A");
|
|
|
|
assert_eq!(decode("%3B"), ~"%3B");
|
|
|
|
assert_eq!(decode("%3D"), ~"%3D");
|
|
|
|
assert_eq!(decode("%3F"), ~"%3F");
|
|
|
|
assert_eq!(decode("%40"), ~"%40");
|
|
|
|
assert_eq!(decode("%5B"), ~"%5B");
|
|
|
|
assert_eq!(decode("%5D"), ~"%5D");
|
2012-07-24 22:21:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_decode_component() {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(decode_component(""), ~"");
|
|
|
|
assert_eq!(decode_component("abc/def 123"), ~"abc/def 123");
|
|
|
|
assert_eq!(decode_component("abc%2Fdef%20123"), ~"abc/def 123");
|
|
|
|
assert_eq!(decode_component("%20"), ~" ");
|
|
|
|
assert_eq!(decode_component("%21"), ~"!");
|
|
|
|
assert_eq!(decode_component("%22"), ~"\"");
|
|
|
|
assert_eq!(decode_component("%23"), ~"#");
|
|
|
|
assert_eq!(decode_component("%24"), ~"$");
|
|
|
|
assert_eq!(decode_component("%25"), ~"%");
|
|
|
|
assert_eq!(decode_component("%26"), ~"&");
|
|
|
|
assert_eq!(decode_component("%27"), ~"'");
|
|
|
|
assert_eq!(decode_component("%28"), ~"(");
|
|
|
|
assert_eq!(decode_component("%29"), ~")");
|
|
|
|
assert_eq!(decode_component("%2A"), ~"*");
|
|
|
|
assert_eq!(decode_component("%2B"), ~"+");
|
|
|
|
assert_eq!(decode_component("%2C"), ~",");
|
|
|
|
assert_eq!(decode_component("%2F"), ~"/");
|
|
|
|
assert_eq!(decode_component("%3A"), ~":");
|
|
|
|
assert_eq!(decode_component("%3B"), ~";");
|
|
|
|
assert_eq!(decode_component("%3D"), ~"=");
|
|
|
|
assert_eq!(decode_component("%3F"), ~"?");
|
|
|
|
assert_eq!(decode_component("%40"), ~"@");
|
|
|
|
assert_eq!(decode_component("%5B"), ~"[");
|
|
|
|
assert_eq!(decode_component("%5D"), ~"]");
|
2012-07-24 22:21:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_encode_form_urlencoded() {
|
2013-04-03 08:28:36 -05:00
|
|
|
let mut m = HashMap::new();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(encode_form_urlencoded(&m), ~"");
|
2012-07-24 22:21:32 -05:00
|
|
|
|
2012-12-21 09:47:32 -06:00
|
|
|
m.insert(~"", ~[]);
|
|
|
|
m.insert(~"foo", ~[]);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(encode_form_urlencoded(&m), ~"");
|
2012-07-24 22:21:32 -05:00
|
|
|
|
2013-04-03 08:28:36 -05:00
|
|
|
let mut m = HashMap::new();
|
2012-12-21 09:47:32 -06:00
|
|
|
m.insert(~"foo", ~[~"bar", ~"123"]);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(encode_form_urlencoded(&m), ~"foo=bar&foo=123");
|
2012-07-24 22:21:32 -05:00
|
|
|
|
2013-04-03 08:28:36 -05:00
|
|
|
let mut m = HashMap::new();
|
2012-12-21 09:47:32 -06:00
|
|
|
m.insert(~"foo bar", ~[~"abc", ~"12 = 34"]);
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(encode_form_urlencoded(&m) ==
|
2013-03-06 21:09:17 -06:00
|
|
|
~"foo+bar=abc&foo+bar=12+%3D+34");
|
2012-07-24 22:21:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_decode_form_urlencoded() {
|
2013-01-11 15:40:15 -06:00
|
|
|
// FIXME #4449: Commented out because this causes an ICE, but only
|
|
|
|
// on FreeBSD
|
|
|
|
/*
|
2013-05-23 11:39:00 -05:00
|
|
|
assert_eq!(decode_form_urlencoded([]).len(), 0);
|
2012-07-24 22:21:32 -05:00
|
|
|
|
2012-12-21 09:47:32 -06:00
|
|
|
let s = str::to_bytes("a=1&foo+bar=abc&foo+bar=12+%3D+34");
|
|
|
|
let form = decode_form_urlencoded(s);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(form.len(), 2);
|
|
|
|
assert_eq!(form.get_ref(&~"a"), &~[~"1"]);
|
|
|
|
assert_eq!(form.get_ref(&~"foo bar"), &~[~"abc", ~"12 = 34"]);
|
2013-01-11 15:40:15 -06:00
|
|
|
*/
|
2012-07-24 22:21:32 -05:00
|
|
|
}
|
2012-07-28 00:23:36 -05:00
|
|
|
}
|