Skip to content
Snippets Groups Projects
Commit 8d669447 authored by codecraft's avatar codecraft :crocodile:
Browse files

Implement Display for HTTP Status Codes

parent e40abea4
No related branches found
No related tags found
1 merge request!1Initial feature merge
...@@ -70,7 +70,9 @@ pub fn handle_connection(mut stream: TcpStream, mountpoints: Vec<MountPoint>) { ...@@ -70,7 +70,9 @@ pub fn handle_connection(mut stream: TcpStream, mountpoints: Vec<MountPoint>) {
let mut buffer: Vec<u8> = vec![0; length]; let mut buffer: Vec<u8> = vec![0; length];
let read_len = buf_reader.read(&mut buffer).unwrap(); let read_len = buf_reader.read(&mut buffer).unwrap();
if read_len != length { if read_len != length {
eprintln!("User inputted invalid BUFLEN"); let respone = len_not_defined(Status { code: 411 });
stream.write_all(respone.0.as_bytes()).unwrap();
stream.write(&respone.1).unwrap();
return; return;
} }
data.is_complete = true; data.is_complete = true;
...@@ -130,3 +132,15 @@ fn failure_handler(status: Status) -> (String, Vec<u8>) { ...@@ -130,3 +132,15 @@ fn failure_handler(status: Status) -> (String, Vec<u8>) {
page_404.get_data(), page_404.get_data(),
) )
} }
fn len_not_defined(status: Status) -> (String, Vec<u8>) {
let page_411 = NamedFile::open(PathBuf::from("411.html")).unwrap();
(
format!(
"HTTP/1.1 {status}\r\nContent-Length: {}\r\nContent-Type: {}\r\n\r\n",
page_411.get_len(),
page_411.get_mime()
),
page_411.get_data(),
)
}
use std::fmt::Display;
use mime::Mime; use mime::Mime;
use super::routes::Body; use super::routes::Body;
...@@ -68,3 +70,98 @@ pub struct Response { ...@@ -68,3 +70,98 @@ pub struct Response {
pub struct Status { pub struct Status {
pub code: u16, pub code: u16,
} }
impl Display for Status {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.code {
// INFORMATIONAL RESPONSE
100 => write!(f, "{} Continue", self.code),
101 => write!(f, "{} Switching Protocols", self.code),
// WebDAV
102 => write!(f, "{} Processing", self.code),
// Experimental
103 => write!(f, "{} Early Hints", self.code),
// SUCCESSFUL RESPONSE
200 => write!(f, "{} OK", self.code),
201 => write!(f, "{} Created", self.code),
203 => write!(f, "{} Non-Authorative Information", self.code),
204 => write!(f, "{} No Content", self.code),
205 => write!(f, "{} Reset Content", self.code),
206 => write!(f, "{} Partial Content", self.code),
// WebDAV
207 => write!(f, "{} Mutli-Status", self.code),
208 => write!(f, "{} Already Reported", self.code),
// HTTP Delta Encoding
226 => write!(f, "{} IM Used", self.code),
// REDIRECTION MESSAGES
300 => write!(f, "{} Multiple Choices", self.code),
301 => write!(f, "{} Moved Permanently", self.code),
302 => write!(f, "{} Found", self.code),
303 => write!(f, "{} See Other", self.code),
304 => write!(f, "{} Not Modified", self.code),
307 => write!(f, "{} Temporary Redirect", self.code),
308 => write!(f, "{} Permanent Redirect", self.code),
// Unused / Deprecated
305 => write!(f, "{} Use Proxy", self.code),
306 => write!(f, "{} unused", self.code),
// CLIENT ERROR
400 => write!(f, "{} Bad Request", self.code),
401 => write!(f, "{} Unauthorized", self.code),
403 => write!(f, "{} Forbidden", self.code),
404 => write!(f, "{} Not Found", self.code),
405 => write!(f, "{} Method Not Allowed", self.code),
406 => write!(f, "{} Not Acceptable", self.code),
407 => write!(f, "{} Proxy Athentication Required", self.code),
408 => write!(f, "{} Request Timout", self.code),
409 => write!(f, "{} Conflict", self.code),
410 => write!(f, "{} Gone", self.code),
411 => write!(f, "{} Length Required", self.code),
412 => write!(f, "{} Precondition Failed", self.code),
413 => write!(f, "{} Payload Too Large", self.code),
414 => write!(f, "{} URI Too Long", self.code),
415 => write!(f, "{} Unsupported Media Type", self.code),
416 => write!(f, "{} Range Not Satisfiable", self.code),
417 => write!(f, "{} Expectation Failed", self.code),
418 => write!(f, "{} I'm a Teapot", self.code),
421 => write!(f, "{} Misdirected Request", self.code),
422 => write!(f, "{} Unprocessable Content", self.code),
426 => write!(f, "{} Upgrade Required", self.code),
428 => write!(f, "{} Precondition Required", self.code),
429 => write!(f, "{} Too Many Requests", self.code),
431 => write!(f, "{} Request Header Fields Too Large", self.code),
451 => write!(f, "{} Unavailable For Legal Reasons", self.code),
// WebDAV
423 => write!(f, "{} Locked", self.code),
424 => write!(f, "{} Failed Dependency", self.code),
// Experimental / Not in use
402 => write!(f, "{} Payment Required", self.code),
425 => write!(f, "{} Too Early", self.code),
// SERVER ERROR RESPONSES
500 => write!(f, "{} Internal Server Error", self.code),
501 => write!(f, "{} Not Implmenented", self.code),
502 => write!(f, "{} Bad Getaway", self.code),
503 => write!(f, "{} Service Unavailable", self.code),
504 => write!(f, "{} Getaway Timeout", self.code),
505 => write!(f, "{} HTTP Version Not Supported", self.code),
506 => write!(f, "{} Variant Also Negotiates", self.code),
507 => write!(f, "{} Insufficient Storage", self.code),
508 => write!(f, "{} Loop Detected", self.code),
510 => write!(f, "{} Not Extendend", self.code),
511 => write!(f, "{} Network Authentication Required", self.code),
_ => write!(f, "500 "),
}
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>411</title>
</head>
<h1>411</h1>
</html>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment