Skip to content

Commit

Permalink
Merge pull request hyperium#1 from glennw/rustc_workaround
Browse files Browse the repository at this point in the history
Update servo branch for rust upgrade + rustc workaround for SendStr issue.
  • Loading branch information
jdm committed Jan 8, 2015
2 parents dd9d183 + 73ce21c commit bbc38be
Show file tree
Hide file tree
Showing 44 changed files with 2,542 additions and 1,248 deletions.
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ authors = ["Sean McArthur <sean.monstar@gmail.com>"]
git = "https://github.com/servo/rust-url"

[dependencies.openssl]
git = "https://github.com/servo/rust-openssl"
git = "https://github.com/sfackler/rust-openssl"

[dependencies.mime]
git = "https://github.com/hyperium/mime.rs"

[dependencies.unsafe-any]
git = "https://github.com/reem/rust-unsafe-any"

[dependencies.move-acceptor]
git = "https://github.com/reem/rust-move-acceptor"

[dependencies.typeable]
git = "https://github.com/reem/rust-typeable"

[dev-dependencies.curl]
git = "https://github.com/carllerche/curl-rust"

[dev-dependencies.http]
git = "https://github.com/chris-morgan/rust-http"

[dependencies.cookie]
git = "https://github.com/servo/cookie-rs"
git = "https://github.com/alexcrichton/cookie-rs"

[dependencies.time]
git = "https://github.com/rust-lang/time"

[dependencies.mucell]
git = "https://github.com/chris-morgan/mucell"
49 changes: 22 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ 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/S client an HTTP server which can be used to drive
Hyper offers both an HTTP/S client and HTTP 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).
Expand All @@ -25,14 +25,11 @@ in non-backwards-compatible ways without warning.__
Hello World Server:

