refactor: move try_old_curl!() to util::network

This commit is contained in:
bebecue 2023-02-18 01:24:53 +08:00
parent f182b84813
commit 6dbfde37e1
3 changed files with 21 additions and 39 deletions

View file

@ -651,23 +651,6 @@ impl<'cfg> PackageSet<'cfg> {
}
}
// When dynamically linked against libcurl, we want to ignore some failures
// when using old versions that don't support certain features.
macro_rules! try_old_curl {
($e:expr, $msg:expr) => {
let result = $e;
if cfg!(target_os = "macos") {
if let Err(e) = result {
warn!("ignoring libcurl {} error: {}", $msg, e);
}
} else {
result.with_context(|| {
anyhow::format_err!("failed to enable {}, is curl not built right?", $msg)
})?;
}
};
}
impl<'a, 'cfg> Downloads<'a, 'cfg> {
/// Starts to download the package for the `id` specified.
///
@ -748,7 +731,7 @@ impl<'a, 'cfg> Downloads<'a, 'cfg> {
// errors here on OSX, but consider this a fatal error to not activate
// HTTP/2 on all other platforms.
if self.set.multiplexing {
try_old_curl!(handle.http_version(HttpVersion::V2), "HTTP2");
crate::try_old_curl!(handle.http_version(HttpVersion::V2), "HTTP2");
} else {
handle.http_version(HttpVersion::V11)?;
}
@ -760,7 +743,7 @@ impl<'a, 'cfg> Downloads<'a, 'cfg> {
// Once the main one is opened we realized that pipelining is possible
// and multiplexing is possible with static.crates.io. All in all this
// reduces the number of connections down to a more manageable state.
try_old_curl!(handle.pipewait(true), "pipewait");
crate::try_old_curl!(handle.pipewait(true), "pipewait");
handle.write_function(move |buf| {
debug!("{} - {} bytes of data", token, buf.len());

View file

@ -391,25 +391,6 @@ impl<'cfg> HttpRegistry<'cfg> {
}
}
// copied from src/cargo/core/package.rs to keep change minimal
//
// When dynamically linked against libcurl, we want to ignore some failures
// when using old versions that don't support certain features.
macro_rules! try_old_curl {
($e:expr, $msg:expr) => {
let result = $e;
if cfg!(target_os = "macos") {
if let Err(e) = result {
warn!("ignoring libcurl {} error: {}", $msg, e);
}
} else {
result.with_context(|| {
anyhow::format_err!("failed to enable {}, is curl not built right?", $msg)
})?;
}
};
}
impl<'cfg> RegistryData for HttpRegistry<'cfg> {
fn prepare(&self) -> CargoResult<()> {
Ok(())
@ -572,7 +553,7 @@ impl<'cfg> RegistryData for HttpRegistry<'cfg> {
// Enable HTTP/2 if possible.
if self.multiplexing {
try_old_curl!(handle.http_version(HttpVersion::V2), "HTTP2");
crate::try_old_curl!(handle.http_version(HttpVersion::V2), "HTTP2");
} else {
handle.http_version(HttpVersion::V11)?;
}

View file

@ -110,6 +110,24 @@ where
}
}
// When dynamically linked against libcurl, we want to ignore some failures
// when using old versions that don't support certain features.
#[macro_export]
macro_rules! try_old_curl {
($e:expr, $msg:expr) => {
let result = $e;
if cfg!(target_os = "macos") {
if let Err(e) = result {
warn!("ignoring libcurl {} error: {}", $msg, e);
}
} else {
result.with_context(|| {
anyhow::format_err!("failed to enable {}, is curl not built right?", $msg)
})?;
}
};
}
#[test]
fn with_retry_repeats_the_call_then_works() {
use crate::core::Shell;