Got it working with docker
This commit is contained in:
@ -24,6 +24,7 @@ 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 ./www /www
|
||||
|
||||
EXPOSE 8080
|
||||
|
||||
|
288
src/main.rs
288
src/main.rs
@ -2,8 +2,10 @@ 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,
|
||||
};
|
||||
@ -120,9 +122,10 @@ fn parse_start_line(input: &str) -> Result<RequestLine, Vec<u8>> {
|
||||
let mut response_body: Vec<u8> = vec![];
|
||||
|
||||
if input.ends_with(" ") {
|
||||
for byte in b"There is whitespace between the start-line and the first field-line\r\n" {
|
||||
response_body.push(*byte);
|
||||
}
|
||||
b"There is whitespace between the start-line and the first field-line"
|
||||
.iter()
|
||||
.for_each(|byte| response_body.push(*byte));
|
||||
|
||||
response_field_lines.insert(
|
||||
String::from("Content-Length"),
|
||||
response_body.len().to_string(),
|
||||
@ -133,7 +136,7 @@ fn parse_start_line(input: &str) -> Result<RequestLine, Vec<u8>> {
|
||||
RequestMethods::GET,
|
||||
"HTTP/1.1 400 Bad Request",
|
||||
response_field_lines,
|
||||
response_body,
|
||||
Some(response_body),
|
||||
));
|
||||
}
|
||||
|
||||
@ -141,14 +144,15 @@ fn parse_start_line(input: &str) -> Result<RequestLine, Vec<u8>> {
|
||||
let vec = input.trim().split_ascii_whitespace().collect::<Vec<&str>>();
|
||||
|
||||
let body = format!(
|
||||
"The start-line has an incorrect amount of items. Got the value: {}\r\n",
|
||||
"The start-line has an incorrect amount of items. Got the value: {}",
|
||||
vec.len()
|
||||
);
|
||||
|
||||
if vec.len() != 3 {
|
||||
for byte in body.as_bytes() {
|
||||
response_body.push(*byte);
|
||||
}
|
||||
body.as_bytes()
|
||||
.iter()
|
||||
.for_each(|byte| response_body.push(*byte));
|
||||
|
||||
response_field_lines.insert(
|
||||
String::from("Content-Length"),
|
||||
response_body.len().to_string(),
|
||||
@ -159,7 +163,7 @@ fn parse_start_line(input: &str) -> Result<RequestLine, Vec<u8>> {
|
||||
RequestMethods::GET,
|
||||
"HTTP/1.1 400 Bad Request",
|
||||
response_field_lines,
|
||||
response_body,
|
||||
Some(response_body),
|
||||
));
|
||||
}
|
||||
|
||||
@ -174,6 +178,23 @@ fn parse_start_line(input: &str) -> Result<RequestLine, Vec<u8>> {
|
||||
if method == String::from("HEAD") {
|
||||
start_line.method = RequestMethods::HEAD;
|
||||
}
|
||||
} else {
|
||||
b"Server only supports GET and HEAD"
|
||||
.iter()
|
||||
.for_each(|byte| response_body.push(*byte));
|
||||
|
||||
response_field_lines.insert(
|
||||
String::from("Content-Length"),
|
||||
response_body.len().to_string(),
|
||||
);
|
||||
response_field_lines.insert(String::from("Content-Type"), String::from("text/plain"));
|
||||
|
||||
return Err(response_builder(
|
||||
RequestMethods::GET,
|
||||
"HTTP/1.1 501 Not Implemented",
|
||||
response_field_lines,
|
||||
Some(response_body),
|
||||
));
|
||||
}
|
||||
|
||||
let target = vec[1];
|
||||
@ -191,78 +212,120 @@ fn parse_start_line(input: &str) -> Result<RequestLine, Vec<u8>> {
|
||||
return Ok(start_line);
|
||||
}
|
||||
|
||||
fn parse_field_lines(reader: &mut BufReader<&mut TcpStream>) -> Option<HashMap<String, String>> {
|
||||
let mut line: String;
|
||||
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();
|
||||
let mut response_body: Vec<u8> = vec![];
|
||||
let mut field_lines: HashMap<String, String> = HashMap::new();
|
||||
let mut is_first_line = true;
|
||||
|
||||
// Read field-lines till I hit an empty line
|
||||
loop {
|
||||
line = String::new();
|
||||
let mut line = String::new();
|
||||
reader.read_line(&mut line).unwrap();
|
||||
|
||||
if line.starts_with(" ") && is_first_line {
|
||||
return None;
|
||||
}
|
||||
|
||||
if !line.ends_with("\r\n") {
|
||||
return None;
|
||||
b"Lines need to end with a CRLF"
|
||||
.iter()
|
||||
.for_each(|byte| response_body.push(*byte));
|
||||
|
||||
response_field_lines.insert(
|
||||
String::from("Content-Length"),
|
||||
response_body.len().to_string(),
|
||||
);
|
||||
response_field_lines.insert(String::from("Content-Type"), String::from("text/plain"));
|
||||
|
||||
return Err(response_builder(
|
||||
RequestMethods::GET,
|
||||
"HTTP/1.1 400 Bad Request",
|
||||
response_field_lines,
|
||||
Some(response_body),
|
||||
));
|
||||
}
|
||||
|
||||
if line.trim().is_empty() {
|
||||
break;
|
||||
}
|
||||
|
||||
let field_line = line.split_once(":").expect("Shits fucked: {}");
|
||||
let field_line = match line.split_once(":") {
|
||||
Some(val) => val,
|
||||
None => {
|
||||
b"Invalid field-line"
|
||||
.iter()
|
||||
.for_each(|byte| response_body.push(*byte));
|
||||
|
||||
// Check if client has send more than one Host line
|
||||
if field_lines.contains_key(&String::from("Host")) && field_line.0 == "Host" {
|
||||
return None;
|
||||
response_field_lines.insert(
|
||||
String::from("Content-Length"),
|
||||
response_body.len().to_string(),
|
||||
);
|
||||
response_field_lines
|
||||
.insert(String::from("Content-Type"), String::from("text/plain"));
|
||||
|
||||
return Err(response_builder(
|
||||
RequestMethods::GET,
|
||||
"HTTP/1.1 400 Bad Request",
|
||||
response_field_lines,
|
||||
Some(response_body),
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
field_lines.insert(field_line.0.to_owned(), field_line.1.trim().to_owned());
|
||||
|
||||
is_first_line = false;
|
||||
}
|
||||
|
||||
if !field_lines.contains_key(&String::from("Host")) {
|
||||
return None;
|
||||
b"field-line with key HOST is missing"
|
||||
.iter()
|
||||
.for_each(|byte| response_body.push(*byte));
|
||||
|
||||
response_field_lines.insert(
|
||||
String::from("Content-Length"),
|
||||
response_body.len().to_string(),
|
||||
);
|
||||
response_field_lines.insert(String::from("Content-Type"), String::from("text/plain"));
|
||||
|
||||
return Err(response_builder(
|
||||
RequestMethods::GET,
|
||||
"HTTP/1.1 400 Bad Request",
|
||||
response_field_lines,
|
||||
Some(response_body),
|
||||
));
|
||||
}
|
||||
|
||||
return Some(field_lines);
|
||||
return Ok(field_lines);
|
||||
}
|
||||
|
||||
fn response_builder(
|
||||
method: RequestMethods,
|
||||
status_line: &str,
|
||||
field_lines: HashMap<String, String>,
|
||||
body: Vec<u8>,
|
||||
body: Option<Vec<u8>>,
|
||||
) -> Vec<u8> {
|
||||
let mut response: Vec<u8> = vec![];
|
||||
|
||||
// TODO: replaces with eiter Option<T> or Result<T, T>
|
||||
if status_line.is_empty() {
|
||||
return response;
|
||||
}
|
||||
|
||||
for byte in status_line.as_bytes().iter() {
|
||||
response.push(*byte);
|
||||
}
|
||||
status_line
|
||||
.as_bytes()
|
||||
.iter()
|
||||
.for_each(|byte| response.push(*byte));
|
||||
response.push(b'\r');
|
||||
response.push(b'\n');
|
||||
|
||||
if !field_lines.is_empty() {
|
||||
for field_line in field_lines.iter() {
|
||||
for byte in field_line.0.as_bytes().iter() {
|
||||
response.push(*byte);
|
||||
}
|
||||
field_line
|
||||
.0
|
||||
.as_bytes()
|
||||
.iter()
|
||||
.for_each(|byte| response.push(*byte));
|
||||
|
||||
response.push(b':');
|
||||
response.push(b' ');
|
||||
|
||||
for byte in field_line.1.as_bytes().iter() {
|
||||
response.push(*byte);
|
||||
}
|
||||
field_line
|
||||
.1
|
||||
.as_bytes()
|
||||
.iter()
|
||||
.for_each(|byte| response.push(*byte));
|
||||
|
||||
response.push(b'\r');
|
||||
response.push(b'\n');
|
||||
@ -273,81 +336,135 @@ fn response_builder(
|
||||
response.push(b'\r');
|
||||
response.push(b'\n');
|
||||
|
||||
if body.is_empty() {
|
||||
return response;
|
||||
}
|
||||
|
||||
if method != RequestMethods::HEAD && method != RequestMethods::NULL {
|
||||
for byte in body.iter() {
|
||||
response.push(*byte);
|
||||
}
|
||||
if method != RequestMethods::HEAD {
|
||||
match body {
|
||||
Some(val) => {
|
||||
val.iter().for_each(|byte| response.push(*byte));
|
||||
response.push(b'\r');
|
||||
response.push(b'\n');
|
||||
}
|
||||
None => (),
|
||||
}
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
fn handle_request(mut stream: TcpStream) {
|
||||
fn handle_request(mut stream: TcpStream) -> Result<(), Box<dyn Error>> {
|
||||
let mut line = String::new();
|
||||
let mut reader = BufReader::new(&mut stream);
|
||||
|
||||
// Request can have one or many empty lines preceding the start-line and I will ignore these
|
||||
loop {
|
||||
match reader.read_line(&mut line) {
|
||||
Ok(val) => {
|
||||
if val > 2 {
|
||||
if reader.read_line(&mut line)? > 2 && line != "\r\n" {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// TODO: Replace with stream.write_all
|
||||
Err(err) => {
|
||||
eprintln!("{err}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
if line != "\r\n" {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
let start_line = match parse_start_line(&line) {
|
||||
Ok(val) => val,
|
||||
Err(response) => {
|
||||
stream.write_all(&response).unwrap();
|
||||
return;
|
||||
stream.write_all(&response)?;
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
dbg!(&start_line);
|
||||
|
||||
if start_line.method == RequestMethods::NULL {
|
||||
stream
|
||||
.write_all(b"HTTP/1.1 501 Not Implemented\r\n\r\n")
|
||||
.unwrap();
|
||||
return;
|
||||
}
|
||||
|
||||
let field_lines = match parse_field_lines(&mut reader) {
|
||||
Some(val) => val,
|
||||
None => {
|
||||
stream
|
||||
.write_all(b"HTTP/1.1 400 Bad Request\r\n\r\nInvalid Header")
|
||||
.unwrap();
|
||||
return;
|
||||
Ok(val) => val,
|
||||
Err(response) => {
|
||||
stream.write_all(&response)?;
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
dbg!(&field_lines);
|
||||
|
||||
// TODO: Read the body
|
||||
// let mut body: Vec<u8> = vec![];
|
||||
// reader.read_to_end(&mut body).unwrap();
|
||||
// reader.read_to_end(&mut body)?;
|
||||
// dbg!(&body);
|
||||
// TODO: Act upon the request
|
||||
|
||||
stream
|
||||
.write_all(b"HTTP/1.1 200 OK\r\n\r\nHello, World!\r\n")
|
||||
.unwrap();
|
||||
let mut response_field_lines: HashMap<String, String> = HashMap::new();
|
||||
let mut response_body: Vec<u8> = vec![];
|
||||
|
||||
// TODO: Act upon the request
|
||||
if start_line.target == "/" {
|
||||
let file = match fs::read("./www/index.html") {
|
||||
Ok(val) => val,
|
||||
Err(_) => {
|
||||
b"The is no index.html, only you and me."
|
||||
.iter()
|
||||
.for_each(|byte| response_body.push(*byte));
|
||||
|
||||
response_field_lines.insert(
|
||||
String::from("Content-Length"),
|
||||
response_body.len().to_string(),
|
||||
);
|
||||
response_field_lines
|
||||
.insert(String::from("Content-Type"), String::from("text/plain"));
|
||||
|
||||
let response = response_builder(
|
||||
start_line.method,
|
||||
"HTTP/1.1 200 OK",
|
||||
response_field_lines,
|
||||
Some(response_body),
|
||||
);
|
||||
|
||||
stream.write_all(&response)?;
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
file.iter().for_each(|byte| response_body.push(*byte));
|
||||
|
||||
response_field_lines.insert(String::from("Content-Length"), file.len().to_string());
|
||||
response_field_lines.insert(String::from("Content-Type"), String::from("text/html"));
|
||||
|
||||
let response = response_builder(
|
||||
start_line.method,
|
||||
"HTTP/1.1 200 OK",
|
||||
response_field_lines,
|
||||
Some(response_body),
|
||||
);
|
||||
|
||||
stream.write_all(&response)?;
|
||||
} else {
|
||||
let path: PathBuf = 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"),
|
||||
response_body.len().to_string(),
|
||||
);
|
||||
// TODO: get mime-type of file and use that here
|
||||
response_field_lines.insert(String::from("Content-Type"), String::from("*/*"));
|
||||
|
||||
let response = response_builder(
|
||||
start_line.method,
|
||||
"HTTP/1.1 200 OK",
|
||||
response_field_lines,
|
||||
Some(response_body),
|
||||
);
|
||||
|
||||
stream.write_all(&response)?;
|
||||
}
|
||||
Err(_) => {
|
||||
let response = response_builder(
|
||||
start_line.method,
|
||||
"HTTP/1.1 404 Not Found",
|
||||
response_field_lines,
|
||||
None,
|
||||
);
|
||||
|
||||
stream.write_all(&response)?;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
@ -368,7 +485,8 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
|
||||
for stream in listener.incoming() {
|
||||
let stream = stream?;
|
||||
handle_request(stream);
|
||||
handle_request(stream)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
10
www/index.html
Normal file
10
www/index.html
Normal file
@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>http-server</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Hello, World!</h1>
|
||||
</body>
|
||||
</html>
|
Reference in New Issue
Block a user