From 2c86e8078ec01db2283e1fee1461db4c7bf76d3f Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Tue, 5 Apr 2016 21:46:32 -0700 Subject: [PATCH] feat(net): Split Ssl into SslClient and SslServer 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 --- src/net.rs | 46 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/src/net.rs b/src/net.rs index e913467e95..6495964c3d 100644 --- a/src/net.rs +++ b/src/net.rs @@ -410,7 +410,9 @@ impl NetworkConnector for F where F: Fn(&str, u16, &str) -> io::Result ::Result; } +/// 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; +} + +/// 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; +} + +impl SslClient for S { + type Stream = ::Stream; + + fn wrap_client(&self, stream: HttpStream, host: &str) -> ::Result { + Ssl::wrap_client(self, stream, host) + } +} + +impl SslServer for S { + type Stream = ::Stream; + + fn wrap_server(&self, stream: HttpStream) -> ::Result { + Ssl::wrap_server(self, stream) + } +} + /// A stream over the HTTP protocol, possibly protected by SSL. #[derive(Debug, Clone)] pub enum HttpsStream { @@ -493,7 +527,7 @@ impl NetworkStream for HttpsStream { /// A Http Listener over SSL. #[derive(Clone)] -pub struct HttpsListener { +pub struct HttpsListener { listener: HttpListener, ssl: S, } @@ -516,7 +550,7 @@ impl HttpsListener { } } -impl NetworkListener for HttpsListener { +impl NetworkListener for HttpsListener { type Stream = S::Stream; #[inline] @@ -532,18 +566,18 @@ impl NetworkListener for HttpsListener { /// A connector that can protect HTTP streams using SSL. #[derive(Debug, Default)] -pub struct HttpsConnector { +pub struct HttpsConnector { ssl: S } -impl HttpsConnector { +impl HttpsConnector { /// Create a new connector using the provided SSL implementation. pub fn new(s: S) -> HttpsConnector { HttpsConnector { ssl: s } } } -impl NetworkConnector for HttpsConnector { +impl NetworkConnector for HttpsConnector { type Stream = HttpsStream; fn connect(&self, host: &str, port: u16, scheme: &str) -> ::Result {