Skip to content

Commit 12155aa

Browse files
Merge pull request #851 from charlespierce/attohttpc_with_proxy
Move back to attohttpc, now that it supports proxies
2 parents f675514 + ee18f39 commit 12155aa

File tree

9 files changed

+68
-590
lines changed

9 files changed

+68
-590
lines changed

Cargo.lock

Lines changed: 25 additions & 542 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/archive/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ verbatim = "0.1"
1515
cfg-if = "0.1"
1616
hyperx = "1.0.0"
1717
thiserror = "1.0.16"
18-
reqwest = { version = "0.10", features = ["blocking", "json"] }
18+
attohttpc = { version = "0.16.0", features = ["json"] }

crates/archive/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub use crate::zip::Zip;
1515
#[derive(Error, Debug)]
1616
pub enum ArchiveError {
1717
#[error("HTTP failure ({0})")]
18-
HttpError(::reqwest::StatusCode),
18+
HttpError(attohttpc::StatusCode),
1919

2020
#[error("HTTP header '{0}' not found")]
2121
MissingHeaderError(String),
@@ -27,7 +27,7 @@ pub enum ArchiveError {
2727
IoError(#[from] std::io::Error),
2828

2929
#[error("{0}")]
30-
ReqwestError(#[from] reqwest::Error),
30+
AttohttpcError(#[from] attohttpc::Error),
3131

3232
#[error("{0}")]
3333
ZipError(#[from] zip_rs::result::ZipError),

crates/archive/src/tarball.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use std::io::{Read, Seek, SeekFrom};
66
use std::path::Path;
77

88
use super::{Archive, ArchiveError, Origin};
9+
use attohttpc::header::HeaderMap;
910
use flate2::read::GzDecoder;
1011
use fs_utils::ensure_containing_dir_exists;
1112
use hyperx::header::{
1213
AcceptRanges, ByteRangeSpec, ContentLength, Header, Range, RangeUnit, TypedHeaders,
1314
};
1415
use progress_read::ProgressRead;
15-
use reqwest::blocking::Response;
1616
use tee::TeeReader;
1717

1818
/// A Node installation tarball.
@@ -29,9 +29,8 @@ pub struct Tarball {
2929

3030
/// Determines the length of an HTTP response's content in bytes, using
3131
/// the HTTP `"Content-Length"` header.
32-
fn content_length(response: &Response) -> Result<u64, ArchiveError> {
33-
response
34-
.headers()
32+
fn content_length(headers: &HeaderMap) -> Result<u64, ArchiveError> {
33+
headers
3534
.decode::<ContentLength>()
3635
.ok()
3736
.map(|v| v.0)
@@ -55,14 +54,14 @@ impl Tarball {
5554
/// tarball that can be streamed (and that tees its data to a local
5655
/// file as it streams).
5756
pub fn fetch(url: &str, cache_file: &Path) -> Result<Box<dyn Archive>, ArchiveError> {
58-
let response = reqwest::blocking::get(url)?;
57+
let (status, headers, response) = attohttpc::get(url).send()?.split();
5958

60-
if !response.status().is_success() {
61-
return Err(ArchiveError::HttpError(response.status()));
59+
if !status.is_success() {
60+
return Err(ArchiveError::HttpError(status));
6261
}
6362

64-
let compressed_size = content_length(&response)?;
65-
let uncompressed_size = if accepts_byte_ranges(&response) {
63+
let compressed_size = content_length(&headers)?;
64+
let uncompressed_size = if accepts_byte_ranges(&headers) {
6665
fetch_uncompressed_size(url, compressed_size)
6766
} else {
6867
None
@@ -128,18 +127,17 @@ fn unpack_isize(packed: [u8; 4]) -> u64 {
128127
/// downloading the entire gzip file. For very small files it's unlikely to be
129128
/// more efficient than simply downloading the entire file up front.
130129
fn fetch_isize(url: &str, len: u64) -> Result<[u8; 4], ArchiveError> {
131-
let client = reqwest::blocking::Client::new();
132130
let range_header = Range::Bytes(vec![ByteRangeSpec::FromTo(len - 4, len - 1)]);
133-
let mut response = client
134-
.get(url)
131+
let (status, headers, mut response) = attohttpc::get(url)
135132
.header(Range::header_name(), range_header.to_string())
136-
.send()?;
133+
.send()?
134+
.split();
137135

138-
if !response.status().is_success() {
139-
return Err(ArchiveError::HttpError(response.status()));
136+
if !status.is_success() {
137+
return Err(ArchiveError::HttpError(status));
140138
}
141139

142-
let actual_length = content_length(&response)?;
140+
let actual_length = content_length(&headers)?;
143141

144142
if actual_length != 4 {
145143
return Err(ArchiveError::UnexpectedContentLengthError(actual_length));
@@ -160,9 +158,8 @@ fn load_isize(file: &mut File) -> Result<[u8; 4], ArchiveError> {
160158
Ok(buf)
161159
}
162160

163-
fn accepts_byte_ranges(response: &Response) -> bool {
164-
response
165-
.headers()
161+
fn accepts_byte_ranges(headers: &HeaderMap) -> bool {
162+
headers
166163
.decode::<AcceptRanges>()
167164
.ok()
168165
.map(|v| v.iter().any(|unit| *unit == RangeUnit::Bytes))

crates/archive/src/zip.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ impl Zip {
3434
/// Initiate fetching of a Node zip archive from the given URL, returning
3535
/// a `Remote` data source.
3636
pub fn fetch(url: &str, cache_file: &Path) -> Result<Box<dyn Archive>, ArchiveError> {
37-
let mut response = reqwest::blocking::get(url)?;
37+
let (status, _, mut response) = attohttpc::get(url).send()?.split();
3838

39-
if !response.status().is_success() {
40-
return Err(ArchiveError::HttpError(response.status()));
39+
if !status.is_success() {
40+
return Err(ArchiveError::HttpError(status));
4141
}
4242

4343
{

crates/volta-core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ double-checked-cell = "2.0.2"
4848
dunce = "1.0.0"
4949
ci_info = "0.10.0"
5050
hyperx = "1.0.0"
51-
reqwest = { version = "0.10", features = ["blocking", "json"] }
51+
attohttpc = { version = "0.16.0", features = ["json"] }
5252
chain-map = "0.1.0"
5353
indexmap = "1.3.2"
5454
retry = "1.0.0"

crates/volta-core/src/tool/node/resolve.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ use crate::session::Session;
1515
use crate::style::progress_spinner;
1616
use crate::tool::Node;
1717
use crate::version::{VersionSpec, VersionTag};
18+
use attohttpc::header::HeaderMap;
19+
use attohttpc::Response;
1820
use cfg_if::cfg_if;
1921
use fs_utils::ensure_containing_dir_exists;
2022
use hyperx::header::{CacheControl, CacheDirective, Expires, HttpDate, TypedHeaders};
2123
use log::debug;
22-
use reqwest::blocking::Response;
2324
use semver::{Version, VersionReq};
2425

2526
// ISSUE (#86): Move public repository URLs to config file
@@ -226,8 +227,8 @@ fn read_cached_opt(url: &str) -> Fallible<Option<RawNodeIndex>> {
226227
}
227228

228229
/// Get the cache max-age of an HTTP reponse.
229-
fn max_age(response: &reqwest::blocking::Response) -> u32 {
230-
if let Ok(cache_control_header) = response.headers().decode::<CacheControl>() {
230+
fn max_age(headers: &HeaderMap) -> u32 {
231+
if let Ok(cache_control_header) = headers.decode::<CacheControl>() {
231232
for cache_directive in cache_control_header.iter() {
232233
if let CacheDirective::MaxAge(max_age) = cache_directive {
233234
return *max_age;
@@ -249,15 +250,16 @@ fn resolve_node_versions(url: &str) -> Fallible<RawNodeIndex> {
249250
debug!("Node index cache was not found or was invalid");
250251
let spinner = progress_spinner(&format!("Fetching public registry: {}", url));
251252

252-
let response: Response = reqwest::blocking::get(url)
253+
let (_, headers, response) = attohttpc::get(url)
254+
.send()
253255
.and_then(Response::error_for_status)
254-
.with_context(registry_fetch_error("Node", url))?;
256+
.with_context(registry_fetch_error("Node", url))?
257+
.split();
255258

256-
let expires = if let Ok(expires_header) = response.headers().decode::<Expires>() {
259+
let expires = if let Ok(expires_header) = headers.decode::<Expires>() {
257260
expires_header.to_string()
258261
} else {
259-
let expiry_date =
260-
SystemTime::now() + Duration::from_secs(max_age(&response).into());
262+
let expiry_date = SystemTime::now() + Duration::from_secs(max_age(&headers).into());
261263
HttpDate::from(expiry_date).to_string()
262264
};
263265

crates/volta-core/src/tool/npm/resolve.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ use crate::session::Session;
1111
use crate::style::progress_spinner;
1212
use crate::tool::Npm;
1313
use crate::version::{VersionSpec, VersionTag};
14+
use attohttpc::header::ACCEPT;
15+
use attohttpc::Response;
1416
use log::debug;
15-
use reqwest::blocking::Client;
16-
use reqwest::blocking::Response;
17-
use reqwest::header::ACCEPT;
1817
use semver::{Version, VersionReq};
1918

2019
pub fn resolve(matching: VersionSpec, session: &mut Session) -> Fallible<Option<Version>> {
@@ -43,9 +42,7 @@ fn fetch_npm_index(hooks: Option<&ToolHooks<Npm>>) -> Fallible<(String, PackageI
4342
};
4443

4544
let spinner = progress_spinner(&format!("Fetching public registry: {}", url));
46-
let http_client = Client::new();
47-
let metadata: RawPackageMetadata = http_client
48-
.get(&url)
45+
let metadata: RawPackageMetadata = attohttpc::get(&url)
4946
.header(ACCEPT, NPM_ABBREVIATED_ACCEPT_HEADER)
5047
.send()
5148
.and_then(Response::error_for_status)

crates/volta-core/src/tool/yarn/resolve.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ use crate::session::Session;
1212
use crate::style::progress_spinner;
1313
use crate::tool::Yarn;
1414
use crate::version::{parse_version, VersionSpec, VersionTag};
15+
use attohttpc::header::ACCEPT;
16+
use attohttpc::Response;
1517
use log::debug;
16-
use reqwest::blocking::Client;
17-
use reqwest::blocking::Response;
18-
use reqwest::header::ACCEPT;
1918
use semver::{Version, VersionReq};
2019

2120
pub fn resolve(matching: VersionSpec, session: &mut Session) -> Fallible<Version> {
@@ -73,9 +72,7 @@ fn resolve_semver(matching: VersionReq, hooks: Option<&ToolHooks<Yarn>>) -> Fall
7372
fn fetch_yarn_index() -> Fallible<(String, PackageIndex)> {
7473
let url = public_registry_index("yarn");
7574
let spinner = progress_spinner(&format!("Fetching public registry: {}", url));
76-
let http_client = Client::new();
77-
let metadata: RawPackageMetadata = http_client
78-
.get(&url)
75+
let metadata: RawPackageMetadata = attohttpc::get(&url)
7976
.header(ACCEPT, NPM_ABBREVIATED_ACCEPT_HEADER)
8077
.send()
8178
.and_then(Response::error_for_status)
@@ -99,7 +96,8 @@ fn resolve_custom_tag(tag: String) -> Fallible<Version> {
9996
}
10097

10198
fn resolve_latest_legacy(url: String) -> Fallible<Version> {
102-
let response_text = reqwest::blocking::get(&url)
99+
let response_text = attohttpc::get(&url)
100+
.send()
103101
.and_then(Response::error_for_status)
104102
.and_then(Response::text)
105103
.with_context(|| ErrorKind::YarnLatestFetchError {
@@ -135,7 +133,8 @@ fn resolve_semver_from_registry(matching: VersionReq) -> Fallible<Version> {
135133

136134
fn resolve_semver_legacy(matching: VersionReq, url: String) -> Fallible<Version> {
137135
let spinner = progress_spinner(&format!("Fetching public registry: {}", url));
138-
let releases: RawYarnIndex = reqwest::blocking::get(&url)
136+
let releases: RawYarnIndex = attohttpc::get(&url)
137+
.send()
139138
.and_then(Response::error_for_status)
140139
.and_then(Response::json)
141140
.with_context(registry_fetch_error("Yarn", &url))?;

0 commit comments

Comments
 (0)