-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
wip: ssl-verify #168
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
wip: ssl-verify #168
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -190,6 +190,31 @@ impl<W: Writer> HttpWriter<W> { | |
} | ||
} | ||
|
||
/// Access the inner Writer. | ||
#[inline] | ||
pub fn get_ref<'a>(&'a self) -> &'a W { | ||
match *self { | ||
ThroughWriter(ref w) => w, | ||
ChunkedWriter(ref w) => w, | ||
SizedWriter(ref w, _) => w, | ||
EmptyWriter(ref w) => w, | ||
} | ||
} | ||
|
||
/// Access the inner Writer mutably. | ||
/// | ||
/// Warning: You should not write to this directly, as you can corrupt | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was following suite to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. I think the warning should probably be enough. |
||
/// the state. | ||
#[inline] | ||
pub fn get_mut<'a>(&'a mut self) -> &'a mut W { | ||
match *self { | ||
ThroughWriter(ref mut w) => w, | ||
ChunkedWriter(ref mut w) => w, | ||
SizedWriter(ref mut w, _) => w, | ||
EmptyWriter(ref mut w) => w, | ||
} | ||
} | ||
|
||
/// Ends the HttpWriter, and returns the underlying Writer. | ||
/// | ||
/// A final `write()` is called with an empty message, and then flushed. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,6 @@ use std::io::net::ip::{SocketAddr, ToSocketAddr}; | |
use std::io::net::tcp::{TcpStream, TcpListener, TcpAcceptor}; | ||
use std::mem::{mod, transmute, transmute_copy}; | ||
use std::raw::{mod, TraitObject}; | ||
use std::sync::{Arc, Mutex}; | ||
|
||
use uany::UncheckedBoxAnyDowncast; | ||
use openssl::ssl::{SslStream, SslContext}; | ||
|
@@ -53,9 +52,9 @@ pub trait NetworkStream: Stream + Any + Clone + Send { | |
} | ||
|
||
/// A connector creates a NetworkStream. | ||
pub trait NetworkConnector: NetworkStream { | ||
pub trait NetworkConnector<S: NetworkStream> { | ||
/// Connect to a remote address. | ||
fn connect<To: ToSocketAddr>(addr: To, scheme: &str) -> IoResult<Self>; | ||
fn connect<To: ToSocketAddr>(&mut self, addr: To, scheme: &str) -> IoResult<S>; | ||
} | ||
|
||
impl fmt::Show for Box<NetworkStream + Send> { | ||
|
@@ -189,19 +188,15 @@ pub enum HttpStream { | |
/// A stream over the HTTP protocol. | ||
Http(TcpStream), | ||
/// A stream over the HTTP protocol, protected by SSL. | ||
// You may be asking wtf an Arc and Mutex? That's because SslStream | ||
// doesn't implement Clone, and we need Clone to use the stream for | ||
// both the Request and Response. | ||
// FIXME: https://github.com/sfackler/rust-openssl/issues/6 | ||
Https(Arc<Mutex<SslStream<TcpStream>>>, SocketAddr), | ||
Https(SslStream<TcpStream>), | ||
} | ||
|
||
impl Reader for HttpStream { | ||
#[inline] | ||
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. \o/ this was fixed upstream There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ya, huge improvement on our end. |
||
match *self { | ||
Http(ref mut inner) => inner.read(buf), | ||
Https(ref mut inner, _) => inner.lock().read(buf) | ||
Https(ref mut inner) => inner.read(buf) | ||
} | ||
} | ||
} | ||
|
@@ -211,44 +206,43 @@ impl Writer for HttpStream { | |
fn write(&mut self, msg: &[u8]) -> IoResult<()> { | ||
match *self { | ||
Http(ref mut inner) => inner.write(msg), | ||
Https(ref mut inner, _) => inner.lock().write(msg) | ||
Https(ref mut inner) => inner.write(msg) | ||
} | ||
} | ||
#[inline] | ||
fn flush(&mut self) -> IoResult<()> { | ||
match *self { | ||
Http(ref mut inner) => inner.flush(), | ||
Https(ref mut inner, _) => inner.lock().flush(), | ||
Https(ref mut inner) => inner.flush(), | ||
} | ||
} | ||
} | ||
|
||
|
||
impl NetworkStream for HttpStream { | ||
fn peer_name(&mut self) -> IoResult<SocketAddr> { | ||
match *self { | ||
Http(ref mut inner) => inner.peer_name(), | ||
Https(_, addr) => Ok(addr) | ||
Https(ref mut inner) => inner.get_mut().peer_name() | ||
} | ||
} | ||
} | ||
|
||
impl NetworkConnector for HttpStream { | ||
fn connect<To: ToSocketAddr>(addr: To, scheme: &str) -> IoResult<HttpStream> { | ||
/// A connector that will produce HttpStreams. | ||
pub struct HttpConnector; | ||
|
||
impl NetworkConnector<HttpStream> for HttpConnector { | ||
fn connect<To: ToSocketAddr>(&mut self, addr: To, scheme: &str) -> IoResult<HttpStream> { | ||
match scheme { | ||
"http" => { | ||
debug!("http scheme"); | ||
Ok(Http(try!(TcpStream::connect(addr)))) | ||
}, | ||
"https" => { | ||
debug!("https scheme"); | ||
let mut stream = try!(TcpStream::connect(addr)); | ||
// we can't access the tcp stream once it's wrapped in an | ||
// SslStream, so grab the ip address now, just in case. | ||
let peer_addr = try!(stream.peer_name()); | ||
let stream = try!(TcpStream::connect(addr)); | ||
let context = try!(SslContext::new(Sslv23).map_err(lift_ssl_error)); | ||
let stream = try!(SslStream::new(&context, stream).map_err(lift_ssl_error)); | ||
Ok(Https(Arc::new(Mutex::new(stream)), peer_addr)) | ||
Ok(Https(stream)) | ||
}, | ||
_ => { | ||
Err(IoError { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra newline here