mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-05-06 04:00:02 +00:00
Refactor: Replace REQWESTCONFIG
with initialize_reqwest_client
so that we don't need two `OnceCell`s. Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
50b436100e
commit
6582eefd25
3 changed files with 29 additions and 34 deletions
|
@ -88,7 +88,7 @@ impl QuickInstall {
|
|||
let url = Url::parse(&stats_url)?;
|
||||
debug!("Sending installation report to quickinstall ({url})");
|
||||
|
||||
new_reqwest_client()?
|
||||
new_reqwest_client()
|
||||
.request(Method::HEAD, url.clone())
|
||||
.send()
|
||||
.await
|
||||
|
|
|
@ -35,15 +35,6 @@ pub use path_ext::*;
|
|||
mod tls_version;
|
||||
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
|
||||
pub fn load_manifest_path<P: AsRef<Path>>(
|
||||
manifest_path: P,
|
||||
|
@ -59,33 +50,42 @@ pub fn load_manifest_path<P: AsRef<Path>>(
|
|||
})
|
||||
}
|
||||
|
||||
fn new_reqwest_client_builder() -> ClientBuilder {
|
||||
static CLIENT: OnceCell<Client> = OnceCell::new();
|
||||
|
||||
/// Should only be called once in main::entry.
|
||||
pub fn initialize_reqwest_client(
|
||||
secure: bool,
|
||||
min_tls: Option<tls::Version>,
|
||||
) -> Result<(), BinstallError> {
|
||||
const USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
let mut builder = ClientBuilder::new().user_agent(USER_AGENT);
|
||||
|
||||
if let Some(ReqwestConfig { secure, min_tls }) = REQWESTGLOBALCONFIG.get() {
|
||||
if *secure {
|
||||
if secure {
|
||||
builder = builder
|
||||
.https_only(true)
|
||||
.min_tls_version(tls::Version::TLS_1_2)
|
||||
.min_tls_version(tls::Version::TLS_1_2);
|
||||
}
|
||||
|
||||
if let Some(ver) = *min_tls {
|
||||
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(())
|
||||
}
|
||||
|
||||
builder
|
||||
}
|
||||
|
||||
pub fn new_reqwest_client() -> reqwest::Result<&'static Client> {
|
||||
static CLIENT: OnceCell<Client> = OnceCell::new();
|
||||
CLIENT.get_or_try_init(|| new_reqwest_client_builder().build())
|
||||
pub fn new_reqwest_client() -> &'static Client {
|
||||
CLIENT.get().expect("Reqwest client is not initialized")
|
||||
}
|
||||
|
||||
pub async fn remote_exists(url: Url, method: Method) -> Result<bool, BinstallError> {
|
||||
let req = new_reqwest_client()?
|
||||
let req = new_reqwest_client()
|
||||
.request(method.clone(), url.clone())
|
||||
.send()
|
||||
.await
|
||||
|
@ -98,7 +98,7 @@ async fn create_request(
|
|||
) -> Result<impl Stream<Item = reqwest::Result<Bytes>>, BinstallError> {
|
||||
debug!("Downloading from: '{url}'");
|
||||
|
||||
new_reqwest_client()?
|
||||
new_reqwest_client()
|
||||
.get(url.clone())
|
||||
.send()
|
||||
.await
|
||||
|
|
|
@ -200,13 +200,8 @@ async fn entry() -> Result<()> {
|
|||
bin_dir: opts.bin_dir.take(),
|
||||
};
|
||||
|
||||
// Initialize REQWESTGLOBALCONFIG
|
||||
REQWESTGLOBALCONFIG
|
||||
.set(ReqwestConfig {
|
||||
secure: opts.secure,
|
||||
min_tls: opts.min_tls_version.map(|v| v.into()),
|
||||
})
|
||||
.unwrap();
|
||||
// Initialize reqwest client
|
||||
initialize_reqwest_client(opts.secure, opts.min_tls_version.map(|v| v.into()))?;
|
||||
|
||||
// Setup logging
|
||||
let mut log_config = ConfigBuilder::new();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue