Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,25 @@ impl Client {
}

pub async fn get_text<U: IntoUrl>(&self, url: U) -> Result<String> {
self.get_text_with_headers(url, &HeaderMap::new()).await
}

pub async fn get_text_with_headers<U: IntoUrl>(
&self,
url: U,
extra_headers: &HeaderMap,
) -> Result<String> {
let mut url = url.into_url().unwrap();
let resp = self.get_async(url.clone()).await?;
// Merge GitHub headers with any extra headers provided
let mut headers = github_headers(&url);
headers.extend(extra_headers.clone());
let resp = self.get_async_with_headers(url.clone(), &headers).await?;
let text = resp.text().await?;
if text.starts_with("<!DOCTYPE html>") {
if url.scheme() == "http" {
// try with https since http may be blocked
url.set_scheme("https").unwrap();
return Box::pin(self.get_text(url)).await;
return Box::pin(self.get_text_with_headers(url, extra_headers)).await;
}
bail!("Got HTML instead of text from {}", url);
}
Expand Down Expand Up @@ -243,10 +254,22 @@ impl Client {

/// POST JSON data to a URL. Returns Ok(true) on success, Ok(false) on non-success status.
/// Errors only on network/connection failures.
#[allow(dead_code)]
pub async fn post_json<U: IntoUrl, T: serde::Serialize>(
&self,
url: U,
body: &T,
) -> Result<bool> {
self.post_json_with_headers(url, body, &HeaderMap::new())
.await
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused post_json function marked with dead code suppression

Low Severity

The post_json function is now unused after this PR, since its only caller (track_install_async) now calls post_json_with_headers directly. Searching for .post_json( returns no matches in the codebase. Instead of removing this dead code, the PR adds #[allow(dead_code)] to suppress the compiler warning. Dead code with suppressed warnings clutters the codebase and adds maintenance burden.

Fix in Cursor Fix in Web


/// POST JSON data to a URL with custom headers.
pub async fn post_json_with_headers<U: IntoUrl, T: serde::Serialize>(
&self,
url: U,
body: &T,
headers: &HeaderMap,
) -> Result<bool> {
ensure!(!*env::OFFLINE, "offline mode is enabled");
let url = url.into_url()?;
Expand All @@ -255,6 +278,7 @@ impl Client {
.reqwest
.post(url)
.header("Content-Type", "application/json")
.headers(headers.clone())
.json(body)
.send()
.await?;
Expand Down
20 changes: 18 additions & 2 deletions src/versions_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::http;
use crate::http::HTTP_FETCH;
use crate::plugins::core::CORE_PLUGINS;
use crate::registry::REGISTRY;
use reqwest::header::{HeaderMap, HeaderValue};
use std::{
collections::{HashMap, HashSet},
sync::{
Expand All @@ -13,6 +14,15 @@ use std::{
};
use tokio::sync::Mutex;

/// Headers for requests to mise-versions, including CI detection
static VERSIONS_HOST_HEADERS: LazyLock<HeaderMap> = LazyLock::new(|| {
let mut headers = HeaderMap::new();
if ci_info::is_ci() {
headers.insert("x-mise-ci", HeaderValue::from_static("true"));
}
headers
});

/// Tools that use the versions host for listing versions
/// (excludes java/python due to complex version schemes)
static PLUGINS_USE_VERSION_HOST: LazyLock<HashSet<&str>> = LazyLock::new(|| {
Expand Down Expand Up @@ -68,7 +78,10 @@ pub async fn list_versions(tool: &str) -> eyre::Result<Option<Vec<VersionInfo>>>

// Use TOML format which includes created_at timestamps
let url = format!("https://mise-versions.jdx.dev/tools/{}.toml", tool);
let versions: Vec<VersionInfo> = match HTTP_FETCH.get_text(&url).await {
let versions: Vec<VersionInfo> = match HTTP_FETCH
.get_text_with_headers(&url, &VERSIONS_HOST_HEADERS)
.await
{
Ok(body) => {
let response: VersionsResponse = toml::from_str(&body)?;
response
Expand Down Expand Up @@ -150,7 +163,10 @@ async fn track_install_async(tool: &str, full: &str, version: &str) -> eyre::Res
"arch": *ARCH
});

match HTTP_FETCH.post_json(url, &body).await {
match HTTP_FETCH
.post_json_with_headers(url, &body, &VERSIONS_HOST_HEADERS)
.await
{
Ok(true) => trace!("Tracked install: {full}@{version}"),
Ok(false) => trace!("Track request failed"),
Err(e) => trace!("Track request error: {e}"),
Expand Down
Loading