Skip to main content

cpa_plugin/store/
doer.rs

1//! The production [`Doer`]: Go's `http.Client` for the configured proxy, sending one
2//! request per call (Go clones the client with `CheckRedirect` returning
3//! `ErrUseLastResponse`; the store follows redirects itself, re-checking auth and the
4//! rate limiter on every hop).
5
6use futures_util::StreamExt;
7use futures_util::future::BoxFuture;
8
9use super::auth::Headers;
10use super::client::{Doer, DoerResponse};
11
12pub struct WreqDoer {
13    client: wreq::Client,
14}
15
16impl WreqDoer {
17    /// `client` must not follow redirects (`cpa_exec::proxy::GoClients` clients do not).
18    pub fn new(client: wreq::Client) -> Self {
19        Self { client }
20    }
21}
22
23impl Doer for WreqDoer {
24    fn get(&self, url: &str, headers: &Headers) -> BoxFuture<'_, Result<DoerResponse, String>> {
25        let url = url.to_owned();
26        let mut go = cpa_exec::proxy::GoHeaders::new();
27        for (name, values) in headers {
28            for value in values {
29                go.add_raw(name, value.clone());
30            }
31        }
32        Box::pin(async move {
33            let request = self.client.get(&url).redirect(wreq::redirect::Policy::none());
34            // Go's transport asks for gzip itself and then decodes it transparently.
35            let (request, auto_gzip) = go.apply(request, None);
36            let response = request
37                .send()
38                .await
39                .map_err(|e| crate::hosthttp::transport_cause(&e, cpa_exec::xai_url::parse(&url).ok().as_ref()))?;
40            let status = response.status().as_u16();
41            let gzip = auto_gzip
42                && response
43                    .headers()
44                    .get(http::header::CONTENT_ENCODING)
45                    .and_then(|v| v.to_str().ok())
46                    .is_some_and(|v| v.eq_ignore_ascii_case("gzip"));
47            let mut out = Headers::new();
48            for (name, value) in response.headers() {
49                if gzip && (name == http::header::CONTENT_ENCODING || name == http::header::CONTENT_LENGTH) {
50                    continue;
51                }
52                out.entry(cpa_exec::proxy::canonical_header(name.as_str()))
53                    .or_default()
54                    .push(String::from_utf8_lossy(value.as_bytes()).into_owned());
55            }
56            let stream = response.bytes_stream().map(|r| r.map_err(std::io::Error::other));
57            let body = if gzip {
58                let reader = tokio::io::BufReader::new(tokio_util::io::StreamReader::new(stream));
59                let mut decoder = async_compression::tokio::bufread::GzipDecoder::new(reader);
60                decoder.multiple_members(true);
61                tokio_util::io::ReaderStream::new(decoder).boxed()
62            } else {
63                stream.boxed()
64            };
65            Ok(DoerResponse {
66                status,
67                headers: out,
68                body: body
69                    .map(|chunk| chunk.map_err(|e| crate::hosthttp::go_read_error(&e)))
70                    .boxed(),
71            })
72        })
73    }
74}