| 1 |
use regex::Regex; |
| 2 |
|
| 3 |
pub fn get_hostname_regex(url: &str) -> Option<(usize, (usize, usize))> { |
| 4 |
lazy_static! { |
| 5 |
static ref HOSTNAME_REGEX_STR: &'static str = concat!( |
| 6 |
r"(?P<scheme>[a-z][a-z0-9+\-.]*)://", |
| 7 |
r"(?:[a-z0-9\-._~%!$&'()*+,;=]+@)?", |
| 8 |
r"(?P<host>[\w\-.~%]+", |
| 9 |
r"|\[[a-f0-9:.]+\]", |
| 10 |
r"|\[v[a-f0-9][a-z0-9\-._~%!$&'()*+,;=:]+\])", |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
); |
| 16 |
static ref HOST_REGEX: Regex = Regex::new(&HOSTNAME_REGEX_STR).unwrap(); |
| 17 |
} |
| 18 |
|
| 19 |
HOST_REGEX.captures(url) |
| 20 |
.and_then(|c| { |
| 21 |
Some((c.name("scheme")?.end(), (c.name("host")?.start(), c.name("host")?.end()))) |
| 22 |
}) |
| 23 |
} |
| 24 |
|
| 25 |
pub fn get_url_host(url: &str) -> Option<(String, usize, (usize, usize))> { |
| 26 |
let decode_flags = idna::uts46::Flags { |
| 27 |
use_std3_ascii_rules: true, |
| 28 |
transitional_processing: true, |
| 29 |
verify_dns_length: true, |
| 30 |
}; |
| 31 |
get_hostname_regex(&url) |
| 32 |
.and_then(|(schema_end, (hostname_start, hostname_end))| { |
| 33 |
let host = &url[hostname_start..hostname_end]; |
| 34 |
if host.is_ascii() { |
| 35 |
Some((url.to_owned(), schema_end, (hostname_start, hostname_end))) |
| 36 |
} else { |
| 37 |
idna::uts46::to_ascii(&host, decode_flags).map(|h| { |
| 38 |
let normalised = format!("{}://{}{}", &url[..schema_end], &h, &url[hostname_end..]); |
| 39 |
(normalised, schema_end, (hostname_start, hostname_start + h.len())) |
| 40 |
}).ok() |
| 41 |
} |
| 42 |
}) |
| 43 |
} |
| 44 |
|
| 45 |
impl super::UrlParser for crate::request::Request { |
| 46 |
fn parse_url(url: &str) -> Option<super::RequestUrl> { |
| 47 |
let parsed = get_url_host(&url); |
| 48 |
parsed.map(|(url, schema_end, (host_start, host_end))| { |
| 49 |
super::RequestUrl { |
| 50 |
url: url, |
| 51 |
schema_end: schema_end, |
| 52 |
hostname_pos: (host_start, host_end), |
| 53 |
domain: super::get_host_domain(&url[host_start..host_end]) |
| 54 |
} |
| 55 |
}) |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
#[cfg(test)] |
| 61 |
mod parse_tests { |
| 62 |
use super::*; |
| 63 |
|
| 64 |
#[test] |
| 65 |
|
| 66 |
fn parses_hostname() { |
| 67 |
assert_eq!(get_url_host("http://example.foo.edu.au"), Some(("http://example.foo.edu.au".to_owned(), 4, (7, 25)))); |
| 68 |
assert_eq!(get_url_host("http://example.foo.edu.sh"), Some(("http://example.foo.edu.sh".to_owned(), 4, (7, 25)))); |
| 69 |
assert_eq!(get_url_host("http://example.foo.nom.br"), Some(("http://example.foo.nom.br".to_owned(), 4, (7, 25)))); |
| 70 |
assert_eq!(get_url_host("http://example.foo.nom.br:80/"), Some(("http://example.foo.nom.br:80/".to_owned(), 4, (7, 25)))); |
| 71 |
assert_eq!(get_url_host("http://example.foo.nom.br:8080/hello?world=true"), Some(("http://example.foo.nom.br:8080/hello?world=true".to_owned(), 4, (7, 25)))); |
| 72 |
assert_eq!(get_url_host("http://example.foo.nom.br/hello#world"), Some(("http://example.foo.nom.br/hello#world".to_owned(), 4, (7, 25)))); |
| 73 |
assert_eq!(get_url_host("http://127.0.0.1:80"), Some(("http://127.0.0.1:80".to_owned(), 4, (7, 16)))); |
| 74 |
assert_eq!(get_url_host("http://[2001:470:20::2]"), Some(("http://[2001:470:20::2]".to_owned(), 4, (7, 23)))); |
| 75 |
assert_eq!(get_url_host("http://[2001:4860:4860::1:8888]"), Some(("http://[2001:4860:4860::1:8888]".to_owned(), 4, (7, 31)))); |
| 76 |
} |
| 77 |
} |