Skip to content

Commit b6bc92c

Browse files
author
Trevor Miranda
committed
Abandon cURL
1 parent 194026d commit b6bc92c

File tree

7 files changed

+14
-302
lines changed

7 files changed

+14
-302
lines changed

Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ build = "build.rs"
2020

2121
[features]
2222

23-
default = ["curl-backend", "reqwest-backend"]
24-
25-
curl-backend = ["download/curl-backend"]
26-
reqwest-backend = ["download/reqwest-backend"]
2723
vendored-openssl = ['openssl/vendored']
2824

2925
# Include in the default set to disable self-update and uninstall.

src/download/Cargo.toml

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,12 @@ authors = [ "Brian Anderson <[email protected]>" ]
77

88
license = "MIT/Apache-2.0"
99

10-
[features]
11-
12-
default = ["curl-backend"]
13-
14-
curl-backend = ["curl"]
15-
reqwest-backend = ["reqwest", "env_proxy", "lazy_static"]
16-
1710
[dependencies]
1811
error-chain = "0.12"
1912
url = "1.7"
20-
curl = { version = "0.4.11", optional = true }
21-
env_proxy = { version = "0.2.0", optional = true }
22-
lazy_static = { version = "1.0", optional = true }
23-
reqwest = { version = "0.9", optional = true }
13+
env_proxy = "0.2.0"
14+
lazy_static = "1.0"
15+
reqwest = "0.9"
2416

2517
[dev-dependencies]
2618
futures = "0.1"

src/download/src/errors.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,5 @@ error_chain! {
1818
FileNotFound {
1919
description("file not found")
2020
}
21-
BackendUnavailable(be: &'static str) {
22-
description("download backend unavailable")
23-
display("download backend '{}' unavailable", be)
24-
}
2521
}
2622
}

src/download/src/lib.rs

Lines changed: 4 additions & 186 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ use url::Url;
66
mod errors;
77
pub use crate::errors::*;
88

9-
#[derive(Debug, Copy, Clone)]
10-
pub enum Backend {
11-
Curl,
12-
Reqwest,
13-
}
14-
159
#[derive(Debug, Copy, Clone)]
1610
pub enum Event<'a> {
1711
ResumingPartialDownload,
@@ -21,20 +15,7 @@ pub enum Event<'a> {
2115
DownloadDataReceived(&'a [u8]),
2216
}
2317

