use std::io::{Write, BufReader, BufRead};
use std::net::{TcpListener, TcpStream};
use std::fs;
use std::string::{String};
fn get_page(request: String) -> String {
// The loaded page should be left immutable as it does
// not need to be modified by the server.
let filename = "index.html";
let page = fs::read_to_string(filename);
match page {
Ok(i) => i.to_string(),
Err(_e) => "
404 Not Found
The resource you are trying to locate cannot be accessed!
".to_string(),
}
}
fn process_request(request: Vec) -> String {
let mut input = String::from_utf8_lossy(&request).to_string();
let mut output = String::new();
if input.contains("GET") {
// To-do: find a less atrocious way to do this.
println!("Stream sent GET request.");
input = input.replace("GET ", "");
input = input.replace(" HTTP/1.1", "");
// Theoretically by this point, the request
// will have been cut down to just the
// requested resource, but in my experience
// this code is gonna result in like 50k errors
// and I'm gonna have to rewrite it to get it
// to actually work the way I want it to.
if input == "/" {
// And now for ignoring the entire variable!
output = "index.html".to_string();
} else {
output = "index.html".to_string();
}
} else {
// It's get_page()'s problem now.
println!("Stream sent unhandlable request type.");
output = ".503".to_string();
}
println!("{}", output);
return output;
}
fn serve(mut stream: TcpStream) {
let mut request = Vec::new();
let mut reader = BufReader::new(&mut stream);
reader
.read_until(b'\n', &mut request)
.expect("Failed to read from stream!");
let resource = process_request(request);
let contents = get_page(resource);
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();
}
fn main() -> std::io::Result<()> {
let listen = TcpListener::bind("127.0.0.1:8080")?;
for stream in listen.incoming() {
println!("Serving incoming stream.");
serve(stream?);
}
Ok(())
}