Skip to content

Commit 2c86e80

Browse files
committed
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
1 parent bc3878d commit 2c86e80

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

src/net.rs

+40-6
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,9 @@ impl<F> NetworkConnector for F where F: Fn(&str, u16, &str) -> io::Result<TcpStr
410410
}
411411
}
412412

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

425+
/// An abstraction to allow any SSL implementation to be used with client-side HttpsStreams.
426+
pub trait SslClient {
427+
/// The protected stream.
428+
type Stream: NetworkStream + Send + Clone;
429+
/// Wrap a client stream with SSL.
430+
fn wrap_client(&self, stream: HttpStream, host: &str) -> ::Result<Self::Stream>;
431+
}
432+
433+
/// An abstraction to allow any SSL implementation to be used with server-side HttpsStreams.
434+
pub trait SslServer {
435+
/// The protected stream.
436+
type Stream: NetworkStream + Send + Clone;
437+
/// Wrap a server stream with SSL.
438+
fn wrap_server(&self, stream: HttpStream) -> ::Result<Self::Stream>;
439+
}
440+
441+
impl<S: Ssl> SslClient for S {
442+
type Stream = <S as Ssl>::Stream;
443+
444+
fn wrap_client(&self, stream: HttpStream, host: &str) -> ::Result<Self::Stream> {
445+
Ssl::wrap_client(self, stream, host)
446+
}
447+
}
448+
449+
impl<S: Ssl> SslServer for S {
450+
type Stream = <S as Ssl>::Stream;
451+
452+
fn wrap_server(&self, stream: HttpStream) -> ::Result<Self::Stream> {
453+
Ssl::wrap_server(self, stream)
454+
}
455+
}
456+
423457
/// A stream over the HTTP protocol, possibly protected by SSL.
424458
#[derive(Debug, Clone)]
425459
pub enum HttpsStream<S: NetworkStream> {
@@ -493,7 +527,7 @@ impl<S: NetworkStream> NetworkStream for HttpsStream<S> {
493527

494528
/// A Http Listener over SSL.
495529
#[derive(Clone)]
496-
pub struct HttpsListener<S: Ssl> {
530+
pub struct HttpsListener<S: SslServer> {
497531
listener: HttpListener,
498532
ssl: S,
499533
}
@@ -516,7 +550,7 @@ impl<S: Ssl> HttpsListener<S> {
516550
}
517551
}
518552

519-
impl<S: Ssl + Clone> NetworkListener for HttpsListener<S> {
553+
impl<S: SslServer + Clone> NetworkListener for HttpsListener<S> {
520554
type Stream = S::Stream;
521555

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

533567
/// A connector that can protect HTTP streams using SSL.
534568
#[derive(Debug, Default)]
535-
pub struct HttpsConnector<S: Ssl> {
569+
pub struct HttpsConnector<S: SslClient> {
536570
ssl: S
537571
}
538572

539-
impl<S: Ssl> HttpsConnector<S> {
573+
impl<S: SslClient> HttpsConnector<S> {
540574
/// Create a new connector using the provided SSL implementation.
541575
pub fn new(s: S) -> HttpsConnector<S> {
542576
HttpsConnector { ssl: s }
543577
}
544578
}
545579

546-
impl<S: Ssl> NetworkConnector for HttpsConnector<S> {
580+
impl<S: SslClient> NetworkConnector for HttpsConnector<S> {
547581
type Stream = HttpsStream<S::Stream>;
548582

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

0 commit comments

Comments
 (0)