change signature of Hashmap
This commit is contained in:
80
src/main.rs
80
src/main.rs
@ -110,7 +110,7 @@ impl StartLine {
|
||||
}
|
||||
|
||||
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>>();
|
||||
@ -126,9 +126,12 @@ fn parse_start_line(input: &str) -> Result<StartLine, Vec<u8>> {
|
||||
|
||||
response_field_lines.insert(
|
||||
String::from("Content-Length"),
|
||||
response_body.len().to_string(),
|
||||
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,
|
||||
@ -166,9 +169,12 @@ fn parse_start_line(input: &str) -> Result<StartLine, Vec<u8>> {
|
||||
|
||||
response_field_lines.insert(
|
||||
String::from("Content-Length"),
|
||||
response_body.len().to_string(),
|
||||
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::Head,
|
||||
@ -200,10 +206,10 @@ fn parse_start_line(input: &str) -> Result<StartLine, Vec<u8>> {
|
||||
|
||||
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 {
|
||||
@ -218,9 +224,12 @@ fn parse_field_lines(
|
||||
|
||||
response_field_lines.insert(
|
||||
String::from("Content-Length"),
|
||||
response_body.len().to_string(),
|
||||
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,
|
||||
@ -244,10 +253,12 @@ fn parse_field_lines(
|
||||
|
||||
response_field_lines.insert(
|
||||
String::from("Content-Length"),
|
||||
response_body.len().to_string(),
|
||||
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,
|
||||
@ -258,7 +269,10 @@ fn parse_field_lines(
|
||||
}
|
||||
};
|
||||
|
||||
field_lines.insert(field_line.0.to_owned(), field_line.1.trim().to_owned());
|
||||
field_lines.insert(
|
||||
field_line.0.to_owned(),
|
||||
vec![field_line.1.trim().to_owned()],
|
||||
);
|
||||
}
|
||||
|
||||
if !field_lines.contains_key(&String::from("Host")) {
|
||||
@ -269,9 +283,12 @@ fn parse_field_lines(
|
||||
|
||||
response_field_lines.insert(
|
||||
String::from("Content-Length"),
|
||||
response_body.len().to_string(),
|
||||
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,
|
||||
@ -287,7 +304,7 @@ fn parse_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![];
|
||||
@ -310,11 +327,9 @@ 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');
|
||||
@ -336,8 +351,8 @@ fn response_builder(
|
||||
response
|
||||
}
|
||||
|
||||
fn try_get_file(start_line: &StartLine, _field_lines: &HashMap<String, String>) -> Vec<u8> {
|
||||
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 path: PathBuf = match start_line.target.as_str() {
|
||||
@ -360,13 +375,13 @@ fn try_get_file(start_line: &StartLine, _field_lines: &HashMap<String, String>)
|
||||
|
||||
response_field_lines.insert(
|
||||
String::from("Content-Length"),
|
||||
response_body.len().to_string(),
|
||||
vec![response_body.len().to_string()],
|
||||
);
|
||||
|
||||
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_field_lines.insert(String::from("Content-Type"), vec![mime_type.to_string()]);
|
||||
|
||||
response_builder(
|
||||
start_line.method,
|
||||
@ -382,7 +397,7 @@ fn try_get_file(start_line: &StartLine, _field_lines: &HashMap<String, String>)
|
||||
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
|
||||
@ -400,9 +415,12 @@ fn handle_request(mut stream: TcpStream) -> Result<(), Box<dyn Error>> {
|
||||
|
||||
response_field_lines.insert(
|
||||
String::from("Content-Length"),
|
||||
response_body.len().to_string(),
|
||||
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,
|
||||
@ -438,8 +456,8 @@ fn handle_request(mut stream: TcpStream) -> Result<(), Box<dyn Error>> {
|
||||
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),
|
||||
// "/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)?;
|
||||
|
Reference in New Issue
Block a user