http_client.rs

  1mod async_body;
  2pub mod github;
  3
  4pub use anyhow::{anyhow, Result};
  5pub use async_body::{AsyncBody, Inner};
  6use derive_more::Deref;
  7pub use http::{self, Method, Request, Response, StatusCode, Uri};
  8
  9use futures::future::BoxFuture;
 10use http::request::Builder;
 11#[cfg(feature = "test-support")]
 12use std::fmt;
 13use std::{
 14    any::type_name,
 15    sync::{Arc, Mutex},
 16    time::Duration,
 17};
 18pub use url::Url;
 19
 20#[derive(Clone)]
 21pub struct ReadTimeout(pub Duration);
 22impl Default for ReadTimeout {
 23    fn default() -> Self {
 24        Self(Duration::from_secs(5))
 25    }
 26}
 27
 28#[derive(Default, Debug, Clone, PartialEq, Eq, Hash)]
 29
 30pub enum RedirectPolicy {
 31    #[default]
 32    NoFollow,
 33    FollowLimit(u32),
 34    FollowAll,
 35}
 36pub struct FollowRedirects(pub bool);
 37
 38pub trait HttpRequestExt {
 39    /// Set a read timeout on the request.
 40    /// For isahc, this is the low_speed_timeout.
 41    /// For other clients, this is the timeout used for read calls when reading the response.
 42    /// In all cases this prevents servers stalling completely, but allows them to send data slowly.
 43    fn read_timeout(self, timeout: Duration) -> Self;
 44    /// Whether or not to follow redirects
 45    fn follow_redirects(self, follow: RedirectPolicy) -> Self;
 46}
 47
 48impl HttpRequestExt for http::request::Builder {
 49    fn read_timeout(self, timeout: Duration) -> Self {
 50        self.extension(ReadTimeout(timeout))
 51    }
 52
 53    fn follow_redirects(self, follow: RedirectPolicy) -> Self {
 54        self.extension(follow)
 55    }
 56}
 57
 58pub trait HttpClient: 'static + Send + Sync {
 59    fn type_name(&self) -> &'static str;
 60
 61    fn send(
 62        &self,
 63        req: http::Request<AsyncBody>,
 64    ) -> BoxFuture<'static, Result<Response<AsyncBody>, anyhow::Error>>;
 65
 66    fn get<'a>(
 67        &'a self,
 68        uri: &str,
 69        body: AsyncBody,
 70        follow_redirects: bool,
 71    ) -> BoxFuture<'a, Result<Response<AsyncBody>, anyhow::Error>> {
 72        let request = Builder::new()
 73            .uri(uri)
 74            .follow_redirects(if follow_redirects {
 75                RedirectPolicy::FollowAll
 76            } else {
 77                RedirectPolicy::NoFollow
 78            })
 79            .body(body);
 80
 81        match request {
 82            Ok(request) => Box::pin(async move { self.send(request).await.map_err(Into::into) }),
 83            Err(e) => Box::pin(async move { Err(e.into()) }),
 84        }
 85    }
 86
 87    fn post_json<'a>(
 88        &'a self,
 89        uri: &str,
 90        body: AsyncBody,
 91    ) -> BoxFuture<'a, Result<Response<AsyncBody>, anyhow::Error>> {
 92        let request = Builder::new()
 93            .uri(uri)
 94            .method(Method::POST)
 95            .header("Content-Type", "application/json")
 96            .body(body);
 97
 98        match request {
 99            Ok(request) => Box::pin(async move { self.send(request).await.map_err(Into::into) }),
100            Err(e) => Box::pin(async move { Err(e.into()) }),
101        }
102    }
103
104    fn proxy(&self) -> Option<&Uri>;
105}
106
107/// An [`HttpClient`] that may have a proxy.
108#[derive(Deref)]
109pub struct HttpClientWithProxy {
110    #[deref]
111    client: Arc<dyn HttpClient>,
112    proxy: Option<Uri>,
113}
114
115impl HttpClientWithProxy {
116    /// Returns a new [`HttpClientWithProxy`] with the given proxy URL.
117    pub fn new(client: Arc<dyn HttpClient>, proxy_url: Option<String>) -> Self {
118        let proxy_uri = proxy_url
119            .and_then(|proxy| proxy.parse().ok())
120            .or_else(read_proxy_from_env);
121
122        Self::new_uri(client, proxy_uri)
123    }
124    pub fn new_uri(client: Arc<dyn HttpClient>, proxy_uri: Option<Uri>) -> Self {
125        Self {
126            client,
127            proxy: proxy_uri,
128        }
129    }
130}
131
132impl HttpClient for HttpClientWithProxy {
133    fn send(
134        &self,
135        req: Request<AsyncBody>,
136    ) -> BoxFuture<'static, Result<Response<AsyncBody>, anyhow::Error>> {
137        self.client.send(req)
138    }
139
140    fn proxy(&self) -> Option<&Uri> {
141        self.proxy.as_ref()
142    }
143
144    fn type_name(&self) -> &'static str {
145        self.client.type_name()
146    }
147}
148
149impl HttpClient for Arc<HttpClientWithProxy> {
150    fn send(
151        &self,
152        req: Request<AsyncBody>,
153    ) -> BoxFuture<'static, Result<Response<AsyncBody>, anyhow::Error>> {
154        self.client.send(req)
155    }
156
157    fn proxy(&self) -> Option<&Uri> {
158        self.proxy.as_ref()
159    }
160
161    fn type_name(&self) -> &'static str {
162        self.client.type_name()
163    }
164}
165
166/// An [`HttpClient`] that has a base URL.
167pub struct HttpClientWithUrl {
168    base_url: Mutex<String>,
169    client: HttpClientWithProxy,
170}
171
172impl std::ops::Deref for HttpClientWithUrl {
173    type Target = HttpClientWithProxy;
174
175    fn deref(&self) -> &Self::Target {
176        &self.client
177    }
178}
179
180impl HttpClientWithUrl {
181    /// Returns a new [`HttpClientWithUrl`] with the given base URL.
182    pub fn new(
183        client: Arc<dyn HttpClient>,
184        base_url: impl Into<String>,
185        proxy_url: Option<String>,
186    ) -> Self {
187        let client = HttpClientWithProxy::new(client, proxy_url);
188
189        Self {
190            base_url: Mutex::new(base_url.into()),
191            client,
192        }
193    }
194
195    pub fn new_uri(
196        client: Arc<dyn HttpClient>,
197        base_url: impl Into<String>,
198        proxy_uri: Option<Uri>,
199    ) -> Self {
200        let client = HttpClientWithProxy::new_uri(client, proxy_uri);
201
202        Self {
203            base_url: Mutex::new(base_url.into()),
204            client,
205        }
206    }
207
208    /// Returns the base URL.
209    pub fn base_url(&self) -> String {
210        self.base_url
211            .lock()
212            .map_or_else(|_| Default::default(), |url| url.clone())
213    }
214
215    /// Sets the base URL.
216    pub fn set_base_url(&self, base_url: impl Into<String>) {
217        let base_url = base_url.into();
218        self.base_url
219            .lock()
220            .map(|mut url| {
221                *url = base_url;
222            })
223            .ok();
224    }
225
226    /// Builds a URL using the given path.
227    pub fn build_url(&self, path: &str) -> String {
228        format!("{}{}", self.base_url(), path)
229    }
230
231    /// Builds a Zed API URL using the given path.
232    pub fn build_zed_api_url(&self, path: &str, query: &[(&str, &str)]) -> Result<Url> {
233        let base_url = self.base_url();
234        let base_api_url = match base_url.as_ref() {
235            "https://zed.dev" => "https://api.zed.dev",
236            "https://staging.zed.dev" => "https://api-staging.zed.dev",
237            "http://localhost:3000" => "http://localhost:8080",
238            other => other,
239        };
240
241        Ok(Url::parse_with_params(
242            &format!("{}{}", base_api_url, path),
243            query,
244        )?)
245    }
246
247    /// Builds a Zed LLM URL using the given path.
248    pub fn build_zed_llm_url(&self, path: &str, query: &[(&str, &str)]) -> Result<Url> {
249        let base_url = self.base_url();
250        let base_api_url = match base_url.as_ref() {
251            "https://zed.dev" => "https://llm.zed.dev",
252            "https://staging.zed.dev" => "https://llm-staging.zed.dev",
253            "http://localhost:3000" => "http://localhost:8080",
254            other => other,
255        };
256
257        Ok(Url::parse_with_params(
258            &format!("{}{}", base_api_url, path),
259            query,
260        )?)
261    }
262}
263
264impl HttpClient for Arc<HttpClientWithUrl> {
265    fn send(
266        &self,
267        req: Request<AsyncBody>,
268    ) -> BoxFuture<'static, Result<Response<AsyncBody>, anyhow::Error>> {
269        self.client.send(req)
270    }
271
272    fn proxy(&self) -> Option<&Uri> {
273        self.client.proxy.as_ref()
274    }
275
276    fn type_name(&self) -> &'static str {
277        self.client.type_name()
278    }
279}
280
281impl HttpClient for HttpClientWithUrl {
282    fn send(
283        &self,
284        req: Request<AsyncBody>,
285    ) -> BoxFuture<'static, Result<Response<AsyncBody>, anyhow::Error>> {
286        self.client.send(req)
287    }
288
289    fn proxy(&self) -> Option<&Uri> {
290        self.client.proxy.as_ref()
291    }
292
293    fn type_name(&self) -> &'static str {
294        self.client.type_name()
295    }
296}
297
298pub fn read_proxy_from_env() -> Option<Uri> {
299    const ENV_VARS: &[&str] = &[
300        "ALL_PROXY",
301        "all_proxy",
302        "HTTPS_PROXY",
303        "https_proxy",
304        "HTTP_PROXY",
305        "http_proxy",
306    ];
307
308    for var in ENV_VARS {
309        if let Ok(env) = std::env::var(var) {
310            return env.parse::<Uri>().ok();
311        }
312    }
313
314    None
315}
316
317pub struct BlockedHttpClient;
318
319impl HttpClient for BlockedHttpClient {
320    fn send(
321        &self,
322        _req: Request<AsyncBody>,
323    ) -> BoxFuture<'static, Result<Response<AsyncBody>, anyhow::Error>> {
324        Box::pin(async {
325            Err(std::io::Error::new(
326                std::io::ErrorKind::PermissionDenied,
327                "BlockedHttpClient disallowed request",
328            )
329            .into())
330        })
331    }
332
333    fn proxy(&self) -> Option<&Uri> {
334        None
335    }
336
337    fn type_name(&self) -> &'static str {
338        type_name::<Self>()
339    }
340}
341
342#[cfg(feature = "test-support")]
343type FakeHttpHandler = Box<
344    dyn Fn(Request<AsyncBody>) -> BoxFuture<'static, Result<Response<AsyncBody>, anyhow::Error>>
345        + Send
346        + Sync
347        + 'static,
348>;
349
350#[cfg(feature = "test-support")]
351pub struct FakeHttpClient {
352    handler: FakeHttpHandler,
353}
354
355#[cfg(feature = "test-support")]
356impl FakeHttpClient {
357    pub fn create<Fut, F>(handler: F) -> Arc<HttpClientWithUrl>
358    where
359        Fut: futures::Future<Output = Result<Response<AsyncBody>, anyhow::Error>> + Send + 'static,
360        F: Fn(Request<AsyncBody>) -> Fut + Send + Sync + 'static,
361    {
362        Arc::new(HttpClientWithUrl {
363            base_url: Mutex::new("http://test.example".into()),
364            client: HttpClientWithProxy {
365                client: Arc::new(Self {
366                    handler: Box::new(move |req| Box::pin(handler(req))),
367                }),
368                proxy: None,
369            },
370        })
371    }
372
373    pub fn with_404_response() -> Arc<HttpClientWithUrl> {
374        Self::create(|_| async move {
375            Ok(Response::builder()
376                .status(404)
377                .body(Default::default())
378                .unwrap())
379        })
380    }
381
382    pub fn with_200_response() -> Arc<HttpClientWithUrl> {
383        Self::create(|_| async move {
384            Ok(Response::builder()
385                .status(200)
386                .body(Default::default())
387                .unwrap())
388        })
389    }
390}
391
392#[cfg(feature = "test-support")]
393impl fmt::Debug for FakeHttpClient {
394    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
395        f.debug_struct("FakeHttpClient").finish()
396    }
397}
398
399#[cfg(feature = "test-support")]
400impl HttpClient for FakeHttpClient {
401    fn send(
402        &self,
403        req: Request<AsyncBody>,
404    ) -> BoxFuture<'static, Result<Response<AsyncBody>, anyhow::Error>> {
405        let future = (self.handler)(req);
406        future
407    }
408
409    fn proxy(&self) -> Option<&Uri> {
410        None
411    }
412
413    fn type_name(&self) -> &'static str {
414        type_name::<Self>()
415    }
416}