From 3a1bd38503f192083e62037624e7aec50e9742b7 Mon Sep 17 00:00:00 2001 From: Finn Evers Date: Mon, 28 Apr 2025 17:12:16 +0200 Subject: [PATCH] reqwest_client: Only register proxies with valid proxy URIs (#27773) Closes #27641 This PR fixes invalid proxy URIs being registered despite the URI not being a valid proxy URI. Whilst investigating #27641 , I noticed that currently any proxy URI passed to `RequestClient::proxy_and_user_agent` will be assigned to the created client, even if the URI is not a valid proxy URI. Given a test as an example: We create an URI here and pass it as a proxy to `ReqwestClient::proxy_and_user_agent`: https://github.com/zed-industries/zed/blob/main/crates/reqwest_client/src/reqwest_client.rs#L272-L273 In `ReqwestClient::proxy_and_user_agent`we take the proxy parameter here https://github.com/zed-industries/zed/blob/9b40770e9f92a4431fe8f04cbb03b106569606c8/crates/reqwest_client/src/reqwest_client.rs#L46 and set it unconditionally here: https://github.com/zed-industries/zed/blob/9b40770e9f92a4431fe8f04cbb03b106569606c8/crates/reqwest_client/src/reqwest_client.rs#L62 , not considering at all whether the proxy was successfully created above. Concluding, we currently do not actually check whether a proxy was successfully created, but rather whether an URI is equal to itself, which trivially holds. The existing test for a malformed proxy URI https://github.com/zed-industries/zed/blob/9b40770e9f92a4431fe8f04cbb03b106569606c8/crates/reqwest_client/src/reqwest_client.rs#L293-L297 does not check whether invalid proxies cause an error, but rather checks whether `http::Uri::from_static` panics on an invalid URI, [which it does as documented](https://docs.rs/http/latest/http/uri/struct.Uri.html#panics). Thus, the tests currently do not really check anything proxy-related and invalid proxies are assigned as valid proxies. --- This PR fixes the behaviour by considering whether the proxy was actually properly parsed and only assigning it if that is the case. Furthermore, it improves logging in case of errors so issues like the linked one are easier to debug (for the linked issue, the log will now include that the proxy schema is not supported in the logs). Lastly, it also updates the test for a malformed proxy URI. The test now actually checks that malformed proxy URIs are not registered for the client rather than testing the `http` crate. The update also initially caused the [test for a `socks4a` proxy](https://github.com/zed-industries/zed/blob/9b40770e9f92a4431fe8f04cbb03b106569606c8/crates/reqwest_client/src/reqwest_client.rs#L280C1-L282C50) to fail. This happened because the reqwest-library introduced supports for `socks4a` proxies in [version 0.12.13](https://github.com/seanmonstar/reqwest/blob/master/CHANGELOG.md#v01213). Thus, this PR includes a bump of the reqwest library to add proper support for socks4a proxies. Release Notes: - Added support for socks4a proxies. --------- Co-authored-by: Peter Tripp --- Cargo.lock | 103 +++++++++++++++++--- Cargo.toml | 2 +- crates/client/src/client.rs | 2 +- crates/client/src/socks.rs | 12 +-- crates/eval/src/eval.rs | 4 +- crates/gpui/src/app.rs | 4 +- crates/http_client/src/http_client.rs | 43 ++++---- crates/node_runtime/src/node_runtime.rs | 12 +-- crates/remote_server/src/unix.rs | 6 +- crates/reqwest_client/src/reqwest_client.rs | 53 ++++++---- crates/zed/src/main.rs | 4 +- tooling/workspace-hack/Cargo.toml | 24 +++++ 12 files changed, 193 insertions(+), 76 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bc4752bf11..ff76ab5a18 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8245,7 +8245,7 @@ dependencies = [ "prost 0.9.0", "prost-build 0.9.0", "prost-types 0.9.0", - "reqwest 0.12.8", + "reqwest 0.12.15", "serde", "workspace-hack", ] @@ -12018,8 +12018,8 @@ dependencies = [ [[package]] name = "reqwest" -version = "0.12.8" -source = "git+https://github.com/zed-industries/reqwest.git?rev=fd110f6998da16bbca97b6dddda9be7827c50e29#fd110f6998da16bbca97b6dddda9be7827c50e29" +version = "0.12.15" +source = "git+https://github.com/zed-industries/reqwest.git?rev=951c770a32f1998d6e999cef3e59e0013e6c4415#951c770a32f1998d6e999cef3e59e0013e6c4415" dependencies = [ "base64 0.22.1", "bytes 1.10.1", @@ -12054,13 +12054,14 @@ dependencies = [ "tokio-rustls 0.26.2", "tokio-socks", "tokio-util", + "tower 0.5.2", "tower-service", "url", "wasm-bindgen", "wasm-bindgen-futures", "wasm-streams", "web-sys", - "windows-registry 0.2.0", + "windows-registry 0.4.0", ] [[package]] @@ -12075,7 +12076,7 @@ dependencies = [ "http_client_tls", "log", "regex", - "reqwest 0.12.8", + "reqwest 0.12.15", "serde", "smol", "tokio", @@ -15134,6 +15135,11 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d039ad9159c98b70ecfd540b2573b97f7f52c3e8d9f8ad57a24b916a536975f9" dependencies = [ + "futures-core", + "futures-util", + "pin-project-lite", + "sync_wrapper 1.0.2", + "tokio", "tower-layer", "tower-service", ] @@ -17220,13 +17226,13 @@ dependencies = [ [[package]] name = "windows-registry" -version = "0.2.0" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e400001bb720a623c1c69032f8e3e4cf09984deec740f007dd2b03ec864804b0" +checksum = "4286ad90ddb45071efd1a66dfa43eb02dd0dfbae1545ad6cc3c51cf34d7e8ba3" dependencies = [ - "windows-result 0.2.0", - "windows-strings 0.1.0", - "windows-targets 0.52.6", + "windows-result 0.3.2", + "windows-strings 0.3.1", + "windows-targets 0.53.0", ] [[package]] @@ -17277,6 +17283,15 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "windows-strings" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87fa48cc5d406560701792be122a10132491cff9d0aeb23583cc2dcafc847319" +dependencies = [ + "windows-link", +] + [[package]] name = "windows-strings" version = "0.4.0" @@ -17361,13 +17376,29 @@ dependencies = [ "windows_aarch64_gnullvm 0.52.6", "windows_aarch64_msvc 0.52.6", "windows_i686_gnu 0.52.6", - "windows_i686_gnullvm", + "windows_i686_gnullvm 0.52.6", "windows_i686_msvc 0.52.6", "windows_x86_64_gnu 0.52.6", "windows_x86_64_gnullvm 0.52.6", "windows_x86_64_msvc 0.52.6", ] +[[package]] +name = "windows-targets" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1e4c7e8ceaaf9cb7d7507c974735728ab453b67ef8f18febdd7c11fe59dca8b" +dependencies = [ + "windows_aarch64_gnullvm 0.53.0", + "windows_aarch64_msvc 0.53.0", + "windows_i686_gnu 0.53.0", + "windows_i686_gnullvm 0.53.0", + "windows_i686_msvc 0.53.0", + "windows_x86_64_gnu 0.53.0", + "windows_x86_64_gnullvm 0.53.0", + "windows_x86_64_msvc 0.53.0", +] + [[package]] name = "windows_aarch64_gnullvm" version = "0.42.2" @@ -17386,6 +17417,12 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86b8d5f90ddd19cb4a147a5fa63ca848db3df085e25fee3cc10b39b6eebae764" + [[package]] name = "windows_aarch64_msvc" version = "0.42.2" @@ -17404,6 +17441,12 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" +[[package]] +name = "windows_aarch64_msvc" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7651a1f62a11b8cbd5e0d42526e55f2c99886c77e007179efff86c2b137e66c" + [[package]] name = "windows_i686_gnu" version = "0.42.2" @@ -17422,12 +17465,24 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" +[[package]] +name = "windows_i686_gnu" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1dc67659d35f387f5f6c479dc4e28f1d4bb90ddd1a5d3da2e5d97b42d6272c3" + [[package]] name = "windows_i686_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" +[[package]] +name = "windows_i686_gnullvm" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ce6ccbdedbf6d6354471319e781c0dfef054c81fbc7cf83f338a4296c0cae11" + [[package]] name = "windows_i686_msvc" version = "0.42.2" @@ -17446,6 +17501,12 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" +[[package]] +name = "windows_i686_msvc" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "581fee95406bb13382d2f65cd4a908ca7b1e4c2f1917f143ba16efe98a589b5d" + [[package]] name = "windows_x86_64_gnu" version = "0.42.2" @@ -17464,6 +17525,12 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" +[[package]] +name = "windows_x86_64_gnu" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e55b5ac9ea33f2fc1716d1742db15574fd6fc8dadc51caab1c16a3d3b4190ba" + [[package]] name = "windows_x86_64_gnullvm" version = "0.42.2" @@ -17482,6 +17549,12 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a6e035dd0599267ce1ee132e51c27dd29437f63325753051e71dd9e42406c57" + [[package]] name = "windows_x86_64_msvc" version = "0.42.2" @@ -17500,6 +17573,12 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" +[[package]] +name = "windows_x86_64_msvc" +version = "0.53.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "271414315aff87387382ec3d271b52d7ae78726f5d44ac98b4f4030c91880486" + [[package]] name = "winnow" version = "0.7.6" @@ -18010,6 +18089,7 @@ dependencies = [ "subtle", "syn 1.0.109", "syn 2.0.100", + "sync_wrapper 1.0.2", "thiserror 2.0.12", "time", "time-macros", @@ -18020,6 +18100,7 @@ dependencies = [ "tokio-util", "toml_datetime", "toml_edit", + "tower 0.5.2", "tracing", "tracing-core", "tungstenite 0.26.2", diff --git a/Cargo.toml b/Cargo.toml index ccd9dbeece..234a0cbf49 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -506,7 +506,7 @@ rayon = "1.8" ref-cast = "1.0.24" regex = "1.5" repair_json = "0.1.0" -reqwest = { git = "https://github.com/zed-industries/reqwest.git", rev = "fd110f6998da16bbca97b6dddda9be7827c50e29", default-features = false, features = [ +reqwest = { git = "https://github.com/zed-industries/reqwest.git", rev = "951c770a32f1998d6e999cef3e59e0013e6c4415", default-features = false, features = [ "charset", "http2", "macos-system-configuration", diff --git a/crates/client/src/client.rs b/crates/client/src/client.rs index db832ac134..495c0b96f1 100644 --- a/crates/client/src/client.rs +++ b/crates/client/src/client.rs @@ -564,7 +564,7 @@ impl Client { pub fn production(cx: &mut App) -> Arc { let clock = Arc::new(clock::RealSystemClock); - let http = Arc::new(HttpClientWithUrl::new_uri( + let http = Arc::new(HttpClientWithUrl::new_url( cx.http_client(), &ClientSettings::get_global(cx).server_url, cx.http_client().proxy().cloned(), diff --git a/crates/client/src/socks.rs b/crates/client/src/socks.rs index d29d0d433d..64ae45e080 100644 --- a/crates/client/src/socks.rs +++ b/crates/client/src/socks.rs @@ -1,10 +1,10 @@ //! socks proxy use anyhow::{Result, anyhow}; -use http_client::Uri; +use http_client::Url; use tokio_socks::tcp::{Socks4Stream, Socks5Stream}; pub(crate) async fn connect_socks_proxy_stream( - proxy: Option<&Uri>, + proxy: Option<&Url>, rpc_host: (&str, u16), ) -> Result> { let stream = match parse_socks_proxy(proxy) { @@ -32,9 +32,9 @@ pub(crate) async fn connect_socks_proxy_stream( Ok(stream) } -fn parse_socks_proxy(proxy: Option<&Uri>) -> Option<((String, u16), SocksVersion)> { - let proxy_uri = proxy?; - let scheme = proxy_uri.scheme_str()?; +fn parse_socks_proxy(proxy: Option<&Url>) -> Option<((String, u16), SocksVersion)> { + let proxy_url = proxy?; + let scheme = proxy_url.scheme(); let socks_version = if scheme.starts_with("socks4") { // socks4 SocksVersion::V4 @@ -44,7 +44,7 @@ fn parse_socks_proxy(proxy: Option<&Uri>) -> Option<((String, u16), SocksVersion } else { return None; }; - if let (Some(host), Some(port)) = (proxy_uri.host(), proxy_uri.port_u16()) { + if let Some((host, port)) = proxy_url.host().zip(proxy_url.port_or_known_default()) { Some(((host.to_string(), port), socks_version)) } else { None diff --git a/crates/eval/src/eval.rs b/crates/eval/src/eval.rs index e54a54539e..482e94c902 100644 --- a/crates/eval/src/eval.rs +++ b/crates/eval/src/eval.rs @@ -17,7 +17,7 @@ use client::{Client, ProxySettings, UserStore}; use collections::{HashMap, HashSet}; use extension::ExtensionHostProxy; use futures::future; -use gpui::http_client::{Uri, read_proxy_from_env}; +use gpui::http_client::read_proxy_from_env; use gpui::{App, AppContext, Application, AsyncApp, Entity, SemanticVersion, UpdateGlobal}; use gpui_tokio::Tokio; use language::LanguageRegistry; @@ -358,7 +358,7 @@ pub fn init(cx: &mut App) -> Arc { let proxy_str = ProxySettings::get_global(cx).proxy.to_owned(); let proxy_url = proxy_str .as_ref() - .and_then(|input| input.parse::().ok()) + .and_then(|input| input.parse().ok()) .or_else(read_proxy_from_env); let http = { let _guard = Tokio::handle(cx).enter(); diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index 2f586a5c5c..85f4c1a268 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -24,7 +24,7 @@ pub use async_context::*; use collections::{FxHashMap, FxHashSet, HashMap, VecDeque}; pub use context::*; pub use entity_map::*; -use http_client::HttpClient; +use http_client::{HttpClient, Url}; use smallvec::SmallVec; #[cfg(any(test, feature = "test-support"))] pub use test_context::*; @@ -1890,7 +1890,7 @@ impl HttpClient for NullHttpClient { async move { Err(anyhow!("No HttpClient available")) }.boxed() } - fn proxy(&self) -> Option<&http_client::Uri> { + fn proxy(&self) -> Option<&Url> { None } diff --git a/crates/http_client/src/http_client.rs b/crates/http_client/src/http_client.rs index cbbc2f710b..6295bddace 100644 --- a/crates/http_client/src/http_client.rs +++ b/crates/http_client/src/http_client.rs @@ -82,7 +82,7 @@ pub trait HttpClient: 'static + Send + Sync { } } - fn proxy(&self) -> Option<&Uri>; + fn proxy(&self) -> Option<&Url>; } /// An [`HttpClient`] that may have a proxy. @@ -90,22 +90,22 @@ pub trait HttpClient: 'static + Send + Sync { pub struct HttpClientWithProxy { #[deref] client: Arc, - proxy: Option, + proxy: Option, } impl HttpClientWithProxy { /// Returns a new [`HttpClientWithProxy`] with the given proxy URL. pub fn new(client: Arc, proxy_url: Option) -> Self { - let proxy_uri = proxy_url + let proxy_url = proxy_url .and_then(|proxy| proxy.parse().ok()) .or_else(read_proxy_from_env); - Self::new_uri(client, proxy_uri) + Self::new_url(client, proxy_url) } - pub fn new_uri(client: Arc, proxy_uri: Option) -> Self { + pub fn new_url(client: Arc, proxy_url: Option) -> Self { Self { client, - proxy: proxy_uri, + proxy: proxy_url, } } } @@ -118,7 +118,7 @@ impl HttpClient for HttpClientWithProxy { self.client.send(req) } - fn proxy(&self) -> Option<&Uri> { + fn proxy(&self) -> Option<&Url> { self.proxy.as_ref() } @@ -135,7 +135,7 @@ impl HttpClient for Arc { self.client.send(req) } - fn proxy(&self) -> Option<&Uri> { + fn proxy(&self) -> Option<&Url> { self.proxy.as_ref() } @@ -173,12 +173,12 @@ impl HttpClientWithUrl { } } - pub fn new_uri( + pub fn new_url( client: Arc, base_url: impl Into, - proxy_uri: Option, + proxy_url: Option, ) -> Self { - let client = HttpClientWithProxy::new_uri(client, proxy_uri); + let client = HttpClientWithProxy::new_url(client, proxy_url); Self { base_url: Mutex::new(base_url.into()), @@ -250,7 +250,7 @@ impl HttpClient for Arc { self.client.send(req) } - fn proxy(&self) -> Option<&Uri> { + fn proxy(&self) -> Option<&Url> { self.client.proxy.as_ref() } @@ -267,7 +267,7 @@ impl HttpClient for HttpClientWithUrl { self.client.send(req) } - fn proxy(&self) -> Option<&Uri> { + fn proxy(&self) -> Option<&Url> { self.client.proxy.as_ref() } @@ -276,7 +276,7 @@ impl HttpClient for HttpClientWithUrl { } } -pub fn read_proxy_from_env() -> Option { +pub fn read_proxy_from_env() -> Option { const ENV_VARS: &[&str] = &[ "ALL_PROXY", "all_proxy", @@ -286,13 +286,10 @@ pub fn read_proxy_from_env() -> Option { "http_proxy", ]; - for var in ENV_VARS { - if let Ok(env) = std::env::var(var) { - return env.parse::().ok(); - } - } - - None + ENV_VARS + .iter() + .find_map(|var| std::env::var(var).ok()) + .and_then(|env| env.parse().ok()) } pub struct BlockedHttpClient; @@ -317,7 +314,7 @@ impl HttpClient for BlockedHttpClient { }) } - fn proxy(&self) -> Option<&Uri> { + fn proxy(&self) -> Option<&Url> { None } @@ -393,7 +390,7 @@ impl HttpClient for FakeHttpClient { future } - fn proxy(&self) -> Option<&Uri> { + fn proxy(&self) -> Option<&Url> { None } diff --git a/crates/node_runtime/src/node_runtime.rs b/crates/node_runtime/src/node_runtime.rs index ae0260799e..c2ccba34c4 100644 --- a/crates/node_runtime/src/node_runtime.rs +++ b/crates/node_runtime/src/node_runtime.rs @@ -5,7 +5,7 @@ pub use archive::extract_zip; use async_compression::futures::bufread::GzipDecoder; use async_tar::Archive; use futures::AsyncReadExt; -use http_client::{HttpClient, Uri}; +use http_client::{HttpClient, Url}; use semver::Version; use serde::Deserialize; use smol::io::BufReader; @@ -247,7 +247,7 @@ trait NodeRuntimeTrait: Send + Sync { async fn run_npm_subcommand( &self, directory: Option<&Path>, - proxy: Option<&Uri>, + proxy: Option<&Url>, subcommand: &str, args: &[&str], ) -> Result; @@ -394,7 +394,7 @@ impl NodeRuntimeTrait for ManagedNodeRuntime { async fn run_npm_subcommand( &self, directory: Option<&Path>, - proxy: Option<&Uri>, + proxy: Option<&Url>, subcommand: &str, args: &[&str], ) -> Result { @@ -535,7 +535,7 @@ impl NodeRuntimeTrait for SystemNodeRuntime { async fn run_npm_subcommand( &self, directory: Option<&Path>, - proxy: Option<&Uri>, + proxy: Option<&Url>, subcommand: &str, args: &[&str], ) -> anyhow::Result { @@ -613,7 +613,7 @@ impl NodeRuntimeTrait for UnavailableNodeRuntime { async fn run_npm_subcommand( &self, _: Option<&Path>, - _: Option<&Uri>, + _: Option<&Url>, _: &str, _: &[&str], ) -> anyhow::Result { @@ -632,7 +632,7 @@ impl NodeRuntimeTrait for UnavailableNodeRuntime { fn configure_npm_command( command: &mut smol::process::Command, directory: Option<&Path>, - proxy: Option<&Uri>, + proxy: Option<&Url>, ) { if let Some(directory) = directory { command.current_dir(directory); diff --git a/crates/remote_server/src/unix.rs b/crates/remote_server/src/unix.rs index d7edb429d0..d81ae30129 100644 --- a/crates/remote_server/src/unix.rs +++ b/crates/remote_server/src/unix.rs @@ -11,7 +11,7 @@ use futures::{AsyncRead, AsyncWrite, AsyncWriteExt, FutureExt, SinkExt, select, use git::GitHostingProviderRegistry; use gpui::{App, AppContext as _, Context, Entity, SemanticVersion, UpdateGlobal as _}; use gpui_tokio::Tokio; -use http_client::{Uri, read_proxy_from_env}; +use http_client::{Url, read_proxy_from_env}; use language::LanguageRegistry; use node_runtime::{NodeBinaryOptions, NodeRuntime}; use paths::logs_dir; @@ -853,13 +853,13 @@ pub fn handle_settings_file_changes( .detach(); } -fn read_proxy_settings(cx: &mut Context) -> Option { +fn read_proxy_settings(cx: &mut Context) -> Option { let proxy_str = ProxySettings::get_global(cx).proxy.to_owned(); let proxy_url = proxy_str .as_ref() .and_then(|input: &String| { input - .parse::() + .parse::() .inspect_err(|e| log::error!("Error parsing proxy settings: {}", e)) .ok() }) diff --git a/crates/reqwest_client/src/reqwest_client.rs b/crates/reqwest_client/src/reqwest_client.rs index 11afe18ce9..0bcbe88f9f 100644 --- a/crates/reqwest_client/src/reqwest_client.rs +++ b/crates/reqwest_client/src/reqwest_client.rs @@ -1,10 +1,11 @@ +use std::error::Error; use std::sync::{LazyLock, OnceLock}; use std::{any::type_name, borrow::Cow, mem, pin::Pin, task::Poll, time::Duration}; use anyhow::anyhow; use bytes::{BufMut, Bytes, BytesMut}; use futures::{AsyncRead, TryStreamExt as _}; -use http_client::{RedirectPolicy, http}; +use http_client::{RedirectPolicy, Url, http}; use regex::Regex; use reqwest::{ header::{HeaderMap, HeaderValue}, @@ -18,7 +19,7 @@ static REDACT_REGEX: LazyLock = LazyLock::new(|| Regex::new(r"key=[^&]+") pub struct ReqwestClient { client: reqwest::Client, - proxy: Option, + proxy: Option, handle: tokio::runtime::Handle, } @@ -43,23 +44,34 @@ impl ReqwestClient { Ok(client.into()) } - pub fn proxy_and_user_agent(proxy: Option, agent: &str) -> anyhow::Result { + pub fn proxy_and_user_agent(proxy: Option, agent: &str) -> anyhow::Result { let mut map = HeaderMap::new(); map.insert(http::header::USER_AGENT, HeaderValue::from_str(agent)?); let mut client = Self::builder().default_headers(map); - if let Some(proxy) = proxy.clone().and_then(|proxy_uri| { - reqwest::Proxy::all(proxy_uri.to_string()) - .inspect_err(|e| log::error!("Failed to parse proxy URI {}: {}", proxy_uri, e)) + let client_has_proxy; + + if let Some(proxy) = proxy.as_ref().and_then(|proxy_url| { + reqwest::Proxy::all(proxy_url.clone()) + .inspect_err(|e| { + log::error!( + "Failed to parse proxy URL '{}': {}", + proxy_url, + e.source().unwrap_or(&e as &_) + ) + }) .ok() }) { client = client.proxy(proxy); - } + client_has_proxy = true; + } else { + client_has_proxy = false; + }; let client = client .use_preconfigured_tls(http_client_tls::tls_config()) .build()?; let mut client: ReqwestClient = client.into(); - client.proxy = proxy; + client.proxy = client_has_proxy.then_some(proxy).flatten(); Ok(client) } } @@ -195,7 +207,7 @@ fn redact_error(mut error: reqwest::Error) -> reqwest::Error { } impl http_client::HttpClient for ReqwestClient { - fn proxy(&self) -> Option<&http::Uri> { + fn proxy(&self) -> Option<&Url> { self.proxy.as_ref() } @@ -256,7 +268,7 @@ impl http_client::HttpClient for ReqwestClient { #[cfg(test)] mod tests { - use http_client::{HttpClient, http}; + use http_client::{HttpClient, Url}; use crate::ReqwestClient; @@ -265,35 +277,38 @@ mod tests { let client = ReqwestClient::new(); assert_eq!(client.proxy(), None); - let proxy = http::Uri::from_static("http://localhost:10809"); + let proxy = Url::parse("http://localhost:10809").unwrap(); let client = ReqwestClient::proxy_and_user_agent(Some(proxy.clone()), "test").unwrap(); assert_eq!(client.proxy(), Some(&proxy)); - let proxy = http::Uri::from_static("https://localhost:10809"); + let proxy = Url::parse("https://localhost:10809").unwrap(); let client = ReqwestClient::proxy_and_user_agent(Some(proxy.clone()), "test").unwrap(); assert_eq!(client.proxy(), Some(&proxy)); - let proxy = http::Uri::from_static("socks4://localhost:10808"); + let proxy = Url::parse("socks4://localhost:10808").unwrap(); let client = ReqwestClient::proxy_and_user_agent(Some(proxy.clone()), "test").unwrap(); assert_eq!(client.proxy(), Some(&proxy)); - let proxy = http::Uri::from_static("socks4a://localhost:10808"); + let proxy = Url::parse("socks4a://localhost:10808").unwrap(); let client = ReqwestClient::proxy_and_user_agent(Some(proxy.clone()), "test").unwrap(); assert_eq!(client.proxy(), Some(&proxy)); - let proxy = http::Uri::from_static("socks5://localhost:10808"); + let proxy = Url::parse("socks5://localhost:10808").unwrap(); let client = ReqwestClient::proxy_and_user_agent(Some(proxy.clone()), "test").unwrap(); assert_eq!(client.proxy(), Some(&proxy)); - let proxy = http::Uri::from_static("socks5h://localhost:10808"); + let proxy = Url::parse("socks5h://localhost:10808").unwrap(); let client = ReqwestClient::proxy_and_user_agent(Some(proxy.clone()), "test").unwrap(); assert_eq!(client.proxy(), Some(&proxy)); } #[test] - #[should_panic] fn test_invalid_proxy_uri() { - let proxy = http::Uri::from_static("file:///etc/hosts"); - ReqwestClient::proxy_and_user_agent(Some(proxy), "test").unwrap(); + let proxy = Url::parse("socks://127.0.0.1:20170").unwrap(); + let client = ReqwestClient::proxy_and_user_agent(Some(proxy), "test").unwrap(); + assert!( + client.proxy.is_none(), + "An invalid proxy URL should add no proxy to the client!" + ) } } diff --git a/crates/zed/src/main.rs b/crates/zed/src/main.rs index 6bf29d964d..7d9c3d2de1 100644 --- a/crates/zed/src/main.rs +++ b/crates/zed/src/main.rs @@ -20,7 +20,7 @@ use git::GitHostingProviderRegistry; use gpui::{App, AppContext as _, Application, AsyncApp, UpdateGlobal as _}; use gpui_tokio::Tokio; -use http_client::{Uri, read_proxy_from_env}; +use http_client::{Url, read_proxy_from_env}; use language::LanguageRegistry; use prompt_store::PromptBuilder; use reqwest_client::ReqwestClient; @@ -354,7 +354,7 @@ fn main() { .as_ref() .and_then(|input| { input - .parse::() + .parse::() .inspect_err(|e| log::error!("Error parsing proxy settings: {}", e)) .ok() }) diff --git a/tooling/workspace-hack/Cargo.toml b/tooling/workspace-hack/Cargo.toml index fcb0d9bd63..cf1d8fa3a1 100644 --- a/tooling/workspace-hack/Cargo.toml +++ b/tooling/workspace-hack/Cargo.toml @@ -263,9 +263,11 @@ rustix-dff4ba8e3ae991db = { package = "rustix", version = "1", features = ["fs", scopeguard = { version = "1" } security-framework = { version = "3", features = ["OSX_10_14"] } security-framework-sys = { version = "2", features = ["OSX_10_14"] } +sync_wrapper = { version = "1", default-features = false, features = ["futures"] } tokio-rustls = { version = "0.26", default-features = false, features = ["ring"] } tokio-socks = { version = "0.5", features = ["futures-io"] } tokio-stream = { version = "0.1", features = ["fs"] } +tower = { version = "0.5", default-features = false, features = ["timeout", "util"] } [target.x86_64-apple-darwin.build-dependencies] clang-sys = { version = "1", default-features = false, features = ["clang_11_0", "runtime"] } @@ -288,9 +290,11 @@ rustix-dff4ba8e3ae991db = { package = "rustix", version = "1", features = ["fs", scopeguard = { version = "1" } security-framework = { version = "3", features = ["OSX_10_14"] } security-framework-sys = { version = "2", features = ["OSX_10_14"] } +sync_wrapper = { version = "1", default-features = false, features = ["futures"] } tokio-rustls = { version = "0.26", default-features = false, features = ["ring"] } tokio-socks = { version = "0.5", features = ["futures-io"] } tokio-stream = { version = "0.1", features = ["fs"] } +tower = { version = "0.5", default-features = false, features = ["timeout", "util"] } [target.aarch64-apple-darwin.dependencies] core-foundation = { version = "0.9" } @@ -311,9 +315,11 @@ rustix-dff4ba8e3ae991db = { package = "rustix", version = "1", features = ["fs", scopeguard = { version = "1" } security-framework = { version = "3", features = ["OSX_10_14"] } security-framework-sys = { version = "2", features = ["OSX_10_14"] } +sync_wrapper = { version = "1", default-features = false, features = ["futures"] } tokio-rustls = { version = "0.26", default-features = false, features = ["ring"] } tokio-socks = { version = "0.5", features = ["futures-io"] } tokio-stream = { version = "0.1", features = ["fs"] } +tower = { version = "0.5", default-features = false, features = ["timeout", "util"] } [target.aarch64-apple-darwin.build-dependencies] clang-sys = { version = "1", default-features = false, features = ["clang_11_0", "runtime"] } @@ -336,9 +342,11 @@ rustix-dff4ba8e3ae991db = { package = "rustix", version = "1", features = ["fs", scopeguard = { version = "1" } security-framework = { version = "3", features = ["OSX_10_14"] } security-framework-sys = { version = "2", features = ["OSX_10_14"] } +sync_wrapper = { version = "1", default-features = false, features = ["futures"] } tokio-rustls = { version = "0.26", default-features = false, features = ["ring"] } tokio-socks = { version = "0.5", features = ["futures-io"] } tokio-stream = { version = "0.1", features = ["fs"] } +tower = { version = "0.5", default-features = false, features = ["timeout", "util"] } [target.x86_64-unknown-linux-gnu.dependencies] aes = { version = "0.8", default-features = false, features = ["zeroize"] } @@ -373,11 +381,13 @@ rustix-dff4ba8e3ae991db = { package = "rustix", version = "1", features = ["fs", scopeguard = { version = "1" } smallvec = { version = "1", default-features = false, features = ["write"] } syn-f595c2ba2a3f28df = { package = "syn", version = "2", features = ["extra-traits", "fold", "full", "visit", "visit-mut"] } +sync_wrapper = { version = "1", default-features = false, features = ["futures"] } tokio-rustls = { version = "0.26", default-features = false, features = ["ring"] } tokio-socks = { version = "0.5", features = ["futures-io"] } tokio-stream = { version = "0.1", features = ["fs"] } toml_datetime = { version = "0.6", default-features = false, features = ["serde"] } toml_edit = { version = "0.22", default-features = false, features = ["display", "parse", "serde"] } +tower = { version = "0.5", default-features = false, features = ["timeout", "util"] } zeroize = { version = "1", features = ["zeroize_derive"] } zvariant = { version = "5", default-features = false, features = ["enumflags2", "gvariant", "url"] } @@ -412,11 +422,13 @@ rustix-d585fab2519d2d1 = { package = "rustix", version = "0.38", features = ["ev rustix-dff4ba8e3ae991db = { package = "rustix", version = "1", features = ["fs", "net", "process", "termios", "time"] } scopeguard = { version = "1" } smallvec = { version = "1", default-features = false, features = ["write"] } +sync_wrapper = { version = "1", default-features = false, features = ["futures"] } tokio-rustls = { version = "0.26", default-features = false, features = ["ring"] } tokio-socks = { version = "0.5", features = ["futures-io"] } tokio-stream = { version = "0.1", features = ["fs"] } toml_datetime = { version = "0.6", default-features = false, features = ["serde"] } toml_edit = { version = "0.22", default-features = false, features = ["display", "parse", "serde"] } +tower = { version = "0.5", default-features = false, features = ["timeout", "util"] } zeroize = { version = "1", features = ["zeroize_derive"] } zvariant = { version = "5", default-features = false, features = ["enumflags2", "gvariant", "url"] } @@ -453,11 +465,13 @@ rustix-dff4ba8e3ae991db = { package = "rustix", version = "1", features = ["fs", scopeguard = { version = "1" } smallvec = { version = "1", default-features = false, features = ["write"] } syn-f595c2ba2a3f28df = { package = "syn", version = "2", features = ["extra-traits", "fold", "full", "visit", "visit-mut"] } +sync_wrapper = { version = "1", default-features = false, features = ["futures"] } tokio-rustls = { version = "0.26", default-features = false, features = ["ring"] } tokio-socks = { version = "0.5", features = ["futures-io"] } tokio-stream = { version = "0.1", features = ["fs"] } toml_datetime = { version = "0.6", default-features = false, features = ["serde"] } toml_edit = { version = "0.22", default-features = false, features = ["display", "parse", "serde"] } +tower = { version = "0.5", default-features = false, features = ["timeout", "util"] } zeroize = { version = "1", features = ["zeroize_derive"] } zvariant = { version = "5", default-features = false, features = ["enumflags2", "gvariant", "url"] } @@ -492,11 +506,13 @@ rustix-d585fab2519d2d1 = { package = "rustix", version = "0.38", features = ["ev rustix-dff4ba8e3ae991db = { package = "rustix", version = "1", features = ["fs", "net", "process", "termios", "time"] } scopeguard = { version = "1" } smallvec = { version = "1", default-features = false, features = ["write"] } +sync_wrapper = { version = "1", default-features = false, features = ["futures"] } tokio-rustls = { version = "0.26", default-features = false, features = ["ring"] } tokio-socks = { version = "0.5", features = ["futures-io"] } tokio-stream = { version = "0.1", features = ["fs"] } toml_datetime = { version = "0.6", default-features = false, features = ["serde"] } toml_edit = { version = "0.22", default-features = false, features = ["display", "parse", "serde"] } +tower = { version = "0.5", default-features = false, features = ["timeout", "util"] } zeroize = { version = "1", features = ["zeroize_derive"] } zvariant = { version = "5", default-features = false, features = ["enumflags2", "gvariant", "url"] } @@ -512,9 +528,11 @@ naga = { version = "23", features = ["spv-out", "wgsl-in"] } ring = { version = "0.17", features = ["std"] } rustix-d585fab2519d2d1 = { package = "rustix", version = "0.38", features = ["event"] } scopeguard = { version = "1" } +sync_wrapper = { version = "1", default-features = false, features = ["futures"] } tokio-rustls = { version = "0.26", default-features = false, features = ["ring"] } tokio-socks = { version = "0.5", features = ["futures-io"] } tokio-stream = { version = "0.1", features = ["fs"] } +tower = { version = "0.5", default-features = false, features = ["timeout", "util"] } winapi = { version = "0.3", default-features = false, features = ["cfg", "consoleapi", "errhandlingapi", "evntrace", "fileapi", "handleapi", "in6addr", "inaddr", "knownfolders", "minwinbase", "ntsecapi", "objbase", "processenv", "processthreadsapi", "shlobj", "std", "sysinfoapi", "winbase", "windef", "winerror", "winioctl"] } windows-core = { version = "0.61" } windows-numerics = { version = "0.2" } @@ -535,9 +553,11 @@ proc-macro2 = { version = "1", default-features = false, features = ["span-locat ring = { version = "0.17", features = ["std"] } rustix-d585fab2519d2d1 = { package = "rustix", version = "0.38", features = ["event"] } scopeguard = { version = "1" } +sync_wrapper = { version = "1", default-features = false, features = ["futures"] } tokio-rustls = { version = "0.26", default-features = false, features = ["ring"] } tokio-socks = { version = "0.5", features = ["futures-io"] } tokio-stream = { version = "0.1", features = ["fs"] } +tower = { version = "0.5", default-features = false, features = ["timeout", "util"] } winapi = { version = "0.3", default-features = false, features = ["cfg", "consoleapi", "errhandlingapi", "evntrace", "fileapi", "handleapi", "in6addr", "inaddr", "knownfolders", "minwinbase", "ntsecapi", "objbase", "processenv", "processthreadsapi", "shlobj", "std", "sysinfoapi", "winbase", "windef", "winerror", "winioctl"] } windows-core = { version = "0.61" } windows-numerics = { version = "0.2" } @@ -578,11 +598,13 @@ rustix-dff4ba8e3ae991db = { package = "rustix", version = "1", features = ["fs", scopeguard = { version = "1" } smallvec = { version = "1", default-features = false, features = ["write"] } syn-f595c2ba2a3f28df = { package = "syn", version = "2", features = ["extra-traits", "fold", "full", "visit", "visit-mut"] } +sync_wrapper = { version = "1", default-features = false, features = ["futures"] } tokio-rustls = { version = "0.26", default-features = false, features = ["ring"] } tokio-socks = { version = "0.5", features = ["futures-io"] } tokio-stream = { version = "0.1", features = ["fs"] } toml_datetime = { version = "0.6", default-features = false, features = ["serde"] } toml_edit = { version = "0.22", default-features = false, features = ["display", "parse", "serde"] } +tower = { version = "0.5", default-features = false, features = ["timeout", "util"] } zeroize = { version = "1", features = ["zeroize_derive"] } zvariant = { version = "5", default-features = false, features = ["enumflags2", "gvariant", "url"] } @@ -617,11 +639,13 @@ rustix-d585fab2519d2d1 = { package = "rustix", version = "0.38", features = ["ev rustix-dff4ba8e3ae991db = { package = "rustix", version = "1", features = ["fs", "net", "process", "termios", "time"] } scopeguard = { version = "1" } smallvec = { version = "1", default-features = false, features = ["write"] } +sync_wrapper = { version = "1", default-features = false, features = ["futures"] } tokio-rustls = { version = "0.26", default-features = false, features = ["ring"] } tokio-socks = { version = "0.5", features = ["futures-io"] } tokio-stream = { version = "0.1", features = ["fs"] } toml_datetime = { version = "0.6", default-features = false, features = ["serde"] } toml_edit = { version = "0.22", default-features = false, features = ["display", "parse", "serde"] } +tower = { version = "0.5", default-features = false, features = ["timeout", "util"] } zeroize = { version = "1", features = ["zeroize_derive"] } zvariant = { version = "5", default-features = false, features = ["enumflags2", "gvariant", "url"] }