From bf97aa9fc88f36553c7f54676023cc9e26f7002e Mon Sep 17 00:00:00 2001 From: Martin Algesten Date: Sat, 1 Sep 2018 13:42:48 +0200 Subject: [PATCH] fix building without tls. close #1 --- src/error.rs | 18 ++++++++++++++++-- src/stream.rs | 5 +++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/error.rs b/src/error.rs index 35cb6d6..7b82833 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,7 +1,11 @@ -use native_tls::Error as TlsError; -use native_tls::HandshakeError; use std::io::Error as IoError; + +#[cfg(feature = "tls")] use std::net::TcpStream; +#[cfg(feature = "tls")] +use native_tls::Error as TlsError; +#[cfg(feature = "tls")] +use native_tls::HandshakeError; /// Errors that are translated to ["synthetic" responses](struct.Response.html#method.synthetic). #[derive(Debug)] @@ -23,8 +27,10 @@ pub enum Error { /// Some unspecified `std::io::Error`. Synthetic error `500`. Io(IoError), /// Some unspecified TLS error. Synthetic error `400`. + #[cfg(feature = "tls")] Tls(TlsError), /// Some unspecified TLS handshake error. Synthetic error `500`. + #[cfg(feature = "tls")] TlsHandshake(HandshakeError), } @@ -40,7 +46,9 @@ impl Error { Error::BadStatus => 500, Error::BadHeader => 500, Error::Io(_) => 500, + #[cfg(feature = "tls")] Error::Tls(_) => 400, + #[cfg(feature = "tls")] Error::TlsHandshake(_) => 500, } } @@ -56,7 +64,9 @@ impl Error { Error::BadStatus => "Bad Status", Error::BadHeader => "Bad Header", Error::Io(_) => "Network Error", + #[cfg(feature = "tls")] Error::Tls(_) => "TLS Error", + #[cfg(feature = "tls")] Error::TlsHandshake(_) => "TLS Handshake Error", } } @@ -72,7 +82,9 @@ impl Error { Error::BadStatus => "Bad Status".to_string(), Error::BadHeader => "Bad Header".to_string(), Error::Io(ioe) => format!("Network Error: {}", ioe), + #[cfg(feature = "tls")] Error::Tls(tls) => format!("TLS Error: {}", tls), + #[cfg(feature = "tls")] Error::TlsHandshake(he) => format!("TLS Handshake Error: {}", he), } } @@ -84,12 +96,14 @@ impl From for Error { } } +#[cfg(feature = "tls")] impl From for Error { fn from(err: TlsError) -> Error { Error::Tls(err) } } +#[cfg(feature = "tls")] impl From> for Error { fn from(err: HandshakeError) -> Error { Error::TlsHandshake(err) diff --git a/src/stream.rs b/src/stream.rs index a717e74..b316ed5 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -25,6 +25,7 @@ impl ::std::fmt::Debug for Stream { "Stream[{}]", match self { Stream::Http(_) => "http", + #[cfg(feature = "tls")] Stream::Https(_) => "https", Stream::Cursor(_) => "cursor", #[cfg(test)] @@ -38,6 +39,7 @@ impl Stream { pub fn is_poolable(&self) -> bool { match self { Stream::Http(_) => true, + #[cfg(feature = "tls")] Stream::Https(_) => true, _ => false, } @@ -56,6 +58,7 @@ impl Read for Stream { fn read(&mut self, buf: &mut [u8]) -> IoResult { match self { Stream::Http(sock) => sock.read(buf), + #[cfg(feature = "tls")] Stream::Https(stream) => stream.read(buf), Stream::Cursor(read) => read.read(buf), #[cfg(test)] @@ -68,6 +71,7 @@ impl Write for Stream { fn write(&mut self, buf: &[u8]) -> IoResult { match self { Stream::Http(sock) => sock.write(buf), + #[cfg(feature = "tls")] Stream::Https(stream) => stream.write(buf), Stream::Cursor(_) => panic!("Write to read only stream"), #[cfg(test)] @@ -77,6 +81,7 @@ impl Write for Stream { fn flush(&mut self) -> IoResult<()> { match self { Stream::Http(sock) => sock.flush(), + #[cfg(feature = "tls")] Stream::Https(stream) => stream.flush(), Stream::Cursor(_) => panic!("Flush read only stream"), #[cfg(test)]