| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668 |
- // Copyright 2013-2014 Simon Sapin.
- //
- // 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.
- use std::ascii::StrAsciiExt;
- use encoding;
- use encoding::EncodingRef;
- use encoding::all::UTF_8;
- use super::{
- ParseResult, ErrorHandler, Url, RelativeSchemeData, OtherSchemeData,
- SchemeRelativeUrl, UserInfo, Host, Domain,
- utf8_percent_encode, percent_encode_byte,
- SimpleEncodeSet, DefaultEncodeSet, UserInfoEncodeSet};
- macro_rules! is_match(
- ($value:expr, $($pattern:pat)|+) => (
- match $value { $($pattern)|+ => true, _ => false }
- );
- )
- pub fn parse_url(input: &str, base_url: Option<&Url>, parse_error: ErrorHandler)
- -> ParseResult<Url> {
- let input = input.trim_chars(&[' ', '\t', '\n', '\r', '\x0C']);
- match parse_scheme(input) {
- Some((scheme, remaining)) => {
- if scheme.as_slice() == "file" {
- // Relative state?
- match base_url {
- Some(base) if scheme == base.scheme => {
- try!(parse_error("Relative URL with a scheme"));
- parse_relative_url(scheme, remaining, base, parse_error)
- },
- _ => parse_relative_url(scheme, remaining, &Url {
- scheme: String::new(), query: None, fragment: None,
- scheme_data: RelativeSchemeData(SchemeRelativeUrl {
- userinfo: None, host: Domain(String::new()),
- port: String::new(), path: Vec::new()
- })
- }, parse_error),
- }
- } else if is_relative_scheme(scheme.as_slice()) {
- match base_url {
- Some(base) if scheme == base.scheme => {
- // Relative or authority state
- if remaining.starts_with("//") {
- parse_absolute_url(scheme, remaining, parse_error)
- } else {
- try!(parse_error("Relative URL with a scheme"));
- parse_relative_url(scheme, remaining, base, parse_error)
- }
- },
- _ => parse_absolute_url(scheme, remaining, parse_error),
- }
- } else {
- // Scheme data state
- let (scheme_data, remaining) = try!(parse_scheme_data(remaining, parse_error));
- let (query, fragment) = try!(parse_query_and_fragment(remaining, parse_error));
- Ok(Url { scheme: scheme, scheme_data: OtherSchemeData(scheme_data),
- query: query, fragment: fragment })
- }
- },
- // No-scheme state
- None => match base_url {
- None => Err("Relative URL without a base"),
- Some(base) => parse_relative_url(base.scheme.clone(), input, base, parse_error)
- }
- }
- }
- fn parse_scheme<'a>(input: &'a str) -> Option<(String, &'a str)> {
- if !input.is_empty() && starts_with_ascii_alpha(input) {
- for (i, c) in input.char_indices() {
- match c {
- 'a'..'z' | 'A'..'Z' | '0'..'9' | '+' | '-' | '.' => (),
- ':' => return Some((
- input.slice_to(i).to_ascii_lower(),
- input.slice_from(i + 1),
- )),
- _ => break,
- }
- }
- }
- None
- }
- fn parse_absolute_url<'a>(scheme: String, input: &'a str, parse_error: ErrorHandler)
- -> ParseResult<Url> {
- // Authority first slash state
- let remaining = try!(skip_slashes(input, parse_error));
- // Authority state
- let (userinfo, remaining) = try!(parse_userinfo(remaining, parse_error));
- // Host state
- let (host, port, remaining) = try!(parse_hostname(remaining, scheme.as_slice(), parse_error));
- let (path, remaining) = try!(parse_path_start(
- remaining,
- /* full_url= */ true,
- /* in_file_scheme= */ false,
- parse_error));
- let scheme_data = RelativeSchemeData(SchemeRelativeUrl { userinfo: userinfo, host: host, port: port, path: path });
- let (query, fragment) = try!(parse_query_and_fragment(remaining, parse_error));
- Ok(Url { scheme: scheme, scheme_data: scheme_data, query: query, fragment: fragment })
- }
- fn parse_relative_url<'a>(scheme: String, input: &'a str, base: &Url, parse_error: ErrorHandler)
- -> ParseResult<Url> {
- match base.scheme_data {
- OtherSchemeData(_) => Err("Relative URL with a non-relative-scheme base"),
- RelativeSchemeData(ref base_scheme_data) => if input.is_empty() {
- Ok(Url { scheme: scheme, scheme_data: base.scheme_data.clone(),
- query: base.query.clone(), fragment: None })
- } else {
- let in_file_scheme = scheme.as_slice() == "file";
- match input.char_at(0) {
- '/' | '\\' => {
- // Relative slash state
- if input.len() > 1 && is_match!(input.char_at(1), '/' | '\\') {
- if in_file_scheme {
- let remaining = input.slice_from(2);
- let (host, remaining) = if remaining.len() >= 2
- && starts_with_ascii_alpha(remaining)
- && is_match!(remaining.char_at(1), ':' | '|')
- && (remaining.len() == 2
- || is_match!(remaining.char_at(2),
- '/' | '\\' | '?' | '#'))
- {
- // Windows drive letter quirk
- (Domain(String::new()), remaining)
- } else {
- // File host state
- try!(parse_file_host(remaining, parse_error))
- };
- let (path, remaining) = try!(parse_path_start(
- remaining, /* full_url= */ true,
- in_file_scheme, parse_error));
- let scheme_data = RelativeSchemeData(SchemeRelativeUrl {
- userinfo: None, host: host, port: String::new(), path: path });
- let (query, fragment) = try!(parse_query_and_fragment(
- remaining, parse_error));
- Ok(Url { scheme: scheme, scheme_data: scheme_data,
- query: query, fragment: fragment })
- } else {
- parse_absolute_url(scheme, input, parse_error)
- }
- } else {
- // Relative path state
- let (path, remaining) = try!(parse_path(
- Vec::new(), input.slice_from(1), /* full_url= */ true,
- in_file_scheme, parse_error));
- let scheme_data = RelativeSchemeData(if in_file_scheme {
- SchemeRelativeUrl {
- userinfo: None, host: Domain(String::new()),
- port: String::new(), path: path
- }
- } else {
- SchemeRelativeUrl {
- userinfo: base_scheme_data.userinfo.clone(),
- host: base_scheme_data.host.clone(),
- port: base_scheme_data.port.clone(),
- path: path
- }
- });
- let (query, fragment) = try!(
- parse_query_and_fragment(remaining, parse_error));
- Ok(Url { scheme: scheme, scheme_data: scheme_data,
- query: query, fragment: fragment })
- }
- },
- '?' => {
- let (query, fragment) = try!(parse_query_and_fragment(input, parse_error));
- Ok(Url { scheme: scheme, scheme_data: base.scheme_data.clone(),
- query: query, fragment: fragment })
- },
- '#' => {
- Ok(Url { scheme: scheme, scheme_data: base.scheme_data.clone(),
- query: base.query.clone(),
- fragment: Some(try!(
- parse_fragment(input.slice_from(1), parse_error))) })
- }
- _ => {
- let (scheme_data, remaining) = if in_file_scheme
- && input.len() >= 2
- && starts_with_ascii_alpha(input)
- && is_match!(input.char_at(1), ':' | '|')
- && (input.len() == 2
- || is_match!(input.char_at(2), '/' | '\\' | '?' | '#'))
- {
- // Windows drive letter quirk
- let (path, remaining) = try!(parse_path(
- Vec::new(), input, /* full_url= */ true,
- in_file_scheme, parse_error));
- (RelativeSchemeData(SchemeRelativeUrl {
- userinfo: None,
- host: Domain(String::new()),
- port: String::new(),
- path: path
- }), remaining)
- } else {
- let base_path = base_scheme_data.path.as_slice();
- let initial_path = Vec::from_slice(
- base_path.slice_to(base_path.len() - 1));
- // Relative path state
- let (path, remaining) = try!(parse_path(
- initial_path, input, /* full_url= */ true,
- in_file_scheme, parse_error));
- (RelativeSchemeData(SchemeRelativeUrl {
- userinfo: base_scheme_data.userinfo.clone(),
- host: base_scheme_data.host.clone(),
- port: base_scheme_data.port.clone(),
- path: path
- }), remaining)
- };
- let (query, fragment) = try!(parse_query_and_fragment(remaining, parse_error));
- Ok(Url { scheme: scheme, scheme_data: scheme_data,
- query: query, fragment: fragment })
- }
- }
- }
- }
- }
- fn skip_slashes<'a>(input: &'a str, parse_error: ErrorHandler) -> ParseResult<&'a str> {
- let first_non_slash = input.find(|c| !is_match!(c, '/' | '\\')).unwrap_or(input.len());
- if input.slice_to(first_non_slash) != "//" {
- try!(parse_error("Expected two slashes"));
- }
- Ok(input.slice_from(first_non_slash))
- }
- fn parse_userinfo<'a>(input: &'a str, parse_error: ErrorHandler)
- -> ParseResult<(Option<UserInfo>, &'a str)> {
- let mut last_at = None;
- for (i, c) in input.char_indices() {
- match c {
- '@' => last_at = Some(i),
- '/' | '\\' | '?' | '#' => break,
- _ => (),
- }
- }
- Ok(match last_at {
- None => (None, input),
- Some(at) => (Some(try!(parse_userinfo_inner(input.slice_to(at), parse_error))),
- input.slice_from(at + 1))
- })
- }
- fn parse_userinfo_inner(input: &str, parse_error: ErrorHandler) -> ParseResult<UserInfo> {
- let mut username = String::new();
- for (i, c) in input.char_indices() {
- match c {
- ':' => return parse_userinfo_password(input.slice_from(i + 1), username, parse_error),
- '\t' | '\n' | '\r' => try!(parse_error("Invalid character")),
- _ => {
- if c == '%' {
- if !starts_with_2_hex(input.slice_from(i + 1)) {
- try!(parse_error("Invalid percent-encoded sequence"));
- }
- } else if !is_url_code_point(c) {
- try!(parse_error("Non-URL code point"));
- }
- utf8_percent_encode(input.slice(i, i + c.len_utf8_bytes()),
- UserInfoEncodeSet, &mut username);
- }
- }
- }
- Ok(UserInfo { username: username, password: None })
- }
- fn parse_userinfo_password(input: &str, username: String, parse_error: ErrorHandler)
- -> ParseResult<UserInfo> {
- let mut password = String::new();
- for (i, c) in input.char_indices() {
- match c {
- '\t' | '\n' | '\r' => try!(parse_error("Invalid character")),
- _ => {
- if c == '%' {
- if !starts_with_2_hex(input.slice_from(i + 1)) {
- try!(parse_error("Invalid percent-encoded sequence"));
- }
- } else if !is_url_code_point(c) {
- try!(parse_error("Non-URL code point"));
- }
- utf8_percent_encode(input.slice(i, i + c.len_utf8_bytes()),
- UserInfoEncodeSet, &mut password);
- }
- }
- }
- Ok(UserInfo { username: username, password: Some(password) })
- }
- fn parse_hostname<'a>(input: &'a str, scheme: &str, parse_error: ErrorHandler)
- -> ParseResult<(Host, String, &'a str)> {
- let mut inside_square_brackets = false;
- let mut host_input = String::new();
- let mut end = input.len();
- for (i, c) in input.char_indices() {
- match c {
- ':' if !inside_square_brackets => {
- let host = try!(Host::parse(host_input.as_slice()));
- let (port, remaining) = try!(
- parse_port(input.slice_from(i + 1), scheme, parse_error));
- return Ok((host, port, remaining))
- },
- '/' | '\\' | '?' | '#' => {
- end = i;
- break
- },
- '\t' | '\n' | '\r' => try!(parse_error("Invalid character")),
- c => {
- match c {
- '[' => inside_square_brackets = true,
- ']' => inside_square_brackets = false,
- _ => (),
- }
- host_input.push_char(c)
- }
- }
- }
- let host = try!(Host::parse(host_input.as_slice()));
- Ok((host, String::new(), input.slice_from(end)))
- }
- fn parse_port<'a>(input: &'a str, scheme: &str, parse_error: ErrorHandler)
- -> ParseResult<(String, &'a str)> {
- let mut port = String::new();
- let mut has_initial_zero = false;
- let mut end = input.len();
- for (i, c) in input.char_indices() {
- match c {
- '1'..'9' => port.push_char(c),
- '0' => {
- if port.is_empty() {
- has_initial_zero = true
- } else {
- port.push_char(c)
- }
- },
- '/' | '\\' | '?' | '#' => {
- end = i;
- break
- },
- '\t' | '\n' | '\r' => try!(parse_error("Invalid character")),
- _ => return Err("Invalid port number")
- }
- }
- if port.is_empty() && has_initial_zero {
- port.push_str("0")
- }
- match (scheme, port.as_slice()) {
- ("ftp", "21") | ("gopher", "70") | ("http", "80") |
- ("https", "443") | ("ws", "80") | ("wss", "443")
- => port = String::new(),
- _ => (),
- }
- return Ok((port, input.slice_from(end)))
- }
- fn parse_file_host<'a>(input: &'a str, parse_error: ErrorHandler) -> ParseResult<(Host, &'a str)> {
- let mut host_input = String::new();
- let mut end = input.len();
- for (i, c) in input.char_indices() {
- match c {
- '/' | '\\' | '?' | '#' => {
- end = i;
- break
- },
- '\t' | '\n' | '\r' => try!(parse_error("Invalid character")),
- _ => host_input.push_char(c)
- }
- }
- let host = if host_input.is_empty() {
- Domain(String::new())
- } else {
- try!(Host::parse(host_input.as_slice()))
- };
- Ok((host, input.slice_from(end)))
- }
- fn parse_path_start<'a>(input: &'a str, full_url: bool, in_file_scheme: bool,
- parse_error: ErrorHandler)
- -> ParseResult<(Vec<String>, &'a str)> {
- let mut i = 0;
- // Relative path start state
- if !input.is_empty() {
- match input.char_at(0) {
- '/' => i = 1,
- '\\' => {
- try!(parse_error("Backslash"));
- i = 1;
- },
- _ => ()
- }
- }
- parse_path(Vec::new(), input.slice_from(i), full_url, in_file_scheme, parse_error)
- }
- fn parse_path<'a>(base_path: Vec<String>, input: &'a str, full_url: bool, in_file_scheme: bool,
- parse_error: ErrorHandler)
- -> ParseResult<(Vec<String>, &'a str)> {
- // Relative path state
- let mut path = base_path;
- let mut iter = input.char_indices();
- let mut end;
- loop {
- let mut path_part = String::new();
- let mut ends_with_slash = false;
- end = input.len();
- for (i, c) in iter {
- match c {
- '/' => {
- ends_with_slash = true;
- end = i;
- break
- },
- '\\' => {
- try!(parse_error("Backslash"));
- ends_with_slash = true;
- end = i;
- break
- },
- '?' | '#' if full_url => {
- end = i;
- break
- },
- '\t' | '\n' | '\r' => try!(parse_error("Invalid character")),
- _ => {
- if c == '%' {
- if !starts_with_2_hex(input.slice_from(i + 1)) {
- try!(parse_error("Invalid percent-encoded sequence"));
- }
- } else if !is_url_code_point(c) {
- try!(parse_error("Non-URL code point"));
- }
- utf8_percent_encode(input.slice(i, i + c.len_utf8_bytes()),
- DefaultEncodeSet, &mut path_part);
- }
- }
- }
- match path_part.as_slice() {
- ".." | ".%2e" | ".%2E" | "%2e." | "%2E." |
- "%2e%2e" | "%2E%2e" | "%2e%2E" | "%2E%2E" => {
- path.pop();
- if !ends_with_slash {
- path.push(String::new());
- }
- },
- "." | "%2e" | "%2E" => {
- if !ends_with_slash {
- path.push(String::new());
- }
- },
- _ => {
- if in_file_scheme
- && path.is_empty()
- && path_part.len() == 2
- && starts_with_ascii_alpha(path_part.as_slice())
- && path_part.as_slice().char_at(1) == '|' {
- // Windows drive letter quirk
- unsafe {
- *path_part.as_mut_vec().get_mut(1) = b':'
- }
- }
- path.push(path_part)
- }
- }
- if !ends_with_slash {
- break
- }
- }
- Ok((path, input.slice_from(end)))
- }
- fn parse_scheme_data<'a>(input: &'a str, parse_error: ErrorHandler)
- -> ParseResult<(String, &'a str)> {
- let mut scheme_data = String::new();
- let mut end = input.len();
- for (i, c) in input.char_indices() {
- match c {
- '?' | '#' => {
- end = i;
- break
- },
- '\t' | '\n' | '\r' => try!(parse_error("Invalid character")),
- _ => {
- if c == '%' {
- if !starts_with_2_hex(input.slice_from(i + 1)) {
- try!(parse_error("Invalid percent-encoded sequence"));
- }
- } else if !is_url_code_point(c) {
- try!(parse_error("Non-URL code point"));
- }
- utf8_percent_encode(input.slice(i, i + c.len_utf8_bytes()),
- SimpleEncodeSet, &mut scheme_data);
- }
- }
- }
- Ok((scheme_data, input.slice_from(end)))
- }
- fn parse_query_and_fragment(input: &str, parse_error: ErrorHandler)
- -> ParseResult<(Option<String>, Option<String>)> {
- Ok(if input.is_empty() {
- (None, None)
- } else {
- match input.char_at(0) {
- '#' => (None, Some(try!(parse_fragment(input.slice_from(1), parse_error)))),
- '?' => {
- let (query, remaining) = try!(parse_query(
- input.slice_from(1),
- UTF_8 as EncodingRef, // TODO
- /* full_url = */ true,
- parse_error));
- (Some(query), match remaining {
- Some(remaining) => Some(try!(parse_fragment(remaining, parse_error))),
- None => None
- })
- },
- _ => fail!("Programming error")
- }
- })
- }
- fn parse_query<'a>(input: &'a str, encoding_override: EncodingRef, full_url: bool,
- parse_error: ErrorHandler)
- -> ParseResult<(String, Option<&'a str>)> {
- let mut query = String::new();
- let mut remaining = None;
- for (i, c) in input.char_indices() {
- match c {
- '#' if full_url => {
- remaining = Some(input.slice_from(i + 1));
- break
- },
- '\t' | '\n' | '\r' => try!(parse_error("Invalid character")),
- _ => {
- if c == '%' {
- if !starts_with_2_hex(input.slice_from(i + 1)) {
- try!(parse_error("Invalid percent-encoded sequence"));
- }
- } else if !is_url_code_point(c) {
- try!(parse_error("Non-URL code point"));
- }
- query.push_char(c);
- }
- }
- }
- let query_bytes = encoding_override.encode(query.as_slice(), encoding::EncodeReplace).unwrap();
- let mut query_encoded = String::new();
- for &byte in query_bytes.iter() {
- match byte {
- b'\x00'.. b' ' | b'"' | b'#' | b'<' | b'>' | b'`' | b'~'..b'\xFF'
- => percent_encode_byte(byte, &mut query_encoded),
- _
- => unsafe { query_encoded.push_byte(byte) }
- }
- }
- Ok((query_encoded, remaining))
- }
- fn parse_fragment<'a>(input: &'a str, parse_error: ErrorHandler) -> ParseResult<String> {
- let mut fragment = String::new();
- for (i, c) in input.char_indices() {
- match c {
- '\t' | '\n' | '\r' => try!(parse_error("Invalid character")),
- _ => {
- if c == '%' {
- if !starts_with_2_hex(input.slice_from(i + 1)) {
- try!(parse_error("Invalid percent-encoded sequence"));
- }
- } else if !is_url_code_point(c) {
- try!(parse_error("Non-URL code point"));
- }
- utf8_percent_encode(input.slice(i, i + c.len_utf8_bytes()),
- SimpleEncodeSet, &mut fragment);
- }
- }
- }
- Ok(fragment)
- }
- #[inline]
- fn starts_with_ascii_alpha(string: &str) -> bool {
- match string.char_at(0) {
- 'a'..'z' | 'A'..'Z' => true,
- _ => false,
- }
- }
- #[inline]
- fn is_ascii_hex_digit(byte: u8) -> bool {
- match byte {
- b'a'..b'f' | b'A'..b'F' | b'0'..b'9' => true,
- _ => false,
- }
- }
- #[inline]
- fn starts_with_2_hex(input: &str) -> bool {
- input.len() >= 2
- && is_ascii_hex_digit(input.as_bytes()[0])
- && is_ascii_hex_digit(input.as_bytes()[1])
- }
- #[inline]
- fn is_url_code_point(c: char) -> bool {
- match c {
- 'a'..'z' |
- 'A'..'Z' |
- '0'..'9' |
- '!' | '$' | '&' | '\'' | '(' | ')' | '*' | '+' | ',' | '-' |
- '.' | '/' | ':' | ';' | '=' | '?' | '@' | '_' | '~' |
- '\u00A0'..'\uD7FF' | '\uE000'..'\uFDCF' | '\uFDF0'..'\uFFEF' |
- '\U00010000'..'\U0001FFFD' | '\U00020000'..'\U0002FFFD' |
- '\U00030000'..'\U0003FFFD' | '\U00040000'..'\U0004FFFD' |
- '\U00050000'..'\U0005FFFD' | '\U00060000'..'\U0006FFFD' |
- '\U00070000'..'\U0007FFFD' | '\U00080000'..'\U0008FFFD' |
- '\U00090000'..'\U0009FFFD' | '\U000A0000'..'\U000AFFFD' |
- '\U000B0000'..'\U000BFFFD' | '\U000C0000'..'\U000CFFFD' |
- '\U000D0000'..'\U000DFFFD' | '\U000E1000'..'\U000EFFFD' |
- '\U000F0000'..'\U000FFFFD' | '\U00100000'..'\U0010FFFD' => true,
- _ => false
- }
- }
- // Non URL code points:
- // U+0000 to U+0020 (space)
- // " # % < > [ \ ] ^ ` { | }
- // U+007F to U+009F
- // surrogates
- // U+FDD0 to U+FDEF
- // U+FFF0 to U+FFFF
- // Last two of each plane: U+__FFFE to U+__FFFF for __ in 01 to 10 hex
- fn is_relative_scheme(scheme: &str) -> bool {
- is_match!(scheme, "ftp" | "file" | "gopher" | "http" | "https" | "ws" | "wss")
- }
|