24-
fn download_with_backend(
25-
backend: Backend,
26-
url: &Url,
27-
resume_from: u64,
28-
callback: &dyn Fn(Event<'_>) -> Result<()>,
29-
) -> Result<()> {
30-
match backend {
31-
Backend::Curl => curl::download(url, resume_from, callback),
32-
Backend::Reqwest => reqwest_be::download(url, resume_from, callback),
33-
}
34-
}
35-
36-
pub fn download_to_path_with_backend(
37-
backend: Backend,
18+
pub fn download_to_path(
3819
url: &Url,
3920
path: &Path,
4021
resume_from_partial: bool,
@@ -94,7 +75,7 @@ pub fn download_to_path_with_backend(
9475

9576
let file = RefCell::new(file);
9677

97-
download_with_backend(backend, url, resume_from, &|event| {
78+
reqwest_be::download(url, resume_from, &|event| {
9879
if let Event::DownloadDataReceived(data) = event {
9980
file.borrow_mut()
10081
.write_all(data)
@@ -118,133 +99,6 @@ pub fn download_to_path_with_backend(
11899
})
119100
}
120101

121-
/// Download via libcurl; encrypt with the native (or OpenSSl) TLS
122-
/// stack via libcurl
123-
#[cfg(feature = "curl-backend")]
124-
pub mod curl {
125-
126-
use curl;
127-
128-
use self::curl::easy::Easy;
129-
use super::Event;
130-
use crate::errors::*;
131-
use std::cell::RefCell;
132-
use std::str;
133-
use std::time::Duration;
134-
use url::Url;
135-
136-
pub fn download(
137-
url: &Url,
138-
resume_from: u64,
139-
callback: &dyn Fn(Event<'_>) -> Result<()>,
140-
) -> Result<()> {
141-
// Fetch either a cached libcurl handle (which will preserve open
142-
// connections) or create a new one if it isn't listed.
143-
//
144-
// Once we've acquired it, reset the lifetime from 'static to our local
145-
// scope.
146-
thread_local!(static EASY: RefCell<Easy> = RefCell::new(Easy::new()));
147-
EASY.with(|handle| {
148-
let mut handle = handle.borrow_mut();
149-
150-
handle
151-
.url(&url.to_string())
152-
.chain_err(|| "failed to set url")?;
153-
handle
154-
.follow_location(true)
155-
.chain_err(|| "failed to set follow redirects")?;
156-
157-
if resume_from > 0 {
158-
handle
159-
.resume_from(resume_from)
160-
.chain_err(|| "setting the range header for download resumption")?;
161-
} else {
162-
// an error here indicates that the range header isn't supported by underlying curl,
163-
// so there's nothing to "clear" - safe to ignore this error.
164-
let _ = handle.resume_from(0);
165-
}
166-
167-
// Take at most 30s to connect
168-
handle
169-
.connect_timeout(Duration::new(30, 0))
170-
.chain_err(|| "failed to set connect timeout")?;
171-
172-
{
173-
let cberr = RefCell::new(None);
174-
let mut transfer = handle.transfer();
175-
176-
// Data callback for libcurl which is called with data that's
177-
// downloaded. We just feed it into our hasher and also write it out
178-
// to disk.
179-
transfer
180-
.write_function(|data| match callback(Event::DownloadDataReceived(data)) {
181-
Ok(()) => Ok(data.len()),
182-
Err(e) => {
183-
*cberr.borrow_mut() = Some(e);
184-
Ok(0)
185-
}
186-
})
187-
.chain_err(|| "failed to set write")?;
188-
189-
// Listen for headers and parse out a `Content-Length` (case-insensitive) if it
190-
// comes so we know how much we're downloading.
191-
transfer
192-
.header_function(|header| {
193-
if let Ok(data) = str::from_utf8(header) {
194-
let prefix = "content-length: ";
195-
if data.to_ascii_lowercase().starts_with(prefix) {
196-
if let Ok(s) = data[prefix.len()..].trim().parse::<u64>() {
197-
let msg = Event::DownloadContentLengthReceived(s + resume_from);
198-
match callback(msg) {
199-
Ok(()) => (),
200-
Err(e) => {
201-
*cberr.borrow_mut() = Some(e);
202-
return false;
203-
}
204-
}
205-
}
206-
}
207-
}
208-
true
209-
})
210-
.chain_err(|| "failed to set header")?;
211-
212-
// If an error happens check to see if we had a filesystem error up
213-
// in `cberr`, but we always want to punt it up.
214-
transfer.perform().or_else(|e| {
215-
// If the original error was generated by one of our
216-
// callbacks, return it.
217-
match cberr.borrow_mut().take() {
218-
Some(cberr) => Err(cberr),
219-
None => {
220-
// Otherwise, return the error from curl
221-
if e.is_file_couldnt_read_file() {
222-
Err(e).chain_err(|| ErrorKind::FileNotFound)
223-
} else {
224-
Err(e).chain_err(|| "error during download")
225-
}
226-
}
227-
}
228-
})?;
229-
}
230-
231-
// If we didn't get a 20x or 0 ("OK" for files) then return an error
232-
let code = handle
233-
.response_code()
234-
.chain_err(|| "failed to get response code")?;
235-
match code {
236-
0 | 200..=299 => {}
237-
_ => {
238-
return Err(ErrorKind::HttpStatus(code).into());
239-
}
240-
};
241-
242-
Ok(())
243-
})
244-
}
245-
}
246-
247-
#[cfg(feature = "reqwest-backend")]
248102
pub mod reqwest_be {
249103
use super::Event;
250104
use crate::errors::*;
@@ -302,12 +156,8 @@ pub mod reqwest_be {
302156
.build()
303157
};
304158

305-
// woah, an unwrap?!
306-
// It's OK. This is the same as what is happening in curl.
307-
//
308-
// The curl::Easy::new() internally assert!s that the initialized
309-
// Easy is not null. Inside reqwest, the errors here would be from
310-
// the TLS library returning a null pointer as well.
159+
// Inside reqwest, the errors here would be from the TLS library
160+
// returning a null pointer.
311161
catcher().unwrap()
312162
};
313163
}
@@ -366,35 +216,3 @@ pub mod reqwest_be {
366216
}
367217
}
368218
}
369-
370-
#[cfg(not(feature = "curl-backend"))]
371-
pub mod curl {
372-
373-
use super::Event;
374-
use errors::*;
375-
use url::Url;
376-
377-
pub fn download(
378-
_url: &Url,
379-
_resume_from: u64,
380-
_callback: &Fn(Event) -> Result<()>,
381-
) -> Result<()> {
382-
Err(ErrorKind::BackendUnavailable("curl").into())
383-
}
384-
}
385-
386-
#[cfg(not(feature = "reqwest-backend"))]
387-
pub mod reqwest_be {
388-
389-
use super::Event;
390-
use errors::*;
391-
use url::Url;
392-
393-
pub fn download(
394-
_url: &Url,
395-
_resume_from: u64,
396-
_callback: &Fn(Event) -> Result<()>,
397-
) -> Result<()> {
398-
Err(ErrorKind::BackendUnavailable("reqwest").into())
399-
}
400-
}

src/download/tests/download-curl-resume.rs

Lines changed: 0 additions & 76 deletions
This file was deleted.

src/rustup-utils/src/notifications.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ pub enum Notification<'a> {
2020
DownloadFinished,
2121
NoCanonicalPath(&'a Path),
2222
ResumingPartialDownload,
23-
UsingCurl,
24-
UsingReqwest,
2523
UsingHyperDeprecated,
2624
}
2725

@@ -36,9 +34,7 @@ impl<'a> Notification<'a> {
3634
| DownloadContentLengthReceived(_)
3735
| DownloadDataReceived(_)
3836
| DownloadFinished
39-
| ResumingPartialDownload
40-
| UsingCurl
41-
| UsingReqwest => NotificationLevel::Verbose,
37+
| ResumingPartialDownload => NotificationLevel::Verbose,
4238
UsingHyperDeprecated | NoCanonicalPath(_) => NotificationLevel::Warn,
4339
}
4440
}
@@ -62,12 +58,9 @@ impl<'a> Display for Notification<'a> {
6258
DownloadFinished => write!(f, "download finished"),
6359
NoCanonicalPath(path) => write!(f, "could not canonicalize path: '{}'", path.display()),
6460
ResumingPartialDownload => write!(f, "resuming partial download"),
65-
UsingCurl => write!(f, "downloading with curl"),
66-
UsingReqwest => write!(f, "downloading with reqwest"),
67-
UsingHyperDeprecated => f.write_str(
68-
"RUSTUP_USE_HYPER environment variable is deprecated,\
69-
use RUSTUP_USE_REQWEST instead",
70-
),
61+
UsingHyperDeprecated => {
62+
f.write_str("RUSTUP_USE_HYPER environment variable is deprecated.")
63+
}
7164
}
7265
}
7366
}

0 commit comments

Comments
 (0)