From fa19bc98acb6a570354fbf8c56cda5ba7e0df7d9 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 30 Jul 2024 11:12:37 +0200 Subject: [PATCH] Provide user agent when performing HTTP requests (#15470) Release Notes: - N/A --------- Co-authored-by: Nathan --- crates/client/src/client.rs | 7 ++++++ crates/collab/src/rpc.rs | 3 ++- crates/extension/src/extension_store.rs | 5 +--- crates/extension_cli/src/main.rs | 8 ++++++- crates/gpui/src/app.rs | 6 ++--- crates/http_client/src/http_client.rs | 31 ++++++++++++++----------- crates/semantic_index/examples/index.rs | 2 +- crates/zed/src/main.rs | 2 +- 8 files changed, 40 insertions(+), 24 deletions(-) diff --git a/crates/client/src/client.rs b/crates/client/src/client.rs index 9559d12b6a..16a8873ec7 100644 --- a/crates/client/src/client.rs +++ b/crates/client/src/client.rs @@ -541,9 +541,16 @@ impl Client { } pub fn production(cx: &mut AppContext) -> Arc { + let user_agent = format!( + "Zed/{} ({}; {})", + AppVersion::global(cx), + std::env::consts::OS, + std::env::consts::ARCH + ); let clock = Arc::new(clock::RealSystemClock); let http = Arc::new(HttpClientWithUrl::new( &ClientSettings::get_global(cx).server_url, + Some(user_agent), ProxySettings::get_global(cx).proxy.clone(), )); Self::new(clock, http.clone(), cx) diff --git a/crates/collab/src/rpc.rs b/crates/collab/src/rpc.rs index bced91eb60..25e8f9edf8 100644 --- a/crates/collab/src/rpc.rs +++ b/crates/collab/src/rpc.rs @@ -995,7 +995,8 @@ impl Server { tracing::Span::current().record("connection_id", format!("{}", connection_id)); tracing::info!("connection opened"); - let http_client = match IsahcHttpClient::new() { + let user_agent = format!("Zed Server/{}", env!("CARGO_PKG_VERSION")); + let http_client = match IsahcHttpClient::builder().default_header("User-Agent", user_agent).build() { Ok(http_client) => Arc::new(http_client), Err(error) => { tracing::error!(?error, "failed to create HTTP client"); diff --git a/crates/extension/src/extension_store.rs b/crates/extension/src/extension_store.rs index 316c32832a..f5bcd08285 100644 --- a/crates/extension/src/extension_store.rs +++ b/crates/extension/src/extension_store.rs @@ -243,10 +243,7 @@ impl ExtensionStore { extension_index: Default::default(), installed_dir, index_path, - builder: Arc::new(ExtensionBuilder::new( - ::http_client::client(http_client.proxy().cloned()), - build_dir, - )), + builder: Arc::new(ExtensionBuilder::new(http_client.clone(), build_dir)), outstanding_operations: Default::default(), modified_extensions: Default::default(), reload_complete_senders: Vec::new(), diff --git a/crates/extension_cli/src/main.rs b/crates/extension_cli/src/main.rs index 71bf095b1b..4f231d4953 100644 --- a/crates/extension_cli/src/main.rs +++ b/crates/extension_cli/src/main.rs @@ -60,7 +60,13 @@ async fn main() -> Result<()> { log::info!("compiling extension"); - let http_client = Arc::new(HttpClientWithProxy::new(None)); + let user_agent = format!( + "Zed Extension CLI/{} ({}; {})", + env!("CARGO_PKG_VERSION"), + std::env::consts::OS, + std::env::consts::ARCH + ); + let http_client = Arc::new(HttpClientWithProxy::new(Some(user_agent), None)); let builder = ExtensionBuilder::new(http_client, scratch_dir); builder .compile_extension( diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index edbe96e582..a00b8cc652 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -114,7 +114,7 @@ impl App { Self(AppContext::new( current_platform(false), Arc::new(()), - http_client::client(None), + http_client::client(None, None), )) } @@ -125,7 +125,7 @@ impl App { Self(AppContext::new( current_platform(true), Arc::new(()), - http_client::client(None), + http_client::client(None, None), )) } @@ -667,7 +667,7 @@ impl AppContext { } /// Updates the http client assigned to GPUI - pub fn update_http_client(&mut self, new_client: Arc) { + pub fn set_http_client(&mut self, new_client: Arc) { self.http_client = new_client; } diff --git a/crates/http_client/src/http_client.rs b/crates/http_client/src/http_client.rs index 9605b46b83..fe5174a982 100644 --- a/crates/http_client/src/http_client.rs +++ b/crates/http_client/src/http_client.rs @@ -73,7 +73,7 @@ pub struct HttpClientWithProxy { impl HttpClientWithProxy { /// Returns a new [`HttpClientWithProxy`] with the given proxy URL. - pub fn new(proxy_url: Option) -> Self { + pub fn new(user_agent: Option, proxy_url: Option) -> Self { let proxy_url = proxy_url .and_then(|input| { input @@ -84,7 +84,7 @@ impl HttpClientWithProxy { .or_else(read_proxy_from_env); Self { - client: client(proxy_url.clone()), + client: client(user_agent, proxy_url.clone()), proxy: proxy_url, } } @@ -124,8 +124,12 @@ pub struct HttpClientWithUrl { impl HttpClientWithUrl { /// Returns a new [`HttpClientWithUrl`] with the given base URL. - pub fn new(base_url: impl Into, proxy_url: Option) -> Self { - let client = HttpClientWithProxy::new(proxy_url); + pub fn new( + base_url: impl Into, + user_agent: Option, + proxy_url: Option, + ) -> Self { + let client = HttpClientWithProxy::new(user_agent, proxy_url); Self { base_url: Mutex::new(base_url.into()), @@ -199,16 +203,17 @@ impl HttpClient for HttpClientWithUrl { } } -pub fn client(proxy: Option) -> Arc { +pub fn client(user_agent: Option, proxy: Option) -> Arc { + let mut builder = isahc::HttpClient::builder() + .connect_timeout(Duration::from_secs(5)) + .low_speed_timeout(100, Duration::from_secs(5)) + .proxy(proxy.clone()); + if let Some(user_agent) = user_agent { + builder = builder.default_header("User-Agent", user_agent); + } + Arc::new(HttpClientWithProxy { - client: Arc::new( - isahc::HttpClient::builder() - .connect_timeout(Duration::from_secs(5)) - .low_speed_timeout(100, Duration::from_secs(5)) - .proxy(proxy.clone()) - .build() - .unwrap(), - ), + client: Arc::new(builder.build().unwrap()), proxy, }) } diff --git a/crates/semantic_index/examples/index.rs b/crates/semantic_index/examples/index.rs index cc09375167..e536ea1db6 100644 --- a/crates/semantic_index/examples/index.rs +++ b/crates/semantic_index/examples/index.rs @@ -26,7 +26,7 @@ fn main() { }); let clock = Arc::new(FakeSystemClock::default()); - let http = Arc::new(HttpClientWithUrl::new("http://localhost:11434", None)); + let http = Arc::new(HttpClientWithUrl::new("http://localhost:11434", None, None)); let client = client::Client::new(clock, http.clone(), cx); Client::set_global(client.clone(), cx); diff --git a/crates/zed/src/main.rs b/crates/zed/src/main.rs index d0bda24385..4cd3c5539b 100644 --- a/crates/zed/src/main.rs +++ b/crates/zed/src/main.rs @@ -416,7 +416,7 @@ fn main() { client::init_settings(cx); let client = Client::production(cx); - cx.update_http_client(client.http_client().clone()); + cx.set_http_client(client.http_client().clone()); let mut languages = LanguageRegistry::new(login_shell_env_loaded, cx.background_executor().clone()); languages.set_language_server_download_dir(paths::languages_dir().clone());