From 5035cb72acd963fe974834de48d01a524d2fa372 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz <17555470+Az107@users.noreply.github.com> Date: Wed, 19 Feb 2025 20:11:02 +0100 Subject: [PATCH 1/2] Add more httpstatus and better number parser --- src/hteapot/status.rs | 154 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 134 insertions(+), 20 deletions(-) diff --git a/src/hteapot/status.rs b/src/hteapot/status.rs index 0db0cda..4aec09a 100644 --- a/src/hteapot/status.rs +++ b/src/hteapot/status.rs @@ -3,41 +3,117 @@ pub enum HttpStatus { OK = 200, Created = 201, Accepted = 202, + NonAuthoritativeInformation = 203, NoContent = 204, + ResetContent = 205, + PartialContent = 206, + + MultipleChoices = 300, MovedPermanently = 301, - MovedTemporarily = 302, + Found = 302, // (Renombrado de MovedTemporarily) + SeeOther = 303, NotModified = 304, + UseProxy = 305, + TemporaryRedirect = 307, + PermanentRedirect = 308, + BadRequest = 400, Unauthorized = 401, + PaymentRequired = 402, Forbidden = 403, NotFound = 404, + MethodNotAllowed = 405, + NotAcceptable = 406, + ProxyAuthenticationRequired = 407, + RequestTimeout = 408, + Conflict = 409, + Gone = 410, + LengthRequired = 411, + PreconditionFailed = 412, + PayloadTooLarge = 413, + URITooLong = 414, + UnsupportedMediaType = 415, + RangeNotSatisfiable = 416, + ExpectationFailed = 417, IAmATeapot = 418, + MisdirectedRequest = 421, + UnprocessableEntity = 422, + Locked = 423, + FailedDependency = 424, + TooEarly = 425, + UpgradeRequired = 426, + PreconditionRequired = 428, + TooManyRequests = 429, + RequestHeaderFieldsTooLarge = 431, + InternalServerError = 500, NotImplemented = 501, BadGateway = 502, ServiceUnavailable = 503, + GatewayTimeout = 504, + HTTPVersionNotSupported = 505, + VariantAlsoNegotiates = 506, + InsufficientStorage = 507, + LoopDetected = 508, + NotExtended = 510, + NetworkAuthenticationRequired = 511, } impl HttpStatus { - pub fn from_u16(status: u16) -> HttpStatus { + pub fn from_u16(status: u16) -> Result { match status { - 200 => HttpStatus::OK, - 201 => HttpStatus::Created, - 202 => HttpStatus::Accepted, - 204 => HttpStatus::NoContent, - 301 => HttpStatus::MovedPermanently, - 302 => HttpStatus::MovedTemporarily, - 304 => HttpStatus::NotModified, - 400 => HttpStatus::BadRequest, - 401 => HttpStatus::Unauthorized, - 403 => HttpStatus::Forbidden, - 404 => HttpStatus::NotFound, - 418 => HttpStatus::IAmATeapot, - 500 => HttpStatus::InternalServerError, - 501 => HttpStatus::NotImplemented, - 502 => HttpStatus::BadGateway, - 503 => HttpStatus::ServiceUnavailable, - _ => panic!("Invalid HTTP status"), + 200 => Ok(HttpStatus::OK), + 201 => Ok(HttpStatus::Created), + 202 => Ok(HttpStatus::Accepted), + 203 => Ok(HttpStatus::NonAuthoritativeInformation), + 204 => Ok(HttpStatus::NoContent), + 300 => Ok(HttpStatus::MultipleChoices), + 301 => Ok(HttpStatus::MovedPermanently), + 302 => Ok(HttpStatus::Found), + 303 => Ok(HttpStatus::SeeOther), + 304 => Ok(HttpStatus::NotModified), + 305 => Ok(HttpStatus::UseProxy), + 307 => Ok(HttpStatus::TemporaryRedirect), + 308 => Ok(HttpStatus::PermanentRedirect), + 400 => Ok(HttpStatus::BadRequest), + 401 => Ok(HttpStatus::Unauthorized), + 402 => Ok(HttpStatus::PaymentRequired), + 403 => Ok(HttpStatus::Forbidden), + 404 => Ok(HttpStatus::NotFound), + 405 => Ok(HttpStatus::MethodNotAllowed), + 406 => Ok(HttpStatus::NotAcceptable), + 407 => Ok(HttpStatus::ProxyAuthenticationRequired), + 408 => Ok(HttpStatus::RequestTimeout), + 409 => Ok(HttpStatus::Conflict), + 410 => Ok(HttpStatus::Gone), + 411 => Ok(HttpStatus::LengthRequired), + 412 => Ok(HttpStatus::PreconditionFailed), + 413 => Ok(HttpStatus::PayloadTooLarge), + 414 => Ok(HttpStatus::URITooLong), + 415 => Ok(HttpStatus::UnsupportedMediaType), + 416 => Ok(HttpStatus::RangeNotSatisfiable), + 418 => Ok(HttpStatus::IAmATeapot), + 421 => Ok(HttpStatus::MisdirectedRequest), + 422 => Ok(HttpStatus::UnprocessableEntity), + 423 => Ok(HttpStatus::Locked), + 424 => Ok(HttpStatus::FailedDependency), + 425 => Ok(HttpStatus::TooEarly), + 426 => Ok(HttpStatus::UpgradeRequired), + 428 => Ok(HttpStatus::PreconditionRequired), + 429 => Ok(HttpStatus::TooManyRequests), + 431 => Ok(HttpStatus::RequestHeaderFieldsTooLarge), + 500 => Ok(HttpStatus::InternalServerError), + 501 => Ok(HttpStatus::NotImplemented), + 502 => Ok(HttpStatus::BadGateway), + 503 => Ok(HttpStatus::ServiceUnavailable), + 504 => Ok(HttpStatus::GatewayTimeout), + 505 => Ok(HttpStatus::HTTPVersionNotSupported), + 506 => Ok(HttpStatus::VariantAlsoNegotiates), + 507 => Ok(HttpStatus::InsufficientStorage), + 508 => Ok(HttpStatus::LoopDetected), + 510 => Ok(HttpStatus::NotExtended), + 511 => Ok(HttpStatus::NetworkAuthenticationRequired), + _ => Err("Invalid HTTP status"), } } @@ -48,7 +124,7 @@ impl HttpStatus { HttpStatus::Accepted => "Accepted", HttpStatus::NoContent => "No Content", HttpStatus::MovedPermanently => "Moved Permanently", - HttpStatus::MovedTemporarily => "Moved Temporarily", + HttpStatus::Found => "Moved Temporarily", HttpStatus::NotModified => "Not Modified", HttpStatus::BadRequest => "Bad Request", HttpStatus::Unauthorized => "Unauthorized", @@ -59,6 +135,44 @@ impl HttpStatus { HttpStatus::NotImplemented => "Not Implemented", HttpStatus::BadGateway => "Bad Gateway", HttpStatus::ServiceUnavailable => "Service Unavailable", + HttpStatus::NonAuthoritativeInformation => "Non Authoritative Information", + HttpStatus::ResetContent => "Reset Content", + HttpStatus::PartialContent => "PartialContent", + HttpStatus::MultipleChoices => "Multiple Choices", + HttpStatus::SeeOther => "See Other", + HttpStatus::UseProxy => "Use Proxy", + HttpStatus::TemporaryRedirect => "Temporary Redirect", + HttpStatus::PermanentRedirect => "Permanent Redirect", + HttpStatus::PaymentRequired => "Payment Required", + HttpStatus::MethodNotAllowed => "Method Not Allowed", + HttpStatus::NotAcceptable => "Not Acceptable", + HttpStatus::ProxyAuthenticationRequired => "Proxy Authentication Required", + HttpStatus::RequestTimeout => "Request Timeout", + HttpStatus::Conflict => "Conflict", + HttpStatus::Gone => "Gone", + HttpStatus::LengthRequired => "Length Required", + HttpStatus::PreconditionFailed => "Precondition Failed", + HttpStatus::PayloadTooLarge => "Payload Too Large", + HttpStatus::URITooLong => "URI Too Long", + HttpStatus::UnsupportedMediaType => "Unsupported Media Type", + HttpStatus::RangeNotSatisfiable => "Range Not Satisfiable", + HttpStatus::ExpectationFailed => "Expectation Failed", + HttpStatus::MisdirectedRequest => "Misdirected Request", + HttpStatus::UnprocessableEntity => "Unprocessable Entity", + HttpStatus::Locked => "Locked", + HttpStatus::FailedDependency => "Failed Dependency", + HttpStatus::TooEarly => "Too Early", + HttpStatus::UpgradeRequired => "Upgrade Required", + HttpStatus::PreconditionRequired => "Precondition Required", + HttpStatus::TooManyRequests => "Too Many Requests", + HttpStatus::RequestHeaderFieldsTooLarge => "Request Header Fields Too Large", + HttpStatus::GatewayTimeout => "Gateway Timeout", + HttpStatus::HTTPVersionNotSupported => "HTTP Version Not Supported", + HttpStatus::VariantAlsoNegotiates => "Variant Also Negotiates", + HttpStatus::InsufficientStorage => "Insufficient Storage", + HttpStatus::LoopDetected => "Loop Detected", + HttpStatus::NotExtended => "Not Extended", + HttpStatus::NetworkAuthenticationRequired => "Network Authentication Required", } } } From 9b46bab1e6e709278239ad7d405a2173047b0ac3 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz <17555470+Az107@users.noreply.github.com> Date: Wed, 19 Feb 2025 20:12:58 +0100 Subject: [PATCH 2/2] update hteapot version --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6b73f5a..8d0b91c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,4 +4,4 @@ version = 3 [[package]] name = "hteapot" -version = "0.4.1" +version = "0.4.2" diff --git a/Cargo.toml b/Cargo.toml index b882d4f..07b4be9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hteapot" -version = "0.4.1" +version = "0.4.2" exclude = ["hteapot.toml", "public/", "readme.md"] license = "MIT" keywords = ["HTTP", "HTTP-SERVER"]