Skip to content

Commit

Permalink
feat(lib): switch to non-blocking (asynchronous) IO
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This breaks a lot of the Client and Server APIs.
  Check the documentation for how Handlers can be used for asynchronous
  events.
  • Loading branch information
seanmonstar committed May 16, 2016
1 parent 1ec56fe commit d35992d
Show file tree
Hide file tree
Showing 65 changed files with 5,492 additions and 4,916 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ matrix:
fast_finish: true
include:
- os: osx
rust: stable
env: FEATURES="--no-default-features --features security-framework"
- rust: nightly
env: FEATURES="--features nightly"
- rust: beta
Expand Down
9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ httparse = "1.0"
language-tags = "0.2"
log = "0.3"
mime = "0.2"
num_cpus = "0.2"
rotor = "0.6"
rustc-serialize = "0.3"
spmc = "0.2"
time = "0.1"
traitobject = "0.0.1"
typeable = "0.1"
unicase = "1.0"
url = "1.0"
vecio = "0.1"

[dependencies.cookie]
version = "0.2"
Expand All @@ -40,16 +42,13 @@ optional = true
version = "0.1.4"
optional = true

[dependencies.solicit]
version = "0.4"
default-features = false

[dependencies.serde]
version = "0.7"
optional = true

[dev-dependencies]
env_logger = "0.3"
num_cpus = "0.2"

[features]
default = ["ssl"]
Expand Down
54 changes: 2 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,66 +10,16 @@ A Modern HTTP library for Rust.

### Documentation

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

## Overview

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

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

The documentation is located at [http://hyperium.github.io/hyper](http://hyperium.github.io/hyper).

## Example

### Hello World Server:

```rust
extern crate hyper;

use hyper::Server;
use hyper::server::Request;
use hyper::server::Response;

fn hello(_: Request, res: Response) {
res.send(b"Hello World!").unwrap();
}

fn main() {
Server::http("127.0.0.1:3000").unwrap()
.handle(hello).unwrap();
}
```

### Client:

```rust
extern crate hyper;

use std::io::Read;

use hyper::Client;
use hyper::header::Connection;

fn main() {
// Create a client.
let client = Client::new();

// Creating an outgoing request.
let mut res = client.get("http://rust-lang.org/")
// set a header
.header(Connection::close())
// let 'er go!
.send().unwrap();

// Read the Response.
let mut body = String::new();
res.read_to_string(&mut body).unwrap();

println!("Response: {}", body);
}
```
111 changes: 0 additions & 111 deletions benches/client.rs

This file was deleted.

81 changes: 59 additions & 22 deletions examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,61 @@ extern crate env_logger;

use std::env;
use std::io;
use std::sync::mpsc;
use std::time::Duration;

use hyper::Client;
use hyper::client::{Client, Request, Response, DefaultTransport as HttpStream};
use hyper::header::Connection;
use hyper::{Decoder, Encoder, Next};

#[derive(Debug)]
struct Dump(mpsc::Sender<()>);

impl Drop for Dump {
fn drop(&mut self) {
let _ = self.0.send(());
}
}

fn read() -> Next {
Next::read().timeout(Duration::from_secs(10))
}

impl hyper::client::Handler<HttpStream> for Dump {
fn on_request(&mut self, req: &mut Request) -> Next {
req.headers_mut().set(Connection::close());
read()
}

fn on_request_writable(&mut self, _encoder: &mut Encoder<HttpStream>) -> Next {
read()
}

fn on_response(&mut self, res: Response) -> Next {
println!("Response: {}", res.status());
println!("Headers:\n{}", res.headers());
read()
}

fn on_response_readable(&mut self, decoder: &mut Decoder<HttpStream>) -> Next {
match io::copy(decoder, &mut io::stdout()) {
Ok(0) => Next::end(),
Ok(_) => read(),
Err(e) => match e.kind() {
io::ErrorKind::WouldBlock => Next::read(),
_ => {
println!("ERROR: {}", e);
Next::end()
}
}
}
}

fn on_error(&mut self, err: hyper::Error) -> Next {
println!("ERROR: {}", err);
Next::remove()
}
}

fn main() {
env_logger::init().unwrap();
Expand All @@ -20,26 +72,11 @@ fn main() {
}
};

let client = match env::var("HTTP_PROXY") {
Ok(mut proxy) => {
// parse the proxy, message if it doesn't make sense
let mut port = 80;
if let Some(colon) = proxy.rfind(':') {
port = proxy[colon + 1..].parse().unwrap_or_else(|e| {
panic!("HTTP_PROXY is malformed: {:?}, port parse error: {}", proxy, e);
});
proxy.truncate(colon);
}
Client::with_http_proxy(proxy, port)
},
_ => Client::new()
};

let mut res = client.get(&*url)
.header(Connection::close())
.send().unwrap();
let (tx, rx) = mpsc::channel();
let client = Client::new().expect("Failed to create a Client");
client.request(url.parse().unwrap(), Dump(tx)).unwrap();

println!("Response: {}", res.status);
println!("Headers:\n{}", res.headers);
io::copy(&mut res, &mut io::stdout()).unwrap();
// wait till done
let _ = rx.recv();
client.close();
}
34 changes: 0 additions & 34 deletions examples/client_http2.rs

This file was deleted.

16 changes: 0 additions & 16 deletions examples/headers.rs

This file was deleted.

Loading

0 comments on commit d35992d

Please sign in to comment.