Skip to main content

arara_cgi/
response.rs

1use std::collections::HashMap;
2use std::{fmt, mem, ptr};
3
4pub enum StatusCode {
5    Custom(u32, String),
6
7    Continue,
8    SwitchingProtocols,
9    Processing,
10    EarlyHints,
11
12    OK,
13    Created,
14    Accepted,
15    NonAuthoritativeInformation,
16    NoContent,
17    ResetContent,
18    PartialContent,
19    MultiStatus,
20    AlreadyReported,
21    IMUsed,
22
23    MultipleChoices,
24    MovedPermanently,
25    Found,
26    SeeOther,
27    NotModified,
28    UseProxy,
29    SwitchProxy,
30    TemporaryRedirect,
31    PermanentRedirect,
32
33    BadRequest, 
34    Unauthorized,
35    PaymentRequired,
36    Forbidden,
37    NotFound,
38    MethodNotAllowed,
39    NotAcceptable,
40    ProxyAuthenticationRequired,
41    RequestTimeout,
42    Conflict,
43    Gone,
44    LengthRequired,
45    PreconditionFailed,
46    PayloadTooLarge,
47    URITooLong,
48    UnsupportedMediaType,
49    RangeNotSatisfiable,
50    ExpectationFailed,
51    IMTeapot,
52    MisdirectedRequest,
53    UnprocessableContent,
54    Locked,
55    FailedDependency,
56    TooEarly,
57    UpgradeRequired,
58    PreconditionRequired,
59    TooManyRequests,
60    RequestHeaderFieldsTooLarge,
61    UnavailableForLegalReasons,
62
63    InternalServerError,
64    NotImplemented,
65    BadGateway,
66    ServiceUnavailable,
67    GatewayTimeout,
68    HTTPVersionNotSupported,
69    VariantAlsoNegotiates,
70    InsufficientStorage,
71    LoopDetected,
72    NotExtended,
73    NetworkAuthenticationRequired
74}
75
76impl fmt::Display for StatusCode {
77    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78        let status_str: String = match self {
79            Self::Custom(code, status) => format!("{} {}", code, status),
80
81            Self::Continue => String::from("100 Continue"),
82            Self::SwitchingProtocols => String::from("101 Switching Protocols"),
83            Self::Processing => String::from("102 Processing"),
84            Self::EarlyHints => String::from("103 Early Hints"),
85
86            Self::OK => String::from("200 OK"),
87            Self::Created => String::from("201 Created"),
88            Self::Accepted => String::from("202 Accepted"),
89            Self::NonAuthoritativeInformation => String::from("203 Non-Authoritative Information"),
90            Self::NoContent => String::from("204 No Content"),
91            Self::ResetContent => String::from("205 Reset Content"),
92            Self::PartialContent => String::from("206 Partial Content"),
93            Self::MultiStatus => String::from("207 Multi-Status"),
94            Self::AlreadyReported => String::from("208 Already Reported"),
95            Self::IMUsed => String::from("226 IM Used"),
96
97            Self::MultipleChoices => String::from("300 Multiple Choices"),
98            Self::MovedPermanently => String::from("301 Moved Permanently"),
99            Self::Found => String::from("302 Found"),
100            Self::SeeOther => String::from("303 See Other"),
101            Self::NotModified => String::from("304 Not Modified"),
102            Self::UseProxy => String::from("305 Use Proxy"),
103            Self::SwitchProxy => String::from("306 Switch Proxy"),
104            Self::TemporaryRedirect => String::from("307 Temporary Redirect"),
105            Self::PermanentRedirect => String::from("308 Permanent Redirect"),
106
107            Self::BadRequest => String::from("400 Bad Request"), 
108            Self::Unauthorized => String::from("401 Unauthorized"),
109            Self::PaymentRequired => String::from("402 Payment Required"),
110            Self::Forbidden => String::from("403 Forbidden"),
111            Self::NotFound => String::from("404 Not Found"),
112            Self::MethodNotAllowed => String::from("405 Method Not Allowed"),
113            Self::NotAcceptable => String::from("406 Not Acceptable"),
114            Self::ProxyAuthenticationRequired => String::from("407 Proxy Authentication Required"),
115            Self::RequestTimeout => String::from("408 Request Timeout"),
116            Self::Conflict => String::from("409 Conflict"),
117            Self::Gone => String::from("410 Gone"),
118            Self::LengthRequired => String::from("411 Length Required"),
119            Self::PreconditionFailed => String::from("412 Precondition Failed"),
120            Self::PayloadTooLarge => String::from("413 Payload Too Large"),
121            Self::URITooLong => String::from("414 URI Too Long"),
122            Self::UnsupportedMediaType => String::from("415 Unsupported Media Type"),
123            Self::RangeNotSatisfiable => String::from("416 Range Not Satisfiable"),
124            Self::ExpectationFailed => String::from("417 Expectation Failed"),
125            Self::IMTeapot => String::from("418 I'm a teapot"),
126            Self::MisdirectedRequest => String::from("421 Misdirected Request"),
127            Self::UnprocessableContent => String::from("422 Unprocessable Entity"),
128            Self::Locked => String::from("423 Locked"),
129            Self::FailedDependency => String::from("424 Failed Dependency"),
130            Self::TooEarly => String::from("425 Too Early"),
131            Self::UpgradeRequired => String::from("426 Upgrade Required"),
132            Self::PreconditionRequired => String::from("428 Precondition Required"),
133            Self::TooManyRequests => String::from("429 Too Many Requests"),
134            Self::RequestHeaderFieldsTooLarge => String::from("431 Request Header Fields Too Large"),
135            Self::UnavailableForLegalReasons => String::from("451 Unavailable For Legal Reasons"),
136
137            Self::InternalServerError => String::from("500 Internal Server Error"),
138            Self::NotImplemented => String::from("501 Not Implemented"),
139            Self::BadGateway => String::from("502 Bad Gateway"),
140            Self::ServiceUnavailable => String::from("503 Service Unavailable"),
141            Self::GatewayTimeout => String::from("504 Gateway Timeout"),
142            Self::HTTPVersionNotSupported => String::from("505 HTTP Version Not Supported"),
143            Self::VariantAlsoNegotiates => String::from("506 Variant Also Negotiates"),
144            Self::InsufficientStorage => String::from("507 Insufficient Storage"),
145            Self::LoopDetected => String::from("508 Loop Detected"),
146            Self::NotExtended => String::from("510 Not Extended"),
147            Self::NetworkAuthenticationRequired => String::from("511 Network Authentication Required"),
148        };
149
150        write!(f, "{}", status_str)
151    }
152
153}
154
155pub struct Response {
156    pub status: StatusCode,
157    pub content_type: &'static str,
158    pub headers: HashMap<String, String>,
159    pub body: &'static str,
160}
161
162impl Response {
163    pub fn new() -> Self {
164        Self {
165            status: StatusCode::OK,
166            content_type: "",
167            headers: HashMap::new(),
168            body: ""
169        }
170    }
171}