summaryrefslogtreecommitdiffstats
path: root/src/doc/book/listings/ch20-web-server/listing-20-03
diff options
context:
space:
mode:
Diffstat (limited to 'src/doc/book/listings/ch20-web-server/listing-20-03')
-rw-r--r--src/doc/book/listings/ch20-web-server/listing-20-03/Cargo.lock6
-rw-r--r--src/doc/book/listings/ch20-web-server/listing-20-03/Cargo.toml6
-rw-r--r--src/doc/book/listings/ch20-web-server/listing-20-03/src/main.rs29
3 files changed, 41 insertions, 0 deletions
diff --git a/src/doc/book/listings/ch20-web-server/listing-20-03/Cargo.lock b/src/doc/book/listings/ch20-web-server/listing-20-03/Cargo.lock
new file mode 100644
index 000000000..f2d069f46
--- /dev/null
+++ b/src/doc/book/listings/ch20-web-server/listing-20-03/Cargo.lock
@@ -0,0 +1,6 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+[[package]]
+name = "hello"
+version = "0.1.0"
+
diff --git a/src/doc/book/listings/ch20-web-server/listing-20-03/Cargo.toml b/src/doc/book/listings/ch20-web-server/listing-20-03/Cargo.toml
new file mode 100644
index 000000000..fe619478a
--- /dev/null
+++ b/src/doc/book/listings/ch20-web-server/listing-20-03/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "hello"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
diff --git a/src/doc/book/listings/ch20-web-server/listing-20-03/src/main.rs b/src/doc/book/listings/ch20-web-server/listing-20-03/src/main.rs
new file mode 100644
index 000000000..c72d4a9c6
--- /dev/null
+++ b/src/doc/book/listings/ch20-web-server/listing-20-03/src/main.rs
@@ -0,0 +1,29 @@
+use std::{
+ io::{prelude::*, BufReader},
+ net::{TcpListener, TcpStream},
+};
+
+fn main() {
+ let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
+
+ for stream in listener.incoming() {
+ let stream = stream.unwrap();
+
+ handle_connection(stream);
+ }
+}
+
+// ANCHOR: here
+fn handle_connection(mut stream: TcpStream) {
+ let buf_reader = BufReader::new(&mut stream);
+ let http_request: Vec<_> = buf_reader
+ .lines()
+ .map(|result| result.unwrap())
+ .take_while(|line| !line.is_empty())
+ .collect();
+
+ let response = "HTTP/1.1 200 OK\r\n\r\n";
+
+ stream.write_all(response.as_bytes()).unwrap();
+}
+// ANCHOR_END: here