Compare commits

...

3 Commits

2 changed files with 32 additions and 35 deletions

View File

@ -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,12 +21,13 @@ 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
EXPOSE 8080

View File

@ -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,
@ -315,34 +315,24 @@ fn response_builder(
return response;
}
fn act_upon_request(
start_line: StartLine,
_field_lines: HashMap<String, String>,
_request_body: Vec<u8>,
) -> Result<Vec<u8>, Box<dyn Error>> {
fn try_get_file(start_line: &StartLine, _field_lines: &HashMap<String, String>) -> Vec<u8> {
let mut response_field_lines: HashMap<String, 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)),
};
// TODO: Check if wanted file is contained in the optional Accept field-line
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Accept for reference
// let mime_type = match MimeGuess::from_path(&path).first_raw() {
// Some(val) => val,
// _ => {
// return response_builder(RequestMethods::HEAD, "HTTP/1.1 500 ", None, None);
// }
// }
match fs::read(&path) {
Ok(val) => {
val.iter().for_each(|byte| response_body.push(*byte));
@ -351,25 +341,21 @@ fn act_upon_request(
String::from("Content-Length"),
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(
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 handle_request(mut stream: TcpStream) -> Result<(), Box<dyn Error>> {
@ -422,11 +408,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" => response_builder(start_line.method, "HTTP/1.1 404 ", None, None),
"/server-info" => response_builder(start_line.method, "HTTP/1.1 404 ", None, None),
_ => try_get_file(&start_line, &field_lines),
};
stream.write_all(&response)?;
Ok(())