Skip to main content

blocking_http/
lib.rs

1//! One blocking HTTP exchange, for callers on host request threads (the Hindsight client, the judge seat): a request,
2//! its status, its `retry-after` header and (for a 2xx answer) its body read up to a size limit, cancelled by a signal
3//! or a deadline. [`ReqwestTransport`] is the real one.
4
5use kernel::Signal;
6use std::sync::mpsc;
7use std::time::Duration;
8
9/// One request as the client builds it. `headers` are final (names lower-case, values validated).
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct HttpRequest {
12    pub method: String,
13    pub url: String,
14    pub headers: Vec<(String, String)>,
15    pub body: Option<Vec<u8>>,
16}
17
18/// A status, its `retry-after` header (as sent, any status) and, for a 2xx status only, the whole body (empty when
19/// there is none). A non-2xx body is never read.
20#[derive(Debug, Clone, PartialEq, Eq, Default)]
21pub struct HttpResponse {
22    pub status: u16,
23    pub retry_after: Option<String>,
24    pub body: Vec<u8>,
25}
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28pub enum TransportError {
29    /// The deadline passed before the body was read.
30    Timeout,
31    /// The signal was aborted before the body was read.
32    Aborted,
33    /// A 2xx answer declared, or streamed, more than the limit.
34    TooLarge,
35    /// No answer: connection, protocol or redirect (redirects are refused, not followed).
36    Failed,
37}
38
39/// Sends one request. A 2xx answer whose `content-length` (read as a JavaScript `Number`) exceeds `max_bytes`, or
40/// whose body grows past it, is [`TransportError::TooLarge`]; the deadline (`timeout` from the call) and the signal
41/// cover the whole exchange, body included.
42pub trait Transport: Send + Sync {
43    fn send(
44        &self,
45        request: HttpRequest,
46        max_bytes: usize,
47        timeout: Duration,
48        signal: &Signal,
49    ) -> Result<HttpResponse, TransportError>;
50}
51
52/// reqwest with rustls on a private one-thread runtime, so blocking callers (host request threads) never run inside
53/// another runtime. No proxy (as Node's fetch), no redirects, no decompression.
54pub struct ReqwestTransport {
55    client: reqwest::Client,
56    runtime: Option<tokio::runtime::Runtime>,
57}
58
59impl ReqwestTransport {
60    pub fn new() -> Result<Self, String> {
61        let runtime = tokio::runtime::Builder::new_multi_thread()
62            .worker_threads(1)
63            .thread_name("blocking-http")
64            .enable_all()
65            .build()
66            .map_err(|e| e.to_string())?;
67        let client = reqwest::Client::builder()
68            .no_proxy()
69            .redirect(reqwest::redirect::Policy::none())
70            .build()
71            .map_err(|e| e.to_string())?;
72        Ok(Self {
73            client,
74            runtime: Some(runtime),
75        })
76    }
77}
78
79impl Drop for ReqwestTransport {
80    fn drop(&mut self) {
81        if let Some(runtime) = self.runtime.take() {
82            runtime.shutdown_background();
83        }
84    }
85}
86
87/// `Number(length) > max` for a `content-length` value; the HTTP stack has already rejected malformed ones.
88fn declared_over(length: &str, max: usize) -> bool {
89    length.trim().parse::<f64>().is_ok_and(|n| n > max as f64)
90}
91
92async fn exchange(
93    client: reqwest::Client,
94    request: HttpRequest,
95    max_bytes: usize,
96) -> Result<HttpResponse, TransportError> {
97    let method = reqwest::Method::from_bytes(request.method.as_bytes()).map_err(|_| TransportError::Failed)?;
98    let mut builder = client.request(method, &request.url);
99    for (name, value) in &request.headers {
100        // Values are Latin-1 (validated by the client): sent as their byte values, as fetch's ByteString does.
101        let bytes: Vec<u8> = value.chars().map(|c| c as u32 as u8).collect();
102        let value = reqwest::header::HeaderValue::from_bytes(&bytes).map_err(|_| TransportError::Failed)?;
103        builder = builder.header(name.as_str(), value);
104    }
105    if let Some(body) = request.body {
106        builder = builder.body(body);
107    }
108    let mut response = builder.send().await.map_err(|_| TransportError::Failed)?;
109    let status = response.status().as_u16();
110    let retry_after = response
111        .headers()
112        .get(reqwest::header::RETRY_AFTER)
113        .map(|value| String::from_utf8_lossy(value.as_bytes()).into_owned());
114    if !(200..300).contains(&status) {
115        return Ok(HttpResponse {
116            status,
117            retry_after,
118            body: Vec::new(),
119        });
120    }
121    if let Some(length) = response.headers().get(reqwest::header::CONTENT_LENGTH)
122        && declared_over(&String::from_utf8_lossy(length.as_bytes()), max_bytes)
123    {
124        return Err(TransportError::TooLarge);
125    }
126    let mut body = Vec::new();
127    while let Some(chunk) = response.chunk().await.map_err(|_| TransportError::Failed)? {
128        if body.len() + chunk.len() > max_bytes {
129            return Err(TransportError::TooLarge);
130        }
131        body.extend_from_slice(&chunk);
132    }
133    Ok(HttpResponse {
134        status,
135        retry_after,
136        body,
137    })
138}
139
140impl Transport for ReqwestTransport {
141    fn send(
142        &self,
143        request: HttpRequest,
144        max_bytes: usize,
145        timeout: Duration,
146        signal: &Signal,
147    ) -> Result<HttpResponse, TransportError> {
148        let Some(runtime) = &self.runtime else {
149            return Err(TransportError::Failed);
150        };
151        if signal.is_aborted() {
152            return Err(TransportError::Aborted);
153        }
154        let (done, outcome) = mpsc::channel();
155        let task = runtime.spawn({
156            let client = self.client.clone();
157            async move {
158                let _ = done.send(exchange(client, request, max_bytes).await);
159            }
160        });
161        // An abort drops the exchange, and with it the sender this waits on.
162        let abort = task.abort_handle();
163        let _link = signal.link(move |_| abort.abort());
164        let result = match outcome.recv_timeout(timeout) {
165            Ok(result) => result,
166            Err(mpsc::RecvTimeoutError::Timeout) => Err(TransportError::Timeout),
167            Err(mpsc::RecvTimeoutError::Disconnected) if signal.is_aborted() => Err(TransportError::Aborted),
168            Err(mpsc::RecvTimeoutError::Disconnected) => Err(TransportError::Failed),
169        };
170        // Dropping the exchange closes its connection.
171        task.abort();
172        result
173    }
174}