Skip to main content

wgpui_http_client/
http_client.rs

1//! Executor-neutral HTTP interfaces for WGPUI.
2
3mod async_body;
4pub mod github;
5pub mod github_download;
6
7pub use anyhow::{Result, anyhow};
8pub use async_body::{AsyncBody, Inner};
9use derive_more::Deref;
10use http::HeaderValue;
11pub use http::{self, Method, Request, Response, StatusCode, Uri, request::Builder};
12
13use futures::{
14    FutureExt as _,
15    future::{self, BoxFuture},
16};
17use parking_lot::Mutex;
18pub use reqwest;
19#[cfg(feature = "test-support")]
20use std::fmt;
21use std::{any::type_name, sync::Arc};
22pub use url::Url;
23
24#[derive(Default, Debug, Clone, PartialEq, Eq, Hash)]
25pub enum RedirectPolicy {
26    #[default]
27    NoFollow,
28    FollowLimit(u32),
29    FollowAll,
30}
31pub struct FollowRedirects(pub bool);
32
33pub trait HttpRequestExt {
34    /// Conditionally modify self with the given closure.
35    fn when(self, condition: bool, then: impl FnOnce(Self) -> Self) -> Self
36    where
37        Self: Sized,
38    {
39        if condition { then(self) } else { self }
40    }
41
42    /// Conditionally unwrap and modify self with the given closure, if the given option is Some.
43    fn when_some<T>(self, option: Option<T>, then: impl FnOnce(Self, T) -> Self) -> Self
44    where
45        Self: Sized,
46    {
47        match option {
48            Some(value) => then(self, value),
49            None => self,
50        }
51    }
52
53    /// Whether or not to follow redirects
54    fn follow_redirects(self, follow: RedirectPolicy) -> Self;
55}
56
57impl HttpRequestExt for http::request::Builder {
58    fn follow_redirects(self, follow: RedirectPolicy) -> Self {
59        self.extension(follow)
60    }
61}
62
63pub trait HttpClient: 'static + Send + Sync {
64    fn type_name(&self) -> &'static str;
65
66    fn user_agent(&self) -> Option<&HeaderValue>;
67
68    fn send(
69        &self,
70        req: http::Request<AsyncBody>,
71    ) -> BoxFuture<'static, anyhow::Result<Response<AsyncBody>>>;
72
73    fn get(
74        &self,
75        uri: &str,
76        body: AsyncBody,
77        follow_redirects: bool,
78    ) -> BoxFuture<'static, anyhow::Result<Response<AsyncBody>>> {
79        let request = Builder::new()
80            .uri(uri)
81            .follow_redirects(if follow_redirects {
82                RedirectPolicy::FollowAll
83            } else {
84                RedirectPolicy::NoFollow
85            })
86            .body(body);
87
88        match request {
89            Ok(request) => self.send(request),
90            Err(e) => Box::pin(async move { Err(e.into()) }),
91        }
92    }
93
94    fn post_json(
95        &self,
96        uri: &str,
97        body: AsyncBody,
98    ) -> BoxFuture<'static, anyhow::Result<Response<AsyncBody>>> {
99        let request = Builder::new()
100            .uri(uri)
101            .method(Method::POST)
102            .header("Content-Type", "application/json")
103            .body(body);
104
105        match request {
106            Ok(request) => self.send(request),
107            Err(e) => Box::pin(async move { Err(e.into()) }),
108        }
109    }
110
111    fn proxy(&self) -> Option<&Url>;
112
113    #[cfg(feature = "test-support")]
114    fn as_fake(&self) -> &FakeHttpClient {
115        panic!("called as_fake on {}", type_name::<Self>())
116    }
117
118    fn send_multipart_form<'a>(
119        &'a self,
120        _url: &str,
121        _request: reqwest::multipart::Form,
122    ) -> BoxFuture<'a, anyhow::Result<Response<AsyncBody>>> {
123        future::ready(Err(anyhow!("not implemented"))).boxed()
124    }
125}
126
127/// An [`HttpClient`] that may have a proxy.
128#[derive(Deref)]
129pub struct HttpClientWithProxy {
130    #[deref]
131    client: Arc<dyn HttpClient>,
132    proxy: Option<Url>,
133}
134
135impl HttpClientWithProxy {
136    /// Returns a new [`HttpClientWithProxy`] with the given proxy URL.
137    pub fn new(client: Arc<dyn HttpClient>, proxy_url: Option<String>) -> Self {
138        let proxy_url = proxy_url
139            .and_then(|proxy| proxy.parse().ok())
140            .or_else(read_proxy_from_env);
141
142        Self::new_url(client, proxy_url)
143    }
144    pub fn new_url(client: Arc<dyn HttpClient>, proxy_url: Option<Url>) -> Self {
145        Self {
146            client,
147            proxy: proxy_url,
148        }
149    }
150}
151
152impl HttpClient for HttpClientWithProxy {
153    fn send(
154        &self,
155        req: Request<AsyncBody>,
156    ) -> BoxFuture<'static, anyhow::Result<Response<AsyncBody>>> {
157        self.client.send(req)
158    }
159
160    fn user_agent(&self) -> Option<&HeaderValue> {
161        self.client.user_agent()
162    }
163
164    fn proxy(&self) -> Option<&Url> {
165        self.proxy.as_ref()
166    }
167
168    fn type_name(&self) -> &'static str {
169        self.client.type_name()
170    }
171
172    #[cfg(feature = "test-support")]
173    fn as_fake(&self) -> &FakeHttpClient {
174        self.client.as_fake()
175    }
176
177    fn send_multipart_form<'a>(
178        &'a self,
179        url: &str,
180        form: reqwest::multipart::Form,
181    ) -> BoxFuture<'a, anyhow::Result<Response<AsyncBody>>> {
182        self.client.send_multipart_form(url, form)
183    }
184}
185
186/// An [`HttpClient`] that has a base URL.
187pub struct HttpClientWithUrl {
188    base_url: Mutex<String>,
189    client: HttpClientWithProxy,
190}
191
192impl std::ops::Deref for HttpClientWithUrl {
193    type Target = HttpClientWithProxy;
194
195    fn deref(&self) -> &Self::Target {
196        &self.client
197    }
198}
199
200impl HttpClientWithUrl {
201    /// Returns a new [`HttpClientWithUrl`] with the given base URL.
202    pub fn new(
203        client: Arc<dyn HttpClient>,
204        base_url: impl Into<String>,
205        proxy_url: Option<String>,
206    ) -> Self {
207        let client = HttpClientWithProxy::new(client, proxy_url);
208
209        Self {
210            base_url: Mutex::new(base_url.into()),
211            client,
212        }
213    }
214
215    pub fn new_url(
216        client: Arc<dyn HttpClient>,
217        base_url: impl Into<String>,
218        proxy_url: Option<Url>,
219    ) -> Self {
220        let client = HttpClientWithProxy::new_url(client, proxy_url);
221
222        Self {
223            base_url: Mutex::new(base_url.into()),
224            client,
225        }
226    }
227
228    /// Returns the base URL.
229    pub fn base_url(&self) -> String {
230        self.base_url.lock().clone()
231    }
232
233    /// Sets the base URL.
234    pub fn set_base_url(&self, base_url: impl Into<String>) {
235        let base_url = base_url.into();
236        *self.base_url.lock() = base_url;
237    }
238
239    /// Builds a URL using the given path.
240    pub fn build_url(&self, path: &str) -> String {
241        format!("{}{}", self.base_url(), path)
242    }
243
244    /// Builds a Zed API URL using the given path.
245    pub fn build_zed_api_url(&self, path: &str, query: &[(&str, &str)]) -> Result<Url> {
246        let base_url = self.base_url();
247        let base_api_url = match base_url.as_ref() {
248            "https://zed.dev" => "https://api.zed.dev",
249            "https://staging.zed.dev" => "https://api-staging.zed.dev",
250            "http://localhost:3000" => "http://localhost:8080",
251            other => other,
252        };
253
254        Ok(Url::parse_with_params(
255            &format!("{}{}", base_api_url, path),
256            query,
257        )?)
258    }
259
260    /// Builds a Zed Cloud URL using the given path.
261    pub fn build_zed_cloud_url(&self, path: &str, query: &[(&str, &str)]) -> Result<Url> {
262        let base_url = self.base_url();
263        let base_api_url = match base_url.as_ref() {
264            "https://zed.dev" => "https://cloud.zed.dev",
265            "https://staging.zed.dev" => "https://cloud.zed.dev",
266            "http://localhost:3000" => "http://localhost:8787",
267            other => other,
268        };
269
270        Ok(Url::parse_with_params(
271            &format!("{}{}", base_api_url, path),
272            query,
273        )?)
274    }
275
276    /// Builds a Zed LLM URL using the given path.
277    pub fn build_zed_llm_url(&self, path: &str, query: &[(&str, &str)]) -> Result<Url> {
278        let base_url = self.base_url();
279        let base_api_url = match base_url.as_ref() {
280            "https://zed.dev" => "https://cloud.zed.dev",
281            "https://staging.zed.dev" => "https://llm-staging.zed.dev",
282            "http://localhost:3000" => "http://localhost:8787",
283            other => other,
284        };
285
286        Ok(Url::parse_with_params(
287            &format!("{}{}", base_api_url, path),
288            query,
289        )?)
290    }
291}
292
293impl HttpClient for HttpClientWithUrl {
294    fn send(
295        &self,
296        req: Request<AsyncBody>,
297    ) -> BoxFuture<'static, anyhow::Result<Response<AsyncBody>>> {
298        self.client.send(req)
299    }
300
301    fn user_agent(&self) -> Option<&HeaderValue> {
302        self.client.user_agent()
303    }
304
305    fn proxy(&self) -> Option<&Url> {
306        self.client.proxy.as_ref()
307    }
308
309    fn type_name(&self) -> &'static str {
310        self.client.type_name()
311    }
312
313    #[cfg(feature = "test-support")]
314    fn as_fake(&self) -> &FakeHttpClient {
315        self.client.as_fake()
316    }
317
318    fn send_multipart_form<'a>(
319        &'a self,
320        url: &str,
321        request: reqwest::multipart::Form,
322    ) -> BoxFuture<'a, anyhow::Result<Response<AsyncBody>>> {
323        self.client.send_multipart_form(url, request)
324    }
325}
326
327pub fn read_proxy_from_env() -> Option<Url> {
328    const ENV_VARS: &[&str] = &[
329        "ALL_PROXY",
330        "all_proxy",
331        "HTTPS_PROXY",
332        "https_proxy",
333        "HTTP_PROXY",
334        "http_proxy",
335    ];
336
337    ENV_VARS
338        .iter()
339        .find_map(|var| std::env::var(var).ok())
340        .and_then(|env| env.parse().ok())
341}
342
343pub fn read_no_proxy_from_env() -> Option<String> {
344    const ENV_VARS: &[&str] = &["NO_PROXY", "no_proxy"];
345
346    ENV_VARS.iter().find_map(|var| std::env::var(var).ok())
347}
348
349#[derive(Default)]
350pub struct BlockedHttpClient;
351
352impl BlockedHttpClient {
353    pub fn new() -> Self {
354        BlockedHttpClient
355    }
356}
357
358impl HttpClient for BlockedHttpClient {
359    fn send(
360        &self,
361        _req: Request<AsyncBody>,
362    ) -> BoxFuture<'static, anyhow::Result<Response<AsyncBody>>> {
363        Box::pin(async {
364            Err(std::io::Error::new(
365                std::io::ErrorKind::PermissionDenied,
366                "BlockedHttpClient disallowed request",
367            )
368            .into())
369        })
370    }
371
372    fn user_agent(&self) -> Option<&HeaderValue> {
373        None
374    }
375
376    fn proxy(&self) -> Option<&Url> {
377        None
378    }
379
380    fn type_name(&self) -> &'static str {
381        type_name::<Self>()
382    }
383
384    #[cfg(feature = "test-support")]
385    fn as_fake(&self) -> &FakeHttpClient {
386        panic!("called as_fake on {}", type_name::<Self>())
387    }
388}
389
390#[cfg(feature = "test-support")]
391type FakeHttpHandler = Arc<
392    dyn Fn(Request<AsyncBody>) -> BoxFuture<'static, anyhow::Result<Response<AsyncBody>>>
393        + Send
394        + Sync
395        + 'static,
396>;
397
398#[cfg(feature = "test-support")]
399pub struct FakeHttpClient {
400    handler: Mutex<FakeHttpHandler>,
401    user_agent: HeaderValue,
402}
403
404#[cfg(feature = "test-support")]
405impl FakeHttpClient {
406    pub fn create<Fut, F>(handler: F) -> Arc<HttpClientWithUrl>
407    where
408        Fut: futures::Future<Output = anyhow::Result<Response<AsyncBody>>> + Send + 'static,
409        F: Fn(Request<AsyncBody>) -> Fut + Send + Sync + 'static,
410    {
411        Arc::new(HttpClientWithUrl {
412            base_url: Mutex::new("http://test.example".into()),
413            client: HttpClientWithProxy {
414                client: Arc::new(Self {
415                    handler: Mutex::new(Arc::new(move |request| Box::pin(handler(request)))),
416                    user_agent: HeaderValue::from_static(type_name::<Self>()),
417                }),
418                proxy: None,
419            },
420        })
421    }
422
423    pub fn with_404_response() -> Arc<HttpClientWithUrl> {
424        Self::create(
425            |_| async move { Ok(Response::builder().status(404).body(AsyncBody::empty())?) },
426        )
427    }
428
429    pub fn with_200_response() -> Arc<HttpClientWithUrl> {
430        Self::create(
431            |_| async move { Ok(Response::builder().status(200).body(AsyncBody::empty())?) },
432        )
433    }
434
435    pub fn replace_handler<Fut, F>(&self, new_handler: F)
436    where
437        Fut: futures::Future<Output = anyhow::Result<Response<AsyncBody>>> + Send + 'static,
438        F: Fn(FakeHttpHandler, Request<AsyncBody>) -> Fut + Send + Sync + 'static,
439    {
440        let mut handler = self.handler.lock();
441        let old_handler = handler.clone();
442        *handler = Arc::new(move |request| Box::pin(new_handler(old_handler.clone(), request)));
443    }
444}
445
446#[cfg(feature = "test-support")]
447impl fmt::Debug for FakeHttpClient {
448    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
449        f.debug_struct("FakeHttpClient").finish()
450    }
451}
452
453#[cfg(feature = "test-support")]
454impl HttpClient for FakeHttpClient {
455    fn send(
456        &self,
457        req: Request<AsyncBody>,
458    ) -> BoxFuture<'static, anyhow::Result<Response<AsyncBody>>> {
459        let handler = self.handler.lock().clone();
460        handler(req)
461    }
462
463    fn user_agent(&self) -> Option<&HeaderValue> {
464        Some(&self.user_agent)
465    }
466
467    fn proxy(&self) -> Option<&Url> {
468        None
469    }
470
471    fn type_name(&self) -> &'static str {
472        type_name::<Self>()
473    }
474
475    fn as_fake(&self) -> &FakeHttpClient {
476        self
477    }
478}
479
480#[cfg(all(test, feature = "test-support"))]
481mod tests {
482    use super::*;
483    use futures::{AsyncReadExt, executor::block_on};
484    use std::sync::atomic::{AtomicUsize, Ordering};
485
486    #[test]
487    fn fake_client_forwards_request_metadata_and_streamed_response() -> Result<()> {
488        block_on(async {
489            let client =
490                FakeHttpClient::create(|mut request| async move {
491                    assert_eq!(request.method(), Method::POST);
492                    assert_eq!(request.uri(), "https://example.test/api");
493                    assert_eq!(
494                        request.headers().get("Content-Type"),
495                        Some(&HeaderValue::from_static("application/json"))
496                    );
497                    let mut body = String::new();
498                    request.body_mut().read_to_string(&mut body).await?;
499                    assert_eq!(body, "{\"answer\":42}");
500                    Ok(Response::builder().status(StatusCode::CREATED).body(
501                        AsyncBody::from_reader(futures::io::Cursor::new(b"streamed".to_vec())),
502                    )?)
503                });
504            let mut response = client
505                .post_json("https://example.test/api", "{\"answer\":42}".into())
506                .await?;
507            assert_eq!(response.status(), StatusCode::CREATED);
508            let mut payload = String::new();
509            response.body_mut().read_to_string(&mut payload).await?;
510            assert_eq!(payload, "streamed");
511            Ok(())
512        })
513    }
514
515    #[test]
516    fn replacement_handler_can_delegate_without_holding_its_lock() -> Result<()> {
517        block_on(async {
518            let client = FakeHttpClient::with_200_response();
519            client
520                .as_fake()
521                .replace_handler(|previous, request| async move {
522                    let mut response = previous(request).await?;
523                    *response.status_mut() = StatusCode::ACCEPTED;
524                    Ok(response)
525                });
526            let response = client
527                .get("https://example.test/", AsyncBody::empty(), true)
528                .await?;
529            assert_eq!(response.status(), StatusCode::ACCEPTED);
530            Ok(())
531        })
532    }
533
534    #[test]
535    fn proxy_wrapper_forwards_the_stable_multipart_type() -> Result<()> {
536        struct MultipartClient(Arc<AtomicUsize>);
537        impl HttpClient for MultipartClient {
538            fn type_name(&self) -> &'static str {
539                "MultipartClient"
540            }
541            fn user_agent(&self) -> Option<&HeaderValue> {
542                None
543            }
544            fn proxy(&self) -> Option<&Url> {
545                None
546            }
547            fn send(
548                &self,
549                _: Request<AsyncBody>,
550            ) -> BoxFuture<'static, Result<Response<AsyncBody>>> {
551                Box::pin(async { Err(anyhow!("unexpected ordinary request")) })
552            }
553            fn send_multipart_form<'a>(
554                &'a self,
555                url: &str,
556                _: reqwest::multipart::Form,
557            ) -> BoxFuture<'a, Result<Response<AsyncBody>>> {
558                assert_eq!(url, "https://example.test/upload");
559                self.0.fetch_add(1, Ordering::SeqCst);
560                Box::pin(async { Ok(Response::new(AsyncBody::empty())) })
561            }
562        }
563        block_on(async {
564            let calls = Arc::new(AtomicUsize::new(0));
565            let proxy = Url::parse("http://proxy.example.test:8080")?;
566            let client = HttpClientWithProxy::new_url(
567                Arc::new(MultipartClient(calls.clone())),
568                Some(proxy.clone()),
569            );
570            assert_eq!(client.proxy(), Some(&proxy));
571            client
572                .send_multipart_form(
573                    "https://example.test/upload",
574                    reqwest::multipart::Form::new().text("key", "value"),
575                )
576                .await?;
577            assert_eq!(calls.load(Ordering::SeqCst), 1);
578            Ok(())
579        })
580    }
581
582    #[test]
583    fn dropping_a_pending_request_releases_its_stream() -> Result<()> {
584        struct Reader(Arc<AtomicUsize>);
585        impl futures::AsyncRead for Reader {
586            fn poll_read(
587                self: std::pin::Pin<&mut Self>,
588                _: &mut std::task::Context<'_>,
589                _: &mut [u8],
590            ) -> std::task::Poll<std::io::Result<usize>> {
591                std::task::Poll::Pending
592            }
593        }
594        impl Drop for Reader {
595            fn drop(&mut self) {
596                self.0.fetch_add(1, Ordering::SeqCst);
597            }
598        }
599        let drops = Arc::new(AtomicUsize::new(0));
600        let client = FakeHttpClient::create(|request| async move {
601            futures::future::pending::<()>().await;
602            drop(request);
603            Ok(Response::new(AsyncBody::empty()))
604        });
605        let request = Request::builder()
606            .uri("https://example.test/pending")
607            .body(AsyncBody::from_reader(Reader(drops.clone())))?;
608        let future = client.send(request);
609        assert_eq!(drops.load(Ordering::SeqCst), 0);
610        drop(future);
611        assert_eq!(drops.load(Ordering::SeqCst), 1);
612        Ok(())
613    }
614
615    #[test]
616    fn blocked_client_reports_permission_denied() {
617        block_on(async {
618            let client = BlockedHttpClient::new();
619            let result = client
620                .get("https://example.test/", AsyncBody::empty(), false)
621                .await;
622            let error = match result {
623                Ok(_) => panic!("blocked client unexpectedly permitted a request"),
624                Err(error) => error,
625            };
626            assert_eq!(
627                error
628                    .downcast_ref::<std::io::Error>()
629                    .map(std::io::Error::kind),
630                Some(std::io::ErrorKind::PermissionDenied)
631            );
632        });
633    }
634}