Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created July 17, 2018 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/4fb79a21cb27d0509e008cf05893609e to your computer and use it in GitHub Desktop.
Save rust-play/4fb79a21cb27d0509e008cf05893609e to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
extern crate futures; // 0.1.21
extern crate h2; // 0.1.10
extern crate http; // 0.1.7
extern crate tokio;
use futures::{Future, Stream};
use h2::server;
use http::{Response, StatusCode};
use tokio::net::TcpListener;
pub fn main() {
let addr = "127.0.0.1:5928".parse().unwrap();
let listener = TcpListener::bind(&addr).unwrap();
// Accept all incoming TCP connections.
let connection = listener
.incoming()
.for_each(move |socket| {
println!("connection opened: {:?}", socket);
// Spawn a new task to process each connection.
tokio::spawn({
// Start the HTTP/2.0 connection handshake
server::handshake(socket)
.and_then(|h2| {
// Accept all inbound HTTP/2.0 streams sent over the
// connection.
h2.for_each(|(mut request, mut respond)| {
// println!("Received request: {:?}", request);
// println!("Request URI: {:?}", request.uri().path());
let stream = request.body_mut();
//ERROR HERE:^^^^^^^ cannot move out of borrowed content
let stream = stream
.for_each(|msg| {
println!("Msg Received: {:?}", msg);
Ok(())
})
.map_err(|e| println!("ERR RecvStream: {:?}", e));
// tokio::spawn(stream);
// Build a response with no body
let response =
Response::builder().status(StatusCode::OK).body(()).unwrap();
// Send the response back to the client
respond.send_response(response, true).unwrap();
Ok(())
})
})
.map_err(|e| panic!("unexpected error = {:?}", e))
});
Ok(())
})
.map_err(|e| println!("connection error: {:?}", e));
tokio::run(connection);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment