Skip to content

Commit c3935d6

Browse files
committed
Merge pull request #577 from hyperium/ssl
feat(ssl): redesign SSL usage
2 parents e689f20 + 53bba6e commit c3935d6

File tree

17 files changed

+339
-375
lines changed

17 files changed

+339
-375
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ matrix:
66
env: FEATURES="--features nightly"
77
- rust: beta
88
- rust: stable
9+
- rust: stable
10+
env: FEATURES="--no-default-features"
911

1012
sudo: false
1113

Cargo.toml

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,33 @@ authors = ["Sean McArthur <[email protected]>",
1212
keywords = ["http", "hyper", "hyperium"]
1313

1414
[dependencies]
15-
cookie = "0.1"
1615
httparse = "0.1"
1716
log = "0.3"
18-
mime = "0.0.11"
17+
mime = "0.0.12"
1918
num_cpus = "0.2"
20-
openssl = "0.6"
2119
rustc-serialize = "0.3"
2220
time = "0.1"
23-
unicase = "0.1"
24-
url = "0.2"
2521
traitobject = "0.0.1"
2622
typeable = "0.1"
27-
solicit = "0.2"
23+
unicase = "0.1"
24+
url = "0.2"
25+
26+
[dependencies.cookie]
27+
version = "0.1"
28+
default-features = false
29+
30+
[dependencies.openssl]
31+
version = "0.6"
32+
optional = true
33+
34+
[dependencies.solicit]
35+
version = "0.3"
36+
default-features = false
2837

2938
[dev-dependencies]
3039
env_logger = "*"
3140

3241
[features]
42+
default = ["ssl"]
43+
ssl = ["openssl", "cookie/secure", "solicit/openssl"]
3344
nightly = []

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn hello(_: Request, res: Response<Fresh>) {
4141
}
4242

4343
fn main() {
44-
Server::http(hello).listen("127.0.0.1:3000").unwrap();
44+
Server::http("127.0.0.1:3000").unwrap().handle(hello);
4545
}
4646
```
4747

examples/hello.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn hello(_: Request, res: Response) {
1212

1313
fn main() {
1414
env_logger::init().unwrap();
15-
let _listening = hyper::Server::http(hello)
16-
.listen("127.0.0.1:3000").unwrap();
15+
let _listening = hyper::Server::http("127.0.0.1:3000").unwrap()
16+
.handle(hello);
1717
println!("Listening on http://127.0.0.1:3000");
1818
}

examples/server.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn echo(mut req: Request, mut res: Response) {
4141

4242
fn main() {
4343
env_logger::init().unwrap();
44-
let server = Server::http(echo);
45-
let _guard = server.listen("127.0.0.1:1337").unwrap();
44+
let server = Server::http("127.0.0.1:1337").unwrap();
45+
let _guard = server.handle(echo);
4646
println!("Listening on http://127.0.0.1:1337");
4747
}

src/client/mod.rs

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ use url::ParseError as UrlError;
6565
use header::{Headers, Header, HeaderFormat};
6666
use header::{ContentLength, Location};
6767
use method::Method;
68-
use net::{NetworkConnector, NetworkStream, ContextVerifier};
68+
use net::{NetworkConnector, NetworkStream};
6969
use {Url};
7070
use Error;
7171

@@ -116,11 +116,6 @@ impl Client {
116116
}
117117
}
118118

119-
/// Set the SSL verifier callback for use with OpenSSL.
120-
pub fn set_ssl_verifier(&mut self, verifier: ContextVerifier) {
121-
self.protocol.set_ssl_verifier(verifier);
122-
}
123-
124119
/// Set the RedirectPolicy.
125120
pub fn set_redirect_policy(&mut self, policy: RedirectPolicy) {
126121
self.redirect_policy = policy;
@@ -417,8 +412,6 @@ mod tests {
417412
use header::Server;
418413
use super::{Client, RedirectPolicy};
419414
use url::Url;
420-
use mock::ChannelMockConnector;
421-
use std::sync::mpsc::{self, TryRecvError};
422415

423416
mock_connector!(MockRedirectPolicy {
424417
"http://127.0.0.1" => "HTTP/1.1 301 Redirect\r\n\
@@ -464,31 +457,4 @@ mod tests {
464457
let res = client.get("http://127.0.0.1").send().unwrap();
465458
assert_eq!(res.headers.get(), Some(&Server("mock2".to_owned())));
466459
}
467-
468-
/// Tests that the `Client::set_ssl_verifier` method does not drop the
469-
/// old connector, but rather delegates the change to the connector itself.
470-
#[test]
471-
fn test_client_set_ssl_verifer() {
472-
let (tx, rx) = mpsc::channel();
473-
let mut client = Client::with_connector(ChannelMockConnector::new(tx));
474-
475-
client.set_ssl_verifier(Box::new(|_| {}));
476-
477-
// Make sure that the client called the `set_ssl_verifier` method
478-
match rx.try_recv() {
479-
Ok(meth) => {
480-
assert_eq!(meth, "set_ssl_verifier");
481-
},
482-
_ => panic!("Expected a call to `set_ssl_verifier`"),
483-
};
484-
// Now make sure that no other method was called, as well as that
485-
// the connector is still alive (i.e. wasn't dropped by the client).
486-
match rx.try_recv() {
487-
Err(TryRecvError::Empty) => {},
488-
Err(TryRecvError::Disconnected) => {
489-
panic!("Expected the connector to still be alive.");
490-
},
491-
Ok(_) => panic!("Did not expect any more method calls."),
492-
};
493-
}
494460
}

src/client/pool.rs

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::io::{self, Read, Write};
55
use std::net::{SocketAddr, Shutdown};
66
use std::sync::{Arc, Mutex};
77

8-
use net::{NetworkConnector, NetworkStream, HttpConnector, ContextVerifier};
8+
use net::{NetworkConnector, NetworkStream, DefaultConnector};
99

1010
/// The `NetworkConnector` that behaves as a connection pool used by hyper's `Client`.
1111
pub struct Pool<C: NetworkConnector> {
@@ -58,11 +58,11 @@ impl<'a> From<&'a str> for Scheme {
5858
}
5959
}
6060

61-
impl Pool<HttpConnector> {
62-
/// Creates a `Pool` with an `HttpConnector`.
61+
impl Pool<DefaultConnector> {
62+
/// Creates a `Pool` with a `DefaultConnector`.
6363
#[inline]
64-
pub fn new(config: Config) -> Pool<HttpConnector> {
65-
Pool::with_connector(config, HttpConnector(None))
64+
pub fn new(config: Config) -> Pool<DefaultConnector> {
65+
Pool::with_connector(config, DefaultConnector::default())
6666
}
6767
}
6868

@@ -119,10 +119,6 @@ impl<C: NetworkConnector<Stream=S>, S: NetworkStream + Send> NetworkConnector fo
119119
pool: self.inner.clone()
120120
})
121121
}
122-
#[inline]
123-
fn set_ssl_verifier(&mut self, verifier: ContextVerifier) {
124-
self.connector.set_ssl_verifier(verifier);
125-
}
126122
}
127123

128124
/// A Stream that will try to be returned to the Pool when dropped.
@@ -181,9 +177,8 @@ impl<S> Drop for PooledStream<S> {
181177
#[cfg(test)]
182178
mod tests {
183179
use std::net::Shutdown;
184-
use mock::{MockConnector, ChannelMockConnector};
180+
use mock::{MockConnector};
185181
use net::{NetworkConnector, NetworkStream};
186-
use std::sync::mpsc;
187182

188183
use super::{Pool, key};
189184

@@ -220,20 +215,4 @@ mod tests {
220215
let locked = pool.inner.lock().unwrap();
221216
assert_eq!(locked.conns.len(), 0);
222217
}
223-
224-
/// Tests that the `Pool::set_ssl_verifier` method sets the SSL verifier of
225-
/// the underlying `Connector` instance that it uses.
226-
#[test]
227-
fn test_set_ssl_verifier_delegates_to_connector() {
228-
let (tx, rx) = mpsc::channel();
229-
let mut pool = Pool::with_connector(
230-
Default::default(), ChannelMockConnector::new(tx));
231-
232-
pool.set_ssl_verifier(Box::new(|_| { }));
233-
234-
match rx.try_recv() {
235-
Ok(meth) => assert_eq!(meth, "set_ssl_verifier"),
236-
_ => panic!("Expected a call to `set_ssl_verifier`"),
237-
};
238-
}
239218
}

src/client/request.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use url::Url;
77
use method::{self, Method};
88
use header::Headers;
99
use header::Host;
10-
use net::{NetworkStream, NetworkConnector, HttpConnector, Fresh, Streaming};
10+
use net::{NetworkStream, NetworkConnector, DefaultConnector, Fresh, Streaming};
1111
use version;
1212
use client::{Response, get_host_and_port};
1313

@@ -66,7 +66,7 @@ impl Request<Fresh> {
6666

6767
/// Create a new client request.
6868
pub fn new(method: method::Method, url: Url) -> ::Result<Request<Fresh>> {
69-
let mut conn = HttpConnector(None);
69+
let mut conn = DefaultConnector::default();
7070
Request::with_connector(method, url, &mut conn)
7171
}
7272

src/error.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ use std::io::Error as IoError;
55
use std::str::Utf8Error;
66

77
use httparse;
8-
use openssl::ssl::error::SslError;
98
use url;
109
use solicit::http::HttpError as Http2Error;
1110

11+
#[cfg(feature = "openssl")]
12+
use openssl::ssl::error::SslError;
13+
1214
use self::Error::{
1315
Method,
1416
Uri,
@@ -43,8 +45,8 @@ pub enum Error {
4345
Status,
4446
/// An `io::Error` that occurred while trying to read or write to a network stream.
4547
Io(IoError),
46-
/// An error from the `openssl` library.
47-
Ssl(SslError),
48+
/// An error from a SSL library.
49+
Ssl(Box<StdError + Send + Sync>),
4850
/// An HTTP/2-specific error, coming from the `solicit` library.
4951
Http2(Http2Error),
5052
/// Parsing a field as string failed
@@ -89,7 +91,7 @@ impl StdError for Error {
8991
fn cause(&self) -> Option<&StdError> {
9092
match *self {
9193
Io(ref error) => Some(error),
92-
Ssl(ref error) => Some(error),
94+
Ssl(ref error) => Some(&**error),
9395
Uri(ref error) => Some(error),
9496
Http2(ref error) => Some(error),
9597
_ => None,
@@ -109,11 +111,12 @@ impl From<url::ParseError> for Error {
109111
}
110112
}
111113

114+
#[cfg(feature = "openssl")]
112115
impl From<SslError> for Error {
113116
fn from(err: SslError) -> Error {
114117
match err {
115118
SslError::StreamError(err) => Io(err),
116-
err => Ssl(err),
119+
err => Ssl(Box::new(err)),
117120
}
118121
}
119122
}
@@ -149,7 +152,6 @@ mod tests {
149152
use std::error::Error as StdError;
150153
use std::io;
151154
use httparse;
152-
use openssl::ssl::error::SslError;
153155
use solicit::http::HttpError as Http2Error;
154156
use url;
155157
use super::Error;
@@ -192,12 +194,8 @@ mod tests {
192194

193195
from_and_cause!(io::Error::new(io::ErrorKind::Other, "other") => Io(..));
194196
from_and_cause!(url::ParseError::EmptyHost => Uri(..));
195-
from_and_cause!(SslError::SslSessionClosed => Ssl(..));
196197
from_and_cause!(Http2Error::UnknownStreamId => Http2(..));
197198

198-
from!(SslError::StreamError(io::Error::new(io::ErrorKind::Other, "ssl negotiation")) => Io(..));
199-
200-
201199
from!(httparse::Error::HeaderName => Header);
202200
from!(httparse::Error::HeaderName => Header);
203201
from!(httparse::Error::HeaderValue => Header);
@@ -207,4 +205,13 @@ mod tests {
207205
from!(httparse::Error::TooManyHeaders => TooLarge);
208206
from!(httparse::Error::Version => Version);
209207
}
208+
209+
#[cfg(feature = "openssl")]
210+
#[test]
211+
fn test_from_ssl() {
212+
use openssl::ssl::error::SslError;
213+
214+
from!(SslError::StreamError(io::Error::new(io::ErrorKind::Other, "ssl negotiation")) => Io(..));
215+
from_and_cause!(SslError::SslSessionClosed => Ssl(..));
216+
}
210217
}

src/header/common/set_cookie.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,13 @@ fn test_fmt() {
168168
fn cookie_jar() {
169169
let jar = CookieJar::new(b"secret");
170170
let cookie = Cookie::new("foo".to_owned(), "bar".to_owned());
171-
jar.encrypted().add(cookie);
171+
jar.add(cookie);
172172

173173
let cookies = SetCookie::from_cookie_jar(&jar);
174174

175175
let mut new_jar = CookieJar::new(b"secret");
176176
cookies.apply_to_cookie_jar(&mut new_jar);
177177

178-
assert_eq!(jar.encrypted().find("foo"), new_jar.encrypted().find("foo"));
178+
assert_eq!(jar.find("foo"), new_jar.find("foo"));
179179
assert_eq!(jar.iter().collect::<Vec<Cookie>>(), new_jar.iter().collect::<Vec<Cookie>>());
180180
}

0 commit comments

Comments
 (0)