Skip to content

Commit

Permalink
feat(net): Split Ssl into SslClient and SslServer
Browse files Browse the repository at this point in the history
SSL libraries other than OpenSSL don't necessarily have the ability to
easily create server and client side connections at the same time. This
is backwards compatible due to blanket impls.

Closes #756
  • Loading branch information
sfackler committed Apr 6, 2016
1 parent bc3878d commit 2c86e80
Showing 1 changed file with 40 additions and 6 deletions.
46 changes: 40 additions & 6 deletions src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,9 @@ impl<F> NetworkConnector for F where F: Fn(&str, u16, &str) -> io::Result<TcpStr
}
}

/// An abstraction to allow any SSL implementation to be used with HttpsStreams.
/// Deprecated
///
/// Use `SslClient` and `SslServer` instead.
pub trait Ssl {
/// The protected stream.
type Stream: NetworkStream + Send + Clone;
Expand All @@ -420,6 +422,38 @@ pub trait Ssl {
fn wrap_server(&self, stream: HttpStream) -> ::Result<Self::Stream>;
}

/// An abstraction to allow any SSL implementation to be used with client-side HttpsStreams.
pub trait SslClient {
/// The protected stream.
type Stream: NetworkStream + Send + Clone;
/// Wrap a client stream with SSL.
fn wrap_client(&self, stream: HttpStream, host: &str) -> ::Result<Self::Stream>;
}

/// An abstraction to allow any SSL implementation to be used with server-side HttpsStreams.
pub trait SslServer {
/// The protected stream.
type Stream: NetworkStream + Send + Clone;
/// Wrap a server stream with SSL.
fn wrap_server(&self, stream: HttpStream) -> ::Result<Self::Stream>;
}

impl<S: Ssl> SslClient for S {
type Stream = <S as Ssl>::Stream;

fn wrap_client(&self, stream: HttpStream, host: &str) -> ::Result<Self::Stream> {
Ssl::wrap_client(self, stream, host)
}
}

impl<S: Ssl> SslServer for S {
type Stream = <S as Ssl>::Stream;

fn wrap_server(&self, stream: HttpStream) -> ::Result<Self::Stream> {
Ssl::wrap_server(self, stream)
}
}

/// A stream over the HTTP protocol, possibly protected by SSL.
#[derive(Debug, Clone)]
pub enum HttpsStream<S: NetworkStream> {
Expand Down Expand Up @@ -493,7 +527,7 @@ impl<S: NetworkStream> NetworkStream for HttpsStream<S> {

/// A Http Listener over SSL.
#[derive(Clone)]
pub struct HttpsListener<S: Ssl> {
pub struct HttpsListener<S: SslServer> {
listener: HttpListener,
ssl: S,
}
Expand All @@ -516,7 +550,7 @@ impl<S: Ssl> HttpsListener<S> {
}
}

impl<S: Ssl + Clone> NetworkListener for HttpsListener<S> {
impl<S: SslServer + Clone> NetworkListener for HttpsListener<S> {
type Stream = S::Stream;

#[inline]
Expand All @@ -532,18 +566,18 @@ impl<S: Ssl + Clone> NetworkListener for HttpsListener<S> {

/// A connector that can protect HTTP streams using SSL.
#[derive(Debug, Default)]
pub struct HttpsConnector<S: Ssl> {
pub struct HttpsConnector<S: SslClient> {
ssl: S
}

impl<S: Ssl> HttpsConnector<S> {
impl<S: SslClient> HttpsConnector<S> {
/// Create a new connector using the provided SSL implementation.
pub fn new(s: S) -> HttpsConnector<S> {
HttpsConnector { ssl: s }
}
}

impl<S: Ssl> NetworkConnector for HttpsConnector<S> {
impl<S: SslClient> NetworkConnector for HttpsConnector<S> {
type Stream = HttpsStream<S::Stream>;

fn connect(&self, host: &str, port: u16, scheme: &str) -> ::Result<Self::Stream> {
Expand Down

0 comments on commit 2c86e80

Please sign in to comment.