2014-02-13 22:28:22 +00:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-07-17 17:47:20 -07:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
//! Types/fns concerning URLs (see RFC 3986)
|
|
|
|
|
2014-05-12 15:30:24 -07:00
|
|
|
#![crate_id = "url#0.11.0-pre"]
|
2014-03-21 18:05:05 -07:00
|
|
|
#![crate_type = "rlib"]
|
|
|
|
#![crate_type = "dylib"]
|
|
|
|
#![license = "MIT/ASL2"]
|
|
|
|
#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
|
|
|
|
html_favicon_url = "http://www.rust-lang.org/favicon.ico",
|
2014-05-21 19:55:39 -07:00
|
|
|
html_root_url = "http://doc.rust-lang.org/")]
|
2014-03-21 18:05:05 -07:00
|
|
|
#![feature(default_type_params)]
|
2014-03-14 11:16:10 -07:00
|
|
|
|
|
|
|
extern crate collections;
|
2013-07-17 17:47:20 -07:00
|
|
|
|
2014-04-02 16:54:22 -07:00
|
|
|
use collections::HashMap;
|
2013-07-17 17:47:20 -07:00
|
|
|
use std::cmp::Eq;
|
2014-02-19 18:56:33 -08:00
|
|
|
use std::fmt;
|
2014-04-02 16:54:22 -07:00
|
|
|
use std::from_str::FromStr;
|
2014-02-25 08:03:41 -08:00
|
|
|
use std::hash::Hash;
|
2014-02-19 18:56:33 -08:00
|
|
|
use std::io::BufReader;
|
2014-04-02 16:54:22 -07:00
|
|
|
use std::strbuf::StrBuf;
|
2013-07-17 17:47:20 -07:00
|
|
|
use std::uint;
|
|
|
|
|
2013-12-01 12:30:32 +00:00
|
|
|
/// A Uniform Resource Locator (URL). A URL is a form of URI (Uniform Resource
|
|
|
|
/// Identifier) that includes network location information, such as hostname or
|
|
|
|
/// port number.
|
2013-12-01 22:25:58 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
2014-03-14 11:16:10 -07:00
|
|
|
/// use url::{Url, UserInfo};
|
2013-12-22 13:31:37 -08:00
|
|
|
///
|
2014-05-13 17:51:05 -07:00
|
|
|
/// let url = Url { scheme: "https".to_strbuf(),
|
|
|
|
/// user: Some(UserInfo { user: "username".to_strbuf(), pass: None }),
|
|
|
|
/// host: "example.com".to_strbuf(),
|
|
|
|
/// port: Some("8080".to_strbuf()),
|
|
|
|
/// path: "/foo/bar".to_strbuf(),
|
|
|
|
/// query: vec!(("baz".to_strbuf(), "qux".to_strbuf())),
|
|
|
|
/// fragment: Some("quz".to_strbuf()) };
|
2013-12-01 22:25:58 +00:00
|
|
|
/// // https://username@example.com:8080/foo/bar?baz=qux#quz
|
|
|
|
/// ```
|
2014-03-23 15:32:18 +01:00
|
|
|
#[deriving(Clone, Eq, TotalEq)]
|
2013-09-25 19:42:02 -07:00
|
|
|
pub struct Url {
|
2013-12-01 22:25:58 +00:00
|
|
|
/// The scheme part of a URL, such as `https` in the above example.
|
2014-05-13 17:51:05 -07:00
|
|
|
pub scheme: StrBuf,
|
2013-12-01 22:25:58 +00:00
|
|
|
/// A URL subcomponent for user authentication. `username` in the above example.
|
2014-03-28 12:41:44 -07:00
|
|
|
pub user: Option<UserInfo>,
|
2013-12-01 22:25:58 +00:00
|
|
|
/// A domain name or IP address. For example, `example.com`.
|
2014-05-13 17:51:05 -07:00
|
|
|
pub host: StrBuf,
|
2013-12-01 12:30:32 +00:00
|
|
|
/// A TCP port number, for example `8080`.
|
2014-05-13 17:51:05 -07:00
|
|
|
pub port: Option<StrBuf>,
|
2013-12-01 22:25:58 +00:00
|
|
|
/// The path component of a URL, for example `/foo/bar`.
|
2014-05-13 17:51:05 -07:00
|
|
|
pub path: StrBuf,
|
|
|
|
/// The query component of a URL.
|
|
|
|
/// `vec!(("baz".to_strbuf(), "qux".to_strbuf()))` represents the fragment
|
|
|
|
/// `baz=qux` in the above example.
|
2014-03-28 12:41:44 -07:00
|
|
|
pub query: Query,
|
2013-12-01 22:25:58 +00:00
|
|
|
/// The fragment component, such as `quz`. Doesn't include the leading `#` character.
|
2014-05-13 17:51:05 -07:00
|
|
|
pub fragment: Option<StrBuf>
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
2014-02-12 21:28:58 -08:00
|
|
|
#[deriving(Clone, Eq)]
|
|
|
|
pub struct Path {
|
|
|
|
/// The path component of a URL, for example `/foo/bar`.
|
2014-05-13 17:51:05 -07:00
|
|
|
pub path: StrBuf,
|
|
|
|
/// The query component of a URL.
|
|
|
|
/// `vec!(("baz".to_strbuf(), "qux".to_strbuf()))` represents the fragment
|
|
|
|
/// `baz=qux` in the above example.
|
2014-03-28 12:41:44 -07:00
|
|
|
pub query: Query,
|
2014-02-12 21:28:58 -08:00
|
|
|
/// The fragment component, such as `quz`. Doesn't include the leading `#` character.
|
2014-05-13 17:51:05 -07:00
|
|
|
pub fragment: Option<StrBuf>
|
2014-02-12 21:28:58 -08:00
|
|
|
}
|
|
|
|
|
2013-12-01 12:30:32 +00:00
|
|
|
/// An optional subcomponent of a URI authority component.
|
2014-03-23 15:32:18 +01:00
|
|
|
#[deriving(Clone, Eq, TotalEq)]
|
2013-09-25 19:42:02 -07:00
|
|
|
pub struct UserInfo {
|
2013-12-01 12:30:32 +00:00
|
|
|
/// The user name.
|
2014-05-13 17:51:05 -07:00
|
|
|
pub user: StrBuf,
|
2013-12-01 12:30:32 +00:00
|
|
|
/// Password or other scheme-specific authentication information.
|
2014-05-13 17:51:05 -07:00
|
|
|
pub pass: Option<StrBuf>
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
2013-12-01 12:30:32 +00:00
|
|
|
/// Represents the query component of a URI.
|
2014-05-13 17:51:05 -07:00
|
|
|
pub type Query = Vec<(StrBuf, StrBuf)>;
|
2013-07-17 17:47:20 -07:00
|
|
|
|
|
|
|
impl Url {
|
2014-05-13 17:51:05 -07:00
|
|
|
pub fn new(scheme: StrBuf,
|
2013-07-17 17:47:20 -07:00
|
|
|
user: Option<UserInfo>,
|
2014-05-13 17:51:05 -07:00
|
|
|
host: StrBuf,
|
|
|
|
port: Option<StrBuf>,
|
|
|
|
path: StrBuf,
|
2013-07-17 17:47:20 -07:00
|
|
|
query: Query,
|
2014-05-13 17:51:05 -07:00
|
|
|
fragment: Option<StrBuf>)
|
2013-07-17 17:47:20 -07:00
|
|
|
-> Url {
|
|
|
|
Url {
|
|
|
|
scheme: scheme,
|
|
|
|
user: user,
|
|
|
|
host: host,
|
|
|
|
port: port,
|
|
|
|
path: path,
|
|
|
|
query: query,
|
|
|
|
fragment: fragment,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 21:28:58 -08:00
|
|
|
impl Path {
|
2014-05-13 17:51:05 -07:00
|
|
|
pub fn new(path: StrBuf,
|
2014-02-12 21:28:58 -08:00
|
|
|
query: Query,
|
2014-05-13 17:51:05 -07:00
|
|
|
fragment: Option<StrBuf>)
|
2014-02-12 21:28:58 -08:00
|
|
|
-> Path {
|
|
|
|
Path {
|
|
|
|
path: path,
|
|
|
|
query: query,
|
|
|
|
fragment: fragment,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-17 17:47:20 -07:00
|
|
|
impl UserInfo {
|
2013-08-19 02:27:57 -07:00
|
|
|
#[inline]
|
2014-05-13 17:51:05 -07:00
|
|
|
pub fn new(user: StrBuf, pass: Option<StrBuf>) -> UserInfo {
|
2013-07-17 17:47:20 -07:00
|
|
|
UserInfo { user: user, pass: pass }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-13 17:51:05 -07:00
|
|
|
fn encode_inner(s: &str, full_url: bool) -> StrBuf {
|
2013-10-13 18:48:47 -07:00
|
|
|
let mut rdr = BufReader::new(s.as_bytes());
|
2014-04-02 16:54:22 -07:00
|
|
|
let mut out = StrBuf::new();
|
2013-10-13 18:48:47 -07:00
|
|
|
|
|
|
|
loop {
|
|
|
|
let mut buf = [0];
|
|
|
|
let ch = match rdr.read(buf) {
|
2014-01-29 17:39:12 -08:00
|
|
|
Err(..) => break,
|
|
|
|
Ok(..) => buf[0] as char,
|
2013-10-13 18:48:47 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
match ch {
|
|
|
|
// unreserved:
|
|
|
|
'A' .. 'Z' |
|
|
|
|
'a' .. 'z' |
|
|
|
|
'0' .. '9' |
|
|
|
|
'-' | '.' | '_' | '~' => {
|
|
|
|
out.push_char(ch);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
if full_url {
|
|
|
|
match ch {
|
|
|
|
// gen-delims:
|
|
|
|
':' | '/' | '?' | '#' | '[' | ']' | '@' |
|
|
|
|
|
|
|
|
// sub-delims:
|
|
|
|
'!' | '$' | '&' | '"' | '(' | ')' | '*' |
|
|
|
|
'+' | ',' | ';' | '=' => {
|
|
|
|
out.push_char(ch);
|
|
|
|
}
|
|
|
|
|
2014-05-16 10:45:16 -07:00
|
|
|
_ => out.push_str(format!("%{:X}", ch as uint).as_slice())
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
2013-10-13 18:48:47 -07:00
|
|
|
} else {
|
2014-05-16 10:45:16 -07:00
|
|
|
out.push_str(format!("%{:X}", ch as uint).as_slice());
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
2013-10-13 18:48:47 -07:00
|
|
|
}
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
}
|
2013-10-13 18:48:47 -07:00
|
|
|
|
2014-05-13 17:51:05 -07:00
|
|
|
out
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-13 22:28:22 +00:00
|
|
|
* Encodes a URI by replacing reserved characters with percent-encoded
|
2013-07-17 17:47:20 -07:00
|
|
|
* character sequences.
|
|
|
|
*
|
|
|
|
* This function is compliant with RFC 3986.
|
2014-02-13 22:28:22 +00:00
|
|
|
*
|
|
|
|
* # Example
|
|
|
|
*
|
|
|
|
* ```rust
|
2014-03-14 11:16:10 -07:00
|
|
|
* use url::encode;
|
2014-02-13 22:28:22 +00:00
|
|
|
*
|
2014-05-01 01:32:13 -04:00
|
|
|
* let url = encode("https://example.com/Rust (programming language)");
|
2014-02-13 22:28:22 +00:00
|
|
|
* println!("{}", url); // https://example.com/Rust%20(programming%20language)
|
|
|
|
* ```
|
2013-07-17 17:47:20 -07:00
|
|
|
*/
|
2014-05-13 17:51:05 -07:00
|
|
|
pub fn encode(s: &str) -> StrBuf {
|
2013-07-17 17:47:20 -07:00
|
|
|
encode_inner(s, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-14 15:48:30 -04:00
|
|
|
* Encodes a URI component by replacing reserved characters with percent-
|
2013-07-17 17:47:20 -07:00
|
|
|
* encoded character sequences.
|
|
|
|
*
|
|
|
|
* This function is compliant with RFC 3986.
|
|
|
|
*/
|
|
|
|
|
2014-05-13 17:51:05 -07:00
|
|
|
pub fn encode_component(s: &str) -> StrBuf {
|
2013-07-17 17:47:20 -07:00
|
|
|
encode_inner(s, false)
|
|
|
|
}
|
|
|
|
|
2014-05-13 17:51:05 -07:00
|
|
|
fn decode_inner(s: &str, full_url: bool) -> StrBuf {
|
2013-10-13 18:48:47 -07:00
|
|
|
let mut rdr = BufReader::new(s.as_bytes());
|
2014-04-02 16:54:22 -07:00
|
|
|
let mut out = StrBuf::new();
|
2013-10-13 18:48:47 -07:00
|
|
|
|
|
|
|
loop {
|
|
|
|
let mut buf = [0];
|
|
|
|
let ch = match rdr.read(buf) {
|
2014-01-29 17:39:12 -08:00
|
|
|
Err(..) => break,
|
|
|
|
Ok(..) => buf[0] as char
|
2013-10-13 18:48:47 -07:00
|
|
|
};
|
|
|
|
match ch {
|
|
|
|
'%' => {
|
|
|
|
let mut bytes = [0, 0];
|
|
|
|
match rdr.read(bytes) {
|
2014-01-29 17:39:12 -08:00
|
|
|
Ok(2) => {}
|
2014-01-26 03:43:42 -05:00
|
|
|
_ => fail!() // FIXME: malformed url?
|
2013-10-13 18:48:47 -07:00
|
|
|
}
|
|
|
|
let ch = uint::parse_bytes(bytes, 16u).unwrap() as u8 as char;
|
|
|
|
|
|
|
|
if full_url {
|
|
|
|
// Only decode some characters:
|
|
|
|
match ch {
|
|
|
|
// gen-delims:
|
|
|
|
':' | '/' | '?' | '#' | '[' | ']' | '@' |
|
|
|
|
|
|
|
|
// sub-delims:
|
|
|
|
'!' | '$' | '&' | '"' | '(' | ')' | '*' |
|
|
|
|
'+' | ',' | ';' | '=' => {
|
|
|
|
out.push_char('%');
|
|
|
|
out.push_char(bytes[0u] as char);
|
|
|
|
out.push_char(bytes[1u] as char);
|
|
|
|
}
|
|
|
|
|
|
|
|
ch => out.push_char(ch)
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
2013-10-13 18:48:47 -07:00
|
|
|
} else {
|
|
|
|
out.push_char(ch);
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
2013-10-13 18:48:47 -07:00
|
|
|
}
|
|
|
|
ch => out.push_char(ch)
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
}
|
2013-10-13 18:48:47 -07:00
|
|
|
|
2014-05-13 17:51:05 -07:00
|
|
|
out
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-13 22:28:22 +00:00
|
|
|
* Decodes a percent-encoded string representing a URI.
|
2013-07-17 17:47:20 -07:00
|
|
|
*
|
2014-02-13 22:28:22 +00:00
|
|
|
* This will only decode escape sequences generated by `encode`.
|
|
|
|
*
|
|
|
|
* # Example
|
|
|
|
*
|
|
|
|
* ```rust
|
2014-03-14 11:16:10 -07:00
|
|
|
* use url::decode;
|
2014-02-13 22:28:22 +00:00
|
|
|
*
|
2014-05-01 01:32:13 -04:00
|
|
|
* let url = decode("https://example.com/Rust%20(programming%20language)");
|
2014-02-13 22:28:22 +00:00
|
|
|
* println!("{}", url); // https://example.com/Rust (programming language)
|
|
|
|
* ```
|
2013-07-17 17:47:20 -07:00
|
|
|
*/
|
2014-05-13 17:51:05 -07:00
|
|
|
pub fn decode(s: &str) -> StrBuf {
|
2013-07-17 17:47:20 -07:00
|
|
|
decode_inner(s, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode a string encoded with percent encoding.
|
|
|
|
*/
|
2014-05-13 17:51:05 -07:00
|
|
|
pub fn decode_component(s: &str) -> StrBuf {
|
2013-07-17 17:47:20 -07:00
|
|
|
decode_inner(s, false)
|
|
|
|
}
|
|
|
|
|
2014-05-13 17:51:05 -07:00
|
|
|
fn encode_plus(s: &str) -> StrBuf {
|
2013-10-13 18:48:47 -07:00
|
|
|
let mut rdr = BufReader::new(s.as_bytes());
|
2014-04-02 16:54:22 -07:00
|
|
|
let mut out = StrBuf::new();
|
2013-07-17 17:47:20 -07:00
|
|
|
|
2013-10-13 18:48:47 -07:00
|
|
|
loop {
|
|
|
|
let mut buf = [0];
|
|
|
|
let ch = match rdr.read(buf) {
|
2014-01-29 17:39:12 -08:00
|
|
|
Ok(..) => buf[0] as char,
|
|
|
|
Err(..) => break,
|
2013-10-13 18:48:47 -07:00
|
|
|
};
|
|
|
|
match ch {
|
|
|
|
'A' .. 'Z' | 'a' .. 'z' | '0' .. '9' | '_' | '.' | '-' => {
|
|
|
|
out.push_char(ch);
|
|
|
|
}
|
|
|
|
' ' => out.push_char('+'),
|
2014-05-16 10:45:16 -07:00
|
|
|
_ => out.push_str(format!("%{:X}", ch as uint).as_slice())
|
2013-10-13 18:48:47 -07:00
|
|
|
}
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
2013-10-13 18:48:47 -07:00
|
|
|
|
2014-05-13 17:51:05 -07:00
|
|
|
out
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode a hashmap to the 'application/x-www-form-urlencoded' media type.
|
|
|
|
*/
|
2014-05-13 17:51:05 -07:00
|
|
|
pub fn encode_form_urlencoded(m: &HashMap<StrBuf, Vec<StrBuf>>) -> StrBuf {
|
2014-04-02 16:54:22 -07:00
|
|
|
let mut out = StrBuf::new();
|
2013-07-17 17:47:20 -07:00
|
|
|
let mut first = true;
|
|
|
|
|
2013-08-03 12:45:23 -04:00
|
|
|
for (key, values) in m.iter() {
|
2014-05-13 17:51:05 -07:00
|
|
|
let key = encode_plus(key.as_slice());
|
2013-07-17 17:47:20 -07:00
|
|
|
|
2013-08-03 12:45:23 -04:00
|
|
|
for value in values.iter() {
|
2013-07-17 17:47:20 -07:00
|
|
|
if first {
|
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
out.push_char('&');
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
2014-05-13 17:51:05 -07:00
|
|
|
out.push_str(format!("{}={}",
|
|
|
|
key,
|
2014-05-16 10:45:16 -07:00
|
|
|
encode_plus(value.as_slice())).as_slice());
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-13 17:51:05 -07:00
|
|
|
out
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode a string encoded with the 'application/x-www-form-urlencoded' media
|
|
|
|
* type into a hashmap.
|
|
|
|
*/
|
2014-04-02 16:54:22 -07:00
|
|
|
#[allow(experimental)]
|
2014-05-13 17:51:05 -07:00
|
|
|
pub fn decode_form_urlencoded(s: &[u8]) -> HashMap<StrBuf, Vec<StrBuf>> {
|
2013-10-13 18:48:47 -07:00
|
|
|
let mut rdr = BufReader::new(s);
|
2014-05-13 17:51:05 -07:00
|
|
|
let mut m: HashMap<StrBuf,Vec<StrBuf>> = HashMap::new();
|
2014-04-02 16:54:22 -07:00
|
|
|
let mut key = StrBuf::new();
|
|
|
|
let mut value = StrBuf::new();
|
2013-10-13 18:48:47 -07:00
|
|
|
let mut parsing_key = true;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let mut buf = [0];
|
|
|
|
let ch = match rdr.read(buf) {
|
2014-01-29 17:39:12 -08:00
|
|
|
Ok(..) => buf[0] as char,
|
|
|
|
Err(..) => break,
|
2013-10-13 18:48:47 -07:00
|
|
|
};
|
|
|
|
match ch {
|
|
|
|
'&' | ';' => {
|
2014-04-02 16:54:22 -07:00
|
|
|
if key.len() > 0 && value.len() > 0 {
|
|
|
|
let mut values = match m.pop_equiv(&key.as_slice()) {
|
2013-10-13 18:48:47 -07:00
|
|
|
Some(values) => values,
|
2014-03-15 15:13:00 -07:00
|
|
|
None => vec!(),
|
2013-10-13 18:48:47 -07:00
|
|
|
};
|
2013-07-17 17:47:20 -07:00
|
|
|
|
2014-05-13 17:51:05 -07:00
|
|
|
values.push(value);
|
|
|
|
m.insert(key, values);
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
2013-10-13 18:48:47 -07:00
|
|
|
parsing_key = true;
|
2014-04-02 16:54:22 -07:00
|
|
|
key = StrBuf::new();
|
|
|
|
value = StrBuf::new();
|
2013-10-13 18:48:47 -07:00
|
|
|
}
|
|
|
|
'=' => parsing_key = false,
|
|
|
|
ch => {
|
|
|
|
let ch = match ch {
|
|
|
|
'%' => {
|
|
|
|
let mut bytes = [0, 0];
|
|
|
|
match rdr.read(bytes) {
|
2014-01-29 17:39:12 -08:00
|
|
|
Ok(2) => {}
|
2014-01-26 03:43:42 -05:00
|
|
|
_ => fail!() // FIXME: malformed?
|
2013-10-13 18:48:47 -07:00
|
|
|
}
|
|
|
|
uint::parse_bytes(bytes, 16u).unwrap() as u8 as char
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
2013-10-13 18:48:47 -07:00
|
|
|
'+' => ' ',
|
|
|
|
ch => ch
|
|
|
|
};
|
|
|
|
|
|
|
|
if parsing_key {
|
|
|
|
key.push_char(ch)
|
|
|
|
} else {
|
|
|
|
value.push_char(ch)
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-13 18:48:47 -07:00
|
|
|
}
|
2013-07-17 17:47:20 -07:00
|
|
|
|
2014-04-02 16:54:22 -07:00
|
|
|
if key.len() > 0 && value.len() > 0 {
|
|
|
|
let mut values = match m.pop_equiv(&key.as_slice()) {
|
2013-10-13 18:48:47 -07:00
|
|
|
Some(values) => values,
|
2014-03-15 15:13:00 -07:00
|
|
|
None => vec!(),
|
2013-10-13 18:48:47 -07:00
|
|
|
};
|
2013-07-17 17:47:20 -07:00
|
|
|
|
2014-05-13 17:51:05 -07:00
|
|
|
values.push(value);
|
|
|
|
m.insert(key, values);
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
2013-10-13 18:48:47 -07:00
|
|
|
|
|
|
|
m
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-13 17:51:05 -07:00
|
|
|
fn split_char_first(s: &str, c: char) -> (StrBuf, StrBuf) {
|
2013-07-17 17:47:20 -07:00
|
|
|
let len = s.len();
|
|
|
|
let mut index = len;
|
|
|
|
let mut mat = 0;
|
2013-10-13 18:48:47 -07:00
|
|
|
let mut rdr = BufReader::new(s.as_bytes());
|
|
|
|
loop {
|
|
|
|
let mut buf = [0];
|
|
|
|
let ch = match rdr.read(buf) {
|
2014-01-29 17:39:12 -08:00
|
|
|
Ok(..) => buf[0] as char,
|
|
|
|
Err(..) => break,
|
2013-10-13 18:48:47 -07:00
|
|
|
};
|
|
|
|
if ch == c {
|
|
|
|
// found a match, adjust markers
|
2014-01-29 17:39:12 -08:00
|
|
|
index = (rdr.tell().unwrap() as uint) - 1;
|
2013-10-13 18:48:47 -07:00
|
|
|
mat = 1;
|
|
|
|
break;
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if index+mat == len {
|
2014-05-13 17:51:05 -07:00
|
|
|
return (s.slice(0, index).to_strbuf(), "".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
} else {
|
2014-05-13 17:51:05 -07:00
|
|
|
return (s.slice(0, index).to_strbuf(),
|
|
|
|
s.slice(index + mat, s.len()).to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 18:56:33 -08:00
|
|
|
impl fmt::Show for UserInfo {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self.pass {
|
2014-05-10 14:05:06 -07:00
|
|
|
Some(ref pass) => write!(f, "{}:{}@", self.user, *pass),
|
|
|
|
None => write!(f, "{}@", self.user),
|
2014-02-19 18:56:33 -08:00
|
|
|
}
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn query_from_str(rawquery: &str) -> Query {
|
2014-03-15 15:13:00 -07:00
|
|
|
let mut query: Query = vec!();
|
2013-07-17 17:47:20 -07:00
|
|
|
if !rawquery.is_empty() {
|
2013-11-23 11:18:51 +01:00
|
|
|
for p in rawquery.split('&') {
|
2013-07-17 17:47:20 -07:00
|
|
|
let (k, v) = split_char_first(p, '=');
|
2014-05-13 17:51:05 -07:00
|
|
|
query.push((decode_component(k.as_slice()),
|
|
|
|
decode_component(v.as_slice())));
|
2013-07-17 17:47:20 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2013-11-17 16:52:14 +00:00
|
|
|
/**
|
|
|
|
* Converts an instance of a URI `Query` type to a string.
|
|
|
|
*
|
|
|
|
* # Example
|
|
|
|
*
|
|
|
|
* ```rust
|
2014-05-13 17:51:05 -07:00
|
|
|
* let query = vec!(("title".to_strbuf(), "The Village".to_strbuf()),
|
|
|
|
("north".to_strbuf(), "52.91".to_strbuf()),
|
|
|
|
("west".to_strbuf(), "4.10".to_strbuf()));
|
2014-01-09 21:06:55 +11:00
|
|
|
* println!("{}", url::query_to_str(&query)); // title=The%20Village&north=52.91&west=4.10
|
2013-11-17 16:52:14 +00:00
|
|
|
* ```
|
|
|
|
*/
|
2014-02-19 18:56:33 -08:00
|
|
|
#[allow(unused_must_use)]
|
2014-05-13 17:51:05 -07:00
|
|
|
pub fn query_to_str(query: &Query) -> StrBuf {
|
2014-02-19 18:56:33 -08:00
|
|
|
use std::io::MemWriter;
|
|
|
|
use std::str;
|
|
|
|
|
|
|
|
let mut writer = MemWriter::new();
|
|
|
|
for (i, &(ref k, ref v)) in query.iter().enumerate() {
|
|
|
|
if i != 0 { write!(&mut writer, "&"); }
|
2014-05-13 17:51:05 -07:00
|
|
|
write!(&mut writer, "{}={}", encode_component(k.as_slice()),
|
|
|
|
encode_component(v.as_slice()));
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
2014-05-13 17:51:05 -07:00
|
|
|
str::from_utf8_lossy(writer.unwrap().as_slice()).to_strbuf()
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
2014-02-13 22:28:22 +00:00
|
|
|
/**
|
|
|
|
* Returns a tuple of the URI scheme and the rest of the URI, or a parsing error.
|
|
|
|
*
|
|
|
|
* Does not include the separating `:` character.
|
|
|
|
*
|
|
|
|
* # Example
|
|
|
|
*
|
|
|
|
* ```rust
|
2014-03-14 11:16:10 -07:00
|
|
|
* use url::get_scheme;
|
2014-02-13 22:28:22 +00:00
|
|
|
*
|
|
|
|
* let scheme = match get_scheme("https://example.com/") {
|
|
|
|
* Ok((sch, _)) => sch,
|
2014-05-13 17:51:05 -07:00
|
|
|
* Err(_) => "(None)".to_strbuf(),
|
2014-02-13 22:28:22 +00:00
|
|
|
* };
|
|
|
|
* println!("Scheme in use: {}.", scheme); // Scheme in use: https.
|
|
|
|
* ```
|
|
|
|
*/
|
2014-05-13 17:51:05 -07:00
|
|
|
pub fn get_scheme(rawurl: &str) -> Result<(StrBuf, StrBuf), StrBuf> {
|
2013-11-23 11:18:51 +01:00
|
|
|
for (i,c) in rawurl.chars().enumerate() {
|
2013-07-17 17:47:20 -07:00
|
|
|
match c {
|
2013-10-01 14:31:03 -07:00
|
|
|
'A' .. 'Z' | 'a' .. 'z' => continue,
|
2013-07-17 17:47:20 -07:00
|
|
|
'0' .. '9' | '+' | '-' | '.' => {
|
|
|
|
if i == 0 {
|
2014-05-13 17:51:05 -07:00
|
|
|
return Err("url: Scheme must begin with a \
|
|
|
|
letter.".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
2013-10-01 14:31:03 -07:00
|
|
|
continue;
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
':' => {
|
|
|
|
if i == 0 {
|
2014-05-13 17:51:05 -07:00
|
|
|
return Err("url: Scheme cannot be empty.".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
} else {
|
2014-05-13 17:51:05 -07:00
|
|
|
return Ok((rawurl.slice(0,i).to_strbuf(),
|
|
|
|
rawurl.slice(i+1,rawurl.len()).to_strbuf()));
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2014-05-13 17:51:05 -07:00
|
|
|
return Err("url: Invalid character in scheme.".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2014-05-13 17:51:05 -07:00
|
|
|
return Err("url: Scheme must be terminated with a colon.".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[deriving(Clone, Eq)]
|
|
|
|
enum Input {
|
|
|
|
Digit, // all digits
|
|
|
|
Hex, // digits and letters a-f
|
|
|
|
Unreserved // all other legal characters
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns userinfo, host, port, and unparsed part, or an error
|
|
|
|
fn get_authority(rawurl: &str) ->
|
2014-05-13 17:51:05 -07:00
|
|
|
Result<(Option<UserInfo>, StrBuf, Option<StrBuf>, StrBuf), StrBuf> {
|
2013-07-17 17:47:20 -07:00
|
|
|
if !rawurl.starts_with("//") {
|
|
|
|
// there is no authority.
|
2014-05-13 17:51:05 -07:00
|
|
|
return Ok((None, "".to_strbuf(), None, rawurl.to_str().to_strbuf()));
|
2013-07-17 17:47:20 -07: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
|
|
|
|
}
|
|
|
|
|
|
|
|
let len = rawurl.len();
|
|
|
|
let mut st = Start;
|
2013-07-31 17:59:59 -04:00
|
|
|
let mut input = Digit; // most restricted, start here.
|
2013-07-17 17:47:20 -07:00
|
|
|
|
|
|
|
let mut userinfo = None;
|
2014-05-13 17:51:05 -07:00
|
|
|
let mut host = "".to_strbuf();
|
2013-07-17 17:47:20 -07:00
|
|
|
let mut port = None;
|
|
|
|
|
|
|
|
let mut colon_count = 0;
|
|
|
|
let mut pos = 0;
|
|
|
|
let mut begin = 2;
|
|
|
|
let mut end = len;
|
|
|
|
|
2013-11-23 11:18:51 +01:00
|
|
|
for (i,c) in rawurl.chars().enumerate() {
|
2013-10-01 14:31:03 -07:00
|
|
|
if i < 2 { continue; } // ignore the leading //
|
2013-07-17 17:47:20 -07:00
|
|
|
|
|
|
|
// deal with input class first
|
|
|
|
match c {
|
|
|
|
'0' .. '9' => (),
|
|
|
|
'A' .. 'F' | 'a' .. 'f' => {
|
2013-07-31 17:59:59 -04:00
|
|
|
if input == Digit {
|
|
|
|
input = Hex;
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
'G' .. 'Z' | 'g' .. 'z' | '-' | '.' | '_' | '~' | '%' |
|
|
|
|
'&' |'\'' | '(' | ')' | '+' | '!' | '*' | ',' | ';' | '=' => {
|
2013-07-31 17:59:59 -04:00
|
|
|
input = Unreserved;
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
':' | '@' | '?' | '#' | '/' => {
|
|
|
|
// separators, don't change anything
|
|
|
|
}
|
|
|
|
_ => {
|
2014-05-13 17:51:05 -07:00
|
|
|
return Err("Illegal character in authority".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now process states
|
|
|
|
match c {
|
|
|
|
':' => {
|
|
|
|
colon_count += 1;
|
|
|
|
match st {
|
|
|
|
Start => {
|
|
|
|
pos = i;
|
|
|
|
st = PassHostPort;
|
|
|
|
}
|
|
|
|
PassHostPort => {
|
|
|
|
// multiple colons means ipv6 address.
|
2013-07-31 17:59:59 -04:00
|
|
|
if input == Unreserved {
|
2013-07-17 17:47:20 -07:00
|
|
|
return Err(
|
2014-05-13 17:51:05 -07:00
|
|
|
"Illegal characters in IPv6 address.".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
st = Ip6Host;
|
|
|
|
}
|
|
|
|
InHost => {
|
|
|
|
pos = i;
|
2013-07-31 17:59:59 -04:00
|
|
|
if input == Unreserved {
|
2013-08-19 02:27:57 -07:00
|
|
|
// must be port
|
2014-05-13 17:51:05 -07:00
|
|
|
host = rawurl.slice(begin, i).to_strbuf();
|
2013-08-19 02:27:57 -07:00
|
|
|
st = InPort;
|
|
|
|
} else {
|
|
|
|
// can't be sure whether this is an ipv6 address or a port
|
|
|
|
st = Ip6Port;
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ip6Port => {
|
2013-07-31 17:59:59 -04:00
|
|
|
if input == Unreserved {
|
2014-05-13 17:51:05 -07:00
|
|
|
return Err("Illegal characters in \
|
|
|
|
authority.".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
st = Ip6Host;
|
|
|
|
}
|
|
|
|
Ip6Host => {
|
|
|
|
if colon_count > 7 {
|
2014-05-13 17:51:05 -07:00
|
|
|
host = rawurl.slice(begin, i).to_strbuf();
|
2013-07-17 17:47:20 -07:00
|
|
|
pos = i;
|
|
|
|
st = InPort;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2014-05-13 17:51:05 -07:00
|
|
|
return Err("Invalid ':' in authority.".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
}
|
2013-07-31 17:59:59 -04:00
|
|
|
input = Digit; // reset input class
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
'@' => {
|
2013-07-31 17:59:59 -04:00
|
|
|
input = Digit; // reset input class
|
2013-07-17 17:47:20 -07:00
|
|
|
colon_count = 0; // reset count
|
|
|
|
match st {
|
|
|
|
Start => {
|
2014-05-13 17:51:05 -07:00
|
|
|
let user = rawurl.slice(begin, i).to_strbuf();
|
2013-07-17 17:47:20 -07:00
|
|
|
userinfo = Some(UserInfo::new(user, None));
|
|
|
|
st = InHost;
|
|
|
|
}
|
|
|
|
PassHostPort => {
|
2014-05-13 17:51:05 -07:00
|
|
|
let user = rawurl.slice(begin, pos).to_strbuf();
|
|
|
|
let pass = rawurl.slice(pos+1, i).to_strbuf();
|
2013-07-17 17:47:20 -07:00
|
|
|
userinfo = Some(UserInfo::new(user, Some(pass)));
|
|
|
|
st = InHost;
|
|
|
|
}
|
|
|
|
_ => {
|
2014-05-13 17:51:05 -07:00
|
|
|
return Err("Invalid '@' in authority.".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
begin = i+1;
|
|
|
|
}
|
|
|
|
|
|
|
|
'?' | '#' | '/' => {
|
|
|
|
end = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// finish up
|
|
|
|
match st {
|
|
|
|
Start => {
|
2014-05-13 17:51:05 -07:00
|
|
|
host = rawurl.slice(begin, end).to_strbuf();
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
PassHostPort | Ip6Port => {
|
2013-07-31 17:59:59 -04:00
|
|
|
if input != Digit {
|
2014-05-13 17:51:05 -07:00
|
|
|
return Err("Non-digit characters in port.".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
2014-05-13 17:51:05 -07:00
|
|
|
host = rawurl.slice(begin, pos).to_strbuf();
|
|
|
|
port = Some(rawurl.slice(pos+1, end).to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
Ip6Host | InHost => {
|
2014-05-13 17:51:05 -07:00
|
|
|
host = rawurl.slice(begin, end).to_strbuf();
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
InPort => {
|
2013-07-31 17:59:59 -04:00
|
|
|
if input != Digit {
|
2014-05-13 17:51:05 -07:00
|
|
|
return Err("Non-digit characters in port.".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
2014-05-13 17:51:05 -07:00
|
|
|
port = Some(rawurl.slice(pos+1, end).to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-13 17:51:05 -07:00
|
|
|
let rest = rawurl.slice(end, len).to_strbuf();
|
2013-07-17 17:47:20 -07:00
|
|
|
return Ok((userinfo, host, port, rest));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// returns the path and unparsed part of url, or an error
|
|
|
|
fn get_path(rawurl: &str, authority: bool) ->
|
2014-05-13 17:51:05 -07:00
|
|
|
Result<(StrBuf, StrBuf), StrBuf> {
|
2013-07-17 17:47:20 -07:00
|
|
|
let len = rawurl.len();
|
|
|
|
let mut end = len;
|
2013-11-23 11:18:51 +01:00
|
|
|
for (i,c) in rawurl.chars().enumerate() {
|
2013-07-17 17:47:20 -07:00
|
|
|
match c {
|
|
|
|
'A' .. 'Z' | 'a' .. 'z' | '0' .. '9' | '&' |'\'' | '(' | ')' | '.'
|
|
|
|
| '@' | ':' | '%' | '/' | '+' | '!' | '*' | ',' | ';' | '='
|
2013-11-07 16:16:15 -05:00
|
|
|
| '_' | '-' | '~' => {
|
2013-10-01 14:31:03 -07:00
|
|
|
continue;
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
'?' | '#' => {
|
|
|
|
end = i;
|
|
|
|
break;
|
|
|
|
}
|
2014-05-13 17:51:05 -07:00
|
|
|
_ => return Err("Invalid character in path.".to_strbuf())
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if authority {
|
|
|
|
if end != 0 && !rawurl.starts_with("/") {
|
2014-04-15 18:17:48 -07:00
|
|
|
return Err("Non-empty path must begin with\
|
2014-05-13 17:51:05 -07:00
|
|
|
'/' in presence of authority.".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ok((decode_component(rawurl.slice(0, end)),
|
2014-05-13 17:51:05 -07:00
|
|
|
rawurl.slice(end, len).to_strbuf()));
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// returns the parsed query and the fragment, if present
|
|
|
|
fn get_query_fragment(rawurl: &str) ->
|
2014-05-13 17:51:05 -07:00
|
|
|
Result<(Query, Option<StrBuf>), StrBuf> {
|
2013-07-17 17:47:20 -07:00
|
|
|
if !rawurl.starts_with("?") {
|
|
|
|
if rawurl.starts_with("#") {
|
|
|
|
let f = decode_component(rawurl.slice(
|
|
|
|
1,
|
|
|
|
rawurl.len()));
|
2014-03-15 15:13:00 -07:00
|
|
|
return Ok((vec!(), Some(f)));
|
2013-07-17 17:47:20 -07:00
|
|
|
} else {
|
2014-03-15 15:13:00 -07:00
|
|
|
return Ok((vec!(), None));
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
let (q, r) = split_char_first(rawurl.slice(1, rawurl.len()), '#');
|
|
|
|
let f = if r.len() != 0 {
|
2014-05-13 17:51:05 -07:00
|
|
|
Some(decode_component(r.as_slice()))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
return Ok((query_from_str(q.as_slice()), f));
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-13 22:28:22 +00:00
|
|
|
* Parses a URL, converting it from a string to `Url` representation.
|
2013-07-17 17:47:20 -07:00
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
2014-02-13 22:28:22 +00:00
|
|
|
* `rawurl` - a string representing the full URL, including scheme.
|
2013-07-17 17:47:20 -07:00
|
|
|
*
|
|
|
|
* # Returns
|
|
|
|
*
|
2014-02-13 22:28:22 +00:00
|
|
|
* A `Url` struct type representing the URL.
|
2013-07-17 17:47:20 -07:00
|
|
|
*/
|
2014-05-13 17:51:05 -07:00
|
|
|
pub fn from_str(rawurl: &str) -> Result<Url, StrBuf> {
|
2013-07-17 17:47:20 -07:00
|
|
|
// scheme
|
|
|
|
let (scheme, rest) = match get_scheme(rawurl) {
|
|
|
|
Ok(val) => val,
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
};
|
|
|
|
|
|
|
|
// authority
|
2014-05-13 17:51:05 -07:00
|
|
|
let (userinfo, host, port, rest) = match get_authority(rest.as_slice()) {
|
2013-07-17 17:47:20 -07:00
|
|
|
Ok(val) => val,
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
};
|
|
|
|
|
|
|
|
// path
|
2014-05-13 17:51:05 -07:00
|
|
|
let has_authority = host.len() > 0;
|
|
|
|
let (path, rest) = match get_path(rest.as_slice(), has_authority) {
|
2013-07-17 17:47:20 -07:00
|
|
|
Ok(val) => val,
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
};
|
|
|
|
|
|
|
|
// query and fragment
|
2014-05-13 17:51:05 -07:00
|
|
|
let (query, fragment) = match get_query_fragment(rest.as_slice()) {
|
2013-07-17 17:47:20 -07:00
|
|
|
Ok(val) => val,
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Url::new(scheme, userinfo, host, port, path, query, fragment))
|
|
|
|
}
|
|
|
|
|
2014-05-13 17:51:05 -07:00
|
|
|
pub fn path_from_str(rawpath: &str) -> Result<Path, StrBuf> {
|
2014-02-12 21:28:58 -08:00
|
|
|
let (path, rest) = match get_path(rawpath, false) {
|
|
|
|
Ok(val) => val,
|
|
|
|
Err(e) => return Err(e)
|
|
|
|
};
|
|
|
|
|
|
|
|
// query and fragment
|
2014-05-13 17:51:05 -07:00
|
|
|
let (query, fragment) = match get_query_fragment(rest.as_slice()) {
|
2014-02-12 21:28:58 -08:00
|
|
|
Ok(val) => val,
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Path{ path: path, query: query, fragment: fragment })
|
|
|
|
}
|
|
|
|
|
2013-07-17 17:47:20 -07:00
|
|
|
impl FromStr for Url {
|
|
|
|
fn from_str(s: &str) -> Option<Url> {
|
|
|
|
match from_str(s) {
|
|
|
|
Ok(url) => Some(url),
|
|
|
|
Err(_) => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 21:28:58 -08:00
|
|
|
impl FromStr for Path {
|
|
|
|
fn from_str(s: &str) -> Option<Path> {
|
|
|
|
match path_from_str(s) {
|
|
|
|
Ok(path) => Some(path),
|
|
|
|
Err(_) => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 18:56:33 -08:00
|
|
|
impl fmt::Show for Url {
|
|
|
|
/**
|
|
|
|
* Converts a URL from `Url` to string representation.
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* `url` - a URL.
|
|
|
|
*
|
|
|
|
* # Returns
|
|
|
|
*
|
|
|
|
* A string 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".
|
|
|
|
*/
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2014-05-10 14:05:06 -07:00
|
|
|
try!(write!(f, "{}:", self.scheme));
|
2014-02-19 18:56:33 -08:00
|
|
|
|
|
|
|
if !self.host.is_empty() {
|
2014-05-10 14:05:06 -07:00
|
|
|
try!(write!(f, "//"));
|
2014-02-19 18:56:33 -08:00
|
|
|
match self.user {
|
2014-05-10 14:05:06 -07:00
|
|
|
Some(ref user) => try!(write!(f, "{}", *user)),
|
2014-02-19 18:56:33 -08:00
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
match self.port {
|
2014-05-10 14:05:06 -07:00
|
|
|
Some(ref port) => try!(write!(f, "{}:{}", self.host,
|
2014-02-19 18:56:33 -08:00
|
|
|
*port)),
|
2014-05-10 14:05:06 -07:00
|
|
|
None => try!(write!(f, "{}", self.host)),
|
2014-02-19 18:56:33 -08:00
|
|
|
}
|
2013-10-19 00:25:03 +11:00
|
|
|
}
|
2014-02-12 21:28:58 -08:00
|
|
|
|
2014-05-10 14:05:06 -07:00
|
|
|
try!(write!(f, "{}", self.path));
|
2014-02-12 21:28:58 -08:00
|
|
|
|
2014-02-19 18:56:33 -08:00
|
|
|
if !self.query.is_empty() {
|
2014-05-10 14:05:06 -07:00
|
|
|
try!(write!(f, "?{}", query_to_str(&self.query)));
|
2014-02-19 18:56:33 -08:00
|
|
|
}
|
2014-02-12 21:28:58 -08:00
|
|
|
|
2014-02-19 18:56:33 -08:00
|
|
|
match self.fragment {
|
2014-05-13 17:51:05 -07:00
|
|
|
Some(ref fragment) => {
|
2014-05-10 14:05:06 -07:00
|
|
|
write!(f, "\\#{}", encode_component(fragment.as_slice()))
|
2014-05-13 17:51:05 -07:00
|
|
|
}
|
2014-02-19 18:56:33 -08:00
|
|
|
None => Ok(()),
|
|
|
|
}
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 18:56:33 -08:00
|
|
|
impl fmt::Show for Path {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2014-05-10 14:05:06 -07:00
|
|
|
try!(write!(f, "{}", self.path));
|
2014-02-19 18:56:33 -08:00
|
|
|
if !self.query.is_empty() {
|
2014-05-10 14:05:06 -07:00
|
|
|
try!(write!(f, "?{}", self.query))
|
2014-02-19 18:56:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
match self.fragment {
|
|
|
|
Some(ref fragment) => {
|
2014-05-10 14:05:06 -07:00
|
|
|
write!(f, "\\#{}", encode_component(fragment.as_slice()))
|
2014-02-19 18:56:33 -08:00
|
|
|
}
|
|
|
|
None => Ok(())
|
|
|
|
}
|
2014-02-12 21:28:58 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-25 08:03:41 -08:00
|
|
|
impl<S: Writer> Hash<S> for Url {
|
|
|
|
fn hash(&self, state: &mut S) {
|
|
|
|
self.to_str().hash(state)
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-25 08:03:41 -08:00
|
|
|
impl<S: Writer> Hash<S> for Path {
|
|
|
|
fn hash(&self, state: &mut S) {
|
|
|
|
self.to_str().hash(state)
|
2014-02-12 21:28:58 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-17 17:47:20 -07: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() {
|
|
|
|
let (u,v) = split_char_first("hello, sweet world", ',');
|
2014-05-13 17:51:05 -07:00
|
|
|
assert_eq!(u, "hello".to_strbuf());
|
|
|
|
assert_eq!(v, " sweet world".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
|
|
|
|
let (u,v) = split_char_first("hello sweet world", ',');
|
2014-05-13 17:51:05 -07:00
|
|
|
assert_eq!(u, "hello sweet world".to_strbuf());
|
|
|
|
assert_eq!(v, "".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_authority() {
|
|
|
|
let (u, h, p, r) = get_authority(
|
|
|
|
"//user:pass@rust-lang.org/something").unwrap();
|
2014-05-13 17:51:05 -07:00
|
|
|
assert_eq!(u, Some(UserInfo::new("user".to_strbuf(), Some("pass".to_strbuf()))));
|
|
|
|
assert_eq!(h, "rust-lang.org".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
assert!(p.is_none());
|
2014-05-13 17:51:05 -07:00
|
|
|
assert_eq!(r, "/something".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
|
|
|
|
let (u, h, p, r) = get_authority(
|
|
|
|
"//rust-lang.org:8000?something").unwrap();
|
|
|
|
assert!(u.is_none());
|
2014-05-13 17:51:05 -07:00
|
|
|
assert_eq!(h, "rust-lang.org".to_strbuf());
|
|
|
|
assert_eq!(p, Some("8000".to_strbuf()));
|
|
|
|
assert_eq!(r, "?something".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
|
|
|
|
let (u, h, p, r) = get_authority(
|
|
|
|
"//rust-lang.org#blah").unwrap();
|
|
|
|
assert!(u.is_none());
|
2014-05-13 17:51:05 -07:00
|
|
|
assert_eq!(h, "rust-lang.org".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
assert!(p.is_none());
|
2014-05-13 17:51:05 -07:00
|
|
|
assert_eq!(r, "#blah".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
|
|
|
|
// ipv6 tests
|
|
|
|
let (_, h, _, _) = get_authority(
|
|
|
|
"//2001:0db8:85a3:0042:0000:8a2e:0370:7334#blah").unwrap();
|
2014-05-13 17:51:05 -07:00
|
|
|
assert_eq!(h, "2001:0db8:85a3:0042:0000:8a2e:0370:7334".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
|
|
|
|
let (_, h, p, _) = get_authority(
|
|
|
|
"//2001:0db8:85a3:0042:0000:8a2e:0370:7334:8000#blah").unwrap();
|
2014-05-13 17:51:05 -07:00
|
|
|
assert_eq!(h, "2001:0db8:85a3:0042:0000:8a2e:0370:7334".to_strbuf());
|
|
|
|
assert_eq!(p, Some("8000".to_strbuf()));
|
2013-07-17 17:47:20 -07:00
|
|
|
|
|
|
|
let (u, h, p, _) = get_authority(
|
|
|
|
"//us:p@2001:0db8:85a3:0042:0000:8a2e:0370:7334:8000#blah"
|
|
|
|
).unwrap();
|
2014-05-13 17:51:05 -07:00
|
|
|
assert_eq!(u, Some(UserInfo::new("us".to_strbuf(), Some("p".to_strbuf()))));
|
|
|
|
assert_eq!(h, "2001:0db8:85a3:0042:0000:8a2e:0370:7334".to_strbuf());
|
|
|
|
assert_eq!(p, Some("8000".to_strbuf()));
|
2013-07-17 17:47:20 -07:00
|
|
|
|
|
|
|
// invalid authorities;
|
|
|
|
assert!(get_authority("//user:pass@rust-lang:something").is_err());
|
|
|
|
assert!(get_authority("//user@rust-lang:something:/path").is_err());
|
|
|
|
assert!(get_authority(
|
|
|
|
"//2001:0db8:85a3:0042:0000:8a2e:0370:7334:800a").is_err());
|
|
|
|
assert!(get_authority(
|
|
|
|
"//2001:0db8:85a3:0042:0000:8a2e:0370:7334:8000:00").is_err());
|
|
|
|
|
|
|
|
// these parse as empty, because they don't start with '//'
|
|
|
|
let (_, h, _, _) = get_authority("user:pass@rust-lang").unwrap();
|
2014-05-13 17:51:05 -07:00
|
|
|
assert_eq!(h, "".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
let (_, h, _, _) = get_authority("rust-lang.org").unwrap();
|
2014-05-13 17:51:05 -07:00
|
|
|
assert_eq!(h, "".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_path() {
|
|
|
|
let (p, r) = get_path("/something+%20orother", true).unwrap();
|
2014-05-13 17:51:05 -07:00
|
|
|
assert_eq!(p, "/something+ orother".to_strbuf());
|
|
|
|
assert_eq!(r, "".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
let (p, r) = get_path("test@email.com#fragment", false).unwrap();
|
2014-05-13 17:51:05 -07:00
|
|
|
assert_eq!(p, "test@email.com".to_strbuf());
|
|
|
|
assert_eq!(r, "#fragment".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
let (p, r) = get_path("/gen/:addr=?q=v", false).unwrap();
|
2014-05-13 17:51:05 -07:00
|
|
|
assert_eq!(p, "/gen/:addr=".to_strbuf());
|
|
|
|
assert_eq!(r, "?q=v".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
|
|
|
|
//failure cases
|
|
|
|
assert!(get_path("something?q", true).is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2014-03-15 15:13:00 -07:00
|
|
|
use {encode_form_urlencoded, decode_form_urlencoded,
|
2014-03-14 11:16:10 -07:00
|
|
|
decode, encode, from_str, encode_component, decode_component,
|
|
|
|
path_from_str, UserInfo, get_scheme};
|
2013-07-17 17:47:20 -07:00
|
|
|
|
2014-02-19 19:29:58 -08:00
|
|
|
use collections::HashMap;
|
2013-07-17 17:47:20 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_url_parse() {
|
2014-05-13 17:51:05 -07:00
|
|
|
let url = "http://user:pass@rust-lang.org:8080/doc/~u?s=v#something";
|
2013-07-17 17:47:20 -07:00
|
|
|
|
|
|
|
let up = from_str(url);
|
|
|
|
let u = up.unwrap();
|
2014-05-13 17:51:05 -07:00
|
|
|
assert_eq!(&u.scheme, &"http".to_strbuf());
|
|
|
|
assert_eq!(&u.user, &Some(UserInfo::new("user".to_strbuf(), Some("pass".to_strbuf()))));
|
|
|
|
assert_eq!(&u.host, &"rust-lang.org".to_strbuf());
|
|
|
|
assert_eq!(&u.port, &Some("8080".to_strbuf()));
|
|
|
|
assert_eq!(&u.path, &"/doc/~u".to_strbuf());
|
|
|
|
assert_eq!(&u.query, &vec!(("s".to_strbuf(), "v".to_strbuf())));
|
|
|
|
assert_eq!(&u.fragment, &Some("something".to_strbuf()));
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
2014-02-12 21:28:58 -08:00
|
|
|
#[test]
|
|
|
|
fn test_path_parse() {
|
2014-05-13 17:51:05 -07:00
|
|
|
let path = "/doc/~u?s=v#something";
|
2014-02-12 21:28:58 -08:00
|
|
|
|
|
|
|
let up = path_from_str(path);
|
|
|
|
let u = up.unwrap();
|
2014-05-13 17:51:05 -07:00
|
|
|
assert_eq!(&u.path, &"/doc/~u".to_strbuf());
|
|
|
|
assert_eq!(&u.query, &vec!(("s".to_strbuf(), "v".to_strbuf())));
|
|
|
|
assert_eq!(&u.fragment, &Some("something".to_strbuf()));
|
2014-02-12 21:28:58 -08:00
|
|
|
}
|
|
|
|
|
2013-07-17 17:47:20 -07:00
|
|
|
#[test]
|
|
|
|
fn test_url_parse_host_slash() {
|
2014-05-13 17:51:05 -07:00
|
|
|
let urlstr = "http://0.42.42.42/";
|
2013-07-17 17:47:20 -07:00
|
|
|
let url = from_str(urlstr).unwrap();
|
2014-05-13 17:51:05 -07:00
|
|
|
assert!(url.host == "0.42.42.42".to_strbuf());
|
|
|
|
assert!(url.path == "/".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
2014-02-12 21:28:58 -08:00
|
|
|
#[test]
|
|
|
|
fn test_path_parse_host_slash() {
|
2014-05-13 17:51:05 -07:00
|
|
|
let pathstr = "/";
|
2014-02-12 21:28:58 -08:00
|
|
|
let path = path_from_str(pathstr).unwrap();
|
2014-05-13 17:51:05 -07:00
|
|
|
assert!(path.path == "/".to_strbuf());
|
2014-02-12 21:28:58 -08:00
|
|
|
}
|
|
|
|
|
2013-08-19 02:27:57 -07:00
|
|
|
#[test]
|
|
|
|
fn test_url_host_with_port() {
|
2014-05-13 17:51:05 -07:00
|
|
|
let urlstr = "scheme://host:1234";
|
2013-08-19 02:27:57 -07:00
|
|
|
let url = from_str(urlstr).unwrap();
|
2014-05-13 17:51:05 -07:00
|
|
|
assert_eq!(&url.scheme, &"scheme".to_strbuf());
|
|
|
|
assert_eq!(&url.host, &"host".to_strbuf());
|
|
|
|
assert_eq!(&url.port, &Some("1234".to_strbuf()));
|
|
|
|
// is empty path really correct? Other tests think so
|
|
|
|
assert_eq!(&url.path, &"".to_strbuf());
|
|
|
|
let urlstr = "scheme://host:1234/";
|
2013-08-19 02:27:57 -07:00
|
|
|
let url = from_str(urlstr).unwrap();
|
2014-05-13 17:51:05 -07:00
|
|
|
assert_eq!(&url.scheme, &"scheme".to_strbuf());
|
|
|
|
assert_eq!(&url.host, &"host".to_strbuf());
|
|
|
|
assert_eq!(&url.port, &Some("1234".to_strbuf()));
|
|
|
|
assert_eq!(&url.path, &"/".to_strbuf());
|
2013-08-19 02:27:57 -07:00
|
|
|
}
|
|
|
|
|
2013-07-17 17:47:20 -07:00
|
|
|
#[test]
|
|
|
|
fn test_url_with_underscores() {
|
2014-05-13 17:51:05 -07:00
|
|
|
let urlstr = "http://dotcom.com/file_name.html";
|
2013-07-17 17:47:20 -07:00
|
|
|
let url = from_str(urlstr).unwrap();
|
2014-05-13 17:51:05 -07:00
|
|
|
assert!(url.path == "/file_name.html".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
2014-02-12 21:28:58 -08:00
|
|
|
#[test]
|
|
|
|
fn test_path_with_underscores() {
|
2014-05-13 17:51:05 -07:00
|
|
|
let pathstr = "/file_name.html";
|
2014-02-12 21:28:58 -08:00
|
|
|
let path = path_from_str(pathstr).unwrap();
|
2014-05-13 17:51:05 -07:00
|
|
|
assert!(path.path == "/file_name.html".to_strbuf());
|
2014-02-12 21:28:58 -08:00
|
|
|
}
|
|
|
|
|
2013-07-17 17:47:20 -07:00
|
|
|
#[test]
|
|
|
|
fn test_url_with_dashes() {
|
2014-05-13 17:51:05 -07:00
|
|
|
let urlstr = "http://dotcom.com/file-name.html";
|
2013-07-17 17:47:20 -07:00
|
|
|
let url = from_str(urlstr).unwrap();
|
2014-05-13 17:51:05 -07:00
|
|
|
assert!(url.path == "/file-name.html".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
2014-02-12 21:28:58 -08:00
|
|
|
#[test]
|
|
|
|
fn test_path_with_dashes() {
|
2014-05-13 17:51:05 -07:00
|
|
|
let pathstr = "/file-name.html";
|
2014-02-12 21:28:58 -08:00
|
|
|
let path = path_from_str(pathstr).unwrap();
|
2014-05-13 17:51:05 -07:00
|
|
|
assert!(path.path == "/file-name.html".to_strbuf());
|
2014-02-12 21:28:58 -08:00
|
|
|
}
|
|
|
|
|
2013-07-17 17:47:20 -07:00
|
|
|
#[test]
|
|
|
|
fn test_no_scheme() {
|
|
|
|
assert!(get_scheme("noschemehere.html").is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_scheme_errors() {
|
|
|
|
assert!(from_str("99://something").is_err());
|
|
|
|
assert!(from_str("://something").is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_full_url_parse_and_format() {
|
2014-05-13 17:51:05 -07:00
|
|
|
let url = "http://user:pass@rust-lang.org/doc?s=v#something";
|
|
|
|
assert_eq!(from_str(url).unwrap().to_str().as_slice(), url);
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_userless_url_parse_and_format() {
|
2014-05-13 17:51:05 -07:00
|
|
|
let url = "http://rust-lang.org/doc?s=v#something";
|
|
|
|
assert_eq!(from_str(url).unwrap().to_str().as_slice(), url);
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_queryless_url_parse_and_format() {
|
2014-05-13 17:51:05 -07:00
|
|
|
let url = "http://user:pass@rust-lang.org/doc#something";
|
|
|
|
assert_eq!(from_str(url).unwrap().to_str().as_slice(), url);
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_empty_query_url_parse_and_format() {
|
2014-05-13 17:51:05 -07:00
|
|
|
let url = "http://user:pass@rust-lang.org/doc?#something";
|
|
|
|
let should_be = "http://user:pass@rust-lang.org/doc#something";
|
|
|
|
assert_eq!(from_str(url).unwrap().to_str().as_slice(), should_be);
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fragmentless_url_parse_and_format() {
|
2014-05-13 17:51:05 -07:00
|
|
|
let url = "http://user:pass@rust-lang.org/doc?q=v";
|
|
|
|
assert_eq!(from_str(url).unwrap().to_str().as_slice(), url);
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_minimal_url_parse_and_format() {
|
2014-05-13 17:51:05 -07:00
|
|
|
let url = "http://rust-lang.org/doc";
|
|
|
|
assert_eq!(from_str(url).unwrap().to_str().as_slice(), url);
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
2013-10-19 00:25:03 +11:00
|
|
|
#[test]
|
|
|
|
fn test_url_with_port_parse_and_format() {
|
2014-05-13 17:51:05 -07:00
|
|
|
let url = "http://rust-lang.org:80/doc";
|
|
|
|
assert_eq!(from_str(url).unwrap().to_str().as_slice(), url);
|
2013-10-19 00:25:03 +11:00
|
|
|
}
|
|
|
|
|
2013-07-17 17:47:20 -07:00
|
|
|
#[test]
|
|
|
|
fn test_scheme_host_only_url_parse_and_format() {
|
2014-05-13 17:51:05 -07:00
|
|
|
let url = "http://rust-lang.org";
|
|
|
|
assert_eq!(from_str(url).unwrap().to_str().as_slice(), url);
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_pathless_url_parse_and_format() {
|
2014-05-13 17:51:05 -07:00
|
|
|
let url = "http://user:pass@rust-lang.org?q=v#something";
|
|
|
|
assert_eq!(from_str(url).unwrap().to_str().as_slice(), url);
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_scheme_host_fragment_only_url_parse_and_format() {
|
2014-05-13 17:51:05 -07:00
|
|
|
let url = "http://rust-lang.org#something";
|
|
|
|
assert_eq!(from_str(url).unwrap().to_str().as_slice(), url);
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_url_component_encoding() {
|
2014-05-13 17:51:05 -07:00
|
|
|
let url = "http://rust-lang.org/doc%20uments?ba%25d%20=%23%26%2B";
|
2013-07-17 17:47:20 -07:00
|
|
|
let u = from_str(url).unwrap();
|
2014-05-13 17:51:05 -07:00
|
|
|
assert!(u.path == "/doc uments".to_strbuf());
|
|
|
|
assert!(u.query == vec!(("ba%d ".to_strbuf(), "#&+".to_strbuf())));
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
2014-02-12 21:28:58 -08:00
|
|
|
#[test]
|
|
|
|
fn test_path_component_encoding() {
|
2014-05-13 17:51:05 -07:00
|
|
|
let path = "/doc%20uments?ba%25d%20=%23%26%2B";
|
2014-02-12 21:28:58 -08:00
|
|
|
let p = path_from_str(path).unwrap();
|
2014-05-13 17:51:05 -07:00
|
|
|
assert!(p.path == "/doc uments".to_strbuf());
|
|
|
|
assert!(p.query == vec!(("ba%d ".to_strbuf(), "#&+".to_strbuf())));
|
2014-02-12 21:28:58 -08:00
|
|
|
}
|
|
|
|
|
2013-07-17 17:47:20 -07:00
|
|
|
#[test]
|
|
|
|
fn test_url_without_authority() {
|
2014-05-13 17:51:05 -07:00
|
|
|
let url = "mailto:test@email.com";
|
|
|
|
assert_eq!(from_str(url).unwrap().to_str().as_slice(), url);
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_encode() {
|
2014-05-13 17:51:05 -07:00
|
|
|
assert_eq!(encode(""), "".to_strbuf());
|
|
|
|
assert_eq!(encode("http://example.com"), "http://example.com".to_strbuf());
|
|
|
|
assert_eq!(encode("foo bar% baz"), "foo%20bar%25%20baz".to_strbuf());
|
|
|
|
assert_eq!(encode(" "), "%20".to_strbuf());
|
|
|
|
assert_eq!(encode("!"), "!".to_strbuf());
|
|
|
|
assert_eq!(encode("\""), "\"".to_strbuf());
|
|
|
|
assert_eq!(encode("#"), "#".to_strbuf());
|
|
|
|
assert_eq!(encode("$"), "$".to_strbuf());
|
|
|
|
assert_eq!(encode("%"), "%25".to_strbuf());
|
|
|
|
assert_eq!(encode("&"), "&".to_strbuf());
|
|
|
|
assert_eq!(encode("'"), "%27".to_strbuf());
|
|
|
|
assert_eq!(encode("("), "(".to_strbuf());
|
|
|
|
assert_eq!(encode(")"), ")".to_strbuf());
|
|
|
|
assert_eq!(encode("*"), "*".to_strbuf());
|
|
|
|
assert_eq!(encode("+"), "+".to_strbuf());
|
|
|
|
assert_eq!(encode(","), ",".to_strbuf());
|
|
|
|
assert_eq!(encode("/"), "/".to_strbuf());
|
|
|
|
assert_eq!(encode(":"), ":".to_strbuf());
|
|
|
|
assert_eq!(encode(";"), ";".to_strbuf());
|
|
|
|
assert_eq!(encode("="), "=".to_strbuf());
|
|
|
|
assert_eq!(encode("?"), "?".to_strbuf());
|
|
|
|
assert_eq!(encode("@"), "@".to_strbuf());
|
|
|
|
assert_eq!(encode("["), "[".to_strbuf());
|
|
|
|
assert_eq!(encode("]"), "]".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_encode_component() {
|
2014-05-13 17:51:05 -07:00
|
|
|
assert_eq!(encode_component(""), "".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
assert!(encode_component("http://example.com") ==
|
2014-05-13 17:51:05 -07:00
|
|
|
"http%3A%2F%2Fexample.com".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
assert!(encode_component("foo bar% baz") ==
|
2014-05-13 17:51:05 -07:00
|
|
|
"foo%20bar%25%20baz".to_strbuf());
|
|
|
|
assert_eq!(encode_component(" "), "%20".to_strbuf());
|
|
|
|
assert_eq!(encode_component("!"), "%21".to_strbuf());
|
|
|
|
assert_eq!(encode_component("#"), "%23".to_strbuf());
|
|
|
|
assert_eq!(encode_component("$"), "%24".to_strbuf());
|
|
|
|
assert_eq!(encode_component("%"), "%25".to_strbuf());
|
|
|
|
assert_eq!(encode_component("&"), "%26".to_strbuf());
|
|
|
|
assert_eq!(encode_component("'"), "%27".to_strbuf());
|
|
|
|
assert_eq!(encode_component("("), "%28".to_strbuf());
|
|
|
|
assert_eq!(encode_component(")"), "%29".to_strbuf());
|
|
|
|
assert_eq!(encode_component("*"), "%2A".to_strbuf());
|
|
|
|
assert_eq!(encode_component("+"), "%2B".to_strbuf());
|
|
|
|
assert_eq!(encode_component(","), "%2C".to_strbuf());
|
|
|
|
assert_eq!(encode_component("/"), "%2F".to_strbuf());
|
|
|
|
assert_eq!(encode_component(":"), "%3A".to_strbuf());
|
|
|
|
assert_eq!(encode_component(";"), "%3B".to_strbuf());
|
|
|
|
assert_eq!(encode_component("="), "%3D".to_strbuf());
|
|
|
|
assert_eq!(encode_component("?"), "%3F".to_strbuf());
|
|
|
|
assert_eq!(encode_component("@"), "%40".to_strbuf());
|
|
|
|
assert_eq!(encode_component("["), "%5B".to_strbuf());
|
|
|
|
assert_eq!(encode_component("]"), "%5D".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_decode() {
|
2014-05-13 17:51:05 -07:00
|
|
|
assert_eq!(decode(""), "".to_strbuf());
|
|
|
|
assert_eq!(decode("abc/def 123"), "abc/def 123".to_strbuf());
|
|
|
|
assert_eq!(decode("abc%2Fdef%20123"), "abc%2Fdef 123".to_strbuf());
|
|
|
|
assert_eq!(decode("%20"), " ".to_strbuf());
|
|
|
|
assert_eq!(decode("%21"), "%21".to_strbuf());
|
|
|
|
assert_eq!(decode("%22"), "%22".to_strbuf());
|
|
|
|
assert_eq!(decode("%23"), "%23".to_strbuf());
|
|
|
|
assert_eq!(decode("%24"), "%24".to_strbuf());
|
|
|
|
assert_eq!(decode("%25"), "%".to_strbuf());
|
|
|
|
assert_eq!(decode("%26"), "%26".to_strbuf());
|
|
|
|
assert_eq!(decode("%27"), "'".to_strbuf());
|
|
|
|
assert_eq!(decode("%28"), "%28".to_strbuf());
|
|
|
|
assert_eq!(decode("%29"), "%29".to_strbuf());
|
|
|
|
assert_eq!(decode("%2A"), "%2A".to_strbuf());
|
|
|
|
assert_eq!(decode("%2B"), "%2B".to_strbuf());
|
|
|
|
assert_eq!(decode("%2C"), "%2C".to_strbuf());
|
|
|
|
assert_eq!(decode("%2F"), "%2F".to_strbuf());
|
|
|
|
assert_eq!(decode("%3A"), "%3A".to_strbuf());
|
|
|
|
assert_eq!(decode("%3B"), "%3B".to_strbuf());
|
|
|
|
assert_eq!(decode("%3D"), "%3D".to_strbuf());
|
|
|
|
assert_eq!(decode("%3F"), "%3F".to_strbuf());
|
|
|
|
assert_eq!(decode("%40"), "%40".to_strbuf());
|
|
|
|
assert_eq!(decode("%5B"), "%5B".to_strbuf());
|
|
|
|
assert_eq!(decode("%5D"), "%5D".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_decode_component() {
|
2014-05-13 17:51:05 -07:00
|
|
|
assert_eq!(decode_component(""), "".to_strbuf());
|
|
|
|
assert_eq!(decode_component("abc/def 123"), "abc/def 123".to_strbuf());
|
|
|
|
assert_eq!(decode_component("abc%2Fdef%20123"), "abc/def 123".to_strbuf());
|
|
|
|
assert_eq!(decode_component("%20"), " ".to_strbuf());
|
|
|
|
assert_eq!(decode_component("%21"), "!".to_strbuf());
|
|
|
|
assert_eq!(decode_component("%22"), "\"".to_strbuf());
|
|
|
|
assert_eq!(decode_component("%23"), "#".to_strbuf());
|
|
|
|
assert_eq!(decode_component("%24"), "$".to_strbuf());
|
|
|
|
assert_eq!(decode_component("%25"), "%".to_strbuf());
|
|
|
|
assert_eq!(decode_component("%26"), "&".to_strbuf());
|
|
|
|
assert_eq!(decode_component("%27"), "'".to_strbuf());
|
|
|
|
assert_eq!(decode_component("%28"), "(".to_strbuf());
|
|
|
|
assert_eq!(decode_component("%29"), ")".to_strbuf());
|
|
|
|
assert_eq!(decode_component("%2A"), "*".to_strbuf());
|
|
|
|
assert_eq!(decode_component("%2B"), "+".to_strbuf());
|
|
|
|
assert_eq!(decode_component("%2C"), ",".to_strbuf());
|
|
|
|
assert_eq!(decode_component("%2F"), "/".to_strbuf());
|
|
|
|
assert_eq!(decode_component("%3A"), ":".to_strbuf());
|
|
|
|
assert_eq!(decode_component("%3B"), ";".to_strbuf());
|
|
|
|
assert_eq!(decode_component("%3D"), "=".to_strbuf());
|
|
|
|
assert_eq!(decode_component("%3F"), "?".to_strbuf());
|
|
|
|
assert_eq!(decode_component("%40"), "@".to_strbuf());
|
|
|
|
assert_eq!(decode_component("%5B"), "[".to_strbuf());
|
|
|
|
assert_eq!(decode_component("%5D"), "]".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_encode_form_urlencoded() {
|
|
|
|
let mut m = HashMap::new();
|
2014-05-13 17:51:05 -07:00
|
|
|
assert_eq!(encode_form_urlencoded(&m), "".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
|
2014-05-13 17:51:05 -07:00
|
|
|
m.insert("".to_strbuf(), vec!());
|
|
|
|
m.insert("foo".to_strbuf(), vec!());
|
|
|
|
assert_eq!(encode_form_urlencoded(&m), "".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
|
|
|
|
let mut m = HashMap::new();
|
2014-05-13 17:51:05 -07:00
|
|
|
m.insert("foo".to_strbuf(), vec!("bar".to_strbuf(), "123".to_strbuf()));
|
|
|
|
assert_eq!(encode_form_urlencoded(&m), "foo=bar&foo=123".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
|
|
|
|
let mut m = HashMap::new();
|
2014-05-13 17:51:05 -07:00
|
|
|
m.insert("foo bar".to_strbuf(), vec!("abc".to_strbuf(), "12 = 34".to_strbuf()));
|
2013-07-17 17:47:20 -07:00
|
|
|
assert!(encode_form_urlencoded(&m) ==
|
2014-05-13 17:51:05 -07:00
|
|
|
"foo+bar=abc&foo+bar=12+%3D+34".to_strbuf());
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_decode_form_urlencoded() {
|
|
|
|
assert_eq!(decode_form_urlencoded([]).len(), 0);
|
|
|
|
|
|
|
|
let s = "a=1&foo+bar=abc&foo+bar=12+%3D+34".as_bytes();
|
|
|
|
let form = decode_form_urlencoded(s);
|
|
|
|
assert_eq!(form.len(), 2);
|
2014-05-13 17:51:05 -07:00
|
|
|
assert_eq!(form.get(&"a".to_strbuf()), &vec!("1".to_strbuf()));
|
|
|
|
assert_eq!(form.get(&"foo bar".to_strbuf()),
|
|
|
|
&vec!("abc".to_strbuf(), "12 = 34".to_strbuf()));
|
2013-07-17 17:47:20 -07:00
|
|
|
}
|
|
|
|
}
|