feat(hyper): switch to std::io, std::net, and std::path.
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.
This commit is contained in:
@@ -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;
|
||||
use std::io::{self, Read, Write, Cursor};
|
||||
use std::net::SocketAddr;
|
||||
|
||||
fn listen() -> hyper::server::Listening {
|
||||
let server = Server::http(Ipv4Addr(127, 0, 0, 1), 0);
|
||||
server.listen(handle).unwrap()
|
||||
use hyper::net;
|
||||
|
||||
static README: &'static [u8] = include_bytes!("../README.md");
|
||||
|
||||
struct MockStream {
|
||||
read: Cursor<Vec<u8>>
|
||||
}
|
||||
|
||||
macro_rules! try_return(
|
||||
($e:expr) => {{
|
||||
match $e {
|
||||
Ok(v) => v,
|
||||
Err(..) => return
|
||||
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)
|
||||
}
|
||||
}}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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 Clone for MockStream {
|
||||
fn clone(&self) -> MockStream {
|
||||
MockStream {
|
||||
read: Cursor::new(self.read.get_ref().clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)]
|
||||
@@ -48,17 +68,36 @@ impl hyper::header::HeaderFormat for Foo {
|
||||
}
|
||||
}
|
||||
|
||||
#[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);
|
||||
b.iter(|| {
|
||||
client.get(url).header(Foo).send().unwrap().read_to_string().unwrap();
|
||||
});
|
||||
listening.close().unwrap()
|
||||
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_mock_hyper(b: &mut test::Bencher) {
|
||||
let url = "http://127.0.0.1:1337/";
|
||||
b.iter(|| {
|
||||
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()
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,98 +0,0 @@
|
||||
#![feature(collections, old_io, test)]
|
||||
extern crate hyper;
|
||||
|
||||
extern crate test;
|
||||
|
||||
use std::fmt;
|
||||
use std::old_io::{IoResult, MemReader};
|
||||
use std::old_io::net::ip::SocketAddr;
|
||||
|
||||
use hyper::net;
|
||||
|
||||
static README: &'static [u8] = include_bytes!("../README.md");
|
||||
|
||||
|
||||
struct MockStream {
|
||||
read: MemReader,
|
||||
}
|
||||
|
||||
impl Clone for MockStream {
|
||||
fn clone(&self) -> MockStream {
|
||||
MockStream::new()
|
||||
}
|
||||
}
|
||||
|
||||
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: MemReader::new(res),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Reader for MockStream {
|
||||
fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> {
|
||||
self.read.read(buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl Writer for MockStream {
|
||||
fn write_all(&mut self, _msg: &[u8]) -> IoResult<()> {
|
||||
// we're mocking, what do we care.
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Foo;
|
||||
|
||||
impl hyper::header::Header for Foo {
|
||||
fn header_name() -> &'static str {
|
||||
"x-foo"
|
||||
}
|
||||
fn parse_header(_: &[Vec<u8>]) -> Option<Foo> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl hyper::header::HeaderFormat for Foo {
|
||||
fn fmt_header(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt.write_str("Bar")
|
||||
}
|
||||
}
|
||||
|
||||
impl net::NetworkStream for MockStream {
|
||||
fn peer_name(&mut self) -> IoResult<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) -> IoResult<MockStream> {
|
||||
Ok(MockStream::new())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[bench]
|
||||
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_connector(
|
||||
hyper::Get, hyper::Url::parse(url).unwrap(), &mut MockConnector
|
||||
).unwrap();
|
||||
req.headers_mut().set(Foo);
|
||||
|
||||
req
|
||||
.start().unwrap()
|
||||
.send().unwrap()
|
||||
.read_to_string().unwrap()
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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};
|
||||
@@ -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) {
|
||||
@@ -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()));
|
||||
|
||||
Reference in New Issue
Block a user