commit 68a4e73a1aa01e4ce70697c0a32fdbe0a14c05e9 Author: rattatwinko Date: Wed Apr 30 21:20:25 2025 +0200 initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..40903d6 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,16 @@ + + + + \ No newline at end of file diff --git a/.idea/material_theme_project_new.xml b/.idea/material_theme_project_new.xml new file mode 100644 index 0000000..e3d7ccb --- /dev/null +++ b/.idea/material_theme_project_new.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..94e2d49 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/rs_http.iml b/.idea/rs_http.iml new file mode 100644 index 0000000..cf84ae4 --- /dev/null +++ b/.idea/rs_http.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..8036025 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "rs_http" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..8319cb4 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rs_http" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/src/index.html b/src/index.html new file mode 100644 index 0000000..afcfcab --- /dev/null +++ b/src/index.html @@ -0,0 +1,16 @@ + + + + + Hello! + + + +

+ Hello! +

+

+ This is a WebSite served from Rust! +

+ + \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..0d2ac83 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,55 @@ +use std::{ + fs, + io::{BufRead, BufReader, Write}, + net::{TcpListener, TcpStream}, + path::Path, +}; + +fn handle_client(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(); + + println!("Request: {:?}", http_request); + + let path = Path::new("index.html"); // Corrected: Use Path directly + + match fs::read_to_string(path) { + Ok(contents) => { + let response = format!( + "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nContent-Type: text/html\r\n\r\n{}", + contents.len(), + contents + ); + stream.write_all(response.as_bytes()).unwrap(); + stream.flush().unwrap(); // Ensure the response is sent + } + Err(e) => { + eprintln!("Error reading file: {}", e); // Log the error + let not_found_response = "HTTP/1.1 404 Not Found\r\nContent-Length: 13\r\n\r\n404 Not Found"; + stream.write_all(not_found_response.as_bytes()).unwrap(); + stream.flush().unwrap(); + } + } +} + +fn main() { + let listener = TcpListener::bind("127.0.0.1:8080").unwrap(); + println!("Listening on port 8080"); + + for stream in listener.incoming() { + match stream { + Ok(stream) => { + std::thread::spawn(move || { + handle_client(stream); + }); + } + Err(e) => { + eprintln!("Error accepting connection: {}", e); + } + } + } +}