Bad requests are handled with 503 errors and process_request() now removes character returns and line feeds at the end of a request :)

This commit is contained in:
abbie 2021-09-02 17:25:53 +01:00
parent 0ef8e438db
commit c0612dad5d
3 changed files with 22 additions and 15 deletions

2
Cargo.lock generated
View file

@ -4,4 +4,4 @@ version = 3
[[package]] [[package]]
name = "herb" name = "herb"
version = "0.1.1" version = "0.1.2"

View file

@ -1,6 +1,6 @@
[package] [package]
name = "herb" name = "herb"
version = "0.1.1" version = "0.1.2"
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

@ -3,17 +3,24 @@ use std::net::{TcpListener, TcpStream};
use std::fs; use std::fs;
use std::string::{String}; use std::string::{String};
fn get_page(request: String) -> String { fn get_page(filename: String) -> String {
// The loaded page should be left immutable as it does // The loaded page should be left immutable as it does
// not need to be modified by the server. // not need to be modified by the server.
let filename = "index.html"; if filename == ".503" {
let result = "<!DOCTYPE HTML><html><body><h1>503 Not Implemented</h1><p>The request sent by your web browser cannot be handled by this server software.</p></body></html>";
return result.to_string();
} else {
println!("Read");
let result = fs::read_to_string(filename);
let page = fs::read_to_string(filename); match result {
match page { Ok(i) => i.to_string(),
Ok(i) => i.to_string(), Err(_e) => "<!DOCTYPE HTML><html><body><h1>404 Not Found</h1><p>The resource you are trying to locate cannot be accessed!</p></body></html>".to_string(),
Err(_e) => "<!DOCTYPE HTML><html><body><h1>404 Not Found</h1><p>The resource you are trying to locate cannot be accessed!</p></body></html>".to_string(), }
} }
} }
@ -24,8 +31,7 @@ fn process_request(request: Vec<u8>) -> String {
// To-do: find a less atrocious way to do this. // To-do: find a less atrocious way to do this.
println!("Stream sent GET request."); println!("Stream sent GET request.");
input = input.replace("GET ", ""); input = input.replace("GET ", "");
input = input.replace(" HTTP/1.1", ""); input = input.replace(" HTTP/1.1\r\n", "");
// Theoretically by this point, the request // Theoretically by this point, the request
// will have been cut down to just the // will have been cut down to just the
// requested resource, but in my experience // requested resource, but in my experience
@ -33,19 +39,20 @@ fn process_request(request: Vec<u8>) -> String {
// and I'm gonna have to rewrite it to get it // and I'm gonna have to rewrite it to get it
// to actually work the way I want it to. // to actually work the way I want it to.
if input == "/" { if input != "/" {
// And now for ignoring the entire variable!
output = "index.html"
} else {
let mut chars = input.chars(); let mut chars = input.chars();
chars.next(); chars.next();
output = chars.as_str(); output = chars.as_str();
println!("CHARS");
} else {
output = "index.html";
} }
} else { } else {
// It's get_page()'s problem now. // It's get_page()'s problem now.
println!("Stream sent unhandlable request type."); println!("Stream sent unhandlable request type.");
output = ".503" output = ".503";
} }
println!("{}", output.to_string()); println!("{}", output.to_string());