remove rust stuff

This commit is contained in:
2025-05-21 21:43:39 +02:00
parent 1f467994f7
commit 684306c25c
4 changed files with 0 additions and 679 deletions

12
.gitignore vendored
View File

@ -1,12 +0,0 @@
# ---> Rust
# Generated by Cargo
# will have compiled files and executables
debug/
target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
# Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk

58
Cargo.lock generated
View File

@ -1,58 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "http_server"
version = "0.1.0"
dependencies = [
"mime_guess",
"signal-hook",
]
[[package]]
name = "libc"
version = "0.2.171"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6"
[[package]]
name = "mime"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
[[package]]
name = "mime_guess"
version = "2.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e"
dependencies = [
"mime",
"unicase",
]
[[package]]
name = "signal-hook"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8621587d4798caf8eb44879d42e56b9a93ea5dcd315a6487c357130095b62801"
dependencies = [
"libc",
"signal-hook-registry",
]
[[package]]
name = "signal-hook-registry"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1"
dependencies = [
"libc",
]
[[package]]
name = "unicase"
version = "2.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75b844d17643ee918803943289730bec8aac480150456169e647ed0b576ba539"

View File

@ -1,8 +0,0 @@
[package]
name = "http_server"
version = "0.1.0"
edition = "2021"
[dependencies]
signal-hook = "0.3.17"
mime_guess = "2.0.5"

View File

