Merge pull request #757 from sfackler/ssl-split

feat(net): Split Ssl into SslClient and SslServer
This commit is contained in:
Sean McArthur
2016-04-06 13:51:13 -07:00

View File

@@ -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 { pub trait Ssl {
/// The protected stream. /// The protected stream.
type Stream: NetworkStream + Send + Clone; type Stream: NetworkStream + Send + Clone;
@@ -420,6 +422,38 @@ pub trait Ssl {
fn wrap_server(&self, stream: HttpStream) -> ::Result<Self::Stream>; 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. /// A stream over the HTTP protocol, possibly protected by SSL.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum HttpsStream<S: NetworkStream> { pub enum HttpsStream<S: NetworkStream> {
@@ -493,7 +527,7 @@ impl<S: NetworkStream> NetworkStream for HttpsStream<S> {
/// A Http Listener over SSL. /// A Http Listener over SSL.
#[derive(Clone)] #[derive(Clone)]
pub struct HttpsListener<S: Ssl> { pub struct HttpsListener<S: SslServer> {
listener: HttpListener, listener: HttpListener,
ssl: S, ssl: S,
} }
@@ -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; type Stream = S::Stream;
#[inline] #[inline]
@@ -532,18 +566,18 @@ impl<S: Ssl + Clone> NetworkListener for HttpsListener<S> {
/// A connector that can protect HTTP streams using SSL. /// A connector that can protect HTTP streams using SSL.
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct HttpsConnector<S: Ssl> { pub struct HttpsConnector<S: SslClient> {
ssl: S ssl: S
} }
impl<S: Ssl> HttpsConnector<S> { impl<S: SslClient> HttpsConnector<S> {
/// Create a new connector using the provided SSL implementation. /// Create a new connector using the provided SSL implementation.
pub fn new(s: S) -> HttpsConnector<S> { pub fn new(s: S) -> HttpsConnector<S> {
HttpsConnector { ssl: s } HttpsConnector { ssl: s }
} }
} }
impl<S: Ssl> NetworkConnector for HttpsConnector<S> { impl<S: SslClient> NetworkConnector for HttpsConnector<S> {
type Stream = HttpsStream<S::Stream>; type Stream = HttpsStream<S::Stream>;
fn connect(&self, host: &str, port: u16, scheme: &str) -> ::Result<Self::Stream> { fn connect(&self, host: &str, port: u16, scheme: &str) -> ::Result<Self::Stream> {