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;
 11use rustls::ClientConfig;
 12use rustls_platform_verifier::ConfigVerifierExt;
 13#[cfg(feature = "test-support")]
 14use std::fmt;
 15use std::{
 16    any::type_name,
 17    sync::{Arc, Mutex, OnceLock},
 18};
 19pub use url::Url;
 20
 21static TLS_CONFIG: OnceLock<rustls::ClientConfig> = OnceLock::new();
 22
 23pub fn tls_config() -> ClientConfig {
 24    TLS_CONFIG
 25        .get_or_init(|| {
 26            // rustls uses the `aws_lc_rs` provider by default
 27            // This only errors if the default provider has already
 28            // been installed. We can ignore this `Result`.
 29            rustls::crypto::aws_lc_rs::default_provider()
 30                .install_default()
 31                .ok();
 32
 33            ClientConfig::with_platform_verifier()
 34        })
 35        .clone()
 36}
 37
 38#[derive(Default, Debug, Clone, PartialEq, Eq, Hash)]
 39pub enum RedirectPolicy {
 40    #[default]
 41    NoFollow,
 42    FollowLimit(u32),
 43    FollowAll,
 44}
 45pub struct FollowRedirects(pub bool);
 46
 47pub trait HttpRequestExt {
 48    /// Whether or not to follow redirects
 49    fn follow_redirects(self, follow: RedirectPolicy) -> Self;
 50}
 51
 52impl HttpRequestExt for http::request::Builder {
 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 }),
 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 }),
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 BlockedHttpClient {
320    pub fn new() -> Self {
321        BlockedHttpClient
322    }
323}
324
325impl HttpClient for BlockedHttpClient {
326    fn send(
327        &self,
328        _req: Request<AsyncBody>,
329    ) -> BoxFuture<'static, Result<Response<AsyncBody>, anyhow::Error>> {
330        Box::pin(async {
331            Err(std::io::Error::new(
332                std::io::ErrorKind::PermissionDenied,
333                "BlockedHttpClient disallowed request",
334            )
335            .into())
336        })
337    }
338
339    fn proxy(&self) -> Option<&Uri> {
340        None
341    }
342
343    fn type_name(&self) -> &'static str {
344        type_name::<Self>()
345    }
346}
347
348#[cfg(feature = "test-support")]
349type FakeHttpHandler = Box<
350    dyn Fn(Request<AsyncBody>) -> BoxFuture<'static, Result<Response<AsyncBody>, anyhow::Error>>
351        + Send
352        + Sync
353        + 'static,
354>;
355
356#[cfg(feature = "test-support")]
357pub struct FakeHttpClient {
358    handler: FakeHttpHandler,
359}
360
361#[cfg(feature = "test-support")]
362impl FakeHttpClient {
363    pub fn create<Fut, F>(handler: F) -> Arc<HttpClientWithUrl>
364    where
365        Fut: futures::Future<Output = Result<Response<AsyncBody>, anyhow::Error>> + Send + 'static,
366        F: Fn(Request<AsyncBody>) -> Fut + Send + Sync + 'static,
367    {
368        Arc::new(HttpClientWithUrl {
369            base_url: Mutex::new("http://test.example".into()),
370            client: HttpClientWithProxy {
371                client: Arc::new(Self {
372                    handler: Box::new(move |req| Box::pin(handler(req))),
373                }),
374                proxy: None,
375            },
376        })
377    }
378
379    pub fn with_404_response() -> Arc<HttpClientWithUrl> {
380        Self::create(|_| async move {
381            Ok(Response::builder()
382                .status(404)
383                .body(Default::default())
384                .unwrap())
385        })
386    }
387
388    pub fn with_200_response() -> Arc<HttpClientWithUrl> {
389        Self::create(|_| async move {
390            Ok(Response::builder()
391                .status(200)
392                .body(Default::default())
393                .unwrap())
394        })
395    }
396}
397
398#[cfg(feature = "test-support")]
399impl fmt::Debug for FakeHttpClient {
400    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
401        f.debug_struct("FakeHttpClient").finish()
402    }
403}
404
405#[cfg(feature = "test-support")]
406impl HttpClient for FakeHttpClient {
407    fn send(
408        &self,
409        req: Request<AsyncBody>,
410    ) -> BoxFuture<'static, Result<Response<AsyncBody>, anyhow::Error>> {
411        let future = (self.handler)(req);
412        future
413    }
414
415    fn proxy(&self) -> Option<&Uri> {
416        None
417    }
418
419    fn type_name(&self) -> &'static str {
420        type_name::<Self>()
421    }
422}