http_client.rs

  1mod async_body;
  2pub mod github;
  3pub mod github_download;
  4
  5pub use anyhow::{Result, anyhow};
  6pub use async_body::{AsyncBody, Inner};
  7use derive_more::Deref;
  8use http::HeaderValue;
  9pub use http::{self, Method, Request, Response, StatusCode, Uri, request::Builder};
 10
 11use futures::{
 12    FutureExt as _,
 13    future::{self, BoxFuture},
 14};
 15use parking_lot::Mutex;
 16use serde::Serialize;
 17#[cfg(feature = "test-support")]
 18use std::fmt;
 19use std::{any::type_name, sync::Arc};
 20pub use url::{Host, Url};
 21
 22#[derive(Default, Debug, Clone, PartialEq, Eq, Hash)]
 23pub enum RedirectPolicy {
 24    #[default]
 25    NoFollow,
 26    FollowLimit(u32),
 27    FollowAll,
 28}
 29pub struct FollowRedirects(pub bool);
 30
 31pub trait HttpRequestExt {
 32    /// Conditionally modify self with the given closure.
 33    fn when(self, condition: bool, then: impl FnOnce(Self) -> Self) -> Self
 34    where
 35        Self: Sized,
 36    {
 37        if condition { then(self) } else { self }
 38    }
 39
 40    /// Conditionally unwrap and modify self with the given closure, if the given option is Some.
 41    fn when_some<T>(self, option: Option<T>, then: impl FnOnce(Self, T) -> Self) -> Self
 42    where
 43        Self: Sized,
 44    {
 45        match option {
 46            Some(value) => then(self, value),
 47            None => self,
 48        }
 49    }
 50
 51    /// Whether or not to follow redirects
 52    fn follow_redirects(self, follow: RedirectPolicy) -> Self;
 53}
 54
 55impl HttpRequestExt for http::request::Builder {
 56    fn follow_redirects(self, follow: RedirectPolicy) -> Self {
 57        self.extension(follow)
 58    }
 59}
 60
 61pub trait HttpClient: 'static + Send + Sync {
 62    fn type_name(&self) -> &'static str;
 63
 64    fn user_agent(&self) -> Option<&HeaderValue>;
 65
 66    fn send(
 67        &self,
 68        req: http::Request<AsyncBody>,
 69    ) -> BoxFuture<'static, anyhow::Result<Response<AsyncBody>>>;
 70
 71    fn get(
 72        &self,
 73        uri: &str,
 74        body: AsyncBody,
 75        follow_redirects: bool,
 76    ) -> BoxFuture<'static, anyhow::Result<Response<AsyncBody>>> {
 77        let request = Builder::new()
 78            .uri(uri)
 79            .follow_redirects(if follow_redirects {
 80                RedirectPolicy::FollowAll
 81            } else {
 82                RedirectPolicy::NoFollow
 83            })
 84            .body(body);
 85
 86        match request {
 87            Ok(request) => self.send(request),
 88            Err(e) => Box::pin(async move { Err(e.into()) }),
 89        }
 90    }
 91
 92    fn post_json(
 93        &self,
 94        uri: &str,
 95        body: AsyncBody,
 96    ) -> BoxFuture<'static, anyhow::Result<Response<AsyncBody>>> {
 97        let request = Builder::new()
 98            .uri(uri)
 99            .method(Method::POST)
