use super::error::{Error, Result};
use super::stats::{HttpStat, ALPN_HTTP1, ALPN_HTTP2};
use bytes::Bytes;
use http::request::Builder;
use http::HeaderValue;
use http::Request;
use http::Uri;
use http::{HeaderMap, Method};
use http_body_util::Full;
use rustls::client::{ClientSessionMemoryCache, ClientSessionStore};
use std::net::IpAddr;
use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;
use std::time::Instant;
const VERSION: &str = env!("CARGO_PKG_VERSION");
pub(crate) fn finish_with_error(
mut stat: HttpStat,
error: impl ToString,
start: Instant,
) -> HttpStat {
stat.error = Some(error.to_string());
stat.total = Some(start.elapsed());
stat
}
#[derive(Debug, Clone)]
pub struct ConnectTo {
src_host: String,
src_port: Option<u16>,
pub dst_host: String,
pub dst_port: Option<u16>,
}
fn parse_host_segment(s: &str) -> (String, &str) {
if let Some(rest) = s.strip_prefix('[') {
if let Some(end) = rest.find(']') {
return (rest[..end].to_string(), &rest[end + 1..]);
}
}
let colon = s.find(':').unwrap_or(s.len());
(s[..colon].to_string(), &s[colon..])
}
impl ConnectTo {
pub fn parse(s: &str) -> Option<Self> {
let (src_host, rest) = parse_host_segment(s);
let rest = rest.strip_prefix(':')?;
let colon = rest.find(':')?;
let src_port = if rest[..colon].is_empty() {
None
} else {
Some(rest[..colon].parse().ok()?)
};
let rest = &rest[colon + 1..];
let (dst_host, rest) = parse_host_segment(rest);
let port2_str = rest.strip_prefix(':').unwrap_or(rest);
let dst_port = if port2_str.is_empty() {
None
} else {
Some(port2_str.parse().ok()?)
};
Some(ConnectTo {
src_host,
src_port,
dst_host,
dst_port,
})
}
pub fn matches(&self, host: &str, port: u16) -> bool {
let host_ok = self.src_host.is_empty() || self.src_host.eq_ignore_ascii_case(host);
let port_ok = self.src_port.is_none() || self.src_port == Some(port);
host_ok && port_ok
}
}
#[derive(Default, Debug, Clone)]
pub struct HttpRequest {
pub uri: Uri, pub method: Option<String>, pub alpn_protocols: Vec<String>, pub resolve: Option<IpAddr>, pub headers: Option<HeaderMap<HeaderValue>>, pub ip_version: Option<i32>, pub skip_verify: bool, pub body: Option<Bytes>, pub dns_servers: Option<Vec<String>>, pub dns_timeout: Option<Duration>, pub tcp_timeout: Option<Duration>, pub tls_timeout: Option<Duration>, pub request_timeout: Option<Duration>, pub quic_timeout: Option<Duration>, pub client_cert: Option<Vec<u8>>, pub client_key: Option<Vec<u8>>, pub proxy: Option<String>, pub use_absolute_uri: bool, pub connect_to: Vec<String>, pub bind_addr: Option<IpAddr>, pub tls_session_store: Option<Arc<dyn ClientSessionStore>>,
}
impl HttpRequest {
pub fn get_port(&self) -> u16 {
let scheme = self.uri.scheme_str().unwrap_or("");
let default_port = if matches!(scheme, "https" | "grpcs") {
443
} else {
80
};
self.uri.port_u16().unwrap_or(default_port)
}
pub fn builder(&self, is_http1: bool) -> Builder {
let uri = &self.uri;
let method = if let Some(method) = &self.method {
Method::from_str(method).unwrap_or(Method::GET)
} else {
Method::GET
};
let mut builder = if is_http1 && !self.use_absolute_uri {
if let Some(value) = uri.path_and_query() {
Request::builder().uri(value.to_string())
} else {
Request::builder().uri(uri)
}
} else {
Request::builder().uri(uri)
};
builder = builder.method(method);
let mut set_host = false;
let mut set_user_agent = false;
if let Some(headers) = &self.headers {
for (key, value) in headers.iter() {
builder = builder.header(key, value);
match key.as_str() {
"host" => set_host = true,
"user-agent" => set_user_agent = true,
_ => {}
}
}
}
if !set_host {
if let Some(host) = uri.host() {
let port = self.get_port();
if port != 80 && port != 443 {
builder = builder.header("Host", format!("{host}:{port}"));
} else {
builder = builder.header("Host", host);
}
}
}
if !set_user_agent {
builder = builder.header("User-Agent", format!("httpstat.rs/{VERSION}"));
}
builder
}
}
pub fn new_tls_session_store(capacity: usize) -> Arc<dyn ClientSessionStore> {
Arc::new(ClientSessionMemoryCache::new(capacity))
}
impl TryFrom<&str> for HttpRequest {
type Error = Error;
fn try_from(url: &str) -> Result<Self> {
let prefixes = ["http://", "https://", "grpc://", "grpcs://"];
let value = if prefixes.iter().any(|prefix| url.starts_with(prefix)) {
url.to_string()
} else {
format!("http://{url}")
};
let uri = value.parse::<Uri>().map_err(|e| Error::Uri { source: e })?;
Ok(Self {
uri,
alpn_protocols: vec![ALPN_HTTP2.to_string(), ALPN_HTTP1.to_string()],
..Default::default()
})
}
}
impl TryFrom<&HttpRequest> for Request<Full<Bytes>> {
type Error = Error;
fn try_from(req: &HttpRequest) -> Result<Self> {
req.builder(true)
.body(Full::new(req.body.clone().unwrap_or_default()))
.map_err(|e| Error::Http { source: e })
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_host_segment_plain_and_bracketed() {
assert_eq!(parse_host_segment("host:443"), ("host".to_string(), ":443"));
assert_eq!(parse_host_segment("[::1]:443"), ("::1".to_string(), ":443"));
assert_eq!(parse_host_segment("host"), ("host".to_string(), ""));
}
#[test]
fn connect_to_full_entry() {
let c = ConnectTo::parse("example.com:443:1.2.3.4:8443").unwrap();
assert_eq!(c.dst_host, "1.2.3.4");
assert_eq!(c.dst_port, Some(8443));
assert!(c.matches("example.com", 443));
assert!(c.matches("EXAMPLE.COM", 443)); assert!(!c.matches("other.com", 443));
assert!(!c.matches("example.com", 80));
}
#[test]
fn connect_to_wildcards() {
let any_host = ConnectTo::parse(":443:1.2.3.4:8443").unwrap();
assert!(any_host.matches("whatever.com", 443));
assert!(!any_host.matches("whatever.com", 80));
let any_port = ConnectTo::parse("example.com::1.2.3.4:8443").unwrap();
assert!(any_port.matches("example.com", 1));
assert!(any_port.matches("example.com", 65535));
assert!(!any_port.matches("other.com", 443));
}
#[test]
fn connect_to_ipv6_and_optional_dst_port() {
let dst6 = ConnectTo::parse("example.com:443:[2001:db8::1]:8443").unwrap();
assert_eq!(dst6.dst_host, "2001:db8::1");
assert_eq!(dst6.dst_port, Some(8443));
let src6 = ConnectTo::parse("[2001:db8::1]:443:1.2.3.4:8443").unwrap();
assert!(src6.matches("2001:db8::1", 443));
let no_dst_port = ConnectTo::parse("example.com:443:1.2.3.4").unwrap();
assert_eq!(no_dst_port.dst_host, "1.2.3.4");
assert_eq!(no_dst_port.dst_port, None);
}
#[test]
fn connect_to_rejects_malformed() {
assert!(ConnectTo::parse("example.com").is_none()); assert!(ConnectTo::parse("example.com:443").is_none()); assert!(ConnectTo::parse("example.com:notaport:1.2.3.4:8443").is_none());
}
#[test]
fn try_from_adds_scheme_and_default_alpn() {
let req = HttpRequest::try_from("example.com").unwrap();
assert_eq!(req.uri.scheme_str(), Some("http"));
assert_eq!(
req.alpn_protocols,
vec![ALPN_HTTP2.to_string(), ALPN_HTTP1.to_string()]
);
assert_eq!(
HttpRequest::try_from("https://example.com/path")
.unwrap()
.uri
.scheme_str(),
Some("https")
);
assert_eq!(
HttpRequest::try_from("grpc://svc:50051")
.unwrap()
.uri
.scheme_str(),
Some("grpc")
);
}
#[test]
fn get_port_defaults_by_scheme() {
assert_eq!(HttpRequest::try_from("http://x").unwrap().get_port(), 80);
assert_eq!(HttpRequest::try_from("https://x").unwrap().get_port(), 443);
assert_eq!(HttpRequest::try_from("grpcs://x").unwrap().get_port(), 443);
assert_eq!(
HttpRequest::try_from("http://x:8080").unwrap().get_port(),
8080
);
}
#[test]
fn builder_sets_default_host_and_user_agent() {
let req = HttpRequest::try_from("http://example.com/path").unwrap();
let r = req.builder(true).body(()).unwrap();
assert_eq!(r.headers().get("host").unwrap(), "example.com");
assert!(r
.headers()
.get("user-agent")
.unwrap()
.to_str()
.unwrap()
.starts_with("httpstat.rs/"));
}
#[test]
fn builder_includes_nondefault_port_in_host() {
let req = HttpRequest::try_from("http://example.com:8080/").unwrap();
let r = req.builder(true).body(()).unwrap();
assert_eq!(r.headers().get("host").unwrap(), "example.com:8080");
}
#[test]
fn builder_respects_custom_host_header() {
let mut req = HttpRequest::try_from("http://example.com/").unwrap();
let mut hm = HeaderMap::new();
hm.insert(http::header::HOST, HeaderValue::from_static("custom.test"));
req.headers = Some(hm);
let r = req.builder(true).body(()).unwrap();
assert_eq!(r.headers().get("host").unwrap(), "custom.test");
}
}