Skip to content

Commit

Permalink
feat(hyper): switch to std::io, std::net, and std::path.
Browse files Browse the repository at this point in the history
All instances of `old_io` and `old_path` were switched to use the new
shiny `std::io`, `std::net`, and `std::path` modules. This means that
`Request` and `Response` implement `Read` and `Write` now.

Because of the changes to `TcpListener`, this also takes the opportunity
to correct the method usage of `Server`. As with other
languages/frameworks, the server is first created with a handler, and
then a host/port is passed to a `listen` method. This reverses what
`Server` used to do.

Closes #347

BREAKING CHANGE: Check the docs. Everything was touched.
  • Loading branch information
seanmonstar committed Mar 3, 2015
1 parent 7235d3f commit 0fd6fcd
Show file tree
Hide file tree
Showing 22 changed files with 641 additions and 639 deletions.
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ Hello World Server:
```rust
extern crate hyper;

use hyper::status::StatusCode;
use hyper::server::Server;
use hyper::server::request::Request;
use hyper::server::response::Response;
use std::io::Write;
use std::net::IpAddr;

use hyper::Server;
use hyper::server::Request;
use hyper::server::Response;
use hyper::net::Fresh;
use hyper::IpAddr::Ipv4Addr;

fn hello(_: Request, mut res: Response<Fresh>) {
let mut res = res.start().unwrap();
Expand All @@ -41,8 +42,7 @@ fn hello(_: Request, mut res: Response<Fresh>) {
}

fn main() {
let server = Server::http(Ipv4Addr(127, 0, 0, 1), 1337);
server.listen(hello).unwrap();
Server::http(hello).listen(IpAddr::new_v4(127, 0, 0, 1), 3000).unwrap();
}
```

