mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-22 13:38:43 +00:00
Merge pull request #198 from NobodyXu/optimize/reuse-client
This commit is contained in:
commit
9294b4e4bc
3 changed files with 32 additions and 39 deletions
|
@ -7,13 +7,10 @@ use tokio::task::JoinHandle;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
use super::Data;
|
use super::Data;
|
||||||
use crate::{
|
use crate::{download_and_extract, get_reqwest_client, remote_exists, BinstallError, PkgFmt};
|
||||||
download_and_extract, new_reqwest_client_builder, remote_exists, BinstallError, PkgFmt,
|
|
||||||
};
|
|
||||||
|
|
||||||
const BASE_URL: &str = "https://github.com/alsuren/cargo-quickinstall/releases/download";
|
const BASE_URL: &str = "https://github.com/alsuren/cargo-quickinstall/releases/download";
|
||||||
const STATS_URL: &str = "https://warehouse-clerk-tmp.vercel.app/api/crate";
|
const STATS_URL: &str = "https://warehouse-clerk-tmp.vercel.app/api/crate";
|
||||||
const USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
|
|
||||||
|
|
||||||
pub struct QuickInstall {
|
pub struct QuickInstall {
|
||||||
package: String,
|
package: String,
|
||||||
|
@ -91,9 +88,7 @@ impl QuickInstall {
|
||||||
let url = Url::parse(&stats_url)?;
|
let url = Url::parse(&stats_url)?;
|
||||||
debug!("Sending installation report to quickinstall ({url})");
|
debug!("Sending installation report to quickinstall ({url})");
|
||||||
|
|
||||||
new_reqwest_client_builder()
|
get_reqwest_client()
|
||||||
.user_agent(USER_AGENT)
|
|
||||||
.build()?
|
|
||||||
.request(Method::HEAD, url.clone())
|
.request(Method::HEAD, url.clone())
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
|
|
|
@ -35,15 +35,6 @@ pub use path_ext::*;
|
||||||
mod tls_version;
|
mod tls_version;
|
||||||
pub use tls_version::TLSVersion;
|
pub use tls_version::TLSVersion;
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct ReqwestConfig {
|
|
||||||
pub secure: bool,
|
|
||||||
pub min_tls: Option<tls::Version>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// (secure mode, min TLS version)
|
|
||||||
pub static REQWESTGLOBALCONFIG: OnceCell<ReqwestConfig> = OnceCell::new();
|
|
||||||
|
|
||||||
/// Load binstall metadata from the crate `Cargo.toml` at the provided path
|
/// Load binstall metadata from the crate `Cargo.toml` at the provided path
|
||||||
pub fn load_manifest_path<P: AsRef<Path>>(
|
pub fn load_manifest_path<P: AsRef<Path>>(
|
||||||
manifest_path: P,
|
manifest_path: P,
|
||||||
|
@ -59,30 +50,42 @@ pub fn load_manifest_path<P: AsRef<Path>>(
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_reqwest_client_builder() -> ClientBuilder {
|
static CLIENT: OnceCell<Client> = OnceCell::new();
|
||||||
let mut builder = ClientBuilder::new();
|
|
||||||
|
|
||||||
if let Some(ReqwestConfig { secure, min_tls }) = REQWESTGLOBALCONFIG.get() {
|
/// Should only be called once in main::entry.
|
||||||
if *secure {
|
pub fn initialize_reqwest_client(
|
||||||
builder = builder
|
secure: bool,
|
||||||
.https_only(true)
|
min_tls: Option<tls::Version>,
|
||||||
.min_tls_version(tls::Version::TLS_1_2)
|
) -> Result<(), BinstallError> {
|
||||||
}
|
const USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
|
||||||
|
|
||||||
if let Some(ver) = *min_tls {
|
let mut builder = ClientBuilder::new().user_agent(USER_AGENT);
|
||||||
builder = builder.min_tls_version(ver);
|
|
||||||
}
|
if secure {
|
||||||
|
builder = builder
|
||||||
|
.https_only(true)
|
||||||
|
.min_tls_version(tls::Version::TLS_1_2);
|
||||||
}
|
}
|
||||||
|
|
||||||
builder
|
if let Some(ver) = min_tls {
|
||||||
|
builder = builder.min_tls_version(ver);
|
||||||
|
}
|
||||||
|
|
||||||
|
let client = builder.build()?;
|
||||||
|
|
||||||
|
CLIENT
|
||||||
|
.set(client)
|
||||||
|
.expect("Reqwest client already initialized");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_reqwest_client() -> reqwest::Result<Client> {
|
pub fn get_reqwest_client() -> &'static Client {
|
||||||
new_reqwest_client_builder().build()
|
CLIENT.get().expect("Reqwest client is not initialized")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn remote_exists(url: Url, method: Method) -> Result<bool, BinstallError> {
|
pub async fn remote_exists(url: Url, method: Method) -> Result<bool, BinstallError> {
|
||||||
let req = new_reqwest_client()?
|
let req = get_reqwest_client()
|
||||||
.request(method.clone(), url.clone())
|
.request(method.clone(), url.clone())
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
|
@ -95,7 +98,7 @@ async fn create_request(
|
||||||
) -> Result<impl Stream<Item = reqwest::Result<Bytes>>, BinstallError> {
|
) -> Result<impl Stream<Item = reqwest::Result<Bytes>>, BinstallError> {
|
||||||
debug!("Downloading from: '{url}'");
|
debug!("Downloading from: '{url}'");
|
||||||
|
|
||||||
new_reqwest_client()?
|
get_reqwest_client()
|
||||||
.get(url.clone())
|
.get(url.clone())
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
|
|
|
@ -200,13 +200,8 @@ async fn entry() -> Result<()> {
|
||||||
bin_dir: opts.bin_dir.take(),
|
bin_dir: opts.bin_dir.take(),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Initialize REQWESTGLOBALCONFIG
|
// Initialize reqwest client
|
||||||
REQWESTGLOBALCONFIG
|
initialize_reqwest_client(opts.secure, opts.min_tls_version.map(|v| v.into()))?;
|
||||||
.set(ReqwestConfig {
|
|
||||||
secure: opts.secure,
|
|
||||||
min_tls: opts.min_tls_version.map(|v| v.into()),
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// Setup logging
|
// Setup logging
|
||||||
let mut log_config = ConfigBuilder::new();
|
let mut log_config = ConfigBuilder::new();
|
||||||
|
|
Loading…
Add table
Reference in a new issue