Fixing tests and review comments
This commit is contained in:
parent
8f13b550d8
commit
99acd4914e
@ -301,6 +301,6 @@ mod tests {
|
|||||||
let url = app.url();
|
let url = app.url();
|
||||||
|
|
||||||
// then
|
// 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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
extern crate https_fetch;
|
extern crate https_fetch;
|
||||||
|
|
||||||
use std::io;
|
use std::io;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::sync::atomic::AtomicBool;
|
||||||
use https_fetch::*;
|
use https_fetch::*;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let client = Client::new().unwrap();
|
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());
|
assert!(result.is_ok());
|
||||||
}).unwrap();
|
}).unwrap();
|
||||||
}
|
}
|
||||||
|
@ -174,7 +174,7 @@ impl mio::Handler for ClientLoop {
|
|||||||
fn should_successfuly_fetch_a_page() {
|
fn should_successfuly_fetch_a_page() {
|
||||||
use std::io::{self, Cursor};
|
use std::io::{self, Cursor};
|
||||||
use std::sync::{mpsc, Arc};
|
use std::sync::{mpsc, Arc};
|
||||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
use std::sync::atomic::{AtomicUsize, AtomicBool, Ordering};
|
||||||
|
|
||||||
struct Writer {
|
struct Writer {
|
||||||
wrote: Arc<AtomicUsize>,
|
wrote: Arc<AtomicUsize>,
|
||||||
@ -200,11 +200,11 @@ fn should_successfuly_fetch_a_page() {
|
|||||||
data: Cursor::new(Vec::new()),
|
data: Cursor::new(Vec::new()),
|
||||||
};
|
};
|
||||||
let (tx, rx) = mpsc::channel();
|
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!(result.is_ok());
|
||||||
assert!(wrote.load(Ordering::Relaxed) > 0);
|
assert!(wrote.load(Ordering::Relaxed) > 0);
|
||||||
tx.send(result).unwrap();
|
tx.send(result).unwrap();
|
||||||
});
|
}).unwrap();
|
||||||
let _ = rx.recv().unwrap();
|
let _ = rx.recv().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,7 +101,7 @@ impl HttpProcessor {
|
|||||||
}
|
}
|
||||||
fn set_state(&mut self, state: State) {
|
fn set_state(&mut self, state: State) {
|
||||||
self.state = state;
|
self.state = state;
|
||||||
debug!("Changing state to {:?}", state);
|
trace!("Changing state to {:?}", state);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn process_buffer(&mut self) -> io::Result<()> {
|
fn process_buffer(&mut self) -> io::Result<()> {
|
||||||
@ -111,7 +111,7 @@ impl HttpProcessor {
|
|||||||
State::WaitingForStatus => {
|
State::WaitingForStatus => {
|
||||||
if let Some(break_index) = self.find_break_index() {
|
if let Some(break_index) = self.find_break_index() {
|
||||||
let status = self.buffer_to_string(break_index);
|
let status = self.buffer_to_string(break_index);
|
||||||
trace!("Read status: {:?}", status);
|
debug!("Read status: {:?}", status);
|
||||||
self.status = Some(status);
|
self.status = Some(status);
|
||||||
self.set_state(State::WaitingForHeaders);
|
self.set_state(State::WaitingForHeaders);
|
||||||
} else {
|
} else {
|
||||||
@ -132,7 +132,7 @@ impl HttpProcessor {
|
|||||||
},
|
},
|
||||||
Some(break_index) => {
|
Some(break_index) => {
|
||||||
let header = self.buffer_to_string(break_index);
|
let header = self.buffer_to_string(break_index);
|
||||||
trace!("Found header: {:?}", header);
|
debug!("Found header: {:?}", header);
|
||||||
self.headers.push(header);
|
self.headers.push(header);
|
||||||
},
|
},
|
||||||
None => return Ok(()),
|
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
|
// Buffers the data until we have a full chunk
|
||||||
State::WritingChunk(left) if self.buffer.get_ref().len() >= left => {
|
State::WritingChunk(left) if self.buffer.get_ref().len() >= left => {
|
||||||
try!(self.body_writer.write_all(&self.buffer.get_ref()[0..left]));
|
try!(self.body_writer.write_all(&self.buffer.get_ref()[0..left]));
|
||||||
@ -236,6 +239,7 @@ mod tests {
|
|||||||
Server: Pari
|
Server: Pari
|
||||||
";
|
";
|
||||||
http.write_all(out.as_bytes()).unwrap();
|
http.write_all(out.as_bytes()).unwrap();
|
||||||
|
http.flush().unwrap();
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assert_eq!(http.status().unwrap(), "HTTP/1.1 200 OK");
|
assert_eq!(http.status().unwrap(), "HTTP/1.1 200 OK");
|
||||||
@ -258,6 +262,7 @@ mod tests {
|
|||||||
\r\n\
|
\r\n\
|
||||||
";
|
";
|
||||||
http.write_all(out.as_bytes()).unwrap();
|
http.write_all(out.as_bytes()).unwrap();
|
||||||
|
http.flush().unwrap();
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assert_eq!(http.status().unwrap(), "HTTP/1.1 200 OK");
|
assert_eq!(http.status().unwrap(), "HTTP/1.1 200 OK");
|
||||||
@ -283,6 +288,7 @@ mod tests {
|
|||||||
Some data\
|
Some data\
|
||||||
";
|
";
|
||||||
http.write_all(out.as_bytes()).unwrap();
|
http.write_all(out.as_bytes()).unwrap();
|
||||||
|
http.flush().unwrap();
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assert_eq!(http.status().unwrap(), "HTTP/1.1 200 OK");
|
assert_eq!(http.status().unwrap(), "HTTP/1.1 200 OK");
|
||||||
@ -317,11 +323,12 @@ mod tests {
|
|||||||
\r\n\
|
\r\n\
|
||||||
";
|
";
|
||||||
http.write_all(out.as_bytes()).unwrap();
|
http.write_all(out.as_bytes()).unwrap();
|
||||||
|
http.flush().unwrap();
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assert_eq!(http.status().unwrap(), "HTTP/1.1 200 OK");
|
assert_eq!(http.status().unwrap(), "HTTP/1.1 200 OK");
|
||||||
assert_eq!(http.headers().len(), 3);
|
assert_eq!(http.headers().len(), 3);
|
||||||
assert_eq!(data.borrow().get_ref()[..], b"Parity in\r\n\r\nchunks."[..]);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
impl TlsClient {
|
||||||
pub fn make_config() -> Result<Arc<rustls::ClientConfig>, FetchError> {
|
pub fn make_config() -> Result<Arc<rustls::ClientConfig>, FetchError> {
|
||||||
let mut config = rustls::ClientConfig::new();
|
let mut config = rustls::ClientConfig::new();
|
||||||
let mut cursor = Cursor::new(if cfg!(feature = "ca-github-only") {
|
let mut cursor = Cursor::new(CA_CERTS.to_vec());
|
||||||
include_bytes!("./ca-github.crt").to_vec()
|
|
||||||
} else {
|
|
||||||
include_bytes!("./ca-certificates.crt").to_vec()
|
|
||||||
});
|
|
||||||
let mut reader = BufReader::new(&mut cursor);
|
let mut reader = BufReader::new(&mut cursor);
|
||||||
try!(config.root_store.add_pem_file(&mut reader).map_err(|_| FetchError::ReadingCaCertificates));
|
try!(config.root_store.add_pem_file(&mut reader).map_err(|_| FetchError::ReadingCaCertificates));
|
||||||
// TODO [ToDr] client certificate?
|
// TODO [ToDr] client certificate?
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
//! Wrapper around tiny-keccak crate.
|
//! Wrapper around tiny-keccak crate.
|
||||||
extern crate sha3 as sha3_ext;
|
extern crate sha3 as sha3_ext;
|
||||||
|
|
||||||
use std::io::{self, Read};
|
use std::io;
|
||||||
use std::mem::uninitialized;
|
use std::mem::uninitialized;
|
||||||
use tiny_keccak::Keccak;
|
use tiny_keccak::Keccak;
|
||||||
use bytes::{BytesConvertable, Populatable};
|
use bytes::{BytesConvertable, Populatable};
|
||||||
@ -67,7 +67,7 @@ impl<T> Hashable for T where T: BytesConvertable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Calculate SHA3 of given stream.
|
/// Calculate SHA3 of given stream.
|
||||||
pub fn sha3<R: io::Read>(r: &mut io::BufReader<R>) -> Result<H256, io::Error> {
|
pub fn sha3(r: &mut io::BufRead) -> Result<H256, io::Error> {
|
||||||
let mut output = [0u8; 32];
|
let mut output = [0u8; 32];
|
||||||
let mut input = [0u8; 1024];
|
let mut input = [0u8; 1024];
|
||||||
let mut sha3 = Keccak::new_keccak256();
|
let mut sha3 = Keccak::new_keccak256();
|
||||||
@ -88,7 +88,7 @@ pub fn sha3<R: io::Read>(r: &mut io::BufReader<R>) -> Result<H256, io::Error> {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io::Write;
|
use std::io::{Write, BufReader};
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -111,7 +111,7 @@ mod tests {
|
|||||||
file.write_all(b"something").unwrap();
|
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
|
// when
|
||||||
let hash = sha3(&mut file).unwrap();
|
let hash = sha3(&mut file).unwrap();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user