Compare commits
3 Commits
046c8d1784
...
26712c8433
Author | SHA1 | Date | |
---|---|---|---|
26712c8433
|
|||
30508bd5a9
|
|||
b71743ff3f
|
@ -1,5 +1,8 @@
|
|||||||
FROM rust:1.85-slim-bookworm AS build
|
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
|
# create a new empty shell project
|
||||||
RUN USER=root cargo new --bin http_server
|
RUN USER=root cargo new --bin http_server
|
||||||
WORKDIR /http_server
|
WORKDIR /http_server
|
||||||
@ -18,12 +21,13 @@ COPY ./src ./src
|
|||||||
# build for release as a static-pie linked binary
|
# build for release as a static-pie linked binary
|
||||||
RUN rm ./target/release/deps/http_server*
|
RUN rm ./target/release/deps/http_server*
|
||||||
ENV RUSTFLAGS='-C target-feature=+crt-static'
|
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
|
FROM scratch
|
||||||
|
|
||||||
# copy the build artifact from the build stage
|
# 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 ./www /www
|
||||||
|
|
||||||
EXPOSE 8080
|
EXPOSE 8080
|
||||||
|
59
src/main.rs
59
src/main.rs
@ -3,7 +3,7 @@ use std::{
|
|||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
error::Error,
|
error::Error,
|
||||||
fs::{self},
|
fs::{self},
|
||||||
io::{BufRead, BufReader, Read, Write},
|
io::{BufRead, BufReader, Write},
|
||||||
net::{TcpListener, TcpStream},
|
net::{TcpListener, TcpStream},
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
process::exit,
|
process::exit,
|
||||||
@ -315,34 +315,24 @@ fn response_builder(
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn act_upon_request(
|
fn try_get_file(start_line: &StartLine, _field_lines: &HashMap<String, String>) -> Vec<u8> {
|
||||||
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();
|
let mut response_field_lines: HashMap<String, String> = HashMap::new();
|
||||||
let mut response_body: Vec<u8> = vec![];
|
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() {
|
let path: PathBuf = match start_line.target.as_str() {
|
||||||
"/" => PathBuf::from("/www/index.html"),
|
"/" => PathBuf::from("/www/index.html"),
|
||||||
_ => PathBuf::from(format!("/www{}", start_line.target)),
|
_ => 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) {
|
match fs::read(&path) {
|
||||||
Ok(val) => {
|
Ok(val) => {
|
||||||
val.iter().for_each(|byte| response_body.push(*byte));
|
val.iter().for_each(|byte| response_body.push(*byte));
|
||||||
@ -351,25 +341,21 @@ fn act_upon_request(
|
|||||||
String::from("Content-Length"),
|
String::from("Content-Length"),
|
||||||
response_body.len().to_string(),
|
response_body.len().to_string(),
|
||||||
);
|
);
|
||||||
// TODO: get mime-type of file and use that here
|
|
||||||
let mime_type = mime_guess::from_path(&path)
|
let mime_type = mime_guess::from_path(&path)
|
||||||
.first_raw()
|
.first_raw()
|
||||||
.expect("Could not guess mime-type from path");
|
.expect("Could not guess mime-type from path");
|
||||||
response_field_lines.insert(String::from("Content-Type"), mime_type.to_string());
|
response_field_lines.insert(String::from("Content-Type"), mime_type.to_string());
|
||||||
|
|
||||||
response = response_builder(
|
response_builder(
|
||||||
start_line.method,
|
start_line.method,
|
||||||
"HTTP/1.1 200 ",
|
"HTTP/1.1 200 ",
|
||||||
Some(response_field_lines),
|
Some(response_field_lines),
|
||||||
Some(response_body),
|
Some(response_body),
|
||||||
);
|
)
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => response_builder(start_line.method, "HTTP/1.1 404 ", None, None),
|
||||||
response = response_builder(start_line.method, "HTTP/1.1 404 ", None, None);
|
}
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(response)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_request(mut stream: TcpStream) -> Result<(), Box<dyn Error>> {
|
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(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
dbg!(&field_lines);
|
||||||
|
|
||||||
let mut body: Vec<u8> = vec![];
|
// let mut request_body: Vec<u8> = vec![];
|
||||||
reader.read_to_end(&mut body)?;
|
// 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)?;
|
stream.write_all(&response)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
Reference in New Issue
Block a user