]> git.proxmox.com Git - rustc.git/blame - src/tools/cargo/src/cargo/util/network/mod.rs
New upstream version 1.74.1+dfsg1
[rustc.git] / src / tools / cargo / src / cargo / util / network / mod.rs
CommitLineData
0a29b90c
FG
1//! Utilities for networking.
2
3use std::task::Poll;
4
fe692bf9 5pub mod http;
49aad941 6pub mod proxy;
0a29b90c
FG
7pub mod retry;
8pub mod sleep;
9
10pub trait PollExt<T> {
11 fn expect(self, msg: &str) -> T;
12}
13
14impl<T> PollExt<T> for Poll<T> {
15 #[track_caller]
16 fn expect(self, msg: &str) -> T {
17 match self {
18 Poll::Ready(val) => val,
19 Poll::Pending => panic!("{}", msg),
20 }
21 }
22}
23
fe692bf9
FG
24/// When dynamically linked against libcurl, we want to ignore some failures
25/// when using old versions that don't support certain features.
0a29b90c
FG
26#[macro_export]
27macro_rules! try_old_curl {
28 ($e:expr, $msg:expr) => {
29 let result = $e;
30 if cfg!(target_os = "macos") {
31 if let Err(e) = result {
781aab86 32 ::tracing::warn!(target: "network", "ignoring libcurl {} error: {}", $msg, e);
0a29b90c
FG
33 }
34 } else {
fe692bf9 35 use ::anyhow::Context;
0a29b90c 36 result.with_context(|| {
fe692bf9 37 ::anyhow::format_err!("failed to enable {}, is curl not built right?", $msg)
0a29b90c
FG
38 })?;
39 }
40 };
41}
fe692bf9
FG
42
43/// Enable HTTP/2 and pipewait to be used as it'll allow true multiplexing
44/// which makes downloads much faster.
45///
46/// Currently Cargo requests the `http2` feature of the `curl` crate which
47/// means it should always be built in. On OSX, however, we ship cargo still
48/// linked against the system libcurl. Building curl with ALPN support for
49/// HTTP/2 requires newer versions of OSX (the SecureTransport API) than we
50/// want to ship Cargo for. By linking Cargo against the system libcurl then
51/// older curl installations won't use HTTP/2 but newer ones will. All that to
52/// basically say we ignore errors here on OSX, but consider this a fatal error
53/// to not activate HTTP/2 on all other platforms.
54///
55/// `pipewait` is an option which indicates that if there's a bunch of parallel
56/// requests to the same host they all wait until the pipelining status of the
57/// host is known. This means that we won't initiate dozens of connections but
58/// rather only one. Once the main one is opened we realized that pipelining is
59/// possible and multiplexing is possible. All in all this reduces the number
60/// of connections down to a more manageable state.
61#[macro_export]
62macro_rules! try_old_curl_http2_pipewait {
63 ($multiplexing:expr, $handle:expr) => {
64 if $multiplexing {
65 $crate::try_old_curl!($handle.http_version(curl::easy::HttpVersion::V2), "HTTP/2");
66 } else {
67 $handle.http_version(curl::easy::HttpVersion::V11)?;
68 }
69 $crate::try_old_curl!($handle.pipewait(true), "pipewait");
70 };
71}