use std::fmt;
use std::future::Future;
use std::time::Duration;
#[cfg(feature = "native")]
use crate::http::{push_capped, ERROR_BODY_CAP, MAX_BODY_BYTES, USER_AGENT};
#[cfg(feature = "native")]
use crate::tls::TlsConfig;
#[cfg(feature = "wasi")]
pub use crate::wasi_transport::WasiTransport;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Method {
Get,
Post,
}
#[derive(Debug, Clone)]
pub struct HttpRequest {
pub method: Method,
pub url: String,
pub headers: Vec<(String, String)>,
pub body: Option<Vec<u8>>,
pub timeout: Option<Duration>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HttpResponse {
pub status: u16,
pub headers: Vec<(String, String)>,
pub body: Vec<u8>,
}
impl HttpResponse {
pub fn header(&self, name: &str) -> Option<&str> {
self.headers
.iter()
.find(|(k, _)| k.eq_ignore_ascii_case(name))
.map(|(_, v)| v.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TransportError {
Timeout(String),
Connect(String),
Tls(String),
BodyTooLarge,
Other(String),
}
impl TransportError {
pub fn is_transient(&self) -> bool {
matches!(
self,
TransportError::Timeout(_) | TransportError::Connect(_)
)
}
}
impl fmt::Display for TransportError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
TransportError::Timeout(url) => write!(f, "request timed out for url ({url})"),
TransportError::Connect(m) => write!(f, "{m}"),
TransportError::Tls(m) => write!(f, "TLS error: {m}"),
TransportError::BodyTooLarge => write!(f, "response body exceeds the size cap"),
TransportError::Other(m) => write!(f, "{m}"),
}
}
}
impl std::error::Error for TransportError {}
pub trait Transport: Send + Sync {
fn send(
&self,
req: HttpRequest,
) -> impl Future<Output = Result<HttpResponse, TransportError>> + Send;
fn sleep(&self, d: Duration) -> impl Future<Output = ()> + Send;
}
impl<T: Transport + ?Sized> Transport for &T {
fn send(
&self,
req: HttpRequest,
) -> impl Future<Output = Result<HttpResponse, TransportError>> + Send {
(**self).send(req)
}
fn sleep(&self, d: Duration) -> impl Future<Output = ()> + Send {
(**self).sleep(d)
}
}
#[cfg(feature = "native")]
#[derive(Debug, Clone)]
pub struct ReqwestTransport {
client: reqwest::Client,
}
#[cfg(feature = "native")]
impl ReqwestTransport {
pub fn new(tls: &TlsConfig) -> Result<Self, TransportError> {
Self::build(tls, None)
}
pub fn pinned(
tls: &TlsConfig,
host: &str,
addrs: &[std::net::SocketAddr],
) -> Result<Self, TransportError> {
Self::build(tls, Some((host, addrs)).filter(|(_, a)| !a.is_empty()))
}
fn build(
tls: &TlsConfig,
pin: Option<(&str, &[std::net::SocketAddr])>,
) -> Result<Self, TransportError> {
let mut builder = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.user_agent(USER_AGENT)
.gzip(true)
.brotli(true);
builder = tls
.apply(builder)
.map_err(|e| TransportError::Other(format!("{e:#}")))?;
if let Some((host, addrs)) = pin {
builder = builder.resolve_to_addrs(host, addrs);
}
let client = builder
.build()
.map_err(|e| TransportError::Other(format!("building HTTP client: {e}")))?;
Ok(Self { client })
}
}
#[cfg(feature = "native")]
fn error_chain(e: reqwest::Error) -> String {
format!("{:#}", anyhow::Error::from(e))
}
#[cfg(feature = "native")]
fn map_send_error(e: reqwest::Error, url: &str) -> TransportError {
if e.is_timeout() {
TransportError::Timeout(url.to_string())
} else if e.is_connect() || e.is_request() {
TransportError::Connect(error_chain(e))
} else {
TransportError::Other(error_chain(e))
}
}
#[cfg(feature = "native")]
async fn read_capped(mut resp: reqwest::Response, cap: usize) -> Result<Vec<u8>, TransportError> {
let mut buf: Vec<u8> = Vec::new();
if let Some(len) = resp.content_length() {
buf.reserve(len.min(cap as u64) as usize);
}
loop {
match resp.chunk().await {
Ok(Some(chunk)) => {
if push_capped(&mut buf, &chunk, cap) {
break;
}
}
Ok(None) => break,
Err(e) if e.is_timeout() => {
return Err(TransportError::Timeout(resp.url().to_string()))
}
Err(e) => return Err(TransportError::Other(error_chain(e))),
}
}
Ok(buf)
}
#[cfg(feature = "native")]
impl Transport for ReqwestTransport {
async fn send(&self, req: HttpRequest) -> Result<HttpResponse, TransportError> {
let mut builder = match req.method {
Method::Get => self.client.get(&req.url),
Method::Post => self.client.post(&req.url),
};
for (name, value) in &req.headers {
builder = builder.header(name.as_str(), value.as_str());
}
if let Some(body) = req.body {
builder = builder.body(body);
}
if let Some(timeout) = req.timeout {
builder = builder.timeout(timeout);
}
let resp = builder
.send()
.await
.map_err(|e| map_send_error(e, &req.url))?;
let status = resp.status().as_u16();
let headers = resp
.headers()
.iter()
.filter_map(|(k, v)| Some((k.as_str().to_string(), v.to_str().ok()?.to_string())))
.collect();
let body = match status {
300..=399 => Vec::new(),
400..=599 => read_capped(resp, ERROR_BODY_CAP).await?,
_ => read_capped(resp, MAX_BODY_BYTES).await?,
};
Ok(HttpResponse {
status,
headers,
body,
})
}
async fn sleep(&self, d: Duration) {
tokio::time::sleep(d).await;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn header_lookup_is_case_insensitive() {
let resp = HttpResponse {
status: 302,
headers: vec![("location".into(), "/next".into())],
body: Vec::new(),
};
assert_eq!(resp.header("Location"), Some("/next"));
assert_eq!(resp.header("content-type"), None);
}
#[test]
fn only_timeouts_and_connection_failures_are_transient() {
assert!(TransportError::Timeout("https://example.test/".into()).is_transient());
assert!(TransportError::Connect("refused".into()).is_transient());
assert!(!TransportError::Tls("unknown issuer".into()).is_transient());
assert!(!TransportError::BodyTooLarge.is_transient());
assert!(!TransportError::Other("bad header".into()).is_transient());
}
#[cfg(feature = "native")]
#[test]
fn native_transport_builds_from_default_tls() {
assert!(ReqwestTransport::new(&TlsConfig::default()).is_ok());
}
#[cfg(feature = "native")]
#[tokio::test]
async fn connection_refused_keeps_the_cause() {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let port = listener.local_addr().unwrap().port();
drop(listener);
let transport = ReqwestTransport::new(&TlsConfig::default()).unwrap();
let err = transport
.send(HttpRequest {
method: Method::Get,
url: format!("http://127.0.0.1:{port}/"),
headers: Vec::new(),
body: None,
timeout: Some(Duration::from_secs(5)),
})
.await
.unwrap_err();
assert!(matches!(err, TransportError::Connect(_)), "{err:?}");
assert!(err.to_string().contains("Connection refused"), "{err}");
}
}