Parse Path from HTTP request
Change-Id: I7686e00ae97589d16a781cc1b6dfd1968ad4de1a
Reviewed-on: https://git.clicks.codes/c/Clicks/BYO/HttpServer/rust/+/247
Tested-by: Skyler Grey <minion@clicks.codes>
Reviewed-by: Skyler Grey <minion@clicks.codes>
diff --git a/src/main.rs b/src/main.rs
index 50cc72e..e78f067 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,17 +1,31 @@
-use std::{net::TcpListener, io::Write};
+use std::{net::TcpListener, io::{Write, Read}};
fn main() {
let listener = TcpListener::bind("127.0.0.1:4221").unwrap();
-
+
for stream in listener.incoming() {
match stream {
Ok(mut _stream) => {
println!("accepted new connection");
- let status = "200 OK\r\n";
- let res_headers = "\r\n";
+ let mut buf: String = Default::default();
- _stream.write(format!("HTTP/1.1 {status}{res_headers}").as_bytes()).unwrap();
+ _stream.read_to_string(&mut buf).unwrap();
+
+ let mut parts = buf.split(" ");
+
+ let _method = parts.next().unwrap();
+ let path = parts.next().unwrap();
+
+ let status = if path == "/" {
+ "200 OK"
+ } else {
+ "404 Not Found"
+ };
+
+ let res_headers = "";
+
+ _stream.write(format!("HTTP/1.1 {status}\r\n{res_headers}\r\n").as_bytes()).unwrap();
}
Err(e) => {
println!("error: {}", e);