0.5.1: use packeteer for requests and fix directory index generation
This commit is contained in:
parent
bbb90c6fdd
commit
bb880abb17
3 changed files with 22 additions and 53 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -4,7 +4,7 @@ version = 3
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "herb"
|
name = "herb"
|
||||||
version = "0.4.2"
|
version = "0.5.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"packeteer",
|
"packeteer",
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "herb"
|
name = "herb"
|
||||||
version = "0.5.0"
|
version = "0.5.1"
|
||||||
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
|
||||||
|
|
59
src/main.rs
59
src/main.rs
|
@ -5,7 +5,7 @@ use std::fs;
|
||||||
use std::string::{String};
|
use std::string::{String};
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
use packeteer::http1::*;
|
use packeteer::http1::*;
|
||||||
use packeteer::generate_kvheader;
|
use packeteer::{generate_kvheader, unwrap_url_into_segments};
|
||||||
|
|
||||||
struct Resource {
|
struct Resource {
|
||||||
contents: String,
|
contents: String,
|
||||||
|
@ -129,9 +129,9 @@ fn check_if_dir(directory: String) -> bool {
|
||||||
|
|
||||||
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/>", 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!("{}<br/><a href={}>{:#?}</a>", index, format!("\"./{}\"", file.as_ref().unwrap().path().display().to_string()), file.unwrap().file_name());
|
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/>Generated by herb {}", index, env!("CARGO_PKG_VERSION")).to_string();
|
return format!("{}<hr/>Generated by herb {}", index, env!("CARGO_PKG_VERSION")).to_string();
|
||||||
|
@ -183,59 +183,38 @@ fn grab_time() -> String{
|
||||||
|
|
||||||
|
|
||||||
fn process_request(request: Vec<u8>) -> Resource {
|
fn process_request(request: Vec<u8>) -> Resource {
|
||||||
let mut input = String::from_utf8_lossy(&request).to_string();
|
let input = String::from_utf8_lossy(&request).to_string();
|
||||||
|
let request = construct_request(&input);
|
||||||
let debug = false;
|
let mut path: String;
|
||||||
|
|
||||||
let mut index = String::new();
|
let mut index = String::new();
|
||||||
let output;
|
let output;
|
||||||
if input.contains("GET") {
|
|
||||||
// To-do: find a less atrocious way to do this.
|
if &request.method == "GET" {
|
||||||
println!("Stream sent GET request.");
|
println!("Stream sent GET request.");
|
||||||
|
|
||||||
if debug { println!("{}", input); }
|
if request.location.segments.len() != 0 {
|
||||||
input = input.replace("GET ", "");
|
path = unwrap_url_into_segments(request.location);
|
||||||
input = input.replace(" HTTP/1.1\r\n", "");
|
path.remove(0);
|
||||||
|
let exists = check_if_path_exists(path.clone());
|
||||||
// Lynx also made me do this
|
|
||||||
input = input.replace(" HTTP/1.0\r\n", "");
|
|
||||||
|
|
||||||
// Theoretically by this point, the request
|
|
||||||
// will have been cut down to just the
|
|
||||||
// requested resource, but in my experience
|
|
||||||
// this code is gonna result in like 50k errors
|
|
||||||
// and I'm gonna have to rewrite it to get it
|
|
||||||
// to actually work the way I want it to.
|
|
||||||
|
|
||||||
if input != "/" {
|
|
||||||
let mut chars = input.chars();
|
|
||||||
chars.next();
|
|
||||||
let path = chars.as_str().to_string();
|
|
||||||
let exists = check_if_path_exists(path);
|
|
||||||
let path = chars.as_str().to_string();
|
|
||||||
|
|
||||||
if exists == false && path.contains(".cgi?") == false {
|
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(), status_code: 404, mime: "text/html".to_string() };
|
||||||
return resource;
|
return resource;
|
||||||
} else {
|
} else {
|
||||||
let path = chars.as_str().to_string();
|
let dir = check_if_dir(path.clone());
|
||||||
let dir = check_if_dir(path);
|
|
||||||
if dir == true {
|
if dir == true {
|
||||||
let path = chars.as_str().to_string();
|
|
||||||
index += &path;
|
index += &path;
|
||||||
index += "/index.html";
|
index += "/index.html";
|
||||||
output = index.as_str().clone();
|
output = index.as_str().clone();
|
||||||
} else {
|
} else {
|
||||||
output = chars.as_str();
|
output = path.as_str().clone();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
output = "index.html";
|
output = "index.html";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// It's get_page()'s problem now.
|
|
||||||
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(), status_code: 501, mime: "text/html".to_string() };
|
||||||
println!("Stream sent unhandlable request.");
|
println!("Stream sent unhandlable request.");
|
||||||
return resource;
|
return resource;
|
||||||
|
@ -262,24 +241,14 @@ fn serve(mut stream: TcpStream) {
|
||||||
|
|
||||||
let resource = process_request(request);
|
let resource = process_request(request);
|
||||||
|
|
||||||
// let contents = get_page(resource.clone());
|
|
||||||
// let header = "HTTP/1.1 200 OK\r\n";
|
|
||||||
// let content_type = format!("Content-Type: {}\r\n", mime);
|
|
||||||
// let server = format!("Server: herb/{}\r\n", env!("CARGO_PKG_VERSION"));
|
|
||||||
// let extra_fields;
|
|
||||||
|
|
||||||
let mut response_constructed = generate_response(resource.status_code, &resource.contents);
|
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("Content-Type", &resource.mime));
|
||||||
response_constructed.headers.push(generate_kvheader("Server", &format!("herb/{}", env!("CARGO_PKG_VERSION"))));
|
response_constructed.headers.push(generate_kvheader("Server", &format!("herb/{}", env!("CARGO_PKG_VERSION"))));
|
||||||
|
|
||||||
if cfg!(unix) {
|
if cfg!(unix) {
|
||||||
response_constructed.headers.push(generate_kvheader("Date", &grab_time()))
|
response_constructed.headers.push(generate_kvheader("Date", &grab_time()))
|
||||||
} else {
|
|
||||||
// I don't have a Windows or macOS box to test anything on
|
|
||||||
// which means others are gonna have to deal with it :/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("{:#?}", response_constructed);
|
|
||||||
stream.write(unpack_response(response_constructed).as_bytes()).unwrap();
|
stream.write(unpack_response(response_constructed).as_bytes()).unwrap();
|
||||||
stream.flush().unwrap();
|
stream.flush().unwrap();
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue