diff --git a/core/http/src/handlers/handlers.rs b/core/http/src/handlers/handlers.rs index b34416b9bb00a3f6325e6166f7bad182cc4112ed..94775694bda4889afecb55a1ed99e05408d037cd 100755 --- a/core/http/src/handlers/handlers.rs +++ b/core/http/src/handlers/handlers.rs @@ -70,7 +70,7 @@ 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 { - let respone = len_not_defined(Status { code: 411 }); + let respone = len_not_defined(Status::LengthRequired); stream.write_all(respone.0.as_bytes()).unwrap(); stream.write(&respone.1).unwrap(); return; @@ -105,15 +105,15 @@ pub fn handle_connection(mut stream: TcpStream, mountpoints: Vec<MountPoint>) { let response = match handled_response { Some(val) => match val { Outcome::Success(success) => ( - format!("HTTP/1.1 {} OK\r\n", success.status.unwrap().code) + format!("HTTP/1.1 {}\r\n", success.status.unwrap()) + &success.headers.join("\r\n") + "\r\n\r\n", success.body.get_data(), ), Outcome::Failure(error) => failure_handler(error), - Outcome::Forward(_) => failure_handler(Status { code: 404 }), + Outcome::Forward(_) => failure_handler(Status::NotFound), }, - None => failure_handler(Status { code: 404 }), + None => failure_handler(Status::NotFound), }; stream.write_all(response.0.as_bytes()).unwrap(); @@ -124,8 +124,8 @@ fn failure_handler(status: Status) -> (String, Vec<u8>) { let page_404 = NamedFile::open(PathBuf::from("404.html")).unwrap(); ( format!( - "HTTP/1.1 {} NOT FOUND\r\nContent-Length: {}\r\nContent-Type: {}\r\n\r\n", - status.code, + "HTTP/1.1 {}\r\nContent-Length: {}\r\nContent-Type: {}\r\n\r\n", + status, page_404.get_len(), page_404.get_mime() ), diff --git a/core/http/src/handling/file_handlers.rs b/core/http/src/handling/file_handlers.rs index fdfd3d0a1fc50e8028acc174a1579ee118058cb2..c2945ae3475e9ce90eede358023b112d8295f7d1 100644 --- a/core/http/src/handling/file_handlers.rs +++ b/core/http/src/handling/file_handlers.rs @@ -32,7 +32,7 @@ impl NamedFile { let data = match data { Ok(dat) => dat, Err(_) => { - return Err(Status { code: 404 }); + return Err(Status::NotFound); } }; Ok(NamedFile { diff --git a/core/http/src/handling/response.rs b/core/http/src/handling/response.rs index 603ca5574af73cb1cddee2057b7cd9101cc3476a..896885234942db9e271dd0676bdff7738b0e7c87 100644 --- a/core/http/src/handling/response.rs +++ b/core/http/src/handling/response.rs @@ -1,4 +1,4 @@ -use std::fmt::Display; +use std::{fmt::Display, fs::write}; use mime::Mime; @@ -67,101 +67,146 @@ pub struct Response { } #[derive(Debug)] -pub struct Status { - pub code: u16, +pub enum Status { + Continue, + SwitchingProtocols, + WebDavProcessing, + ExperimentalEarlyHints, + Ok, + Created, + Accepted, + NonAuthorativeIfnormation, + NoContent, + ResetContent, + PartialContent, + WebDavMultiStatus, + WebDavAlreadyReported, + HttpDataEncodingImUsed, + MultipleChoices, + MovedPermanently, + Found, + SeeOther, + NotModfiied, + DeprecatedUseProxy, + UnusedUnused, + TemporaryRedirect, + PermanentRedirect, + BadRequest, + Unauthorized, + ExperimentalPaymentRequired, + Forbidden, + NotFound, + MethodNotAllowed, + NotAcceptable, + ProxyAuthenticationRequired, + RequestTimeout, + Conflict, + Gone, + LengthRequired, + PreconditionFailed, + PayloadTooLarge, + UriTooLong, + UnsupportedMediaType, + RangeNotSatisfiable, + ExpectationFailed, + ImATeapot, + MisdirectedRequest, + WebDavUnprocessableContent, + WebDavLocked, + WebDavFailedDependency, + ExperimenalTooEarly, + UpgradeRequred, + PreconditionRequired, + TooManyRequests, + RequestHeaderFieldsTooLarge, + UnavailableForLegalReasons, + InternalServerError, + NotImplemented, + BadGetaway, + ServiceUnavailable, + GetawayTimeout, + HttpVersionNotSupported, + VariantAlsoNegotiates, + WebDavInsufficientStorage, + WebDavLoopDetected, + NotExtended, + NetworkAuthenticationRequired, } 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 "), + match self { + Status::Continue => write!(f, "100 Continue"), + Status::SwitchingProtocols => write!(f, "101 Switching Protocols"), + Status::WebDavProcessing => write!(f, "102 Processing"), + Status::ExperimentalEarlyHints => write!(f, "103 Early Hints"), + Status::Ok => write!(f, "200 OK"), + Status::Created => write!(f, "201 Created"), + Status::Accepted => write!(f, "202 Accepted"), + Status::NonAuthorativeIfnormation => write!(f, "203 Non-Authorative Information"), + Status::NoContent => write!(f, "204 No Content"), + Status::ResetContent => write!(f, "205 Reset Content"), + Status::PartialContent => write!(f, "206 Partial Content"), + Status::WebDavMultiStatus => write!(f, "207 Mutli-Status"), + Status::WebDavAlreadyReported => write!(f, "208 Already Reported"), + Status::HttpDataEncodingImUsed => write!(f, "226 IM Used"), + Status::MultipleChoices => write!(f, "300 Multiple Choices"), + Status::MovedPermanently => write!(f, "301 Moved Permanently"), + Status::Found => write!(f, "302 Found"), + Status::SeeOther => write!(f, "303 See Other"), + Status::NotModfiied => write!(f, "304 Not Modified"), + Status::TemporaryRedirect => write!(f, "307 Temporary Redirect"), + Status::PermanentRedirect => write!(f, "308 Permanent Redirect"), + Status::DeprecatedUseProxy => write!(f, "305 Use Proxy"), + Status::UnusedUnused => write!(f, "306 unused"), + Status::BadRequest => write!(f, "400 Bad Request"), + Status::Unauthorized => write!(f, "401 Unauthorized"), + Status::ExperimentalPaymentRequired => write!(f, "402 Payment Required"), + Status::Forbidden => write!(f, "403 Forbidden"), + Status::NotFound => write!(f, "404 Not Found"), + Status::MethodNotAllowed => write!(f, "405 Method Not Allowed"), + Status::NotAcceptable => write!(f, "406 Not Acceptable"), + Status::ProxyAuthenticationRequired => { + write!(f, "407 Proxy Athentication Required") + } + Status::RequestTimeout => write!(f, "408 Request Timout"), + Status::Conflict => write!(f, "409 Conflict"), + Status::Gone => write!(f, "410 Gone"), + Status::LengthRequired => write!(f, "411 Length Required"), + Status::PreconditionFailed => write!(f, "412 Precondition Failed"), + Status::PayloadTooLarge => write!(f, "413 Payload Too Large"), + Status::UriTooLong => write!(f, "414 URI Too Long"), + Status::UnsupportedMediaType => write!(f, "415 Unsupported Media Type"), + Status::RangeNotSatisfiable => write!(f, "416 Range Not Satisfiable"), + Status::ExpectationFailed => write!(f, "417 Expectation Failed"), + Status::ImATeapot => write!(f, "418 I'm a Teapot"), + Status::MisdirectedRequest => write!(f, "421 Misdirected Request"), + Status::WebDavUnprocessableContent => write!(f, "422 Unprocessable Content"), + Status::WebDavLocked => write!(f, "423 Locked"), + Status::WebDavFailedDependency => write!(f, "424 Failed Dependency"), + Status::ExperimenalTooEarly => write!(f, "425 Too Early"), + Status::UpgradeRequred => write!(f, "426 Upgrade Required"), + Status::PreconditionRequired => write!(f, "428 Precondition Required"), + Status::TooManyRequests => write!(f, "429 Too Many Requests"), + Status::RequestHeaderFieldsTooLarge => { + write!(f, "431 Request Header Fields Too Large") + } + Status::UnavailableForLegalReasons => { + write!(f, "451 Unavailable For Legal Reasons") + } + Status::InternalServerError => write!(f, "500 Internal Server Error"), + Status::NotImplemented => write!(f, "501 Not Implmenented"), + Status::BadGetaway => write!(f, "502 Bad Getaway"), + Status::ServiceUnavailable => write!(f, "503 Service Unavailable"), + Status::GetawayTimeout => write!(f, "504 Getaway Timeout"), + Status::HttpVersionNotSupported => write!(f, "505 HTTP Version Not Supported"), + Status::VariantAlsoNegotiates => write!(f, "506 Variant Also Negotiates"), + Status::WebDavInsufficientStorage => write!(f, "507 Insufficient Storage"), + Status::WebDavLoopDetected => write!(f, "508 Loop Detected"), + Status::NotExtended => write!(f, "510 Not Extendend"), + Status::NetworkAuthenticationRequired => { + write!(f, "511 Network Authentication Required") + } } } } diff --git a/site/src/main.rs b/site/src/main.rs index da51a333454d60fb0be7d7af9d50539be641bc93..0d90d268fb4618cc7fc8a8d4d150591dca47fc65 100644 --- a/site/src/main.rs +++ b/site/src/main.rs @@ -16,10 +16,10 @@ fn handler(request: Request, _data: Data) -> Outcome<Response, Status, Data> { format!("Content-Length: {}", dat.get_len()), format!("Content-Type: {}", dat.get_mime()), ], - status: Some(Status { code: 200 }), + status: Some(Status::Ok), body: Box::new(dat), }, - Err(_) => return Outcome::Failure(Status { code: 404 }), + Err(_) => return Outcome::Failure(Status::NotFound), }; Outcome::Success(response) }