use std::io::{Write, BufReader, BufRead};
use std::net::{TcpListener, TcpStream};
use std::fs;
use std::string::{String};
fn get_page(filename: String) -> String {
// The loaded page should be left immutable as it does
// not need to be modified by the server.
if filename == ".503" {
let result = "
503 Not Implemented
The request sent by your web browser cannot be handled by this server software.
";
return result.to_string();
} else {
println!("Read");
let result = fs::read_to_string(filename);
match result {
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 output;
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\r\n", "");
// 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 != "/" {
let mut chars = input.chars();
chars.next();
output = chars.as_str();
println!("CHARS");
} else {
output = "index.html";
}
} else {
// It's get_page()'s problem now.
println!("Stream sent unhandlable request type.");
output = ".503";
}
println!("{}", output.to_string());
// Did you want to see chars.as_str().to_string()?
return output.to_string();
}
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(())
}