http_client.rs

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