reqwest_client.rs

  1use std::error::Error;
  2use std::sync::{LazyLock, OnceLock};
  3use std::{borrow::Cow, mem, pin::Pin, task::Poll, time::Duration};
  4
  5use anyhow::anyhow;
  6use bytes::{BufMut, Bytes, BytesMut};
  7use futures::{AsyncRead, FutureExt as _, TryStreamExt as _};
  8use http_client::{RedirectPolicy, Url, http};
  9use regex::Regex;
 10use reqwest::{
 11    header::{HeaderMap, HeaderValue},
 12    redirect,
 13};
 14
 15const DEFAULT_CAPACITY: usize = 4096;
 16static RUNTIME: OnceLock<tokio::runtime::Runtime> = OnceLock::new();
 17static REDACT_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"key=[^&]+").unwrap());
 18
 19pub struct ReqwestClient {
 20    client: reqwest::Client,
 21    proxy: Option<Url>,
 22    user_agent: Option<HeaderValue>,
 23    handle: tokio::runtime::Handle,
 24}
 25
 26impl ReqwestClient {
 27    fn builder() -> reqwest::ClientBuilder {
 28        reqwest::Client::builder()
 29            .use_rustls_tls()
 30            .connect_timeout(Duration::from_secs(10))
 31    }
 32
 33    pub fn new() -> Self {
 34        Self::builder()
 35            .build()
 36            .expect("Failed to initialize HTTP client")
 37            .into()
 38    }
 39
 40    pub fn user_agent(agent: &str) -> anyhow::Result<Self> {
 41        let mut map = HeaderMap::new();
 42        map.insert(http::header::USER_AGENT, HeaderValue::from_str(agent)?);
 43        let client = Self::builder().default_headers(map).build()?;
 44        Ok(client.into())
 45    }
 46
 47    pub fn proxy_and_user_agent(proxy: Option<Url>, user_agent: &str) -> anyhow::Result<Self> {
 48        let user_agent = HeaderValue::from_str(user_agent)?;
 49
 50        let mut map = HeaderMap::new();
 51        map.insert(http::header::USER_AGENT, user_agent.clone());
 52        let mut client = Self::builder().default_headers(map);
 53        let client_has_proxy;
 54
 55        if let Some(proxy) = proxy.as_ref().and_then(|proxy_url| {
 56            reqwest::Proxy::all(proxy_url.clone())
 57                .inspect_err(|e| {
 58                    log::error!(
 59                        "Failed to parse proxy URL '{}': {}",
 60                        proxy_url,
 61                        e.source().unwrap_or(&e as &_)
 62                    )
 63                })
 64                .ok()
 65        }) {
 66            // Respect NO_PROXY env var
 67            client = client.proxy(proxy.no_proxy(reqwest::NoProxy::from_env()));
 68            client_has_proxy = true;
 69        } else {
 70            client_has_proxy = false;
 71        };
 72
 73        let client = client
 74            .use_preconfigured_tls(http_client_tls::tls_config())
 75            .build()?;
 76        let mut client: ReqwestClient = client.into();
 77        client.proxy = client_has_proxy.then_some(proxy).flatten();
 78        client.user_agent = Some(user_agent);
 79        Ok(client)
 80    }
 81}
 82
 83pub fn runtime() -> &'static tokio::runtime::Runtime {
 84    RUNTIME.get_or_init(|| {
 85        tokio::runtime::Builder::new_multi_thread()
 86            // Since we now have two executors, let's try to keep our footprint small
 87            .worker_threads(1)
 88            .enable_all()
 89            .build()
 90            .expect("Failed to initialize HTTP client")
 91    })
 92}
 93
 94impl From<reqwest::Client> for ReqwestClient {
 95    fn from(client: reqwest::Client) -> Self {
 96        let handle = tokio::runtime::Handle::try_current().unwrap_or_else(|_| {
 97            log::debug!("no tokio runtime found, creating one for Reqwest...");
 98            runtime().handle().clone()
 99        });
100        Self {
101            client,
102            handle,
103            proxy: None,
104            user_agent: None,
105        }
106    }
107}
108
109// This struct is essentially a re-implementation of
110// https://docs.rs/tokio-util/0.7.12/tokio_util/io/struct.ReaderStream.html
111// except outside of Tokio's aegis
112struct StreamReader {
113    reader: Option<Pin<Box<dyn futures::AsyncRead + Send + Sync>>>,
114    buf: BytesMut,
115    capacity: usize,
116}
117
118impl StreamReader {
119    fn new(reader: Pin<Box<dyn futures::AsyncRead + Send + Sync>>) -> Self {
120        Self {
121            reader: Some(reader),
122            buf: BytesMut::new(),
123            capacity: DEFAULT_CAPACITY,
124        }
125    }
126}
127
128impl futures::Stream for StreamReader {
129    type Item = std::io::Result<Bytes>;
130
131    fn poll_next(
132        mut self: Pin<&mut Self>,
133        cx: &mut std::task::Context<'_>,
134    ) -> Poll<Option<Self::Item>> {
135        let mut this = self.as_mut();
136
137        let mut reader = match this.reader.take() {
138            Some(r) => r,
139            None => return Poll::Ready(None),
140        };
141
142        if this.buf.capacity() == 0 {
143            let capacity = this.capacity;
144            this.buf.reserve(capacity);
145        }
146
147        match poll_read_buf(&mut reader, cx, &mut this.buf) {
148            Poll::Pending => Poll::Pending,
149            Poll::Ready(Err(err)) => {
150                self.reader = None;
151
152                Poll::Ready(Some(Err(err)))
153            }
154            Poll::Ready(Ok(0)) => {
155                self.reader = None;
156                Poll::Ready(None)
157            }
158            Poll::Ready(Ok(_)) => {
159                let chunk = this.buf.split();
160                self.reader = Some(reader);
161                Poll::Ready(Some(Ok(chunk.freeze())))
162            }
163        }
164    }
165}
166
167/// Implementation from <https://docs.rs/tokio-util/0.7.12/src/tokio_util/util/poll_buf.rs.html>
168/// Specialized for this use case
169pub fn poll_read_buf(
170    io: &mut Pin<Box<dyn futures::AsyncRead + Send + Sync>>,
171    cx: &mut std::task::Context<'_>,
172    buf: &mut BytesMut,
173) -> Poll<std::io::Result<usize>> {
174    if !buf.has_remaining_mut() {
175        return Poll::Ready(Ok(0));
176    }
177
178    let n = {
179        let dst = buf.chunk_mut();
180
181        // Safety: `chunk_mut()` returns a `&mut UninitSlice`, and `UninitSlice` is a
182        // transparent wrapper around `[MaybeUninit<u8>]`.
183        let dst = unsafe { &mut *(dst as *mut _ as *mut [std::mem::MaybeUninit<u8>]) };
184        let mut buf = tokio::io::ReadBuf::uninit(dst);
185        let ptr = buf.filled().as_ptr();
186        let unfilled_portion = buf.initialize_unfilled();
187        // SAFETY: Pin projection
188        let io_pin = unsafe { Pin::new_unchecked(io) };
189        std::task::ready!(io_pin.poll_read(cx, unfilled_portion)?);
190
191        // Ensure the pointer does not change from under us
192        assert_eq!(ptr, buf.filled().as_ptr());
193        buf.filled().len()
194    };
195
196    // Safety: This is guaranteed to be the number of initialized (and read)
197    // bytes due to the invariants provided by `ReadBuf::filled`.
198    unsafe {
199        buf.advance_mut(n);
200    }
201
202    Poll::Ready(Ok(n))
203}
204
205fn redact_error(mut error: reqwest::Error) -> reqwest::Error {
206    if let Some(url) = error.url_mut()
207        && let Some(query) = url.query()
208        && let Cow::Owned(redacted) = REDACT_REGEX.replace_all(query, "key=REDACTED")
209    {
210        url.set_query(Some(redacted.as_str()));
211    }
212    error
213}
214
215impl http_client::HttpClient for ReqwestClient {
216    fn proxy(&self) -> Option<&Url> {
217        self.proxy.as_ref()
218    }
219
220    fn user_agent(&self) -> Option<&HeaderValue> {
221        self.user_agent.as_ref()
222    }
223
224    fn send(
225        &self,
226        req: http::Request<http_client::AsyncBody>,
227    ) -> futures::future::BoxFuture<
228        'static,
229        anyhow::Result<http_client::Response<http_client::AsyncBody>>,
230    > {
231        let (parts, body) = req.into_parts();
232
233        let mut request = self.client.request(parts.method, parts.uri.to_string());
234        request = request.headers(parts.headers);
235        if let Some(redirect_policy) = parts.extensions.get::<RedirectPolicy>() {
236            request = request.redirect_policy(match redirect_policy {
237                RedirectPolicy::NoFollow => redirect::Policy::none(),
238                RedirectPolicy::FollowLimit(limit) => redirect::Policy::limited(*limit as usize),
239                RedirectPolicy::FollowAll => redirect::Policy::limited(100),
240            });
241        }
242        let request = request.body(match body.0 {
243            http_client::Inner::Empty => reqwest::Body::default(),
244            http_client::Inner::Bytes(cursor) => cursor.into_inner().into(),
245            http_client::Inner::AsyncReader(stream) => {
246                reqwest::Body::wrap_stream(StreamReader::new(stream))
247            }
248        });
249
250        let handle = self.handle.clone();
251        async move {
252            let mut response = handle
253                .spawn(async { request.send().await })
254                .await?
255                .map_err(redact_error)?;
256
257            let headers = mem::take(response.headers_mut());
258            let mut builder = http::Response::builder()
259                .status(response.status().as_u16())
260                .version(response.version());
261            *builder.headers_mut().unwrap() = headers;
262
263            let bytes = response
264                .bytes_stream()
265                .map_err(futures::io::Error::other)
266                .into_async_read();
267            let body = http_client::AsyncBody::from_reader(bytes);
268
269            builder.body(body).map_err(|e| anyhow!(e))
270        }
271        .boxed()
272    }
273
274    fn send_multipart_form<'a>(
275        &'a self,
276        url: &str,
277        form: reqwest::multipart::Form,
278    ) -> futures::future::BoxFuture<'a, anyhow::Result<http_client::Response<http_client::AsyncBody>>>
279    {
280        let response = self.client.post(url).multipart(form).send();
281        self.handle
282            .spawn(async move {
283                let response = response.await?;
284                let mut builder = http::response::Builder::new().status(response.status());
285                for (k, v) in response.headers() {
286                    builder = builder.header(k, v)
287                }
288                Ok(builder.body(response.bytes().await?.into())?)
289            })
290            .map(|e| e?)
291            .boxed()
292    }
293}
294
295#[cfg(test)]
296mod tests {
297    use http_client::{HttpClient, Url};
298
299    use crate::ReqwestClient;
300
301    #[test]
302    fn test_proxy_uri() {
303        let client = ReqwestClient::new();
304        assert_eq!(client.proxy(), None);
305
306        let proxy = Url::parse("http://localhost:10809").unwrap();
307        let client = ReqwestClient::proxy_and_user_agent(Some(proxy.clone()), "test").unwrap();
308        assert_eq!(client.proxy(), Some(&proxy));
309
310        let proxy = Url::parse("https://localhost:10809").unwrap();
311        let client = ReqwestClient::proxy_and_user_agent(Some(proxy.clone()), "test").unwrap();
312        assert_eq!(client.proxy(), Some(&proxy));
313
314        let proxy = Url::parse("socks4://localhost:10808").unwrap();
315        let client = ReqwestClient::proxy_and_user_agent(Some(proxy.clone()), "test").unwrap();
316        assert_eq!(client.proxy(), Some(&proxy));
317
318        let proxy = Url::parse("socks4a://localhost:10808").unwrap();
319        let client = ReqwestClient::proxy_and_user_agent(Some(proxy.clone()), "test").unwrap();
320        assert_eq!(client.proxy(), Some(&proxy));
321
322        let proxy = Url::parse("socks5://localhost:10808").unwrap();
323        let client = ReqwestClient::proxy_and_user_agent(Some(proxy.clone()), "test").unwrap();
324        assert_eq!(client.proxy(), Some(&proxy));
325
326        let proxy = Url::parse("socks5h://localhost:10808").unwrap();
327        let client = ReqwestClient::proxy_and_user_agent(Some(proxy.clone()), "test").unwrap();
328        assert_eq!(client.proxy(), Some(&proxy));
329    }
330
331    #[test]
332    fn test_invalid_proxy_uri() {
333        let proxy = Url::parse("socks://127.0.0.1:20170").unwrap();
334        let client = ReqwestClient::proxy_and_user_agent(Some(proxy), "test").unwrap();
335        assert!(
336            client.proxy.is_none(),
337            "An invalid proxy URL should add no proxy to the client!"
338        )
339    }
340}