add some stuff its been a while

This commit is contained in:
abbie 2024-04-26 00:07:14 +01:00
parent 3c29bbce60
commit c20ab29d0b
Signed by: threeoh6000
GPG key ID: 801FE4AD456E922C
2 changed files with 15 additions and 6 deletions

View file

@ -1,7 +1,7 @@
[package] [package]
name = "herb" name = "herb"
version = "0.9.0-develop" version = "0.9.0-develop"
edition = "2018" edition = "2021"
[dependencies] [dependencies]
frostwalker = "0.1.1" frostwalker = "0.1.1"

View file

@ -37,7 +37,7 @@ struct Settings {
impl Settings { impl Settings {
fn new() -> Settings { fn new() -> Settings {
return Settings { cgi: true, index_generation: true, address: "0.0.0.0:8080".to_string(), logging: false, server_header: true, time_header: false}; return Settings { cgi: true, index_generation: true, address: "0.0.0.0:8080".to_string(), logging: false, server_header: false, time_header: false};
} }
} }
@ -263,14 +263,14 @@ fn check_if_dir(directory: String) -> bool {
return result; return result;
} }
fn generate_index(directory: String) -> String { fn generate_index(directory: String, ) -> String {
let mut index = format!("<!DOCTYPE HTML><html><body><h1>Directory of {}</h1><hr/><a href=\"/{}/../\">Parent Directory</a><br/>", directory, directory); let mut index = format!("<!DOCTYPE HTML><html><body><h1>Directory of {}</h1><hr/><a href=\"/{}/../\">Parent Directory</a><br/>", directory, directory);
for file in fs::read_dir(directory).unwrap() { for file in fs::read_dir(directory).unwrap() {
index = format!("{}<a href={}>{}</a><br/>", index, format!("\"/{}\"", file.as_ref().unwrap().path().display().to_string()), file.unwrap().file_name().into_string().unwrap_or("!!".to_string())); index = format!("{}<a href={}>{}</a><br/>", index, format!("\"/{}\"", file.as_ref().unwrap().path().display().to_string()), file.unwrap().file_name().into_string().unwrap_or("!!".to_string()));
} }
return format!("{}<hr/><i>Generated by herb/{}</i>", index, env!("CARGO_PKG_VERSION")).to_string(); return format!("{}<hr/><i>Generated by herb/{} ({})</i>", index, env!("CARGO_PKG_VERSION"), std::env::consts::OS).to_string();
} }
@ -553,9 +553,11 @@ fn serve(mut stream: TcpStream, settings: Settings) {
let mut response_constructed = generate_response(resource.status_code, ""); 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("Content-Type", &resource.mime));
response_constructed.headers.push(generate_kvheader("Server", &format!("herb/{}", env!("CARGO_PKG_VERSION")))); if settings.server_header {
response_constructed.headers.push(generate_kvheader("Server", &format!("herb/{}", env!("CARGO_PKG_VERSION"))));
}
if cfg!(unix) { if cfg!(unix) && settings.time_header {
response_constructed.headers.push(generate_kvheader("Date", &grab_time())) response_constructed.headers.push(generate_kvheader("Date", &grab_time()))
} }
@ -609,6 +611,13 @@ fn process_settings() -> Settings {
settings.address = hashmap.get("address").unwrap_or(&"0.0.0.0:8080".to_string()).to_string(); settings.address = hashmap.get("address").unwrap_or(&"0.0.0.0:8080".to_string()).to_string();
} }
if hashmap.get("time_header").is_some() {
settings.time_header = hashmap.get("time_header").unwrap_or(&"true".to_string()).parse::<bool>().unwrap_or(true);
}
if hashmap.get("server_header").is_some() {
settings.server_header = hashmap.get("server_header").unwrap_or(&"true".to_string()).parse::<bool>().unwrap_or(true);
}
return settings; return settings;
} }