From fa4df2037ac7b5c697e9e99a4b778b1ec1e41746 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sun, 8 Jan 2017 14:11:48 -0800 Subject: [PATCH] Use external hyper-native-tls crate --- Cargo.toml | 2 +- src/client.rs | 6 ++-- src/lib.rs | 3 +- src/tls.rs | 81 --------------------------------------------------- 4 files changed, 6 insertions(+), 86 deletions(-) delete mode 100644 src/tls.rs diff --git a/Cargo.toml b/Cargo.toml index e2a6d09..31bcd70 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,8 +10,8 @@ license = "MIT/Apache-2.0" [dependencies] hyper = "0.10" +hyper-native-tls = "0.2" log = "0.3" -native-tls = "0.1" serde = "0.8" serde_json = "0.8" serde_urlencoded = "0.3" diff --git a/src/client.rs b/src/client.rs index e7f19ef..8958222 100644 --- a/src/client.rs +++ b/src/client.rs @@ -94,11 +94,13 @@ struct ClientRef { } fn new_hyper_client() -> ::Result<::hyper::Client> { - use tls::TlsClient; + use hyper_native_tls::NativeTlsClient; Ok(::hyper::Client::with_connector( ::hyper::client::Pool::with_connector( Default::default(), - ::hyper::net::HttpsConnector::new(try!(TlsClient::new())) + ::hyper::net::HttpsConnector::new( + try!(NativeTlsClient::new() + .map_err(|e| ::hyper::Error::Ssl(Box::new(e))))) ) )) } diff --git a/src/lib.rs b/src/lib.rs index 42e1968..bf26920 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -91,7 +91,7 @@ extern crate hyper; #[macro_use] extern crate log; -extern crate native_tls; +extern crate hyper_native_tls; extern crate serde; extern crate serde_json; extern crate serde_urlencoded; @@ -115,7 +115,6 @@ mod body; mod client; mod error; mod redirect; -mod tls; /// Shortcut method to quickly make a `GET` request. diff --git a/src/tls.rs b/src/tls.rs deleted file mode 100644 index 6bb6070..0000000 --- a/src/tls.rs +++ /dev/null @@ -1,81 +0,0 @@ -use std::io::{self, Read, Write}; -use std::net::SocketAddr; -use std::time::Duration; -use std::fmt; - -use hyper::net::{SslClient, HttpStream, NetworkStream}; -use native_tls::{TlsConnector, TlsStream as NativeTlsStream, HandshakeError}; - -pub struct TlsClient(TlsConnector); - -impl TlsClient { - pub fn new() -> ::Result { - TlsConnector::builder() - .and_then(|c| c.build()) - .map(TlsClient) - .map_err(|e| ::Error::Http(::hyper::Error::Ssl(Box::new(e)))) - } -} - -impl SslClient for TlsClient { - type Stream = TlsStream; - - fn wrap_client(&self, stream: HttpStream, host: &str) -> ::hyper::Result { - self.0.connect(host, stream).map(TlsStream).map_err(|e| { - match e { - HandshakeError::Failure(e) => ::hyper::Error::Ssl(Box::new(e)), - HandshakeError::Interrupted(..) => { - // while using hyper 0.9, this won't happen, because the - // socket is in blocking mode. once we move to hyper 0.10, - // much of this `tls` module will go away anyways - unreachable!("TlsClient::handshake Interrupted") - } - } - }) - } -} - -impl fmt::Debug for TlsClient { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_tuple("TlsClient").field(&"_").finish() - } -} - -#[derive(Debug)] -pub struct TlsStream(NativeTlsStream); - -impl Read for TlsStream { - fn read(&mut self, buf: &mut [u8]) -> io::Result { - self.0.read(buf) - } -} - -impl Write for TlsStream { - fn write(&mut self, data: &[u8]) -> io::Result { - self.0.write(data) - } - - fn flush(&mut self) -> io::Result<()> { - self.0.flush() - } -} - -impl Clone for TlsStream { - fn clone(&self) -> TlsStream { - unreachable!("TlsStream::clone is never used for the Client") - } -} - -impl NetworkStream for TlsStream { - fn peer_addr(&mut self) -> io::Result { - self.0.get_mut().peer_addr() - } - - fn set_read_timeout(&self, dur: Option) -> io::Result<()> { - self.0.get_ref().set_read_timeout(dur) - } - - fn set_write_timeout(&self, dur: Option) -> io::Result<()> { - self.0.get_ref().set_write_timeout(dur) - } -}