Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(net): Split Ssl into SslClient and SslServer #757

Merged
merged 1 commit into from
Apr 6, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this break constructing HttpsListener for users who don't have the SslServer imported?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe so. You don't need to have the trait imported for a type to implement it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that sounds right on second thought

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