rustls -> native_tls
This commit is contained in:
18
src/conn.rs
18
src/conn.rs
@@ -1,14 +1,12 @@
|
||||
use dns_lookup;
|
||||
use rustls;
|
||||
use std::io::Write;
|
||||
use std::net::IpAddr;
|
||||
use std::net::SocketAddr;
|
||||
use std::net::TcpStream;
|
||||
use std::time::Duration;
|
||||
use stream::Stream;
|
||||
use native_tls::TlsConnector;
|
||||
use url::Url;
|
||||
use webpki;
|
||||
use webpki_roots;
|
||||
|
||||
const CHUNK_SIZE: usize = 1024 * 1024;
|
||||
|
||||
@@ -134,19 +132,11 @@ fn connect_https(request: &Request, url: &Url) -> Result<Stream, Error> {
|
||||
let hostname = url.host_str().unwrap();
|
||||
let port = url.port().unwrap_or(443);
|
||||
|
||||
// TODO let user override TLS roots.
|
||||
let mut config = rustls::ClientConfig::new();
|
||||
config
|
||||
.root_store
|
||||
.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
|
||||
let rc_config = Arc::new(config);
|
||||
|
||||
let socket = connect_host(request, hostname, port)?;
|
||||
let connector = TlsConnector::builder()?.build()?;
|
||||
let stream = connector.connect(hostname, socket)?;
|
||||
|
||||
webpki::DNSNameRef::try_from_ascii_str(&hostname)
|
||||
.map_err(|_| Error::ConnectionFailed(format!("Invalid TLS name: {}", hostname)))
|
||||
.map(|webpki| rustls::ClientSession::new(&rc_config, webpki))
|
||||
.map(|client| Stream::Https(client, socket))
|
||||
Ok(Stream::Https(stream))
|
||||
}
|
||||
|
||||
fn connect_host(request: &Request, hostname: &str, port: u16) -> Result<TcpStream, Error> {
|
||||
|
||||
23
src/error.rs
23
src/error.rs
@@ -1,4 +1,7 @@
|
||||
use std::io::Error as IoError;
|
||||
use native_tls::Error as TlsError;
|
||||
use native_tls::HandshakeError;
|
||||
use std::net::TcpStream;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
@@ -10,6 +13,8 @@ pub enum Error {
|
||||
BadStatus,
|
||||
BadHeader,
|
||||
Io(IoError),
|
||||
Tls(TlsError),
|
||||
TlsHandshake(HandshakeError<TcpStream>),
|
||||
}
|
||||
|
||||
impl Error {
|
||||
@@ -23,6 +28,8 @@ impl Error {
|
||||
Error::BadStatus => 500,
|
||||
Error::BadHeader => 500,
|
||||
Error::Io(_) => 500,
|
||||
Error::Tls(_) => 400,
|
||||
Error::TlsHandshake(_) => 500,
|
||||
}
|
||||
}
|
||||
pub fn status_text(&self) -> &str {
|
||||
@@ -38,6 +45,8 @@ impl Error {
|
||||
Error::BadStatus => "Bad Status",
|
||||
Error::BadHeader => "Bad Header",
|
||||
Error::Io(_) => "Network Error",
|
||||
Error::Tls(_) => "TLS Error",
|
||||
Error::TlsHandshake(_) => "TLS Handshake Error",
|
||||
}
|
||||
}
|
||||
pub fn body_text(&self) -> String {
|
||||
@@ -50,6 +59,8 @@ impl Error {
|
||||
Error::BadStatus => "Bad Status".to_string(),
|
||||
Error::BadHeader => "Bad Header".to_string(),
|
||||
Error::Io(ioe) => format!("Network Error: {}", ioe),
|
||||
Error::Tls(tls) => format!("TLS Error: {}", tls),
|
||||
Error::TlsHandshake(he) => format!("TLS Handshake Error: {}", he),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -59,3 +70,15 @@ impl From<IoError> for Error {
|
||||
Error::Io(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TlsError> for Error {
|
||||
fn from(err: TlsError) -> Error {
|
||||
Error::Tls(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<HandshakeError<TcpStream>> for Error {
|
||||
fn from(err: HandshakeError<TcpStream>) -> Error {
|
||||
Error::TlsHandshake(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,11 +8,9 @@ extern crate encoding;
|
||||
extern crate lazy_static;
|
||||
extern crate mime_guess;
|
||||
extern crate qstring;
|
||||
extern crate rustls;
|
||||
extern crate serde_json;
|
||||
extern crate native_tls;
|
||||
extern crate url;
|
||||
extern crate webpki;
|
||||
extern crate webpki_roots;
|
||||
|
||||
#[macro_use]
|
||||
mod agent;
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
use rustls;
|
||||
use std::io::Read;
|
||||
use std::io::Result;
|
||||
use std::io::Write;
|
||||
use std::net::TcpStream;
|
||||
use native_tls::TlsStream;
|
||||
|
||||
|
||||
pub enum Stream {
|
||||
Http(TcpStream),
|
||||
Https(rustls::ClientSession, TcpStream),
|
||||
Https(TlsStream<TcpStream>),
|
||||
Read(Box<Read>),
|
||||
#[cfg(test)] Test(Box<Read + Send>, Vec<u8>),
|
||||
}
|
||||
@@ -25,7 +26,7 @@ impl Read for Stream {
|
||||
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
|
||||
match self {
|
||||
Stream::Http(sock) => sock.read(buf),
|
||||
Stream::Https(sess, sock) => rustls::Stream::new(sess, sock).read(buf),
|
||||
Stream::Https(stream) => stream.read(buf),
|
||||
Stream::Read(read) => read.read(buf),
|
||||
#[cfg(test)] Stream::Test(reader, _) => reader.read(buf),
|
||||
}
|
||||
@@ -36,7 +37,7 @@ impl Write for Stream {
|
||||
fn write(&mut self, buf: &[u8]) -> Result<usize> {
|
||||
match self {
|
||||
Stream::Http(sock) => sock.write(buf),
|
||||
Stream::Https(sess, sock) => rustls::Stream::new(sess, sock).write(buf),
|
||||
Stream::Https(stream) => stream.write(buf),
|
||||
Stream::Read(_) => panic!("Write to read stream"),
|
||||
#[cfg(test)] Stream::Test(_, writer) => writer.write(buf),
|
||||
}
|
||||
@@ -44,7 +45,7 @@ impl Write for Stream {
|
||||
fn flush(&mut self) -> Result<()> {
|
||||
match self {
|
||||
Stream::Http(sock) => sock.flush(),
|
||||
Stream::Https(sess, sock) => rustls::Stream::new(sess, sock).flush(),
|
||||
Stream::Https(stream) => stream.flush(),
|
||||
Stream::Read(_) => panic!("Flush read stream"),
|
||||
#[cfg(test)] Stream::Test(_, writer) => writer.flush(),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user