Skip to main content

odp_directory/
transport.rs

1use std::{collections::BTreeMap, net::SocketAddr, sync::Arc, time::Duration};
2
3use async_trait::async_trait;
4use thiserror::Error;
5
6/// How long a single exchange may take before it is abandoned.
7///
8/// Nothing in ODP obliges a peer to answer, so without a deadline a half-open connection holds a
9/// task for as long as the peer cares to keep it.
10const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);
11
12/// The most any ODP response may weigh, whatever it is.
13///
14/// Every caller applies its own tighter limit (ERR-21), but those are applied to a body already in
15/// memory. This one is applied while reading, so a peer that answers with an endless body is cut
16/// off rather than allowed to exhaust the process.
17const MAXIMUM_TRANSPORT_BYTES: usize = 2 * 1024 * 1024;
18
19#[derive(Clone, Debug, Eq, PartialEq)]
20pub struct HttpRequest {
21    pub body: Vec<u8>,
22    pub headers: BTreeMap<String, String>,
23    pub method: String,
24    pub url: String,
25}
26
27#[derive(Clone, Debug, Eq, PartialEq)]
28pub struct HttpResponse {
29    pub body: Vec<u8>,
30    pub headers: BTreeMap<String, String>,
31    pub status: u16,
32}
33
34#[derive(Debug, Error)]
35#[error("HTTP transport failed: {message}")]
36pub struct TransportError {
37    pub message: String,
38}
39
40#[async_trait]
41pub trait Transport: Send + Sync {
42    async fn send(&self, request: HttpRequest) -> Result<HttpResponse, TransportError>;
43
44    async fn send_limited(
45        &self,
46        request: HttpRequest,
47        maximum_bytes: usize,
48    ) -> Result<HttpResponse, TransportError> {
49        let response = self.send(request).await?;
50        check_size(
51            response.body.len(),
52            response_limit(response.status, maximum_bytes),
53        )?;
54        Ok(response)
55    }
56
57    /// Connects only to the supplied addresses, without a proxy or a second DNS lookup.
58    /// Implementations must also verify the connected peer before consuming its response.
59    async fn send_to(
60        &self,
61        _request: HttpRequest,
62        _addresses: &[SocketAddr],
63        _maximum_bytes: usize,
64    ) -> Result<HttpResponse, TransportError> {
65        Err(TransportError {
66            message: "Transport does not support pinned destinations".to_owned(),
67        })
68    }
69}
70
71#[derive(Clone)]
72pub struct ReqwestTransport {
73    client: reqwest::Client,
74    timeout: Duration,
75}
76
77impl ReqwestTransport {
78    pub fn new() -> Result<Self, TransportError> {
79        Self::with_timeout(DEFAULT_TIMEOUT)
80    }
81
82    /// A transport that abandons an exchange taking longer than `timeout`.
83    pub fn with_timeout(timeout: Duration) -> Result<Self, TransportError> {
84        let client = reqwest::Client::builder()
85            .redirect(reqwest::redirect::Policy::none())
86            .timeout(timeout)
87            .build()
88            .map_err(|error| TransportError {
89                message: error.to_string(),
90            })?;
91        Ok(Self { client, timeout })
92    }
93}
94
95#[async_trait]
96impl Transport for ReqwestTransport {
97    async fn send(&self, request: HttpRequest) -> Result<HttpResponse, TransportError> {
98        self.send_limited(request, MAXIMUM_TRANSPORT_BYTES).await
99    }
100
101    async fn send_limited(
102        &self,
103        request: HttpRequest,
104        maximum_bytes: usize,
105    ) -> Result<HttpResponse, TransportError> {
106        exchange(&self.client, request, maximum_bytes, None).await
107    }
108
109    async fn send_to(
110        &self,
111        request: HttpRequest,
112        addresses: &[SocketAddr],
113        maximum_bytes: usize,
114    ) -> Result<HttpResponse, TransportError> {
115        let url = reqwest::Url::parse(&request.url).map_err(|error| TransportError {
116            message: error.to_string(),
117        })?;
118        let host = url.host_str().ok_or_else(|| TransportError {
119            message: "Missing destination host".to_owned(),
120        })?;
121        if addresses.is_empty() {
122            return Err(TransportError {
123                message: "Missing pinned destination addresses".to_owned(),
124            });
125        }
126        let client = reqwest::Client::builder()
127            .redirect(reqwest::redirect::Policy::none())
128            .no_proxy()
129            .retry(reqwest::retry::never())
130            .timeout(self.timeout)
131            .resolve_to_addrs(host, addresses)
132            .build()
133            .map_err(|error| TransportError {
134                message: error.to_string(),
135            })?;
136        exchange(&client, request, maximum_bytes, Some(addresses)).await
137    }
138}
139
140async fn exchange(
141    client: &reqwest::Client,
142    request: HttpRequest,
143    maximum_bytes: usize,
144    addresses: Option<&[SocketAddr]>,
145) -> Result<HttpResponse, TransportError> {
146    let method =
147        reqwest::Method::from_bytes(request.method.as_bytes()).map_err(|error| TransportError {
148            message: error.to_string(),
149        })?;
150    let mut builder = client.request(method, request.url).body(request.body);
151    for (name, value) in request.headers {
152        builder = builder.header(&name, &value);
153    }
154    let response = builder.send().await.map_err(|error| TransportError {
155        message: error.to_string(),
156    })?;
157    let status = response.status().as_u16();
158    if addresses.is_some_and(|addresses| {
159        response
160            .remote_addr()
161            .is_none_or(|peer| !addresses.contains(&peer))
162    }) {
163        return Err(TransportError {
164            message: "Connected peer is not a pinned destination".to_owned(),
165        });
166    }
167    let maximum_bytes = response_limit(status, maximum_bytes.min(MAXIMUM_TRANSPORT_BYTES));
168    let headers = response
169        .headers()
170        .iter()
171        .filter_map(|(name, value)| {
172            value
173                .to_str()
174                .ok()
175                .map(|value| (name.as_str().to_ascii_lowercase(), value.to_owned()))
176        })
177        .collect();
178    // ERR-20: a declared length past the limit is refused before the body is read at all.
179    if response
180        .content_length()
181        .is_some_and(|value| value > maximum_bytes as u64)
182    {
183        return Err(TransportError {
184            message: "ODP response declares more than the transport will read".to_owned(),
185        });
186    }
187    let mut body = Vec::new();
188    let mut response = response;
189    while let Some(chunk) = response.chunk().await.map_err(|error| TransportError {
190        message: error.to_string(),
191    })? {
192        if body.len() + chunk.len() > maximum_bytes {
193            return Err(TransportError {
194                message: "ODP response exceeds what the transport will read".to_owned(),
195            });
196        }
197        body.extend_from_slice(&chunk);
198    }
199    Ok(HttpResponse {
200        body,
201        headers,
202        status,
203    })
204}
205
206fn response_limit(status: u16, maximum_bytes: usize) -> usize {
207    if status >= 400 {
208        maximum_bytes.min(16_384)
209    } else {
210        maximum_bytes
211    }
212}
213
214fn check_size(size: usize, maximum_bytes: usize) -> Result<(), TransportError> {
215    if size > maximum_bytes {
216        Err(TransportError {
217            message: "Response exceeds its byte limit".to_owned(),
218        })
219    } else {
220        Ok(())
221    }
222}
223
224pub(crate) fn default_transport() -> Result<Arc<dyn Transport>, TransportError> {
225    Ok(Arc::new(ReqwestTransport::new()?))
226}