@@ -6,12 +6,6 @@ use url::Url;
6
6
mod errors;
7
7
pub use crate :: errors:: * ;
8
8
9
- #[ derive( Debug , Copy , Clone ) ]
10
- pub enum Backend {
11
- Curl ,
12
- Reqwest ,
13
- }
14
-
15
9
#[ derive( Debug , Copy , Clone ) ]
16
10
pub enum Event < ' a > {
17
11
ResumingPartialDownload ,
@@ -21,20 +15,7 @@ pub enum Event<'a> {
21
15
DownloadDataReceived ( & ' a [ u8 ] ) ,
22
16
}
23
17
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 (
38
19
url : & Url ,
39
20
path : & Path ,
40
21
resume_from_partial : bool ,
@@ -94,7 +75,7 @@ pub fn download_to_path_with_backend(
94
75
95
76
let file = RefCell :: new ( file) ;
96
77
97
- download_with_backend ( backend , url, resume_from, & |event| {
78
+ reqwest_be :: download ( url, resume_from, & |event| {
98
79
if let Event :: DownloadDataReceived ( data) = event {
99
80
file. borrow_mut ( )
100
81
. write_all ( data)
@@ -118,133 +99,6 @@ pub fn download_to_path_with_backend(
118
99
} )
119
100
}
120
101
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" ) ]
248
102
pub mod reqwest_be {
249
103
use super :: Event ;
250
104
use crate :: errors:: * ;
@@ -302,12 +156,8 @@ pub mod reqwest_be {
302
156
. build( )
303
157
} ;
304
158
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.
311
161
catcher( ) . unwrap( )
312
162
} ;
313
163
}
@@ -366,35 +216,3 @@ pub mod reqwest_be {
366
216
}
367
217
}
368
218
}
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
- }
0 commit comments