Merge pull request #41 from sfackler/hyper-native-tls

Use external hyper-native-tls crate
This commit is contained in:
Sean McArthur
2017-01-21 12:50:38 -08:00
committed by GitHub
4 changed files with 6 additions and 86 deletions

View File

@@ -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"

View File

@@ -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)))))
)
))
}

View File

@@ -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.

View File

@@ -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<TlsClient> {
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::Stream> {
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<HttpStream>);
impl Read for TlsStream {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.0.read(buf)
}
}
impl Write for TlsStream {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
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<SocketAddr> {
self.0.get_mut().peer_addr()
}
fn set_read_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
self.0.get_ref().set_read_timeout(dur)
}
fn set_write_timeout(&self, dur: Option<Duration>) -> io::Result<()> {
self.0.get_ref().set_write_timeout(dur)
}
}