pub fn parse_and_get_http_host_field(data: &str) -> Option<String> {
let mut headers = [httparse::EMPTY_HEADER; 32];
let mut req = httparse::Request::new(&mut headers);
if req.parse(data.as_bytes()).is_ok() {
if let Some(conn) = req.method {
if conn == "CONNECT" {
return match req.path {
None => None,
Some(s) => Some(String::from(s)),
};
}
}
for h in req.headers.iter() {
if h.name.to_ascii_lowercase() != "host" {
continue;
}
if let Some(t) = req.path {
let port = match url::Url::parse(t) {
Ok(u) => match u.port_or_known_default() {
Some(p) => p,
None => return None,
},
Err(_) => return None,
};
return Some(format!("{}:{}", String::from_utf8_lossy(h.value), port));
};
}
}
None
}
pub fn parse_and_get_http_host_field_with_error<E>(data: &str, e: E) -> Result<String, E> {
match parse_and_get_http_host_field(data) {
Some(o) => Ok(o),
None => Err(e),
}
}