diff --git a/dapps/src/apps/urlhint.rs b/dapps/src/apps/urlhint.rs index fb3b7c419..f57e5e0d7 100644 --- a/dapps/src/apps/urlhint.rs +++ b/dapps/src/apps/urlhint.rs @@ -301,6 +301,6 @@ mod tests { let url = app.url(); // then - assert_eq!(url, "http://github.todr.me/test/xyz/zip/000102030405060708090a0b0c0d0e0f10111213".to_owned()); + assert_eq!(url, "https://codeload.github.com/test/xyz/zip/000102030405060708090a0b0c0d0e0f10111213".to_owned()); } } diff --git a/util/https-fetch/examples/fetch.rs b/util/https-fetch/examples/fetch.rs index 7d0806275..39b8ca946 100644 --- a/util/https-fetch/examples/fetch.rs +++ b/util/https-fetch/examples/fetch.rs @@ -1,12 +1,15 @@ extern crate https_fetch; use std::io; +use std::sync::Arc; +use std::sync::atomic::AtomicBool; use https_fetch::*; fn main() { let client = Client::new().unwrap(); + let aborted = Arc::new(AtomicBool::new(false)); - client.fetch(Url::new("github.com", 443, "/").unwrap(), Box::new(io::stdout()), |result| { + client.fetch(Url::new("github.com", 443, "/").unwrap(), Box::new(io::stdout()), aborted, |result| { assert!(result.is_ok()); }).unwrap(); } diff --git a/util/https-fetch/src/client.rs b/util/https-fetch/src/client.rs index fe9b503f4..3e5d50515 100644 --- a/util/https-fetch/src/client.rs +++ b/util/https-fetch/src/client.rs @@ -174,7 +174,7 @@ impl mio::Handler for ClientLoop { fn should_successfuly_fetch_a_page() { use std::io::{self, Cursor}; use std::sync::{mpsc, Arc}; - use std::sync::atomic::{AtomicUsize, Ordering}; + use std::sync::atomic::{AtomicUsize, AtomicBool, Ordering}; struct Writer { wrote: Arc, @@ -200,11 +200,11 @@ fn should_successfuly_fetch_a_page() { data: Cursor::new(Vec::new()), }; let (tx, rx) = mpsc::channel(); - client.fetch(Url::new("github.com", 443, "/").unwrap(), Box::new(writer), move |result| { + client.fetch(Url::new("github.com", 443, "/").unwrap(), Box::new(writer), Arc::new(AtomicBool::new(false)), move |result| { assert!(result.is_ok()); assert!(wrote.load(Ordering::Relaxed) > 0); tx.send(result).unwrap(); - }); + }).unwrap(); let _ = rx.recv().unwrap(); } diff --git a/util/https-fetch/src/http.rs b/util/https-fetch/src/http.rs index 27b019cbb..d29974c01 100644 --- a/util/https-fetch/src/http.rs +++ b/util/https-fetch/src/http.rs @@ -101,7 +101,7 @@ impl HttpProcessor { } fn set_state(&mut self, state: State) { self.state = state; - debug!("Changing state to {:?}", state); + trace!("Changing state to {:?}", state); } fn process_buffer(&mut self) -> io::Result<()> { @@ -111,7 +111,7 @@ impl HttpProcessor { State::WaitingForStatus => { if let Some(break_index) = self.find_break_index() { let status = self.buffer_to_string(break_index); - trace!("Read status: {:?}", status); + debug!("Read status: {:?}", status); self.status = Some(status); self.set_state(State::WaitingForHeaders); } else { @@ -132,7 +132,7 @@ impl HttpProcessor { }, Some(break_index) => { let header = self.buffer_to_string(break_index); - trace!("Found header: {:?}", header); + debug!("Found header: {:?}", header); self.headers.push(header); }, None => return Ok(()), @@ -162,6 +162,9 @@ impl HttpProcessor { } } }, + State::WritingChunk(0) => { + self.set_state(State::Finished); + }, // Buffers the data until we have a full chunk State::WritingChunk(left) if self.buffer.get_ref().len() >= left => { try!(self.body_writer.write_all(&self.buffer.get_ref()[0..left])); @@ -236,6 +239,7 @@ mod tests { Server: Pari "; http.write_all(out.as_bytes()).unwrap(); + http.flush().unwrap(); // then assert_eq!(http.status().unwrap(), "HTTP/1.1 200 OK"); @@ -258,6 +262,7 @@ mod tests { \r\n\ "; http.write_all(out.as_bytes()).unwrap(); + http.flush().unwrap(); // then assert_eq!(http.status().unwrap(), "HTTP/1.1 200 OK"); @@ -283,6 +288,7 @@ mod tests { Some data\ "; http.write_all(out.as_bytes()).unwrap(); + http.flush().unwrap(); // then assert_eq!(http.status().unwrap(), "HTTP/1.1 200 OK"); @@ -317,11 +323,12 @@ mod tests { \r\n\ "; http.write_all(out.as_bytes()).unwrap(); + http.flush().unwrap(); // then assert_eq!(http.status().unwrap(), "HTTP/1.1 200 OK"); assert_eq!(http.headers().len(), 3); assert_eq!(data.borrow().get_ref()[..], b"Parity in\r\n\r\nchunks."[..]); - assert_eq!(http.state(), State::WaitingForChunk); + assert_eq!(http.state(), State::Finished); } } diff --git a/util/https-fetch/src/tlsclient.rs b/util/https-fetch/src/tlsclient.rs index bdb53e273..e3ce44764 100644 --- a/util/https-fetch/src/tlsclient.rs +++ b/util/https-fetch/src/tlsclient.rs @@ -66,14 +66,15 @@ impl io::Read for TlsClient { } } +#[cfg(feature = "ca-github-only")] +static CA_CERTS: &'static [u8] = include_bytes!("./ca-github.crt"); +#[cfg(not(feature = "ca-github-only"))] +static CA_CERTS: &'static [u8] = include_bytes!("./ca-certificates.crt"); + impl TlsClient { pub fn make_config() -> Result, FetchError> { let mut config = rustls::ClientConfig::new(); - let mut cursor = Cursor::new(if cfg!(feature = "ca-github-only") { - include_bytes!("./ca-github.crt").to_vec() - } else { - include_bytes!("./ca-certificates.crt").to_vec() - }); + let mut cursor = Cursor::new(CA_CERTS.to_vec()); let mut reader = BufReader::new(&mut cursor); try!(config.root_store.add_pem_file(&mut reader).map_err(|_| FetchError::ReadingCaCertificates)); // TODO [ToDr] client certificate? diff --git a/util/src/sha3.rs b/util/src/sha3.rs index 59b66efa9..131024025 100644 --- a/util/src/sha3.rs +++ b/util/src/sha3.rs @@ -17,7 +17,7 @@ //! Wrapper around tiny-keccak crate. extern crate sha3 as sha3_ext; -use std::io::{self, Read}; +use std::io; use std::mem::uninitialized; use tiny_keccak::Keccak; use bytes::{BytesConvertable, Populatable}; @@ -67,7 +67,7 @@ impl Hashable for T where T: BytesConvertable { } /// Calculate SHA3 of given stream. -pub fn sha3(r: &mut io::BufReader) -> Result { +pub fn sha3(r: &mut io::BufRead) -> Result { let mut output = [0u8; 32]; let mut input = [0u8; 1024]; let mut sha3 = Keccak::new_keccak256(); @@ -88,7 +88,7 @@ pub fn sha3(r: &mut io::BufReader) -> Result { #[cfg(test)] mod tests { use std::fs; - use std::io::Write; + use std::io::{Write, BufReader}; use super::*; #[test] @@ -111,7 +111,7 @@ mod tests { file.write_all(b"something").unwrap(); } - let mut file = fs::File::open(&path).unwrap(); + let mut file = BufReader::new(fs::File::open(&path).unwrap()); // when let hash = sha3(&mut file).unwrap();