From 9e868cb728cc5a2d5acf741db8235517dbc5bd4c Mon Sep 17 00:00:00 2001 From: Danilo Bargen Date: Mon, 1 Aug 2016 12:20:41 +0200 Subject: [PATCH] Upgrade curl to 0.3 --- Cargo.lock | 37 ++++--------------------------------- Cargo.toml | 2 +- src/cache.rs | 29 ++++++++++++++++++----------- src/error.rs | 8 ++++---- 4 files changed, 27 insertions(+), 49 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1597304..80e15b2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,7 +4,7 @@ version = "0.2.0" dependencies = [ "ansi_term 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "clippy 0.0.79 (registry+https://github.com/rust-lang/crates.io-index)", - "curl 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)", + "curl 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "docopt 0.6.82 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "flate2 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", @@ -54,19 +54,17 @@ dependencies = [ [[package]] name = "curl" -version = "0.2.19" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "curl-sys 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)", + "curl-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "openssl-sys 0.7.14 (registry+https://github.com/rust-lang/crates.io-index)", - "url 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "curl-sys" -version = "0.1.34" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "gcc 0.3.32 (registry+https://github.com/rust-lang/crates.io-index)", @@ -127,16 +125,6 @@ dependencies = [ "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "idna" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "unicode-bidi 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", - "unicode-normalization 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "kernel32-sys" version = "0.2.2" @@ -332,28 +320,11 @@ dependencies = [ "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "unicode-bidi" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "unicode-normalization" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "url" -version = "1.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "idna 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "user32-sys" version = "0.2.0" diff --git a/Cargo.toml b/Cargo.toml index f3aca11..b9d66b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ dev = ["clippy"] log = "^0.3" xdg = "^2.0" tar = "^0.4" -curl = "^0.2" +curl = "^0.3" time = "^0.1" flate2 = "^0.2" docopt = "^0.6" diff --git a/src/cache.rs b/src/cache.rs index 69313ee..cb40a91 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -8,7 +8,7 @@ use std::path::PathBuf; use xdg::BaseDirectories; use flate2::read::GzDecoder; use tar::Archive; -use curl::http; +use curl::easy::Easy as CurlEasy; use walkdir::{WalkDir, WalkDirIterator, DirEntry}; use time; @@ -55,14 +55,21 @@ impl Cache { } /// Download the archive - fn download(&self) -> Result { - let resp = try!( - http::handle() - .follow_location(1) - .get(&self.url[..]) - .exec() - ); - Ok(resp) + fn download(&self) -> Result, TealdeerError> { + let mut handle = CurlEasy::new(); + try!(handle.url(&self.url)); + try!(handle.follow_location(true)); + try!(handle.fail_on_error(true)); + let mut buf = Vec::new(); + { + let mut transfer = handle.transfer(); + try!(transfer.write_function(|data| { + buf.extend_from_slice(data); + Ok(data.len()) + })); + try!(transfer.perform()); + } + Ok(buf) } /// Decompress and open the archive @@ -74,10 +81,10 @@ impl Cache { /// Update the pages cache. pub fn update(&self) -> Result<(), TealdeerError> { // First, download the compressed data - let response = try!(self.download()); + let bytes: Vec = try!(self.download()); // Decompress the response body into an `Archive` - let mut archive = try!(self.decompress(response.get_body())); + let mut archive = try!(self.decompress(&bytes[..])); // Determine paths let cache_dir = try!(self.get_cache_dir()); diff --git a/src/error.rs b/src/error.rs index b558401..f099eb2 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,4 +1,4 @@ -use curl::ErrCode; +use curl::Error as CurlError; #[derive(Debug)] pub enum TealdeerError { @@ -6,8 +6,8 @@ pub enum TealdeerError { UpdateError(String), } -impl From for TealdeerError { - fn from(err: ErrCode) -> TealdeerError { - TealdeerError::UpdateError(err.to_string()) +impl From for TealdeerError { + fn from(err: CurlError) -> TealdeerError { + TealdeerError::UpdateError(format!("Curl error: {}", err.to_string())) } }