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