100            .header("Content-Type", "application/json")
101            .body(body);
102
103        match request {
104            Ok(request) => self.send(request),
105            Err(e) => Box::pin(async move { Err(e.into()) }),
106        }
107    }
108
109    fn proxy(&self) -> Option<&Url>;
110
111    #[cfg(feature = "test-support")]
112    fn as_fake(&self) -> &FakeHttpClient {
113        panic!("called as_fake on {}", type_name::<Self>())
114    }
115
116    fn send_multipart_form<'a>(
117        &'a self,
118        _url: &str,
119        _request: reqwest::multipart::Form,
120    ) -> BoxFuture<'a, anyhow::Result<Response<AsyncBody>>> {
121        future::ready(Err(anyhow!("not implemented"))).boxed()
122    }
123}
124
125/// An [`HttpClient`] that may have a proxy.
126#[derive(Deref)]
127pub struct HttpClientWithProxy {
128    #[deref]
129    client: Arc<dyn HttpClient>,
130    proxy: Option<Url>,
131}
132
133impl HttpClientWithProxy {
134    /// Returns a new [`HttpClientWithProxy`] with the given proxy URL.
135    pub fn new(client: Arc<dyn HttpClient>, proxy_url: Option<String>) -> Self {
136        let proxy_url = proxy_url
137            .and_then(|proxy| proxy.parse().ok())
138            .or_else(read_proxy_from_env);
139
140        Self::new_url(client, proxy_url)
141    }
142    pub fn new_url(client: Arc<dyn HttpClient>, proxy_url: Option<Url>) -> Self {
143        Self {
144            client,
145            proxy: proxy_url,
146        }
147    }
148}
149
150impl HttpClient for HttpClientWithProxy {
151    fn send(
152        &self,
153        req: Request<AsyncBody>,
154    ) -> BoxFuture<'static, anyhow::Result<Response<AsyncBody>>> {
155        self.client.send(req)
156    }
157
158    fn user_agent(&self) -> Option<&HeaderValue> {
159        self.client.user_agent()
160    }
161
162    fn proxy(&self) -> Option<&Url> {
163        self.proxy.as_ref()
164    }
165
166    fn type_name(&self) -> &'static str {
167        self.client.type_name()
168    }
169
170    #[cfg(feature = "test-support")]
171    fn as_fake(&self) -> &FakeHttpClient {
172        self.client.as_fake()
173    }
174
175    fn send_multipart_form<'a>(
176        &'a self,
177        url: &str,
178        form: reqwest::multipart::Form,
179    ) -> BoxFuture<'a, anyhow::Result<Response<AsyncBody>>> {
180        self.client.send_multipart_form(url, form)
181    }
182}
183
184/// An [`HttpClient`] that has a base URL.
185pub struct HttpClientWithUrl {
186    base_url: Mutex<String>,
187    client: HttpClientWithProxy,
188}
189
190impl std::ops::Deref for HttpClientWithUrl {
191    type Target = HttpClientWithProxy;
192
193    fn deref(&self) -> &Self::Target {
194        &self.client
195    }
196}
197
198impl HttpClientWithUrl {
199    /// Returns a new [`HttpClientWithUrl`] with the given base URL.
200    pub fn new(
201        client: Arc<dyn HttpClient>,
202        base_url: impl Into<String>,
203        proxy_url: Option<String>,
204    ) -> Self {
205        let client = HttpClientWithProxy::new(client, proxy_url);
206
207        Self {
208            base_url: Mutex::new(base_url.into()),
209            client,
210        }
211    }
212
213    pub fn new_url(
214        client: Arc<dyn HttpClient>,
215        base_url: impl Into<String>,
216        proxy_url: Option<Url>,
217    ) -> Self {
218        let client = HttpClientWithProxy::new_url(client, proxy_url);
219
220        Self {
221            base_url: Mutex::new(base_url.into()),
222            client,
223        }
224    }
225
226    /// Returns the base URL.
227    pub fn base_url(&self) -> String {
228        self.base_url.lock().clone()
229    }
230
231    /// Sets the base URL.
232    pub fn set_base_url(&self, base_url: impl Into<String>) {
233        let base_url = base_url.into();
234        *self.base_url.lock() = base_url;
235    }
236
237    /// Builds a URL using the given path.
238    pub fn build_url(&self, path: &str) -> String {
239        format!("{}{}", self.base_url(), path)
240    }
241
242    /// Builds a Zed API URL using the given path.
243    pub fn build_zed_api_url(&self, path: &str, query: &[(&str, &str)]) -> Result<Url> {
244        let base_url = self.base_url();
245        let base_api_url = match base_url.as_ref() {
246            "https://zed.dev" => "https://api.zed.dev",
247            "https://staging.zed.dev" => "https://api-staging.zed.dev",
248            "http://localhost:3000" => "http://localhost:8080",
249            other => other,
250        };
251
252        Ok(Url::parse_with_params(
253            &format!("{}{}", base_api_url, path),
254            query,
255        )?)
256    }
257
258    /// Builds a Zed Cloud URL using the given path.
259    pub fn build_zed_cloud_url(&self, path: &str) -> Result<Url> {
260        let base_url = self.base_url();
261        let base_api_url = match base_url.as_ref() {
262            "https://zed.dev" => "https://cloud.zed.dev",
263            "https://staging.zed.dev" => "https://cloud.zed.dev",
264            "http://localhost:3000" => "http://localhost:8787",
265            other => other,
266        };
267
268        Ok(Url::parse(&format!("{}{}", base_api_url, path))?)
269    }
270
271    /// Builds a Zed Cloud URL using the given path and query params.
272    pub fn build_zed_cloud_url_with_query(&self, path: &str, query: impl Serialize) -> Result<Url> {
273        let base_url = self.base_url();
274        let base_api_url = match base_url.as_ref() {
275            "https://zed.dev" => "https://cloud.zed.dev",
276            "https://staging.zed.dev" => "https://cloud.zed.dev",
277            "http://localhost:3000" => "http://localhost:8787",
278            other => other,
279        };
280        let query = serde_urlencoded::to_string(&query)?;
281        Ok(Url::parse(&format!("{}{}?{}", base_api_url, path, query))?)
282    }
283
284    /// Builds a Zed LLM URL using the given path.
285    pub fn build_zed_llm_url(&self, path: &str, query: &[(&str, &str)]) -> Result<Url> {
286        let base_url = self.base_url();
287        let base_api_url = match base_url.as_ref() {
288            "https://zed.dev" => "https://cloud.zed.dev",
289            "https://staging.zed.dev" => "https://llm-staging.zed.dev",
290            "http://localhost:3000" => "http://localhost:8787",
291            other => other,
292        };
293
294        Ok(Url::parse_with_params(
295            &format!("{}{}", base_api_url, path),
296            query,
297        )?)
298    }
299}
300
301impl HttpClient for HttpClientWithUrl {
302    fn send(
303        &self,
304        req: Request<AsyncBody>,
305    ) -> BoxFuture<'static, anyhow::Result<Response<AsyncBody>>> {
306        self.client.send(req)
307    }
308
309    fn user_agent(&self) -> Option<&HeaderValue> {
310        self.client.user_agent()
311    }
312
313    fn proxy(&self) -> Option<&Url> {
314        self.client.proxy.as_ref()
315    }
316
317    fn type_name(&self) -> &'static str {
318        self.client.type_name()
319    }
320
321    #[cfg(feature = "test-support")]
322    fn as_fake(&self) -> &FakeHttpClient {
323        self.client.as_fake()
324    }
325
326    fn send_multipart_form<'a>(
327        &'a self,
328        url: &str,
329        request: reqwest::multipart::Form,
330    ) -> BoxFuture<'a, anyhow::Result<Response<AsyncBody>>> {
331        self.client.send_multipart_form(url, request)
332    }
333}
334
335pub fn read_proxy_from_env() -> Option<Url> {
336    const ENV_VARS: &[&str] = &[
337        "ALL_PROXY",
338        "all_proxy",
339        "HTTPS_PROXY",
340        "https_proxy",
341        "HTTP_PROXY",
342        "http_proxy",
343    ];
344
345    ENV_VARS
346        .iter()
347        .find_map(|var| std::env::var(var).ok())
348        .and_then(|env| env.parse().ok())
349}
350
351pub fn read_no_proxy_from_env() -> Option<String> {
352    const ENV_VARS: &[&str] = &["NO_PROXY", "no_proxy"];
353
354    ENV_VARS.iter().find_map(|var| std::env::var(var).ok())
355}
356
357pub struct BlockedHttpClient;
358
359impl BlockedHttpClient {
360    pub fn new() -> Self {
361        BlockedHttpClient
362    }
363}
364
365impl HttpClient for BlockedHttpClient {
366    fn send(
367        &self,
368        _req: Request<AsyncBody>,
369    ) -> BoxFuture<'static, anyhow::Result<Response<AsyncBody>>> {
370        Box::pin(async {
371            Err(std::io::Error::new(
372                std::io::ErrorKind::PermissionDenied,
373                "BlockedHttpClient disallowed request",
374            )
375            .into())
376        })
377    }
378
379    fn user_agent(&self) -> Option<&HeaderValue> {
380        None
381    }
382
383    fn proxy(&self) -> Option<&Url> {
384        None
385    }
386
387    fn type_name(&self) -> &'static str {
388        type_name::<Self>()
389    }
390
391    #[cfg(feature = "test-support")]
392    fn as_fake(&self) -> &FakeHttpClient {
393        panic!("called as_fake on {}", type_name::<Self>())
394    }
395}
396
397#[cfg(feature = "test-support")]
398type FakeHttpHandler = Arc<
399    dyn Fn(Request<AsyncBody>) -> BoxFuture<'static, anyhow::Result<Response<AsyncBody>>>
400        + Send
401        + Sync
402        + 'static,
403>;
404
405#[cfg(feature = "test-support")]
406pub struct FakeHttpClient {
407    handler: Mutex<Option<FakeHttpHandler>>,
408    user_agent: HeaderValue,
409}
410
411#[cfg(feature = "test-support")]
412impl FakeHttpClient {
413    pub fn create<Fut, F>(handler: F) -> Arc<HttpClientWithUrl>
414    where
415        Fut: futures::Future<Output = anyhow::Result<Response<AsyncBody>>> + Send + 'static,
416        F: Fn(Request<AsyncBody>) -> Fut + Send + Sync + 'static,
417    {
418        Arc::new(HttpClientWithUrl {
419            base_url: Mutex::new("http://test.example".into()),
420            client: HttpClientWithProxy {
421                client: Arc::new(Self {
422                    handler: Mutex::new(Some(Arc::new(move |req| Box::pin(handler(req))))),
423                    user_agent: HeaderValue::from_static(type_name::<Self>()),
424                }),
425                proxy: None,
426            },
427        })
428    }
429
430    pub fn with_404_response() -> Arc<HttpClientWithUrl> {
431        Self::create(|_| async move {
432            Ok(Response::builder()
433                .status(404)
434                .body(Default::default())
435                .unwrap())
436        })
437    }
438
439    pub fn with_200_response() -> Arc<HttpClientWithUrl> {
440        Self::create(|_| async move {
441            Ok(Response::builder()
442                .status(200)
443                .body(Default::default())
444                .unwrap())
445        })
446    }
447
448    pub fn replace_handler<Fut, F>(&self, new_handler: F)
449    where
450        Fut: futures::Future<Output = anyhow::Result<Response<AsyncBody>>> + Send + 'static,
451        F: Fn(FakeHttpHandler, Request<AsyncBody>) -> Fut + Send + Sync + 'static,
452    {
453        let mut handler = self.handler.lock();
454        let old_handler = handler.take().unwrap();
455        *handler = Some(Arc::new(move |req| {
456            Box::pin(new_handler(old_handler.clone(), req))
457        }));
458    }
459}
460
461#[cfg(feature = "test-support")]
462impl fmt::Debug for FakeHttpClient {
463    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
464        f.debug_struct("FakeHttpClient").finish()
465    }
466}
467
468#[cfg(feature = "test-support")]
469impl HttpClient for FakeHttpClient {
470    fn send(
471        &self,
472        req: Request<AsyncBody>,
473    ) -> BoxFuture<'static, anyhow::Result<Response<AsyncBody>>> {
474        ((self.handler.lock().as_ref().unwrap())(req)) as _
475    }
476
477    fn user_agent(&self) -> Option<&HeaderValue> {
478        Some(&self.user_agent)
479    }
480
481    fn proxy(&self) -> Option<&Url> {
482        None
483    }
484
485    fn type_name(&self) -> &'static str {
486        type_name::<Self>()
487    }
488
489    fn as_fake(&self) -> &FakeHttpClient {
490        self
491    }
492}