add support for sending data other than text

This commit is contained in:
abbie 2024-01-14 19:38:38 +00:00
parent a0b37387eb
commit 245131ee27
Signed by: threeoh6000
GPG key ID: 801FE4AD456E922C

View file

@ -1,18 +1,24 @@
use std::io::{Write, BufReader, BufRead};
use std::io::{Write, BufReader, BufRead, Read};
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: String,
contents: Vec<u8>,
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
@ -138,7 +144,7 @@ fn generate_index(directory: String) -> String {
}
fn get_page(filename: String) -> String {
fn get_page(filename: String) -> GetPageResult {
// The loaded page should be left immutable as it does
// not need to be modified by the server.
@ -147,28 +153,44 @@ fn get_page(filename: String) -> String {
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);
return result.to_string();
resultstruct.contents = result.to_string().into_bytes();
return resultstruct;
} else if path.contains(".cgi?") {
let result = process_cgi(filename);
return result.to_string();
resultstruct.contents = result.to_string().into_bytes();
return resultstruct;
}
}
if filename.contains(".cgi") {
let result = process_cgi(filename);
return result.to_string();
resultstruct.contents = result.to_string().into_bytes();
return resultstruct;
} else {
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(),
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;
}
}
}
@ -199,7 +221,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(), 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().into_bytes(), status_code: 404, mime: "text/html".to_string() };
return resource;
} else {
let dir = check_if_dir(path.clone());
@ -215,14 +237,15 @@ 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(), 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().into_bytes(), 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 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") {
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 {
resource.status_code = 500;
resource.mime = "text/html".to_string();
}
@ -241,7 +264,7 @@ fn serve(mut stream: TcpStream) {
let resource = process_request(request);
let mut response_constructed = generate_response(resource.status_code, &resource.contents);
let mut response_constructed = generate_response(resource.status_code, "");
response_constructed.headers.push(generate_kvheader("Content-Type", &resource.mime));
response_constructed.headers.push(generate_kvheader("Server", &format!("herb/{}", env!("CARGO_PKG_VERSION"))));
@ -249,7 +272,11 @@ fn serve(mut stream: TcpStream) {
response_constructed.headers.push(generate_kvheader("Date", &grab_time()))
}
stream.write(unpack_response(response_constructed).as_bytes()).unwrap();
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.flush().unwrap();
});
}