Reading from filesystem is successful.

This commit is contained in:
abbie 2021-08-30 16:17:11 +01:00
parent 6126fd035d
commit 0e4dc6b834
4 changed files with 47 additions and 41 deletions

34
Cargo.lock generated
View file

@ -2,38 +2,6 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 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]] [[package]]
name = "herb" name = "herb"
version = "0.1.0" version = "0.1.1"
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"

View file

@ -1,6 +1,6 @@
[package] [package]
name = "herb" name = "herb"
version = "0.1.0" version = "0.1.1"
edition = "2018" edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View file

@ -6,23 +6,43 @@ This is a little fun experiment for me to mess around with Rust
## Chonklist ## Chonklist
I need this stuff to actually have a functioning but basic web server. I need this stuff to actually have a functioning but basic web server.
[x] Open a socket [x] Open a socket
[x] Recieve HTTP requests [x] Recieve HTTP requests
[x] Process said requests [x] Process said requests
[x] Send back HTTP requests [x] Send back HTTP requests
[ ] Read pages from filesystem
[x] Read index page from filesystem
### Next Chonklist ### Next Chonklist
The stuff I need to make it usable. 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 [ ] Custom error pages
[ ] User configurable
[ ] Make an actual header [ ] Make it user configurable
[ ] Properly generate headers
[ ] Read other pages from filesystem
### SUPAR Chonklist ### SUPAR Chonklist
Whatever is on here, just to make it extra spicy. Whatever is on here, just to make it extra spicy.
[ ] HTTPS support [ ] HTTPS support
[ ] HTTP/2 support [ ] HTTP/2 support
[ ] Dynamic pages via CGI? [ ] Dynamic pages via CGI?
[ ] Image thumbnailing/compression [ ] Image thumbnailing/compression
[ ] Compressing big files [ ] Compressing big files
[ ] Directory index generation

View file

@ -1,10 +1,28 @@
use std::io::{Write}; use std::io::{Write};
use std::string;
use std::net::{TcpListener, TcpStream}; 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) { fn serve(mut stream: TcpStream) {
let contents = "<h1>herb works!</h1>"; // To-do: Make a header generator to feed into response.
let response = format!("{}{}", "HTTP/1.1 200 OK\r\n\r\n", contents);
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.write(response.as_bytes()).unwrap();
stream.flush().unwrap(); stream.flush().unwrap();