Compare commits

..

No commits in common. "178a5973d02b4a2914263aa2c0012c302c53705c" and "a0b37387ebf2473768edd3c264ee26939daf819e" have entirely different histories.

4 changed files with 20 additions and 47 deletions

View file

@ -1 +1 @@
4
3

2
Cargo.lock generated
View file

@ -4,7 +4,7 @@ version = 3
[[package]]
name = "herb"
version = "0.6.0"
version = "0.5.1"
dependencies = [
"packeteer",
]

View file

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

View file

@ -1,24 +1,18 @@
use std::io::{Write, BufReader, BufRead, Read};
use std::io::{Write, BufReader, BufRead};
use std::thread;
use std::net::{TcpListener, TcpStream};
use std::fs;
use std::fs::File;
use std::string::{String};
use std::process::Command;
use packeteer::http1::*;
use packeteer::{generate_kvheader, unwrap_url_into_segments};
struct Resource {
contents: Vec<u8>,
contents: String,
status_code: i32,
mime: String,
}
struct GetPageResult {
is500: bool,
contents: Vec<u8>,
}
fn process_cgi(filename: String) -> String {
// This is gonna be the boggiest implementation of CGI that anyone
// has ever seen in the history of the fucking world
@ -144,7 +138,7 @@ fn generate_index(directory: String) -> String {
}
fn get_page(filename: String) -> GetPageResult {
fn get_page(filename: String) -> String {
// The loaded page should be left immutable as it does
// not need to be modified by the server.
@ -153,44 +147,28 @@ fn get_page(filename: String) -> GetPageResult {
let checks = check_if_path_exists(path.clone());
let index = check_if_dir(path.clone());
let mut resultstruct = GetPageResult { is500: false, contents: vec![] };
println!("{} {} {}", path, checks, index);
if checks == true && index == false {
if path.contains(".") != true && path.contains(".cgi?") != true {
let result = generate_index(filename);
resultstruct.contents = result.to_string().into_bytes();
return resultstruct;
return result.to_string();
} else if path.contains(".cgi?") {
let result = process_cgi(filename);
resultstruct.contents = result.to_string().into_bytes();
return resultstruct;
return result.to_string();
}
}
if filename.contains(".cgi") {
let result = process_cgi(filename);
resultstruct.contents = result.to_string().into_bytes();
return resultstruct;
return result.to_string();
} else {
let f = File::open(filename);
if f.is_ok() {
let mut buf: Vec<u8> = vec![];
let result = f.unwrap().read_to_end(&mut buf);
if result.is_ok() {
resultstruct.contents = buf;
return resultstruct;
} else {
resultstruct.contents = "<!DOCTYPE HTML><html><body><h1>500 Internal Server Error</h1><p>The resource you are trying to access cannot be read by the server.</p></body></html>".to_string().into_bytes();
resultstruct.is500 = true;
return resultstruct;
}
} else {
resultstruct.contents = "<!DOCTYPE HTML><html><body><h1>500 Internal Server Error</h1><p>The resource you are trying to access cannot be read by the server.</p></body></html>".to_string().into_bytes();
resultstruct.is500 = true;
return resultstruct;
let result = fs::read_to_string(filename);
match result {
Ok(i) => i.to_string(),
Err(_e) => "<!DOCTYPE HTML><html><body><h1>500 Internal Server Error</h1><p>The resource you are trying to access cannot be read by the server.</p></body></html>".to_string(),
}
}
}
@ -221,7 +199,7 @@ fn process_request(request: Vec<u8>) -> Resource {
let exists = check_if_path_exists(path.clone());
if exists == false && path.contains(".cgi?") == false {
let resource = Resource { contents: "<!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().into_bytes(), status_code: 404, mime: "text/html".to_string() };
let resource = Resource { contents: "<!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(), status_code: 404, mime: "text/html".to_string() };
return resource;
} else {
let dir = check_if_dir(path.clone());
@ -237,15 +215,14 @@ fn process_request(request: Vec<u8>) -> Resource {
output = "index.html";
}
} else {
let resource = Resource { contents: "<!DOCTYPE HTML><html><body><h1>501 Not Implemented</h1><p>The request sent by your web browser cannot be handled by this server software.</p></body></html>".to_string().into_bytes(), status_code: 501, mime: "text/html".to_string() };
let resource = Resource { contents: "<!DOCTYPE HTML><html><body><h1>501 Not Implemented</h1><p>The request sent by your web browser cannot be handled by this server software.</p></body></html>".to_string(), status_code: 501, mime: "text/html".to_string() };
println!("Stream sent unhandlable request.");
return resource;
}
// Did you want to see chars.as_str().to_string()?
let rescontents = get_page(output.to_string());
let mut resource = Resource { contents: rescontents.contents, status_code: 200, mime: detect_media_type(output.to_string()) };
if rescontents.is500 {
let mut resource = Resource { contents: get_page(output.to_string()), status_code: 200, mime: detect_media_type(output.to_string()) };
if resource.contents.contains("500 Internal Server Error") {
resource.status_code = 500;
resource.mime = "text/html".to_string();
}
@ -264,7 +241,7 @@ fn serve(mut stream: TcpStream) {
let resource = process_request(request);
let mut response_constructed = generate_response(resource.status_code, "");
let mut response_constructed = generate_response(resource.status_code, &resource.contents);
response_constructed.headers.push(generate_kvheader("Content-Type", &resource.mime));
response_constructed.headers.push(generate_kvheader("Server", &format!("herb/{}", env!("CARGO_PKG_VERSION"))));
@ -272,11 +249,7 @@ fn serve(mut stream: TcpStream) {
response_constructed.headers.push(generate_kvheader("Date", &grab_time()))
}
let mut contents_clone = resource.contents.clone();
let mut unpacked_response = unpack_response(response_constructed).into_bytes();
unpacked_response.append(&mut contents_clone);
stream.write(unpacked_response.as_slice()).unwrap();
stream.write(unpack_response(response_constructed).as_bytes()).unwrap();
stream.flush().unwrap();
});
}