From 819403f54fdf8b33917505bfe64d02964cddb35c Mon Sep 17 00:00:00 2001 From: Jonathan Reem Date: Thu, 20 Nov 2014 00:26:34 -0800 Subject: [PATCH 1/2] Put openssl support behind a Cargo feature. This is a preliminary step to support crates.io since rust-openssl is not yet on crates.io. The feature can be removed or made default in the future. --- Cargo.toml | 5 +++++ src/client/request.rs | 1 - src/lib.rs | 6 ++++-- src/net.rs | 29 +++++++++++++++++++++++------ 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4d179a0bd1..b653e00756 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,11 +4,16 @@ name = "hyper" version = "0.0.1" authors = ["Sean McArthur "] +[features] + +ssl = ["openssl"] + [dependencies.url] git = "https://github.com/servo/rust-url" [dependencies.openssl] git = "https://github.com/sfackler/rust-openssl" +optional = true [dependencies.mime] git = "https://github.com/hyperium/mime.rs" diff --git a/src/client/request.rs b/src/client/request.rs index cd0b325998..1cf1a2eb11 100644 --- a/src/client/request.rs +++ b/src/client/request.rs @@ -15,7 +15,6 @@ use version; use HttpResult; use client::Response; - /// A client request to a remote server. pub struct Request { /// The target URI for this request. diff --git a/src/lib.rs b/src/lib.rs index 088362a328..b451b73975 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ #![feature(macro_rules, phase, default_type_params, if_let, slicing_syntax, - tuple_indexing)] + tuple_indexing, globs)] #![deny(missing_docs)] #![deny(warnings)] #![experimental] @@ -129,7 +129,6 @@ extern crate serialize; extern crate time; extern crate url; -extern crate openssl; #[phase(plugin,link)] extern crate log; #[cfg(test)] extern crate test; extern crate "unsafe-any" as uany; @@ -137,6 +136,9 @@ extern crate "move-acceptor" as macceptor; extern crate typeable; extern crate cookie; +#[cfg(feature = "ssl")] +extern crate openssl; + pub use std::io::net::ip::{SocketAddr, IpAddr, Ipv4Addr, Ipv6Addr, Port}; pub use mimewrapper::mime; pub use url::Url; diff --git a/src/net.rs b/src/net.rs index 9c2d0a3147..95c484b492 100644 --- a/src/net.rs +++ b/src/net.rs @@ -3,20 +3,30 @@ use std::any::{Any, AnyRefExt}; use std::boxed::BoxAny; use std::fmt; use std::intrinsics::TypeId; -use std::io::{IoResult, IoError, ConnectionAborted, InvalidInput, OtherIoError, - Stream, Listener, Acceptor}; +use std::io::{IoResult, IoError, InvalidInput, Stream, Listener, Acceptor}; use std::io::net::ip::{SocketAddr, ToSocketAddr}; use std::io::net::tcp::{TcpStream, TcpListener, TcpAcceptor}; use std::mem::{mod, transmute, transmute_copy}; use std::raw::{mod, TraitObject}; -use std::sync::{Arc, Mutex}; use uany::UncheckedBoxAnyDowncast; use typeable::Typeable; -use openssl::ssl::{SslStream, SslContext, Sslv23}; -use openssl::ssl::error::{SslError, StreamError, OpenSslErrors, SslSessionClosed}; -use self::HttpStream::{Http, Https}; +use self::HttpStream::Http; + +#[cfg(feature = "ssl")] +use self::HttpStream::Https; + +#[cfg(feature = "ssl")] +use self::sslhelpers::*; + +#[cfg(feature = "ssl")] +mod sslhelpers { + pub use openssl::ssl::{SslStream, SslContext, Sslv23}; + pub use openssl::ssl::error::{SslError, StreamError, OpenSslErrors, SslSessionClosed}; + pub use std::sync::{Arc, Mutex}; + pub use std::io::{ConnectionAborted, OtherIoError}; +} /// The write-status indicating headers have not been written. pub struct Fresh; @@ -176,6 +186,7 @@ pub enum HttpStream { /// A stream over the HTTP protocol. Http(TcpStream), /// A stream over the HTTP protocol, protected by SSL. + #[cfg(feature = "ssl")] // You may be asking wtf an Arc and Mutex? That's because SslStream // doesn't implement Clone, and we need Clone to use the stream for // both the Request and Response. @@ -188,6 +199,7 @@ impl Reader for HttpStream { fn read(&mut self, buf: &mut [u8]) -> IoResult { match *self { Http(ref mut inner) => inner.read(buf), + #[cfg(feature = "ssl")] Https(ref mut inner, _) => inner.lock().read(buf) } } @@ -198,6 +210,7 @@ impl Writer for HttpStream { fn write(&mut self, msg: &[u8]) -> IoResult<()> { match *self { Http(ref mut inner) => inner.write(msg), + #[cfg(feature = "ssl")] Https(ref mut inner, _) => inner.lock().write(msg) } } @@ -205,6 +218,7 @@ impl Writer for HttpStream { fn flush(&mut self) -> IoResult<()> { match *self { Http(ref mut inner) => inner.flush(), + #[cfg(feature = "ssl")] Https(ref mut inner, _) => inner.lock().flush(), } } @@ -215,6 +229,7 @@ impl NetworkStream for HttpStream { fn peer_name(&mut self) -> IoResult { match *self { Http(ref mut inner) => inner.peer_name(), + #[cfg(feature = "ssl")] Https(_, addr) => Ok(addr) } } @@ -227,6 +242,7 @@ impl NetworkConnector for HttpStream { debug!("http scheme"); Ok(Http(try!(TcpStream::connect(addr)))) }, + #[cfg(feature = "ssl")] "https" => { debug!("https scheme"); let mut stream = try!(TcpStream::connect(addr)); @@ -248,6 +264,7 @@ impl NetworkConnector for HttpStream { } } +#[cfg(feature = "ssl")] fn lift_ssl_error(ssl: SslError) -> IoError { match ssl { StreamError(err) => err, From b642b77112bba6b5e0f8ebb252a0965878d07de0 Mon Sep 17 00:00:00 2001 From: Jonathan Reem Date: Thu, 20 Nov 2014 00:29:26 -0800 Subject: [PATCH 2/2] Ensure ssl builds work in travis. --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index c5d68e4a52..5c83f56322 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,8 +2,11 @@ language: rust script: - cargo build + - cargo build --features ssl - cargo test + - cargo test --features ssl - cargo bench --no-run + - cargo bench --no-run --features ssl after_success: | [ $TRAVIS_BRANCH = master ] &&