Expand All @@ -51,7 +51,9 @@ Client:
```rust
extern crate hyper;

use hyper::client::Client;
use std::io::Read;

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

Expand All @@ -67,7 +69,8 @@ fn main() {
.send().unwrap();

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

println!("Response: {}", body);
}
Expand Down
101 changes: 70 additions & 31 deletions benches/client.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,53 @@
#![feature(core, old_io, test)]
#![feature(collections, io, net, test)]
extern crate hyper;

extern crate test;

use std::fmt;
use std::old_io::net::ip::Ipv4Addr;
use hyper::server::{Request, Response, Server};
use hyper::header::Headers;
use hyper::Client;

fn listen() -> hyper::server::Listening {
let server = Server::http(Ipv4Addr(127, 0, 0, 1), 0);
server.listen(handle).unwrap()
use std::io::{self, Read, Write, Cursor};
use std::net::SocketAddr;

use hyper::net;

static README: &'static [u8] = include_bytes!("../README.md");

struct MockStream {
read: Cursor<Vec<u8>>
}

impl MockStream {
fn new() -> MockStream {
let head = b"HTTP/1.1 200 OK\r\nServer: Mock\r\n\r\n";
let mut res = head.to_vec();
res.push_all(README);
MockStream {
read: Cursor::new(res)
}
}
}

macro_rules! try_return(
($e:expr) => {{
match $e {
Ok(v) => v,
Err(..) => return
impl Clone for MockStream {
fn clone(&self) -> MockStream {
MockStream {
read: Cursor::new(self.read.get_ref().clone())
}
}}
);

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_all(BODY));
try_return!(res.end());
}
}

impl Read for MockStream {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.read.read(buf)
}
}

impl Write for MockStream {
fn write(&mut self, msg: &[u8]) -> io::Result<usize> {
// we're mocking, what do we care.
Ok(msg.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}

#[derive(Clone)]
Expand All @@ -48,17 +68,36 @@ impl hyper::header::HeaderFormat for Foo {
}
}

impl net::NetworkStream for MockStream {
fn peer_addr(&mut self) -> io::Result<SocketAddr> {
Ok("127.0.0.1:1337".parse().unwrap())
}
}

struct MockConnector;

impl net::NetworkConnector for MockConnector {
type Stream = MockStream;
fn connect(&mut self, _: &str, _: u16, _: &str) -> io::Result<MockStream> {
Ok(MockStream::new())
}

}

#[bench]
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);
fn bench_mock_hyper(b: &mut test::Bencher) {
let url = "http://127.0.0.1:1337/";
b.iter(|| {
client.get(url).header(Foo).send().unwrap().read_to_string().unwrap();
let mut req = hyper::client::Request::with_connector(
hyper::Get, hyper::Url::parse(url).unwrap(), &mut MockConnector
).unwrap();
req.headers_mut().set(Foo);

let mut s = String::new();
req
.start().unwrap()
.send().unwrap()
.read_to_string(&mut s).unwrap()
});
listening.close().unwrap()
}

98 changes: 0 additions & 98 deletions benches/client_mock_tcp.rs

This file was deleted.

12 changes: 7 additions & 5 deletions benches/server.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#![feature(old_io, test)]
#![feature(io, net, test)]
extern crate hyper;
extern crate test;

use test::Bencher;
use std::old_io::net::ip::Ipv4Addr;
use std::io::{Read, Write};
use std::net::IpAddr;

use hyper::method::Method::Get;
use hyper::server::{Request, Response};
Expand All @@ -12,7 +13,8 @@ static PHRASE: &'static [u8] = b"Benchmarking hyper vs others!";

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

fn hyper_handle(_: Request, res: Response) {
Expand All @@ -23,8 +25,8 @@ fn hyper_handle(_: Request, res: Response) {

#[bench]
fn bench_hyper(b: &mut Bencher) {
let server = hyper::Server::http(Ipv4Addr(127, 0, 0, 1), 0);
let mut listener = server.listen(hyper_handle).unwrap();
let server = hyper::Server::http(hyper_handle);
let mut listener = server.listen(IpAddr::new_v4(127, 0, 0, 1), 0).unwrap();

let url = hyper::Url::parse(&*format!("http://{}", listener.socket)).unwrap();
b.iter(|| request(url.clone()));
Expand Down
12 changes: 3 additions & 9 deletions examples/client.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#![feature(env, old_io)]
#![feature(env)]
extern crate hyper;

use std::env;
use std::old_io::stdout;
use std::old_io::util::copy;

use hyper::Client;

Expand All @@ -18,16 +16,12 @@ fn main() {

let mut client = Client::new();

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

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

//TODO: add copy back when std::stdio impls std::io::Write.
}
9 changes: 5 additions & 4 deletions examples/hello.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#![feature(old_io)]
#![feature(io, net)]
extern crate hyper;

use std::old_io::net::ip::Ipv4Addr;
use std::io::Write;
use std::net::IpAddr;
use hyper::server::{Request, Response};

static PHRASE: &'static [u8] = b"Hello World!";
Expand All @@ -13,7 +14,7 @@ fn hello(_: Request, res: Response) {
}

fn main() {
let _listening = hyper::Server::http(Ipv4Addr(127, 0, 0, 1), 3000)
.listen(hello).unwrap();
let _listening = hyper::Server::http(hello)
.listen(IpAddr::new_v4(127, 0, 0, 1), 3000).unwrap();
println!("Listening on http://127.0.0.1:3000");
}
10 changes: 5 additions & 5 deletions examples/server.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#![feature(old_io)]
#![feature(io, net)]
extern crate hyper;
#[macro_use] extern crate log;

use std::old_io::util::copy;
use std::old_io::net::ip::Ipv4Addr;
use std::io::{Write, copy};
use std::net::IpAddr;

use hyper::{Get, Post};
use hyper::header::ContentLength;
Expand Down Expand Up @@ -50,7 +50,7 @@ fn echo(mut req: Request, mut res: Response) {
}

fn main() {
let server = Server::http(Ipv4Addr(127, 0, 0, 1), 1337);
let _guard = server.listen(echo).unwrap();
let server = Server::http(echo);
let _guard = server.listen(IpAddr::new_v4(127, 0, 0, 1), 1337).unwrap();
println!("Listening on http://127.0.0.1:1337");
}
Loading

0 comments on commit 0fd6fcd

Please sign in to comment.