@@ -164,7 +164,7 @@ use std::io::SeekFrom;
164
164
use std:: io:: prelude:: * ;
165
165
use std:: path:: { PathBuf , Path } ;
166
166
167
- use curl:: http ;
167
+ use curl:: easy :: Easy ;
168
168
use flate2:: read:: GzDecoder ;
169
169
use git2;
170
170
use rustc_serialize:: hex:: ToHex ;
@@ -188,7 +188,7 @@ pub struct RegistrySource<'cfg> {
188
188
cache_path : Filesystem ,
189
189
src_path : Filesystem ,
190
190
config : & ' cfg Config ,
191
- handle : Option < http :: Handle > ,
191
+ handle : Option < Easy > ,
192
192
hashes : HashMap < ( String , String ) , String > , // (name, vers) => cksum
193
193
cache : HashMap < String , Vec < ( Summary , bool ) > > ,
194
194
updated : bool ,
@@ -300,24 +300,34 @@ impl<'cfg> RegistrySource<'cfg> {
300
300
self . handle . as_mut ( ) . unwrap ( )
301
301
}
302
302
} ;
303
- // TODO: don't download into memory (curl-rust doesn't expose it)
304
- let resp = try!( handle. get ( url. to_string ( ) ) . follow_redirects ( true ) . exec ( ) ) ;
305
- if resp. get_code ( ) != 200 && resp. get_code ( ) != 0 {
306
- return Err ( internal ( format ! ( "failed to get 200 response from {}\n {}" ,
307
- url, resp) ) )
303
+ // TODO: don't download into memory, but ensure that if we ctrl-c a
304
+ // download we should resume either from the start or the middle
305
+ // on the next time
306
+ try!( handle. get ( true ) ) ;
307
+ try!( handle. url ( & url. to_string ( ) ) ) ;
308
+ try!( handle. follow_location ( true ) ) ;
309
+ let mut state = Sha256 :: new ( ) ;
310
+ let mut body = Vec :: new ( ) ;
311
+ {
312
+ let mut handle = handle. transfer ( ) ;
313
+ try!( handle. write_function ( |buf| {
314
+ state. update ( buf) ;
315
+ body. extend_from_slice ( buf) ;
316
+ Ok ( buf. len ( ) )
317
+ } ) ) ;
318
+ try!( handle. perform ( ) ) ;
319
+ }
320
+ let code = try!( handle. response_code ( ) ) ;
321
+ if code != 200 && code != 0 {
322
+ bail ! ( "failed to get 200 response from `{}`, got {}" , url, code)
308
323
}
309
324
310
325
// Verify what we just downloaded
311
- let actual = {
312
- let mut state = Sha256 :: new ( ) ;
313
- state. update ( resp. get_body ( ) ) ;
314
- state. finish ( )
315
- } ;
316
- if actual. to_hex ( ) != expected_hash {
326
+ if state. finish ( ) . to_hex ( ) != expected_hash {
317
327
bail ! ( "failed to verify the checksum of `{}`" , pkg)
318
328
}
319
329
320
- try!( dst. write_all ( resp . get_body ( ) ) ) ;
330
+ try!( dst. write_all ( & body ) ) ;
321
331
try!( dst. seek ( SeekFrom :: Start ( 0 ) ) ) ;
322
332
Ok ( dst)
323
333
}
0 commit comments