From 0a449f6ea8f3ac352ba540a0ea21ad060a3cba50 Mon Sep 17 00:00:00 2001 From: Sergey Kvachonok Date: Tue, 30 Apr 2024 14:55:32 +0300 Subject: [PATCH] feat: Add HTTP client `User-Agent` header Identify the download client as a proxy server to the crates.io servers. --- src/main.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 06906c6..f4af73e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -96,6 +96,9 @@ const JSON_HTTP_CTYPE: &str = "Content-Type: application/json; charset=utf-8"; /// Program version tag: `".."` const VERSION: &str = env!("CARGO_PKG_VERSION"); +/// HTTP client User Agent string +const HTTP_USER_AGENT: &str = concat!("crates-io-proxy/", env!("CARGO_PKG_VERSION")); + /// Proxy server configuration #[derive(Debug, Clone)] struct ProxyConfig { @@ -135,7 +138,10 @@ struct IndexResponse { /// The global agent instance is required to use HTTP request pipelining. fn ureq_agent() -> ureq::Agent { static AGENT: OnceLock = OnceLock::new(); - AGENT.get_or_init(ureq::agent).clone() + + AGENT + .get_or_init(|| ureq::builder().user_agent(HTTP_USER_AGENT).build()) + .clone() } /// Downloads the crate file from the upstream download server