From 7f75c394502d32a9a8967dcf3602141098fdd07d Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 22 Feb 2026 15:23:52 +0900 Subject: refactor(http): centralize User-Agent string in mozart-core Add mozart_core::http::user_agent() that returns a consistent "Mozart/ (; )" string. Replace all scattered user-agent definitions across mozart-registry and mozart CLI commands. Co-Authored-By: Claude Opus 4.6 --- crates/mozart-core/src/http.rs | 11 +++++++++++ crates/mozart-core/src/lib.rs | 1 + crates/mozart-registry/src/downloader.rs | 5 ++++- crates/mozart-registry/src/packagist.rs | 9 ++++++--- crates/mozart/src/commands/diagnose.rs | 4 ++-- crates/mozart/src/commands/self_update.rs | 4 ++-- 6 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 crates/mozart-core/src/http.rs (limited to 'crates') diff --git a/crates/mozart-core/src/http.rs b/crates/mozart-core/src/http.rs new file mode 100644 index 0000000..ebd28f9 --- /dev/null +++ b/crates/mozart-core/src/http.rs @@ -0,0 +1,11 @@ +/// Returns the common User-Agent string for all HTTP requests. +/// +/// Format: `Mozart/ (; )` +pub fn user_agent() -> String { + format!( + "Mozart/{} ({}; {})", + env!("CARGO_PKG_VERSION"), + std::env::consts::OS, + std::env::consts::ARCH, + ) +} diff --git a/crates/mozart-core/src/lib.rs b/crates/mozart-core/src/lib.rs index b02e5a3..aef7af3 100644 --- a/crates/mozart-core/src/lib.rs +++ b/crates/mozart-core/src/lib.rs @@ -1,5 +1,6 @@ pub mod console; pub mod exit_code; +pub mod http; pub mod package; pub mod platform; pub mod suggest; diff --git a/crates/mozart-registry/src/downloader.rs b/crates/mozart-registry/src/downloader.rs index 9a5ed24..8bd99c7 100644 --- a/crates/mozart-registry/src/downloader.rs +++ b/crates/mozart-registry/src/downloader.rs @@ -108,7 +108,10 @@ pub async fn download_dist( } } - let response = reqwest::get(url).await?; + let client = reqwest::Client::builder() + .user_agent(mozart_core::http::user_agent()) + .build()?; + let response = client.get(url).send().await?; if !response.status().is_success() { anyhow::bail!( diff --git a/crates/mozart-registry/src/packagist.rs b/crates/mozart-registry/src/packagist.rs index e851955..ac290fb 100644 --- a/crates/mozart-registry/src/packagist.rs +++ b/crates/mozart-registry/src/packagist.rs @@ -215,7 +215,10 @@ pub async fn fetch_package_versions( // Cache miss — fetch from Packagist let url = format!("https://repo.packagist.org/p2/{package_name}.json"); - let response = reqwest::get(&url).await?; + let client = reqwest::Client::builder() + .user_agent(mozart_core::http::user_agent()) + .build()?; + let response = client.get(&url).send().await?; if !response.status().is_success() { anyhow::bail!( @@ -285,7 +288,7 @@ pub async fn search_packages( package_type: Option<&str>, ) -> anyhow::Result<(Vec, u64)> { let client = reqwest::Client::builder() - .user_agent("mozart/0.1.0") + .user_agent(mozart_core::http::user_agent()) .build()?; let mut all_results: Vec = Vec::new(); @@ -396,7 +399,7 @@ pub async fn fetch_security_advisories( package_names: &[&str], ) -> anyhow::Result>> { let client = reqwest::Client::builder() - .user_agent("mozart/0.1.0") + .user_agent(mozart_core::http::user_agent()) .build()?; let mut all_advisories: BTreeMap> = BTreeMap::new(); diff --git a/crates/mozart/src/commands/diagnose.rs b/crates/mozart/src/commands/diagnose.rs index da37137..dc1ea85 100644 --- a/crates/mozart/src/commands/diagnose.rs +++ b/crates/mozart/src/commands/diagnose.rs @@ -83,7 +83,7 @@ async fn check_http_connectivity(url: &str) -> CheckResult { let client = match reqwest::Client::builder() .timeout(std::time::Duration::from_secs(10)) - .user_agent(concat!("mozart/", env!("CARGO_PKG_VERSION"))) + .user_agent(mozart_core::http::user_agent()) .build() { Ok(c) => c, @@ -111,7 +111,7 @@ async fn check_github_api() -> CheckResult { let client = match reqwest::Client::builder() .timeout(std::time::Duration::from_secs(10)) - .user_agent(concat!("mozart/", env!("CARGO_PKG_VERSION"))) + .user_agent(mozart_core::http::user_agent()) .build() { Ok(c) => c, diff --git a/crates/mozart/src/commands/self_update.rs b/crates/mozart/src/commands/self_update.rs index 9f6a7fe..ad9ae74 100644 --- a/crates/mozart/src/commands/self_update.rs +++ b/crates/mozart/src/commands/self_update.rs @@ -133,7 +133,7 @@ async fn fetch_releases(include_prerelease: bool) -> anyhow::Result anyhow::Result<()> { let client = reqwest::Client::builder() .timeout(std::time::Duration::from_secs(300)) - .user_agent(concat!("mozart/", env!("CARGO_PKG_VERSION"))) + .user_agent(mozart_core::http::user_agent()) .build() .map_err(|e| anyhow::anyhow!("Could not build HTTP client: {e}"))?; -- cgit v1.3.1