From a2d74924471e925afc1af0d1fa8c7b4cd5507971 Mon Sep 17 00:00:00 2001 From: Chris Swindle Date: Fri, 3 Nov 2017 19:16:12 +0000 Subject: [PATCH] Add support for using a proxy for crate downloads. --- src/cargo/core/source/source_id.rs | 9 +++++++++ src/cargo/sources/registry/remote.rs | 10 ++++++++++ src/cargo/util/errors.rs | 3 +++ 3 files changed, 22 insertions(+) diff --git a/src/cargo/core/source/source_id.rs b/src/cargo/core/source/source_id.rs index 955ecca2107..05773df7a7b 100644 --- a/src/cargo/core/source/source_id.rs +++ b/src/cargo/core/source/source_id.rs @@ -32,6 +32,8 @@ struct SourceIdInner { kind: Kind, // e.g. the exact git revision of the specified branch for a Git Source precise: Option, + /// Optional name to associate with the source + name: Option, } /// The possible kinds of code source. Along with a URL, this fully defines the @@ -72,6 +74,7 @@ impl SourceId { canonical_url: git::canonicalize_url(&url)?, url: url, precise: None, + name: None, }), }; Ok(source_id) @@ -190,10 +193,16 @@ impl SourceId { canonical_url: git::canonicalize_url(&url)?, url: url, precise: None, + name: Some(key.to_string()), }), }) } + /// Get the optional name associated with the source + pub fn name(&self) -> &Option { + &self.inner.name + } + /// Get this source URL pub fn url(&self) -> &Url { &self.inner.url diff --git a/src/cargo/sources/registry/remote.rs b/src/cargo/sources/registry/remote.rs index 84622610714..a7d62530189 100644 --- a/src/cargo/sources/registry/remote.rs +++ b/src/cargo/sources/registry/remote.rs @@ -209,10 +209,20 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> { .push(&pkg.version().to_string()) .push("download"); + let proxy_key = match self.source_id.name() { + &None => "registry.proxy".to_string(), + &Some(ref registry) => format!("registries.{}.proxy", registry), + }; + + if let Some(cache) = self.config.get_string(&proxy_key)? { + url = cache.val.to_url()?.join(url.path())?; + } + // TODO: don't download into memory, but ensure that if we ctrl-c a // download we should resume either from the start or the middle // on the next time let url = url.to_string(); + let mut handle = self.config.http()?.borrow_mut(); handle.get(true)?; handle.url(&url)?; diff --git a/src/cargo/util/errors.rs b/src/cargo/util/errors.rs index 70c50171951..c29e4137b3b 100644 --- a/src/cargo/util/errors.rs +++ b/src/cargo/util/errors.rs @@ -17,6 +17,7 @@ use serde_json; use toml; use registry; use ignore; +use url; error_chain! { types { @@ -40,6 +41,7 @@ error_chain! { Parse(string::ParseError); Git(git2::Error); Curl(curl::Error); + UrlParse(url::ParseError); } errors { @@ -87,6 +89,7 @@ impl CargoError { CargoErrorKind::Git(_) | CargoErrorKind::Internal(_) | CargoErrorKind::CargoTestErrorKind(_) | + CargoErrorKind::UrlParse(_) | CargoErrorKind::__Nonexhaustive { .. } => false } }