Wire in trailers (#34)

Add send and receive trailer support.
This commit is contained in:
Carl Lerche
2017-08-25 10:20:47 -07:00
committed by GitHub
parent c0433e8831
commit 11d5f95236
12 changed files with 365 additions and 25 deletions

View File

@@ -1,27 +1,62 @@
extern crate h2;
extern crate http;
extern crate bytes;
extern crate futures;
extern crate tokio_io;
extern crate tokio_core;
extern crate io_dump;
extern crate env_logger;
use h2::*;
use h2::client::Client;
use http::*;
use futures::*;
use bytes::*;
use tokio_core::reactor;
use tokio_core::net::TcpStream;
struct Process {
body: Body<Bytes>,
trailers: bool,
}
impl Future for Process {
type Item = ();
type Error = ConnectionError;
fn poll(&mut self) -> Poll<(), ConnectionError> {
loop {
if self.trailers {
let trailers = try_ready!(self.body.poll_trailers());
println!("GOT TRAILERS: {:?}", trailers);
return Ok(().into());
} else {
match try_ready!(self.body.poll()) {
Some(chunk) => {
println!("GOT CHUNK = {:?}", chunk);
}
None => {
self.trailers = true;
}
}
}
}
}
}
pub fn main() {
let _ = env_logger::init();
let mut core = reactor::Core::new().unwrap();;
let handle = core.handle();
let tcp = TcpStream::connect(
&"127.0.0.1:5928".parse().unwrap(),
&core.handle());
&handle);
let tcp = tcp.then(|res| {
let tcp = io_dump::Dump::to_stdout(res.unwrap());
@@ -36,11 +71,30 @@ pub fn main() {
.uri("https://http2.akamai.com/")
.body(()).unwrap();
let stream = client.request(request, true).unwrap();
client.join(stream.and_then(|response| {
let mut trailers = h2::HeaderMap::new();
trailers.insert("zomg", "hello".parse().unwrap());
let mut stream = client.request(request, false).unwrap();
// send trailers
stream.send_trailers(trailers).unwrap();
// Spawn a task to run the client...
handle.spawn(client.map_err(|e| println!("GOT ERR={:?}", e)));
stream.and_then(|response| {
println!("GOT RESPONSE: {:?}", response);
Ok(())
}))
// Get the body
let (_, body) = response.into_parts();
Process {
body,
trailers: false,
}
}).map_err(|e| {
println!("GOT ERR={:?}", e);
})
})
;

83
examples/server-tr.rs Normal file
View File

@@ -0,0 +1,83 @@
extern crate h2;
extern crate http;
extern crate bytes;
extern crate futures;
extern crate tokio_io;
extern crate tokio_core;
extern crate io_dump;
extern crate env_logger;
use h2::server::Server;
use http::*;
use bytes::*;
use futures::*;
use tokio_core::reactor;
use tokio_core::net::TcpListener;
pub fn main() {
let _ = env_logger::init();
let mut core = reactor::Core::new().unwrap();;
let handle = core.handle();
let listener = TcpListener::bind(
&"127.0.0.1:5928".parse().unwrap(),
&handle).unwrap();
println!("listening on {:?}", listener.local_addr());
let server = listener.incoming().for_each(move |(socket, _)| {
// let socket = io_dump::Dump::to_stdout(socket);
let connection = Server::handshake(socket)
.and_then(|conn| {
println!("H2 connection bound");
conn.for_each(|(request, mut stream)| {
println!("GOT request: {:?}", request);
let response = Response::builder()
.status(status::OK)
.body(()).unwrap();
if let Err(e) = stream.send_response(response, false) {
println!(" error responding; err={:?}", e);
}
println!(">>>> sending data");
if let Err(e) = stream.send_data(Bytes::from_static(b"hello world"), false) {
println!(" -> err={:?}", e);
}
let mut hdrs = HeaderMap::new();
hdrs.insert("status", "ok".parse().unwrap());
println!(">>>> sending trailers");
if let Err(e) = stream.send_trailers(hdrs) {
println!(" -> err={:?}", e);
}
Ok(())
}).and_then(|_| {
println!("~~~~~~~~~~~~~~~~~~~~~~~~~~~ H2 connection CLOSE !!!!!! ~~~~~~~~~~~");
Ok(())
})
})
.then(|res| {
if let Err(e) = res {
println!(" -> err={:?}", e);
}
Ok(())
})
;
handle.spawn(connection);
Ok(())
});
core.run(server).unwrap();
}