mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-05-05 19:50:02 +00:00
Rename lib to binstalk (#361)
This commit is contained in:
parent
a94d83f0d5
commit
e25aa50ec9
49 changed files with 25 additions and 25 deletions
69
crates/binstalk/src/helpers/remote.rs
Normal file
69
crates/binstalk/src/helpers/remote.rs
Normal file
|
@ -0,0 +1,69 @@
|
|||
use std::env;
|
||||
|
||||
use bytes::Bytes;
|
||||
use futures_util::stream::Stream;
|
||||
use log::debug;
|
||||
use reqwest::{tls, Client, ClientBuilder, Method, Response};
|
||||
use url::Url;
|
||||
|
||||
use crate::errors::BinstallError;
|
||||
|
||||
pub fn create_reqwest_client(min_tls: Option<tls::Version>) -> Result<Client, BinstallError> {
|
||||
const USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
let mut builder = ClientBuilder::new()
|
||||
.user_agent(USER_AGENT)
|
||||
.https_only(true)
|
||||
.min_tls_version(tls::Version::TLS_1_2);
|
||||
|
||||
if let Some(ver) = min_tls {
|
||||
builder = builder.min_tls_version(ver);
|
||||
}
|
||||
|
||||
Ok(builder.build()?)
|
||||
}
|
||||
|
||||
pub async fn remote_exists(
|
||||
client: Client,
|
||||
url: Url,
|
||||
method: Method,
|
||||
) -> Result<bool, BinstallError> {
|
||||
let req = client
|
||||
.request(method.clone(), url.clone())
|
||||
.send()
|
||||
.await
|
||||
.map_err(|err| BinstallError::Http { method, url, err })?;
|
||||
Ok(req.status().is_success())
|
||||
}
|
||||
|
||||
pub async fn get_redirected_final_url(client: &Client, url: Url) -> Result<Url, BinstallError> {
|
||||
let method = Method::HEAD;
|
||||
|
||||
let req = client
|
||||
.request(method.clone(), url.clone())
|
||||
.send()
|
||||
.await
|
||||
.and_then(Response::error_for_status)
|
||||
.map_err(|err| BinstallError::Http { method, url, err })?;
|
||||
|
||||
Ok(req.url().clone())
|
||||
}
|
||||
|
||||
pub(crate) async fn create_request(
|
||||
client: Client,
|
||||
url: Url,
|
||||
) -> Result<impl Stream<Item = reqwest::Result<Bytes>>, BinstallError> {
|
||||
debug!("Downloading from: '{url}'");
|
||||
|
||||
client
|
||||
.get(url.clone())
|
||||
.send()
|
||||
.await
|
||||
.and_then(|r| r.error_for_status())
|
||||
.map_err(|err| BinstallError::Http {
|
||||
method: Method::GET,
|
||||
url,
|
||||
err,
|
||||
})
|
||||
.map(Response::bytes_stream)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue