commit 81fac773ff92c6569e980218d930828646a39a44 Author: imk jet Date: Sat Sep 28 17:37:16 2024 +0200 shit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..ec0fea1 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,54 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "blog" +version = "0.1.0" +dependencies = [ + "regex", +] + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "regex" +version = "1.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..875f234 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "blog" +version = "0.1.0" +edition = "2021" + +[dependencies] +regex = "1.10.6" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..71769f3 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,36 @@ +use regex::Regex; +use std::{ + io::{prelude::*, BufReader}, + net::{TcpListener, TcpStream}, +}; + +fn main() { + let listener = TcpListener::bind("[::]:8001").unwrap(); + // println!("{}",listener.local_addr().unwrap()); + + for stream in listener.incoming() { + let stream = stream.unwrap(); + handle_connection(stream) + } +} + +fn handle_connection(mut stream: TcpStream) { + let buf_reader = BufReader::new(&mut stream); + let request_line = buf_reader.lines().next().unwrap().unwrap(); + + let re = Regex::new(r"^(.*) (.*) (.*)$").unwrap(); + let caps = re.captures(&request_line).unwrap(); + let pathname = caps.get(2).map_or("", |m| m.as_str()); + + let mut filename = "index.html"; + if pathname != "/" { + filename = { {} } + } + let status_line = "HTTP/1.1 404 NOT FOUND"; + let contents = String::new(); + + let length = contents.len(); + let response = format!("{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}"); + + stream.write_all(response.as_bytes()).unwrap(); +}