@ -1,601 +0,0 @@
use signal_hook::{consts::*, iterator::Signals};
use std::{
collections::HashMap,
error::Error,
fs::{self},
io::{BufRead, BufReader, Write},
net::{TcpListener, TcpStream},
path::PathBuf,
process::exit,
thread,
};
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
enum RequestMethods {
Null = -1, // This is only to initialise the struct
Get,
Head,
}
#[derive(Debug)]
struct StartLine {
method: RequestMethods,
target: String,
version: String,
}
impl StartLine {
pub fn new() -> Self {
Self {
method: RequestMethods::Null,
target: String::new(),
version: String::new(),
}
}
// https://datatracker.ietf.org/doc/html/rfc9110#name-methods
pub fn is_valid_method(method: &str) -> bool {
if method.trim().is_empty() {
return false;
}
// Only GET and HEAD are required, the rest is optional
["GET", "HEAD"].contains(&method.trim())
}
// TODO: make the checks less shit and actually correct
// https://datatracker.ietf.org/doc/html/rfc9112#name-request-target
pub fn is_valid_target(target: &str) -> bool {
if target.trim().is_empty() {
return false;
}
// origin-form
if target.starts_with("/") {
if target.contains("?") && target.split("?").count() != 2 {
return false;
}
return true;
}
// absolute-form
if target.starts_with("http://") {
return true;
}
// authority-form
if target.contains(":") && target.split(":").count() == 2 {
return true;
}
// asterisk-form
if target.trim() == "*" {
return true;
}
false
}
pub fn is_valid_version(version: &str) -> bool {
if version.trim().is_empty() {
return false;
}
let http_name = version.trim();
if http_name.starts_with("HTTP/") {
let version_numbers = http_name.trim_start_matches("HTTP/");
let version_numbers = version_numbers.split(".").collect::<Vec<&str>>();
if version_numbers.len() != 2 {
return false;
}
let major = match version_numbers[0].parse::<u8>() {
Ok(val) => val,
Err(_) => {
return false;
}
};
let minor = match version_numbers[1].parse::<u8>() {
Ok(val) => val,
Err(_) => {
return false;
}
};
return major <= 9 && minor <= 9;
}
false
}
}
fn parse_start_line(input: &str) -> Result<StartLine, Vec<u8>> {
let mut response_field_lines: HashMap<String, Vec<String>> = HashMap::new();
let mut response_body: Vec<u8> = vec![];
let mut start_line = StartLine::new();
let vec = input.trim().split_ascii_whitespace().collect::<Vec<&str>>();
if vec.len() != 3 {
format!(
"The start-line has an incorrect amount of items. Got the value: {}",
vec.len()
)
.as_bytes()
.iter()
.for_each(|byte| response_body.push(*byte));
response_field_lines.insert(
String::from("content-length"),
vec![response_body.len().to_string()],
);
response_field_lines.insert(
String::from("content-type"),
vec![String::from("text/plain")],
);
return Err(response_builder(
RequestMethods::Get,
"HTTP/1.1 400 ",
Some(response_field_lines),
Some(response_body),
));
}
let method = vec[0];
let target = vec[1];
let version = vec[2];
if !StartLine::is_valid_method(method) {
return Err(response_builder(
RequestMethods::Head,
"HTTP/1.1 501 ",
None,
None,
));
}
if !StartLine::is_valid_version(version) {
return Err(response_builder(
RequestMethods::Head,
"HTTP/1.1 400 ",
None,
None,
));
}
if version != "HTTP/1.1" && version != "HTTP/1.0" {
"Server only supports major version 1 of HTTP"
.as_bytes()
.iter()
.for_each(|byte| response_body.push(*byte));
response_field_lines.insert(
String::from("content-length"),
vec![response_body.len().to_string()],
);
response_field_lines.insert(
String::from("content-type"),
vec![String::from("text/plain")],
);
return Err(response_builder(
RequestMethods::Head,
"HTTP/1.1 505 ",
Some(response_field_lines),
Some(response_body),
));
}
if !StartLine::is_valid_target(target) {
return Err(response_builder(
RequestMethods::Head,
"HTTP/1.1 400 ",
None,
None,
));
}
// start_line.method will remain RequestMethods::NULL if it is not supported.
match method {
"GET" => start_line.method = RequestMethods::Get,
"HEAD" => start_line.method = RequestMethods::Head,
_ => start_line.method = RequestMethods::Null,
}
start_line.version = version.to_string();
start_line.target = target.to_string();
Ok(start_line)
}
// Example -> Accept: text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8
// Be careful of optional whitespace (OWS) and CRLF
// q=x.yyy is basically a value which says, what the client wants most.
// The closer to 1 the better. If no q=x.yyy was specified, then q=1 is implied.
// This basically mean I should sort the vector by qvalue. So that the one it wants most, is at the beginning.
fn parse_field_line(field_line: (&str, &str)) -> (String, Vec<String>) {
let field_name = field_line.0.to_ascii_lowercase();
let mut fuck: HashMap<String, Vec<String>> = HashMap::new();
if !field_line.1.contains(",") {
return (field_name, vec![field_line.1.trim().to_owned()]);
}
let temp_field_values = field_line.1.split(",").collect::<Vec<&str>>();
for field_value in temp_field_values {
if field_value.contains(";") {
let temp_field_value = field_value.split(";").collect::<Vec<&str>>();
if temp_field_value.len() != 2 {
// TODO: return with an http_response
continue;
}
if !temp_field_value[1].starts_with("q=") && !temp_field_value[1].starts_with("Q=") {
// TODO: return with an http_response
continue;
}
let qvalue = temp_field_value[1]
.trim()
.trim_start_matches("q=")
.trim_start_matches("Q=");
if qvalue == "0" {
continue;
}
if let Some(val) = fuck.get_mut(qvalue) {
val.push(temp_field_value[0].trim().to_owned());
continue;
}
fuck.insert(
qvalue.to_owned(),
vec![temp_field_value[0].trim().to_owned()],
);
} else {
if let Some(val) = fuck.get_mut("1") {
val.push(field_value.trim().to_owned());
continue;
}
fuck.insert("1".to_owned(), vec![field_value.trim().to_owned()]);
}
}
let fuck_keys = fuck.keys();
let mut keys = fuck_keys.collect::<Vec<&String>>();
keys.sort_by(|a, b| b.cmp(a));
let mut field_values: Vec<String> = vec![];
for key in keys {
if let Some(value) = fuck.get(key) {
value.iter().for_each(|val| field_values.push(val.clone()));
}
}
(field_name, field_values)
}
fn parse_field_lines(
reader: &mut BufReader<&mut TcpStream>,
) -> Result<HashMap<String, Vec<String>>, Vec<u8>> {
let mut response_field_lines: HashMap<String, Vec<String>> = HashMap::new();
let mut response_body: Vec<u8> = vec![];
let mut field_lines: HashMap<String, Vec<String>> = HashMap::new();
// Read field-lines till I hit an empty line
loop {
let mut line = String::new();
reader.read_line(&mut line).unwrap();
if !line.ends_with("\r\n") {
"Lines need to end with a CRLF"
.as_bytes()
.iter()
.for_each(|byte| response_body.push(*byte));
response_field_lines.insert(
String::from("content-length"),
vec![response_body.len().to_string()],
);
response_field_lines.insert(
String::from("content-type"),
vec![String::from("text/plain")],
);
return Err(response_builder(
RequestMethods::Get,
"HTTP/1.1 400 ",
Some(response_field_lines),
Some(response_body),
));
}
if line.trim().is_empty() {
break;
}
let field_line: (String, Vec<String>) = match line.split_once(":") {
Some(val) => parse_field_line(val),
None => {
"Invalid field-line"
.as_bytes()
.iter()
.for_each(|byte| response_body.push(*byte));
response_field_lines.insert(
String::from("content-length"),
vec![response_body.len().to_string()],
);
response_field_lines.insert(
String::from("content-type"),
vec![String::from("text/plain")],
);
return Err(response_builder(
RequestMethods::Get,
"HTTP/1.1 400 ",
Some(response_field_lines),
Some(response_body),
));
}
};
field_lines.insert(field_line.0, field_line.1);
}
if !field_lines.contains_key(&String::from("host")) {
"field-line with field-name -> host is missing"
.as_bytes()
.iter()
.for_each(|byte| response_body.push(*byte));
response_field_lines.insert(
String::from("content-length"),
vec![response_body.len().to_string()],
);
response_field_lines.insert(
String::from("content-type"),
vec![String::from("text/plain")],
);
return Err(response_builder(
RequestMethods::Get,
"HTTP/1.1 400 ",
Some(response_field_lines),
Some(response_body),
));
}
Ok(field_lines)
}
fn response_builder(
method: RequestMethods,
status_line: &str,
field_lines: Option<HashMap<String, Vec<String>>>,
body: Option<Vec<u8>>,
) -> Vec<u8> {
let mut response: Vec<u8> = vec![];
status_line
.as_bytes()
.iter()
.for_each(|byte| response.push(*byte));
response.push(b'\r');
response.push(b'\n');
if let Some(val) = field_lines {
for field_line in val.iter() {
field_line
.0
.as_bytes()
.iter()
.for_each(|byte| response.push(*byte));
response.push(b':');
response.push(b' ');
for val in field_line.1 {
val.as_bytes().iter().for_each(|byte| response.push(*byte));
}
response.push(b'\r');
response.push(b'\n');
}
}
// Mandatory empty line between header and body
response.push(b'\r');
response.push(b'\n');
if method != RequestMethods::Head {
if let Some(val) = body {
val.iter().for_each(|byte| response.push(*byte));
response.push(b'\r');
response.push(b'\n');
}
}
response
}
fn try_get_file(start_line: &StartLine, field_lines: &HashMap<String, Vec<String>>) -> Vec<u8> {
let mut response_field_lines: HashMap<String, Vec<String>> = HashMap::new();
let mut response_body: Vec<u8> = vec![];
let path: PathBuf = match start_line.target.as_str() {
"/" => PathBuf::from("/www/index.html"),
_ => PathBuf::from(format!("/www{}", start_line.target)),
};
match fs::read(&path) {
Ok(val) => {
val.iter().for_each(|byte| response_body.push(*byte));
response_field_lines.insert(
String::from("content-length"),
vec![response_body.len().to_string()],
);
let mime_type = mime_guess::from_path(&path)
.first()
.expect("Could not guess mime-type from path");
if let Some(vector) = field_lines.get("accept") {
for value in vector {
if mime_type.to_string() == *value {
response_field_lines
.insert(String::from("content-type"), vec![mime_type.to_string()]);
}
if format!("{}/*", mime_type.type_()) == *value {
response_field_lines
.insert(String::from("content-type"), vec![mime_type.to_string()]);
}
if "*/*" == *value {
response_field_lines
.insert(String::from("content-type"), vec![mime_type.to_string()]);
}
}
} else {
response_field_lines
.insert(String::from("content-type"), vec![mime_type.to_string()]);
}
if !response_field_lines.contains_key("content-type") {
// TODO: make better according to https://datatracker.ietf.org/doc/html/rfc9110#status.406
return response_builder(start_line.method, "HTTP/1.1 406 ", None, None);
}
response_builder(
start_line.method,
"HTTP/1.1 200 ",
Some(response_field_lines),
Some(response_body),
)
}
Err(_) => response_builder(start_line.method, "HTTP/1.1 404 ", None, None),
}
}
fn try_get_file_internal(start_line: &StartLine) -> Vec<u8> {
let mut response_field_lines: HashMap<String, Vec<String>> = HashMap::new();
let mut response_body: Vec<u8> = vec![];
let path: PathBuf = PathBuf::from(format!("/internal{}.html", start_line.target));
match fs::read(&path) {
Ok(val) => {
val.iter().for_each(|byte| response_body.push(*byte));
response_field_lines.insert(
String::from("content-length"),
vec![response_body.len().to_string()],
);
let mime_type = mime_guess::from_path(&path)
.first()
.expect("Could not guess mime-type from path");
response_field_lines.insert(String::from("content-type"), vec![mime_type.to_string()]);
response_builder(
start_line.method,
"HTTP/1.1 200 ",
Some(response_field_lines),
Some(response_body),
)
}
Err(_) => response_builder(start_line.method, "HTTP/1.1 404 ", None, None),
}
}
fn handle_request(mut stream: TcpStream) -> Result<(), Box<dyn Error>> {
let mut line = String::new();
let mut reader = BufReader::new(&mut stream);
let mut response_field_lines: HashMap<String, Vec<String>> = HashMap::new();
let mut response_body: Vec<u8> = vec![];
// Request can have one or many empty lines preceding the start-line and I will ignore these
loop {
if reader.read_line(&mut line)? > 2 && line != "\r\n" {
break;
}
}
if line.ends_with(" ") {
"There is whitespace between the start-line and the first field-line"
.as_bytes()
.iter()
.for_each(|byte| response_body.push(*byte));
response_field_lines.insert(
String::from("content-length"),
vec![response_body.len().to_string()],
);
response_field_lines.insert(
String::from("content-type"),
vec![String::from("text/plain")],
);
let response = response_builder(
RequestMethods::Get,
"HTTP/1.1 400 ",
Some(response_field_lines),
Some(response_body),
);
stream.write_all(&response)?;
return Ok(());
}
let start_line = match parse_start_line(&line) {
Ok(val) => val,
Err(response) => {
stream.write_all(&response)?;
return Ok(());
}
};
// dbg!(&start_line);
let field_lines = match parse_field_lines(&mut reader) {
Ok(val) => val,
Err(response) => {
stream.write_all(&response)?;
return Ok(());
}
};
// dbg!(&field_lines);
// let mut request_body: Vec<u8> = vec![];
// reader.read_to_end(&mut request_body)?;
let response = match start_line.target.as_str() {
// For docker healtcheck. If the server can properly respond, then it must be healthy.
"/server-health" => response_builder(RequestMethods::Head, "HTTP/1.1 200 ", None, None),
"/server-stats" => try_get_file_internal(&start_line),
"/server-info" => try_get_file_internal(&start_line),
_ => try_get_file(&start_line, &field_lines),
};
stream.write_all(&response)?;
Ok(())
}
fn main() -> Result<(), Box<dyn Error>> {
let mut signals = Signals::new([SIGINT, SIGTERM])?;
// TODO: Gracefully shutdown server
thread::spawn(move || {
if let Some(sig) = signals.forever().next() {
println!("Received signal {:?}", sig);
println!("Shutting down");
exit(1);
}
});
let listener = TcpListener::bind("0.0.0.0:8080")?;
println!("Server started");
for stream in listener.incoming() {
let stream = stream?;
handle_request(stream)?;
}
Ok(())
}