```rust
fn hello(mut incoming: Incoming) {
for conn in incoming {
let (_, mut res) = conn.open().unwrap();
*res.status_mut() = status::Ok;
let mut res = res.start().unwrap();
res.write(b"Hello World!");
res.end().unwrap();
}
fn hello(_: Request, res: Response<Fresh>) {
*res.status_mut() = status::Ok;
let mut res = res.start().unwrap();
res.write(b"Hello World!");
res.end().unwrap();
}

fn main() {
Expand All @@ -45,18 +42,18 @@ Client:

```rust
fn main() {
// Creating an outgoing request.
let mut req = Request::get(Url::parse("http://www.gooogle.com/")).unwrap();
// Create a client.
let mut client = Client::new();

// Setting a header.
req.headers_mut().set(Foo);
// Creating an outgoing request.
let mut res = client.get("http://www.gooogle.com/")
// set a header
.header(Connection(vec![Close]))
// let 'er go!
.send();

// Start the Request, writing headers and starting streaming.
let res = req.start().unwrap()
// Send the Request.
.send().unwrap()
// Read the Response.
.read_to_string().unwrap()
// Read the Response.
let body = res.read_to_string().unwrap();

println!("Response: {}", res);
}
Expand All @@ -67,22 +64,20 @@ fn main() {
[Client Bench:](./benches/client.rs)

```
running 3 tests
test bench_curl ... bench: 298416 ns/iter (+/- 132455)
test bench_http ... bench: 292725 ns/iter (+/- 167575)
test bench_hyper ... bench: 222819 ns/iter (+/- 86615)
test bench_curl ... bench: 400253 ns/iter (+/- 143539)
test bench_hyper ... bench: 181703 ns/iter (+/- 46529)
test result: ok. 0 passed; 0 failed; 0 ignored; 3 measured
test result: ok. 0 passed; 0 failed; 0 ignored; 2 measured
```

[Mock Client Bench:](./benches/client_mock_tcp.rs)

```
running 3 tests
test bench_mock_curl ... bench: 25254 ns/iter (+/- 2113)
test bench_mock_http ... bench: 43585 ns/iter (+/- 1206)
test bench_mock_hyper ... bench: 27153 ns/iter (+/- 2227)
test bench_mock_curl ... bench: 53987 ns/iter (+/- 1735)
test bench_mock_http ... bench: 43569 ns/iter (+/- 1409)
test bench_mock_hyper ... bench: 20996 ns/iter (+/- 1742)
test result: ok. 0 passed; 0 failed; 0 ignored; 3 measured
```
Expand Down
40 changes: 22 additions & 18 deletions benches/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,30 @@ extern crate test;

use std::fmt::{mod, Show};
use std::io::net::ip::Ipv4Addr;
use hyper::server::{Incoming, Server};
use hyper::server::{Request, Response, Server};
use hyper::method::Method::Get;
use hyper::header::Headers;
use hyper::Client;
use hyper::client::RequestBuilder;

fn listen() -> hyper::server::Listening {
let server = Server::http(Ipv4Addr(127, 0, 0, 1), 0);
server.listen(handle).unwrap()
}

macro_rules! try_continue(
macro_rules! try_return(
($e:expr) => {{
match $e {
Ok(v) => v,
Err(..) => continue
Err(..) => return
}
}})

fn handle(mut incoming: Incoming) {
for conn in incoming {
let (_, res) = try_continue!(conn.open());
let mut res = try_continue!(res.start());
try_continue!(res.write(b"Benchmarking hyper vs others!"))
try_continue!(res.end());
}
fn handle(_r: Request, res: Response) {
static BODY: &'static [u8] = b"Benchmarking hyper vs others!";
let mut res = try_return!(res.start());
try_return!(res.write(BODY))
try_return!(res.end());
}


Expand All @@ -44,9 +46,10 @@ fn bench_curl(b: &mut test::Bencher) {
.exec()
.unwrap()
});
listening.close().unwrap()
listening.close().unwrap();
}

#[deriving(Clone)]
struct Foo;

impl hyper::header::Header for Foo {
Expand All @@ -69,17 +72,17 @@ fn bench_hyper(b: &mut test::Bencher) {
let mut listening = listen();
let s = format!("http://{}/", listening.socket);
let url = s.as_slice();
let mut client = Client::new();
let mut headers = Headers::new();
headers.set(Foo);
b.iter(|| {
let mut req = hyper::client::Request::get(hyper::Url::parse(url).unwrap()).unwrap();
req.headers_mut().set(Foo);

req.start().unwrap()
.send().unwrap()
.read_to_string().unwrap()
client.get(url).header(Foo).send().unwrap().read_to_string().unwrap();
});
listening.close().unwrap()
}

/*
doesn't handle keep-alive properly...
#[bench]
fn bench_http(b: &mut test::Bencher) {
let mut listening = listen();
Expand All @@ -94,9 +97,10 @@ fn bench_http(b: &mut test::Bencher) {
// cant unwrap because Err contains RequestWriter, which does not implement Show
let mut res = match req.read_response() {
Ok(res) => res,
Err(..) => panic!("http response failed")
Err((_, ioe)) => panic!("http response failed = {}", ioe)
};
res.read_to_string().unwrap();
});
listening.close().unwrap()
}
*/
16 changes: 10 additions & 6 deletions benches/client_mock_tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extern crate hyper;
extern crate test;

use std::fmt::{mod, Show};
use std::from_str::from_str;
use std::str::from_str;
use std::io::{IoResult, MemReader};
use std::io::net::ip::{SocketAddr, ToSocketAddr};
use std::os;
Expand Down Expand Up @@ -55,7 +55,7 @@ impl Writer for MockStream {

#[bench]
fn bench_mock_curl(b: &mut test::Bencher) {
let mut cwd = os::getcwd();
let mut cwd = os::getcwd().unwrap();
cwd.push("README.md");
let s = format!("file://{}", cwd.container_as_str().unwrap());
let url = s.as_slice();
Expand All @@ -68,6 +68,7 @@ fn bench_mock_curl(b: &mut test::Bencher) {
});
}

#[deriving(Clone)]
struct Foo;

impl hyper::header::Header for Foo {
Expand All @@ -91,8 +92,10 @@ impl net::NetworkStream for MockStream {
}
}

impl net::NetworkConnector for MockStream {
fn connect<To: ToSocketAddr>(_addr: To, _scheme: &str) -> IoResult<MockStream> {
struct MockConnector;

impl net::NetworkConnector<MockStream> for MockConnector {
fn connect(&mut self, _: &str, _: u16, _: &str) -> IoResult<MockStream> {
Ok(MockStream::new())
}

Expand All @@ -102,8 +105,9 @@ impl net::NetworkConnector for MockStream {
fn bench_mock_hyper(b: &mut test::Bencher) {
let url = "http://127.0.0.1:1337/";
b.iter(|| {
let mut req = hyper::client::Request::with_stream::<MockStream>(
hyper::Get, hyper::Url::parse(url).unwrap()).unwrap();
let mut req = hyper::client::Request::with_connector(
hyper::Get, hyper::Url::parse(url).unwrap(), &mut MockConnector
).unwrap();
req.headers_mut().set(Foo);

req
Expand Down
15 changes: 7 additions & 8 deletions benches/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,20 @@ use test::Bencher;
use std::io::net::ip::{SocketAddr, Ipv4Addr};

use http::server::Server;
use hyper::method::Method::Get;
use hyper::server::{Request, Response};

static PHRASE: &'static [u8] = b"Benchmarking hyper vs others!";

fn request(url: hyper::Url) {
let req = hyper::client::Request::get(url).unwrap();
let req = hyper::client::Request::new(Get, url).unwrap();
req.start().unwrap().send().unwrap().read_to_string().unwrap();
}

fn hyper_handle(mut incoming: hyper::server::Incoming) {
for conn in incoming {
let (_, res) = conn.open().unwrap();
let mut res = res.start().unwrap();
res.write(PHRASE).unwrap();
res.end().unwrap();
}
fn hyper_handle(_: Request, res: Response) {
let mut res = res.start().unwrap();
res.write(PHRASE).unwrap();
res.end().unwrap();
}

#[bench]
Expand Down
22 changes: 6 additions & 16 deletions examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use std::os;
use std::io::stdout;
use std::io::util::copy;

use hyper::Url;
use hyper::client::Request;
use hyper::Client;

fn main() {
let args = os::args();
Expand All @@ -17,26 +16,17 @@ fn main() {
}
};

let url = match Url::parse(args[1].as_slice()) {
Ok(url) => {
println!("GET {}...", url)
url
},
Err(e) => panic!("Invalid URL: {}", e)
};
let url = &*args[1];

let mut client = Client::new();

let req = match Request::get(url) {
Ok(req) => req,
let mut res = match client.get(url).send() {
Ok(res) => res,
Err(err) => panic!("Failed to connect: {}", err)
};

let mut res = req
.start().unwrap() // failure: Error writing Headers
.send().unwrap(); // failure: Error reading Response head.

println!("Response: {}", res.status);
println!("{}", res.headers);
println!("Headers:\n{}", res.headers);
match copy(&mut res, &mut stdout()) {
Ok(..) => (),
Err(e) => panic!("Stream failure: {}", e)
Expand Down

0 comments on commit bbc38be

Please sign in to comment.