From 0e4dc6b834281d3f61b107652d9d9a48479df442 Mon Sep 17 00:00:00 2001 From: Celeste Date: Mon, 30 Aug 2021 16:17:11 +0100 Subject: [PATCH] Reading from filesystem is successful. --- Cargo.lock | 34 +--------------------------------- Cargo.toml | 2 +- README.md | 28 ++++++++++++++++++++++++---- src/main.rs | 24 +++++++++++++++++++++--- 4 files changed, 47 insertions(+), 41 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1a3569e..ce3bbe5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,38 +2,6 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "bytes" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8" - -[[package]] -name = "fnv" -version = "1.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" - [[package]] name = "herb" -version = "0.1.0" -dependencies = [ - "http", -] - -[[package]] -name = "http" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "527e8c9ac747e28542699a951517aa9a6945af506cd1f2e1b53a576c17b6cc11" -dependencies = [ - "bytes", - "fnv", - "itoa", -] - -[[package]] -name = "itoa" -version = "0.4.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" +version = "0.1.1" diff --git a/Cargo.toml b/Cargo.toml index 308b89d..b1f5091 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "herb" -version = "0.1.0" +version = "0.1.1" edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/README.md b/README.md index 9b0f8c9..60166a5 100644 --- a/README.md +++ b/README.md @@ -6,23 +6,43 @@ This is a little fun experiment for me to mess around with Rust ## Chonklist I need this stuff to actually have a functioning but basic web server. + [x] Open a socket + [x] Recieve HTTP requests + [x] Process said requests + [x] Send back HTTP requests -[ ] Read pages from filesystem + +[x] Read index page from filesystem ### Next Chonklist The stuff I need to make it usable. -[ ] Detect missing files and return 404 + +[ ] Read the stream to detect what resource it wants to access + +[ ] Detect missing files and return a 404 page + [ ] Custom error pages -[ ] User configurable -[ ] Make an actual header + +[ ] Make it user configurable + +[ ] Properly generate headers + +[ ] Read other pages from filesystem ### SUPAR Chonklist Whatever is on here, just to make it extra spicy. + [ ] HTTPS support + [ ] HTTP/2 support + [ ] Dynamic pages via CGI? + [ ] Image thumbnailing/compression + [ ] Compressing big files + +[ ] Directory index generation diff --git a/src/main.rs b/src/main.rs index b9a11f7..7cb72f4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,28 @@ use std::io::{Write}; -use std::string; use std::net::{TcpListener, TcpStream}; +use std::fs; +use std::string::{String}; + +fn get_page() -> String { + // To-do: Make this able to load other pages + // + // The loaded page should be left immutable as it does + // not need to be modified by the server. + + let filename = "index.html"; // BAD!!! + let page = fs::read_to_string(filename) + .expect("Page read failed!"); + + return page; +} fn serve(mut stream: TcpStream) { - let contents = "

herb works!

"; - let response = format!("{}{}", "HTTP/1.1 200 OK\r\n\r\n", contents); + // To-do: Make a header generator to feed into response. + + let contents = get_page(); + let header = "HTTP/1.1 200 OK\r\n\r\n"; + let response = format!("{}{}", header, contents); + stream.write(response.as_bytes()).unwrap(); stream.flush().unwrap();