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 fn read_no_proxy_from_env() -> Option<String> {
322    const ENV_VARS: &[&str] = &["NO_PROXY", "no_proxy"];
323
324    ENV_VARS.iter().find_map(|var| std::env::var(var).ok())
325}
326
327pub struct BlockedHttpClient;
328
329impl BlockedHttpClient {
330    pub fn new() -> Self {
331        BlockedHttpClient
332    }
333}
334
335impl HttpClient for BlockedHttpClient {
336    fn send(
337        &self,
338        _req: Request<AsyncBody>,
339    ) -> BoxFuture<'static, anyhow::Result<Response<AsyncBody>>> {
340        Box::pin(async {
341            Err(std::io::Error::new(
342                std::io::ErrorKind::PermissionDenied,
343                "BlockedHttpClient disallowed request",
344            )
345            .into())
346        })
347    }
348
349    fn user_agent(&self) -> Option<&HeaderValue> {
350        None
351    }
352
353    fn proxy(&self) -> Option<&Url> {
354        None
355    }
356
357    fn type_name(&self) -> &'static str {
358        type_name::<Self>()
359    }
360
361    #[cfg(feature = "test-support")]
362    fn as_fake(&self) -> &FakeHttpClient {
363        panic!("called as_fake on {}", type_name::<Self>())
364    }
365}
366
367#[cfg(feature = "test-support")]
368type FakeHttpHandler = Arc<
369    dyn Fn(Request<AsyncBody>) -> BoxFuture<'static, anyhow::Result<Response<AsyncBody>>>
370        + Send
371        + Sync
372        + 'static,
373>;
374
375#[cfg(feature = "test-support")]
376pub struct FakeHttpClient {
377    handler: Mutex<Option<FakeHttpHandler>>,
378    user_agent: HeaderValue,
379}
380
381#[cfg(feature = "test-support")]
382impl FakeHttpClient {
383    pub fn create<Fut, F>(handler: F) -> Arc<HttpClientWithUrl>
384    where
385        Fut: futures::Future<Output = anyhow::Result<Response<AsyncBody>>> + Send + 'static,
386        F: Fn(Request<AsyncBody>) -> Fut + Send + Sync + 'static,
387    {
388        Arc::new(HttpClientWithUrl {
389            base_url: Mutex::new("http://test.example".into()),
390            client: HttpClientWithProxy {
391                client: Arc::new(Self {
392                    handler: Mutex::new(Some(Arc::new(move |req| Box::pin(handler(req))))),
393                    user_agent: HeaderValue::from_static(type_name::<Self>()),
394                }),
395                proxy: None,
396            },
397        })
398    }
399
400    pub fn with_404_response() -> Arc<HttpClientWithUrl> {
401        Self::create(|_| async move {
402            Ok(Response::builder()
403                .status(404)
404                .body(Default::default())
405                .unwrap())
406        })
407    }
408
409    pub fn with_200_response() -> Arc<HttpClientWithUrl> {
410        Self::create(|_| async move {
411            Ok(Response::builder()
412                .status(200)
413                .body(Default::default())
414                .unwrap())
415        })
416    }
417
418    pub fn replace_handler<Fut, F>(&self, new_handler: F)
419    where
420        Fut: futures::Future<Output = anyhow::Result<Response<AsyncBody>>> + Send + 'static,
421        F: Fn(FakeHttpHandler, Request<AsyncBody>) -> Fut + Send + Sync + 'static,
422    {
423        let mut handler = self.handler.lock();
424        let old_handler = handler.take().unwrap();
425        *handler = Some(Arc::new(move |req| {
426            Box::pin(new_handler(old_handler.clone(), req))
427        }));
428    }
429}
430
431#[cfg(feature = "test-support")]
432impl fmt::Debug for FakeHttpClient {
433    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
434        f.debug_struct("FakeHttpClient").finish()
435    }
436}
437
438#[cfg(feature = "test-support")]
439impl HttpClient for FakeHttpClient {
440    fn send(
441        &self,
442        req: Request<AsyncBody>,
443    ) -> BoxFuture<'static, anyhow::Result<Response<AsyncBody>>> {
444        ((self.handler.lock().as_ref().unwrap())(req)) as _
445    }
446
447    fn user_agent(&self) -> Option<&HeaderValue> {
448        Some(&self.user_agent)
449    }
450
451    fn proxy(&self) -> Option<&Url> {
452        None
453    }
454
455    fn type_name(&self) -> &'static str {
456        type_name::<Self>()
457    }
458
459    fn as_fake(&self) -> &FakeHttpClient {
460        self
461    }
462}