From 8d669447dffb1e635e6e256036a0c3f19103bdc6 Mon Sep 17 00:00:00 2001
From: Darius Auding <Darius.auding@gmx.de>
Date: Fri, 19 May 2023 14:55:14 +0200
Subject: [PATCH] Implement Display for HTTP Status Codes

---
 core/http/src/handlers/handlers.rs | 16 ++++-
 core/http/src/handling/response.rs | 97 ++++++++++++++++++++++++++++++
 site/411.html                      |  9 +++
 3 files changed, 121 insertions(+), 1 deletion(-)
 create mode 100644 site/411.html

diff --git a/core/http/src/handlers/handlers.rs b/core/http/src/handlers/handlers.rs
index c0e77e3..b34416b 100755
--- a/core/http/src/handlers/handlers.rs
+++ b/core/http/src/handlers/handlers.rs
@@ -70,7 +70,9 @@ pub fn handle_connection(mut stream: TcpStream, mountpoints: Vec<MountPoint>) {
         let mut buffer: Vec<u8> = vec![0; length];
         let read_len = buf_reader.read(&mut buffer).unwrap();
         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;
         }
         data.is_complete = true;
@@ -130,3 +132,15 @@ fn failure_handler(status: Status) -> (String, Vec<u8>) {
         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(),
+    )
+}
diff --git a/core/http/src/handling/response.rs b/core/http/src/handling/response.rs
index 9ca6f4b..603ca55 100644
--- a/core/http/src/handling/response.rs
+++ b/core/http/src/handling/response.rs
@@ -1,3 +1,5 @@
+use std::fmt::Display;
+
 use mime::Mime;
 
 use super::routes::Body;
@@ -68,3 +70,98 @@ pub struct Response {
 pub struct Status {
     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 "),
+        }
+    }
+}
diff --git a/site/411.html b/site/411.html
new file mode 100644
index 0000000..802df86
--- /dev/null
+++ b/site/411.html
@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+
+<html>
+  <head>
+    <meta charset="utf-8" />
+    <title>411</title>
+  </head>
+  <h1>411</h1>
+</html>
-- 
GitLab