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:
parent
0ef8e438db
commit
c0612dad5d
3 changed files with 22 additions and 15 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -4,4 +4,4 @@ version = 3
|
|||
|
||||
[[package]]
|
||||
name = "herb"
|
||||
version = "0.1.1"
|
||||
version = "0.1.2"
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "herb"
|
||||
version = "0.1.1"
|
||||
version = "0.1.2"
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
|
33
src/main.rs
33
src/main.rs
|
@ -3,17 +3,24 @@ use std::net::{TcpListener, TcpStream};
|
|||
use std::fs;
|
||||
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
|
||||
// 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 page {
|
||||
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(),
|
||||
match result {
|
||||
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(),
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -24,8 +31,7 @@ fn process_request(request: Vec<u8>) -> String {
|
|||
// 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", "");
|
||||
|
||||
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
|
||||
|
@ -33,19 +39,20 @@ fn process_request(request: Vec<u8>) -> String {
|
|||
// 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"
|
||||
} else {
|
||||
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"
|
||||
output = ".503";
|
||||
}
|
||||
println!("{}", output.to_string());
|
||||
|
||||
|
|
Loading…
Reference in a new issue