Compare commits
10 Commits
046c8d1784
...
main
Author | SHA1 | Date | |
---|---|---|---|
1f467994f7
|
|||
7853b4d2df
|
|||
cf48dc3e1c
|
|||
d5b4dcedd6
|
|||
0fd7795ace
|
|||
6472b0a278
|
|||
bb2e683f20
|
|||
26712c8433
|
|||
30508bd5a9
|
|||
b71743ff3f
|
@ -1,5 +1,8 @@
|
||||
FROM rust:1.85-slim-bookworm AS build
|
||||
|
||||
# get x86_64-unknown-linux-musl as a build target
|
||||
RUN rustup target add x86_64-unknown-linux-musl
|
||||
|
||||
# create a new empty shell project
|
||||
RUN USER=root cargo new --bin http_server
|
||||
WORKDIR /http_server
|
||||
@ -18,13 +21,15 @@ COPY ./src ./src
|
||||
# build for release as a static-pie linked binary
|
||||
RUN rm ./target/release/deps/http_server*
|
||||
ENV RUSTFLAGS='-C target-feature=+crt-static'
|
||||
RUN cargo build --release --target x86_64-unknown-linux-gnu
|
||||
RUN cargo build --release --target x86_64-unknown-linux-musl
|
||||
RUN strip /http_server/target/x86_64-unknown-linux-musl/release/http_server
|
||||
|
||||
FROM scratch
|
||||
|
||||
# copy the build artifact from the build stage
|
||||
COPY --from=build /http_server/target/x86_64-unknown-linux-gnu/release/http_server /
|
||||
COPY --from=build /http_server/target/x86_64-unknown-linux-musl/release/http_server /
|
||||
COPY ./www /www
|
||||
COPY ./internal /internal
|
||||
|
||||
EXPOSE 8080
|
||||
|
||||
|
13
internal/server-info.html
Normal file
13
internal/server-info.html
Normal file
@ -0,0 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>server-info</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Server written by AustrianToast</h1>
|
||||
<a href="https://gitea.hopeless-cloud.xyz/AustrianToast/http_server">Source code</a>
|
||||
<br>
|
||||
<a href="mailto:austriantoast@hopeless-cloud.xyz">AustrianToasts e-mail</a>
|
||||
</body>
|
||||
</html>
|
388
src/main.rs
388
src/main.rs
@ -3,7 +3,7 @@ use std::{
|
||||
collections::HashMap,
|
||||
error::Error,
|
||||
fs::{self},
|
||||
io::{BufRead, BufReader, Read, Write},
|
||||
io::{BufRead, BufReader, Write},
|
||||
net::{TcpListener, TcpStream},
|
||||
path::PathBuf,
|
||||
process::exit,
|
||||
@ -12,9 +12,9 @@ use std::{
|
||||
|
||||
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
|
||||
enum RequestMethods {
|
||||
NULL = -1, // This is only to initialise the struct
|
||||
GET,
|
||||
HEAD,
|
||||
Null = -1, // This is only to initialise the struct
|
||||
Get,
|
||||
Head,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@ -27,7 +27,7 @@ struct StartLine {
|
||||
impl StartLine {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
method: RequestMethods::NULL,
|
||||
method: RequestMethods::Null,
|
||||
target: String::new(),
|
||||
version: String::new(),
|
||||
}
|
||||
@ -73,7 +73,7 @@ impl StartLine {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
false
|
||||
}
|
||||
|
||||
pub fn is_valid_version(version: &str) -> bool {
|
||||
@ -102,19 +102,15 @@ impl StartLine {
|
||||
}
|
||||
};
|
||||
|
||||
if major <= 9 && minor <= 9 {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
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, String> = HashMap::new();
|
||||
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>>();
|
||||
@ -129,13 +125,16 @@ fn parse_start_line(input: &str) -> Result<StartLine, Vec<u8>> {
|
||||
.for_each(|byte| response_body.push(*byte));
|
||||
|
||||
response_field_lines.insert(
|
||||
String::from("Content-Length"),
|
||||
response_body.len().to_string(),
|
||||
String::from("content-length"),
|
||||
vec![response_body.len().to_string()],
|
||||
);
|
||||
response_field_lines.insert(
|
||||
String::from("content-type"),
|
||||
vec![String::from("text/plain")],
|
||||
);
|
||||
response_field_lines.insert(String::from("Content-Type"), String::from("text/plain"));
|
||||
|
||||
return Err(response_builder(
|
||||
RequestMethods::GET,
|
||||
RequestMethods::Get,
|
||||
"HTTP/1.1 400 ",
|
||||
Some(response_field_lines),
|
||||
Some(response_body),
|
||||
@ -146,40 +145,138 @@ fn parse_start_line(input: &str) -> Result<StartLine, Vec<u8>> {
|
||||
let target = vec[1];
|
||||
let version = vec[2];
|
||||
|
||||
if StartLine::is_valid_method(&method)
|
||||
&& StartLine::is_valid_target(&target)
|
||||
&& StartLine::is_valid_version(&version)
|
||||
{
|
||||
// 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.target = target.to_string();
|
||||
|
||||
if version == "HTTP/1.1" || version == "HTTP/1.0" {
|
||||
start_line.version = version.to_string();
|
||||
}
|
||||
} else {
|
||||
if !StartLine::is_valid_method(method) {
|
||||
return Err(response_builder(
|
||||
RequestMethods::HEAD,
|
||||
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, String>, Vec<u8>> {
|
||||
let mut response_field_lines: HashMap<String, String> = HashMap::new();
|
||||
) -> 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, String> = HashMap::new();
|
||||
let mut field_lines: HashMap<String, Vec<String>> = HashMap::new();
|
||||
|
||||
// Read field-lines till I hit an empty line
|
||||
loop {
|
||||
@ -187,18 +284,22 @@ fn parse_field_lines(
|
||||
reader.read_line(&mut line).unwrap();
|
||||
|
||||
if !line.ends_with("\r\n") {
|
||||
b"Lines need to end with a CRLF"
|
||||
"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"),
|
||||
response_body.len().to_string(),
|
||||
String::from("content-length"),
|
||||
vec![response_body.len().to_string()],
|
||||
);
|
||||
response_field_lines.insert(
|
||||
String::from("content-type"),
|
||||
vec![String::from("text/plain")],
|
||||
);
|
||||
response_field_lines.insert(String::from("Content-Type"), String::from("text/plain"));
|
||||
|
||||
return Err(response_builder(
|
||||
RequestMethods::GET,
|
||||
RequestMethods::Get,
|
||||
"HTTP/1.1 400 ",
|
||||
Some(response_field_lines),
|
||||
Some(response_body),
|
||||
@ -209,22 +310,25 @@ fn parse_field_lines(
|
||||
break;
|
||||
}
|
||||
|
||||
let field_line = match line.split_once(":") {
|
||||
Some(val) => val,
|
||||
let field_line: (String, Vec<String>) = match line.split_once(":") {
|
||||
Some(val) => parse_field_line(val),
|
||||
None => {
|
||||
b"Invalid field-line"
|
||||
"Invalid field-line"
|
||||
.as_bytes()
|
||||
.iter()
|
||||
.for_each(|byte| response_body.push(*byte));
|
||||
|
||||
response_field_lines.insert(
|
||||
String::from("Content-Length"),
|
||||
response_body.len().to_string(),
|
||||
String::from("content-length"),
|
||||
vec![response_body.len().to_string()],
|
||||
);
|
||||
response_field_lines.insert(
|
||||
String::from("content-type"),
|
||||
vec![String::from("text/plain")],
|
||||
);
|
||||
response_field_lines
|
||||
.insert(String::from("Content-Type"), String::from("text/plain"));
|
||||
|
||||
return Err(response_builder(
|
||||
RequestMethods::GET,
|
||||
RequestMethods::Get,
|
||||
"HTTP/1.1 400 ",
|
||||
Some(response_field_lines),
|
||||
Some(response_body),
|
||||
@ -232,35 +336,39 @@ fn parse_field_lines(
|
||||
}
|
||||
};
|
||||
|
||||
field_lines.insert(field_line.0.to_owned(), field_line.1.trim().to_owned());
|
||||
field_lines.insert(field_line.0, field_line.1);
|
||||
}
|
||||
|
||||
if !field_lines.contains_key(&String::from("Host")) {
|
||||
b"field-line with key HOST is missing"
|
||||
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"),
|
||||
response_body.len().to_string(),
|
||||
String::from("content-length"),
|
||||
vec![response_body.len().to_string()],
|
||||
);
|
||||
response_field_lines.insert(
|
||||
String::from("content-type"),
|
||||
vec![String::from("text/plain")],
|
||||
);
|
||||
response_field_lines.insert(String::from("Content-Type"), String::from("text/plain"));
|
||||
|
||||
return Err(response_builder(
|
||||
RequestMethods::GET,
|
||||
RequestMethods::Get,
|
||||
"HTTP/1.1 400 ",
|
||||
Some(response_field_lines),
|
||||
Some(response_body),
|
||||
));
|
||||
}
|
||||
|
||||
return Ok(field_lines);
|
||||
Ok(field_lines)
|
||||
}
|
||||
|
||||
fn response_builder(
|
||||
method: RequestMethods,
|
||||
status_line: &str,
|
||||
field_lines: Option<HashMap<String, String>>,
|
||||
field_lines: Option<HashMap<String, Vec<String>>>,
|
||||
body: Option<Vec<u8>>,
|
||||
) -> Vec<u8> {
|
||||
let mut response: Vec<u8> = vec![];
|
||||
@ -272,8 +380,7 @@ fn response_builder(
|
||||
response.push(b'\r');
|
||||
response.push(b'\n');
|
||||
|
||||
match field_lines {
|
||||
Some(val) => {
|
||||
if let Some(val) = field_lines {
|
||||
for field_line in val.iter() {
|
||||
field_line
|
||||
.0
|
||||
@ -284,60 +391,33 @@ fn response_builder(
|
||||
response.push(b':');
|
||||
response.push(b' ');
|
||||
|
||||
field_line
|
||||
.1
|
||||
.as_bytes()
|
||||
.iter()
|
||||
.for_each(|byte| response.push(*byte));
|
||||
for val in field_line.1 {
|
||||
val.as_bytes().iter().for_each(|byte| response.push(*byte));
|
||||
}
|
||||
|
||||
response.push(b'\r');
|
||||
response.push(b'\n');
|
||||
}
|
||||
}
|
||||
None => (),
|
||||
}
|
||||
|
||||
// Mandatory empty line between header and body
|
||||
response.push(b'\r');
|
||||
response.push(b'\n');
|
||||
|
||||
if method != RequestMethods::HEAD {
|
||||
match body {
|
||||
Some(val) => {
|
||||
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');
|
||||
}
|
||||
None => (),
|
||||
}
|
||||
}
|
||||
|
||||
return response;
|
||||
response
|
||||
}
|
||||
|
||||
fn act_upon_request(
|
||||
start_line: StartLine,
|
||||
_field_lines: HashMap<String, String>,
|
||||
_request_body: Vec<u8>,
|
||||
) -> Result<Vec<u8>, Box<dyn Error>> {
|
||||
let mut response_field_lines: HashMap<String, String> = HashMap::new();
|
||||
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 response: Vec<u8>;
|
||||
let special_paths = ["/server-health", "/server-stats", "/server-info"];
|
||||
|
||||
if special_paths.contains(&start_line.target.as_str()) {
|
||||
match start_line.target.as_str() {
|
||||
"/server-health" => {
|
||||
response = response_builder(start_line.method, "HTTP/1.1 200 ", None, None);
|
||||
}
|
||||
_ => {
|
||||
response = response_builder(start_line.method, "HTTP/1.1 404 ", None, None);
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(response);
|
||||
}
|
||||
|
||||
let path: PathBuf = match start_line.target.as_str() {
|
||||
"/" => PathBuf::from("/www/index.html"),
|
||||
_ => PathBuf::from(format!("/www{}", start_line.target)),
|
||||
@ -348,34 +428,87 @@ fn act_upon_request(
|
||||
val.iter().for_each(|byte| response_body.push(*byte));
|
||||
|
||||
response_field_lines.insert(
|
||||
String::from("Content-Length"),
|
||||
response_body.len().to_string(),
|
||||
String::from("content-length"),
|
||||
vec![response_body.len().to_string()],
|
||||
);
|
||||
// TODO: get mime-type of file and use that here
|
||||
let mime_type = mime_guess::from_path(&path)
|
||||
.first_raw()
|
||||
.expect("Could not guess mime-type from path");
|
||||
response_field_lines.insert(String::from("Content-Type"), mime_type.to_string());
|
||||
|
||||
response = response_builder(
|
||||
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 = response_builder(start_line.method, "HTTP/1.1 404 ", None, None);
|
||||
Err(_) => response_builder(start_line.method, "HTTP/1.1 404 ", None, None),
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Ok(response)
|
||||
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, String> = HashMap::new();
|
||||
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
|
||||
@ -386,18 +519,22 @@ fn handle_request(mut stream: TcpStream) -> Result<(), Box<dyn Error>> {
|
||||
}
|
||||
|
||||
if line.ends_with(" ") {
|
||||
b"There is whitespace between the start-line and the first field-line"
|
||||
"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"),
|
||||
response_body.len().to_string(),
|
||||
String::from("content-length"),
|
||||
vec![response_body.len().to_string()],
|
||||
);
|
||||
response_field_lines.insert(
|
||||
String::from("content-type"),
|
||||
vec![String::from("text/plain")],
|
||||
);
|
||||
response_field_lines.insert(String::from("Content-Type"), String::from("text/plain"));
|
||||
|
||||
let response = response_builder(
|
||||
RequestMethods::GET,
|
||||
RequestMethods::Get,
|
||||
"HTTP/1.1 400 ",
|
||||
Some(response_field_lines),
|
||||
Some(response_body),
|
||||
@ -413,7 +550,7 @@ fn handle_request(mut stream: TcpStream) -> Result<(), Box<dyn Error>> {
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
dbg!(&start_line);
|
||||
// dbg!(&start_line);
|
||||
|
||||
let field_lines = match parse_field_lines(&mut reader) {
|
||||
Ok(val) => val,
|
||||
@ -422,11 +559,18 @@ fn handle_request(mut stream: TcpStream) -> Result<(), Box<dyn Error>> {
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
// dbg!(&field_lines);
|
||||
|
||||
let mut body: Vec<u8> = vec![];
|
||||
reader.read_to_end(&mut body)?;
|
||||
// let mut request_body: Vec<u8> = vec![];
|
||||
// reader.read_to_end(&mut request_body)?;
|
||||
|
||||
let response = act_upon_request(start_line, field_lines, 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(())
|
||||
@ -437,7 +581,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
|
||||
// TODO: Gracefully shutdown server
|
||||
thread::spawn(move || {
|
||||
for sig in signals.forever() {
|
||||
if let Some(sig) = signals.forever().next() {
|
||||
println!("Received signal {:?}", sig);
|
||||
println!("Shutting down");
|
||||
exit(1);
|
||||
|
Reference in New Issue
Block a user