X-Git-Url: https://piware.de/gitweb/?p=learn-rust.git;a=blobdiff_plain;f=async-http%2Fsrc%2Fmain.rs;fp=async-http%2Fsrc%2Fmain.rs;h=09ca609eaa0d6333fc08cd86cfb08b3ca936dae2;hp=0000000000000000000000000000000000000000;hb=a254aa3419bd260f1e28e8759bb172a6c2b5cf44;hpb=5931f0a88bd65592cb76fcac25896471dd82186d diff --git a/async-http/src/main.rs b/async-http/src/main.rs new file mode 100644 index 0000000..09ca609 --- /dev/null +++ b/async-http/src/main.rs @@ -0,0 +1,39 @@ +use std::fs; +use std::io::prelude::*; +use std::net::TcpListener; +use std::net::TcpStream; + +fn main() { + // Listen for incoming TCP connections on localhost port 7878 + let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); + + // Block forever, handling each request that arrives at this IP address + for stream in listener.incoming() { + let stream = stream.unwrap(); + + handle_connection(stream); + } +} + +fn handle_connection(mut stream: TcpStream) { + // Read the first 1024 bytes of data from the stream + let mut buffer = [0; 1024]; + stream.read(&mut buffer).unwrap(); + + let get = b"GET / HTTP/1.1\r\n"; + + // Respond with greetings or a 404, + // depending on the data in the request + let (status_line, filename) = if buffer.starts_with(get) { + ("HTTP/1.1 200 OK\r\n\r\n", "index.html") + } else { + ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") + }; + let contents = fs::read_to_string(filename).unwrap(); + + // Write response back to the stream, + // and flush the stream to ensure the response is sent back to the client + let response = format!("{status_line}{contents}"); + stream.write_all(response.as_bytes()).unwrap(); + stream.flush().unwrap(); +}