2 use std::time::Duration;
4 use async_std::prelude::*;
5 use async_std::io::{ Read, Write };
6 use async_std::net::{ TcpListener };
8 use futures::stream::StreamExt;
12 env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
14 // Listen for incoming TCP connections on localhost port 7878
15 let listener = TcpListener::bind("127.0.0.1:7878").await.unwrap();
17 listener.incoming().for_each_concurrent(/* limit */ None, |tcpstream| async move {
18 let tcpstream = tcpstream.unwrap();
19 task::spawn(handle_connection(tcpstream));
23 async fn handle_connection(mut stream: impl Read + Write + Unpin) {
24 // Read the first 1024 bytes of data from the stream
25 let mut buffer = [0; 1024];
26 assert!(stream.read(&mut buffer).await.unwrap() > 0);
28 // Respond with greetings or a 404,
29 // depending on the data in the request
30 let (status_line, filename) = if buffer.starts_with(b"GET / HTTP/1.1\r\n") {
31 ("HTTP/1.1 200 OK", "index.html")
32 } else if buffer.starts_with(b"GET /sleep HTTP/1.1\r\n") {
33 task::sleep(Duration::from_secs(5)).await;
34 // sync version, to demonstrate concurrent async vs. parallel threads
35 // std::thread::sleep(Duration::from_secs(5));
36 ("HTTP/1.1 201 Sleep", "index.html")
38 ("HTTP/1.1 404 NOT FOUND", "404.html")
40 let contents = fs::read_to_string(filename).unwrap();
41 log::info!("GET {} {}", filename, status_line);
43 // Write response back to the stream,
44 // and flush the stream to ensure the response is sent back to the client
45 let response = format!("{status_line}\r\n\r\n{contents}");
46 stream.write_all(response.as_bytes()).await.unwrap();
47 stream.flush().await.unwrap();
57 use futures::io::Error;
58 use futures::task::{Context, Poll};
60 struct MockTcpStream {
65 impl Read for MockTcpStream {
70 ) -> Poll<Result<usize, Error>> {
71 let size: usize = cmp::min(self.read_data.len(), buf.len());
72 buf[..size].copy_from_slice(&self.read_data[..size]);
77 impl Write for MockTcpStream {
79 mut self: Pin<&mut Self>,
82 ) -> Poll<Result<usize, Error>> {
83 self.write_data = Vec::from(buf);
85 Poll::Ready(Ok(buf.len()))
88 fn poll_flush(self: Pin<&mut Self>, _: &mut Context) -> Poll<Result<(), Error>> {
92 fn poll_close(self: Pin<&mut Self>, _: &mut Context) -> Poll<Result<(), Error>> {
97 impl Unpin for MockTcpStream {}
100 async fn test_handle_connection() {
101 let input_bytes = b"GET / HTTP/1.1\r\n";
102 let mut contents = vec![0u8; 1024];
103 contents[..input_bytes.len()].clone_from_slice(input_bytes);
104 let mut stream = MockTcpStream {
106 write_data: Vec::new(),
109 handle_connection(&mut stream).await;
111 let expected_contents = fs::read_to_string("index.html").unwrap();
112 let expected_response = format!("HTTP/1.1 200 OK\r\n\r\n{}", expected_contents);
113 assert!(stream.write_data.starts_with(expected_response.as_bytes()));