Skip to content

Commit d35992d

Browse files
committed
feat(lib): switch to non-blocking (asynchronous) IO
BREAKING CHANGE: This breaks a lot of the Client and Server APIs. Check the documentation for how Handlers can be used for asynchronous events.
1 parent 1ec56fe commit d35992d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+5492
-4916
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ matrix:
33
fast_finish: true
44
include:
55
- os: osx
6+
rust: stable
7+
env: FEATURES="--no-default-features --features security-framework"
68
- rust: nightly
79
env: FEATURES="--features nightly"
810
- rust: beta

Cargo.toml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ httparse = "1.0"
1616
language-tags = "0.2"
1717
log = "0.3"
1818
mime = "0.2"
19-
num_cpus = "0.2"
19+
rotor = "0.6"
2020
rustc-serialize = "0.3"
21+
spmc = "0.2"
2122
time = "0.1"
2223
traitobject = "0.0.1"
2324
typeable = "0.1"
2425
unicase = "1.0"
2526
url = "1.0"
27+
vecio = "0.1"
2628

2729
[dependencies.cookie]
2830
version = "0.2"
@@ -40,16 +42,13 @@ optional = true
4042
version = "0.1.4"
4143
optional = true
4244

43-
[dependencies.solicit]
44-
version = "0.4"
45-
default-features = false
46-
4745
[dependencies.serde]
4846
version = "0.7"
4947
optional = true
5048

5149
[dev-dependencies]
5250
env_logger = "0.3"
51+
num_cpus = "0.2"
5352

5453
[features]
5554
default = ["ssl"]

README.md

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,66 +10,16 @@ A Modern HTTP library for Rust.
1010

1111
### Documentation
1212

13-
- [Stable](http://hyperium.github.io/hyper)
13+
- [Released](http://hyperium.github.io/hyper)
1414
- [Master](http://hyperium.github.io/hyper/master)
1515

1616
## Overview
1717

18-
Hyper is a fast, modern HTTP implementation written in and for Rust. It
18+
hyper is a fast, modern HTTP implementation written in and for Rust. It
1919
is a low-level typesafe abstraction over raw HTTP, providing an elegant
2020
layer over "stringly-typed" HTTP.
2121

2222
Hyper offers both an HTTP client and server which can be used to drive
2323
complex web applications written entirely in Rust.
2424

2525
The documentation is located at [http://hyperium.github.io/hyper](http://hyperium.github.io/hyper).
26-
27-
## Example
28-
29-
### Hello World Server:
30-
31-
```rust
32-
extern crate hyper;
33-
34-
use hyper::Server;
35-
use hyper::server::Request;
36-
use hyper::server::Response;
37-
38-
fn hello(_: Request, res: Response) {
39-
res.send(b"Hello World!").unwrap();
40-
}
41-
42-
fn main() {
43-
Server::http("127.0.0.1:3000").unwrap()
44-
.handle(hello).unwrap();
45-
}
46-
```
47-
48-
### Client:
49-
50-
```rust
51-
extern crate hyper;
52-
53-
use std::io::Read;
54-
55-
use hyper::Client;
56-
use hyper::header::Connection;
57-
58-
fn main() {
59-
// Create a client.
60-
let client = Client::new();
61-
62-
// Creating an outgoing request.
63-
let mut res = client.get("http://rust-lang.org/")
64-
// set a header
65-
.header(Connection::close())
66-
// let 'er go!
67-
.send().unwrap();
68-
69-
// Read the Response.
70-
let mut body = String::new();
71-
res.read_to_string(&mut body).unwrap();
72-
73-
println!("Response: {}", body);
74-
}
75-
```

benches/client.rs

Lines changed: 0 additions & 111 deletions
This file was deleted.

examples/client.rs

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,61 @@ extern crate env_logger;
55

66
use std::env;
77
use std::io;
8+
use std::sync::mpsc;
9+
use std::time::Duration;
810

9-
use hyper::Client;
11+
use hyper::client::{Client, Request, Response, DefaultTransport as HttpStream};
1012
use hyper::header::Connection;
13+
use hyper::{Decoder, Encoder, Next};
14+
15+
#[derive(Debug)]
16+
struct Dump(mpsc::Sender<()>);
17+
18+
impl Drop for Dump {
19+
fn drop(&mut self) {
20+
let _ = self.0.send(());
21+
}
22+
}
23+
24+
fn read() -> Next {
25+
Next::read().timeout(Duration::from_secs(10))
26+
}
27+
28+
impl hyper::client::Handler<HttpStream> for Dump {
29+
fn on_request(&mut self, req: &mut Request) -> Next {
30+
req.headers_mut().set(Connection::close());
31+
read()
32+
}
33+
34+
fn on_request_writable(&mut self, _encoder: &mut Encoder<HttpStream>) -> Next {
35+
read()
36+
}
37+
38+
fn on_response(&mut self, res: Response) -> Next {
39+
println!("Response: {}", res.status());
40+
println!("Headers:\n{}", res.headers());
41+
read()
42+
}
43+
44+
fn on_response_readable(&mut self, decoder: &mut Decoder<HttpStream>) -> Next {
45+
match io::copy(decoder, &mut io::stdout()) {
46+
Ok(0) => Next::end(),
47+
Ok(_) => read(),
48+
Err(e) => match e.kind() {
49+
io::ErrorKind::WouldBlock => Next::read(),
50+
_ => {
51+
println!("ERROR: {}", e);
52+
Next::end()
53+
}
54+
}
55+
}
56+
}
57+
58+
fn on_error(&mut self, err: hyper::Error) -> Next {
59+
println!("ERROR: {}", err);
60+
Next::remove()
61+
}
62+
}
1163

1264
fn main() {
1365
env_logger::init().unwrap();
@@ -20,26 +72,11 @@ fn main() {
2072
}
2173
};
2274

23-
let client = match env::var("HTTP_PROXY") {
24-
Ok(mut proxy) => {
25-
// parse the proxy, message if it doesn't make sense
26-
let mut port = 80;
27-
if let Some(colon) = proxy.rfind(':') {
28-
port = proxy[colon + 1..].parse().unwrap_or_else(|e| {
29-
panic!("HTTP_PROXY is malformed: {:?}, port parse error: {}", proxy, e);
30-
});
31-
proxy.truncate(colon);
32-
}
33-
Client::with_http_proxy(proxy, port)
34-
},
35-
_ => Client::new()
36-
};
37-
38-
let mut res = client.get(&*url)
39-
.header(Connection::close())
40-
.send().unwrap();
75+
let (tx, rx) = mpsc::channel();
76+
let client = Client::new().expect("Failed to create a Client");
77+
client.request(url.parse().unwrap(), Dump(tx)).unwrap();
4178

42-
println!("Response: {}", res.status);
43-
println!("Headers:\n{}", res.headers);
44-
io::copy(&mut res, &mut io::stdout()).unwrap();
79+
// wait till done
80+
let _ = rx.recv();
81+
client.close();
4582
}

examples/client_http2.rs

Lines changed: 0 additions & 34 deletions
This file was deleted.

examples/headers.rs

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)