Skip to content

Commit ae88092

Browse files
committed
Merge pull request #168 from hyperium/ssl-verify
r=reem
2 parents e6feb34 + 36429ab commit ae88092

File tree

5 files changed

+60
-36
lines changed

5 files changed

+60
-36
lines changed

benches/client_mock_tcp.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@ impl net::NetworkStream for MockStream {
9292
}
9393
}
9494

95-
impl net::NetworkConnector for MockStream {
96-
fn connect<To: ToSocketAddr>(_addr: To, _scheme: &str) -> IoResult<MockStream> {
95+
struct MockConnector;
96+
97+
impl net::NetworkConnector<MockStream> for MockConnector {
98+
fn connect<To: ToSocketAddr>(&mut self, _addr: To, _scheme: &str) -> IoResult<MockStream> {
9799
Ok(MockStream::new())
98100
}
99101

@@ -103,8 +105,9 @@ impl net::NetworkConnector for MockStream {
103105
fn bench_mock_hyper(b: &mut test::Bencher) {
104106
let url = "http://127.0.0.1:1337/";
105107
b.iter(|| {
106-
let mut req = hyper::client::Request::with_stream::<MockStream>(
107-
hyper::Get, hyper::Url::parse(url).unwrap()).unwrap();
108+
let mut req = hyper::client::Request::with_connector(
109+
hyper::Get, hyper::Url::parse(url).unwrap(), &mut MockConnector
110+
).unwrap();
108111
req.headers_mut().set(Foo);
109112

110113
req

src/client/request.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use method;
77
use method::Method::{Get, Post, Delete, Put, Patch, Head, Options};
88
use header::Headers;
99
use header::common::{mod, Host};
10-
use net::{NetworkStream, NetworkConnector, HttpStream, Fresh, Streaming};
10+
use net::{NetworkStream, NetworkConnector, HttpConnector, Fresh, Streaming};
1111
use HttpError::HttpUriError;
1212
use http::{HttpWriter, LINE_ENDING};
1313
use http::HttpWriter::{ThroughWriter, ChunkedWriter, SizedWriter, EmptyWriter};
@@ -42,11 +42,12 @@ impl<W> Request<W> {
4242
impl Request<Fresh> {
4343
/// Create a new client request.
4444
pub fn new(method: method::Method, url: Url) -> HttpResult<Request<Fresh>> {
45-
Request::with_stream::<HttpStream>(method, url)
45+
let mut conn = HttpConnector;
46+
Request::with_connector(method, url, &mut conn)
4647
}
4748

4849
/// Create a new client request with a specific underlying NetworkStream.
49-
pub fn with_stream<S: NetworkConnector>(method: method::Method, url: Url) -> HttpResult<Request<Fresh>> {
50+
pub fn with_connector<C: NetworkConnector<S>, S: NetworkStream>(method: method::Method, url: Url, connector: &mut C) -> HttpResult<Request<Fresh>> {
5051
debug!("{} {}", method, url);
5152
let host = match url.serialize_host() {
5253
Some(host) => host,
@@ -59,7 +60,7 @@ impl Request<Fresh> {
5960
};
6061
debug!("port={}", port);
6162

62-
let stream: S = try!(NetworkConnector::connect((host[], port), url.scheme.as_slice()));
63+
let stream: S = try!(connector.connect((host[], port), &*url.scheme));
6364
let stream = ThroughWriter(BufferedWriter::new(box stream as Box<NetworkStream + Send>));
6465

6566
let mut headers = Headers::new();
@@ -210,13 +211,13 @@ mod tests {
210211
use std::str::from_utf8;
211212
use url::Url;
212213
use method::Method::{Get, Head};
213-
use mock::MockStream;
214+
use mock::{MockStream, MockConnector};
214215
use super::Request;
215216

216217
#[test]
217218
fn test_get_empty_body() {
218-
let req = Request::with_stream::<MockStream>(
219-
Get, Url::parse("http://example.dom").unwrap()
219+
let req = Request::with_connector(
220+
Get, Url::parse("http://example.dom").unwrap(), &mut MockConnector
220221
).unwrap();
221222
let req = req.start().unwrap();
222223
let stream = *req.body.end().unwrap().into_inner().downcast::<MockStream>().unwrap();
@@ -228,8 +229,8 @@ mod tests {
228229

229230
#[test]
230231
fn test_head_empty_body() {
231-
let req = Request::with_stream::<MockStream>(
232-
Head, Url::parse("http://example.dom").unwrap()
232+
let req = Request::with_connector(
233+
Head, Url::parse("http://example.dom").unwrap(), &mut MockConnector
233234
).unwrap();
234235
let req = req.start().unwrap();
235236
let stream = *req.body.end().unwrap().into_inner().downcast::<MockStream>().unwrap();

src/http.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,31 @@ impl<W: Writer> HttpWriter<W> {
190190
}
191191
}
192192

193+
/// Access the inner Writer.
194+
#[inline]
195+
pub fn get_ref<'a>(&'a self) -> &'a W {
196+
match *self {
197+
ThroughWriter(ref w) => w,
198+
ChunkedWriter(ref w) => w,
199+
SizedWriter(ref w, _) => w,
200+
EmptyWriter(ref w) => w,
201+
}
202+
}
203+
204+
/// Access the inner Writer mutably.
205+
///
206+
/// Warning: You should not write to this directly, as you can corrupt
207+
/// the state.
208+
#[inline]
209+
pub fn get_mut<'a>(&'a mut self) -> &'a mut W {
210+
match *self {
211+
ThroughWriter(ref mut w) => w,
212+
ChunkedWriter(ref mut w) => w,
213+
SizedWriter(ref mut w, _) => w,
214+
EmptyWriter(ref mut w) => w,
215+
}
216+
}
217+
193218
/// Ends the HttpWriter, and returns the underlying Writer.
194219
///
195220
/// A final `write()` is called with an empty message, and then flushed.

src/mock.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ impl Writer for MockStream {
6161
}
6262

6363
impl NetworkStream for MockStream {
64-
6564
fn peer_name(&mut self) -> IoResult<SocketAddr> {
6665
Ok(from_str("127.0.0.1:1337").unwrap())
6766
}
6867
}
6968

70-
impl NetworkConnector for MockStream {
71-
fn connect<To: ToSocketAddr>(_addr: To, _scheme: &str) -> IoResult<MockStream> {
69+
pub struct MockConnector;
70+
71+
impl NetworkConnector<MockStream> for MockConnector {
72+
fn connect<To: ToSocketAddr>(&mut self, _addr: To, _scheme: &str) -> IoResult<MockStream> {
7273
Ok(MockStream::new())
7374
}
7475
}

src/net.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use std::io::net::ip::{SocketAddr, ToSocketAddr};
99
use std::io::net::tcp::{TcpStream, TcpListener, TcpAcceptor};
1010
use std::mem::{mod, transmute, transmute_copy};
1111
use std::raw::{mod, TraitObject};
12-
use std::sync::{Arc, Mutex};
1312

1413
use uany::UncheckedBoxAnyDowncast;
1514
use openssl::ssl::{SslStream, SslContext};
@@ -53,9 +52,9 @@ pub trait NetworkStream: Stream + Any + Clone + Send {
5352
}
5453

5554
/// A connector creates a NetworkStream.
56-
pub trait NetworkConnector: NetworkStream {
55+
pub trait NetworkConnector<S: NetworkStream> {
5756
/// Connect to a remote address.
58-
fn connect<To: ToSocketAddr>(addr: To, scheme: &str) -> IoResult<Self>;
57+
fn connect<To: ToSocketAddr>(&mut self, addr: To, scheme: &str) -> IoResult<S>;
5958
}
6059

6160
impl fmt::Show for Box<NetworkStream + Send> {
@@ -189,19 +188,15 @@ pub enum HttpStream {
189188
/// A stream over the HTTP protocol.
190189
Http(TcpStream),
191190
/// A stream over the HTTP protocol, protected by SSL.
192-
// You may be asking wtf an Arc and Mutex? That's because SslStream
193-
// doesn't implement Clone, and we need Clone to use the stream for
194-
// both the Request and Response.
195-
// FIXME: https://github.com/sfackler/rust-openssl/issues/6
196-
Https(Arc<Mutex<SslStream<TcpStream>>>, SocketAddr),
191+
Https(SslStream<TcpStream>),
197192
}
198193

199194
impl Reader for HttpStream {
200195
#[inline]
201196
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
202197
match *self {
203198
Http(ref mut inner) => inner.read(buf),
204-
Https(ref mut inner, _) => inner.lock().read(buf)
199+
Https(ref mut inner) => inner.read(buf)
205200
}
206201
}
207202
}
@@ -211,44 +206,43 @@ impl Writer for HttpStream {
211206
fn write(&mut self, msg: &[u8]) -> IoResult<()> {
212207
match *self {
213208
Http(ref mut inner) => inner.write(msg),
214-
Https(ref mut inner, _) => inner.lock().write(msg)
209+
Https(ref mut inner) => inner.write(msg)
215210
}
216211
}
217212
#[inline]
218213
fn flush(&mut self) -> IoResult<()> {
219214
match *self {
220215
Http(ref mut inner) => inner.flush(),
221-
Https(ref mut inner, _) => inner.lock().flush(),
216+
Https(ref mut inner) => inner.flush(),
222217
}
223218
}
224219
}
225220

226-
227221
impl NetworkStream for HttpStream {
228222
fn peer_name(&mut self) -> IoResult<SocketAddr> {
229223
match *self {
230224
Http(ref mut inner) => inner.peer_name(),
231-
Https(_, addr) => Ok(addr)
225+
Https(ref mut inner) => inner.get_mut().peer_name()
232226
}
233227
}
234228
}
235229

236-
impl NetworkConnector for HttpStream {
237-
fn connect<To: ToSocketAddr>(addr: To, scheme: &str) -> IoResult<HttpStream> {
230+
/// A connector that will produce HttpStreams.
231+
pub struct HttpConnector;
232+
233+
impl NetworkConnector<HttpStream> for HttpConnector {
234+
fn connect<To: ToSocketAddr>(&mut self, addr: To, scheme: &str) -> IoResult<HttpStream> {
238235
match scheme {
239236
"http" => {
240237
debug!("http scheme");
241238
Ok(Http(try!(TcpStream::connect(addr))))
242239
},
243240
"https" => {
244241
debug!("https scheme");
245-
let mut stream = try!(TcpStream::connect(addr));
246-
// we can't access the tcp stream once it's wrapped in an
247-
// SslStream, so grab the ip address now, just in case.
248-
let peer_addr = try!(stream.peer_name());
242+
let stream = try!(TcpStream::connect(addr));
249243
let context = try!(SslContext::new(Sslv23).map_err(lift_ssl_error));
250244
let stream = try!(SslStream::new(&context, stream).map_err(lift_ssl_error));
251-
Ok(Https(Arc::new(Mutex::new(stream)), peer_addr))
245+
Ok(Https(stream))
252246
},
253247
_ => {
254248
Err(IoError {

0 commit comments

Comments
 (0)