simbld_http/responses/
server.rs

1/// The above Rust code defines an enum representing HTTP server response codes with associated descriptions and provides functions to retrieve specific response code details.
2use crate::helpers::{from_u16_helper::FromU16, to_u16_helper::ToU16};
3use num_enum::{IntoPrimitive, TryFromPrimitive};
4use strum::EnumProperty;
5use strum_macros::{Display, EnumIter, EnumProperty};
6
7#[derive(
8  Display, IntoPrimitive, TryFromPrimitive, EnumProperty, EnumIter, Debug, Copy, Clone, PartialEq,
9)]
10#[repr(u16)]
11
12pub enum ResponsesServerCodes {
13  #[strum(props(
14    Description = "The server encountered an unexpected condition that prevented it from fulfilling the request. This could be due to a misconfiguration, an unhandled exception, or resource exhaustion"
15  ))]
16  InternalServerError = 500,
17  #[strum(props(
18    Description = "The server does not support the functionality required to fulfill the request. This might be because the server does not recognize the request method or lacks the capability to process it"
19  ))]
20  NotImplemented = 501,
21  #[strum(props(
22    Description = "The server, while acting as a gateway or proxy, received an invalid response from an upstream server. This could be due to the upstream server being down or misconfigured"
23  ))]
24  BadGateway = 502, // WARNING: can affect the rate at which Googlebot explanation :: https://http.dev/502
25  #[strum(props(
26    Description = "The server is currently unable to handle the request due to temporary overloading or maintenance. This is usually a temporary state"
27  ))]
28  ServiceUnavailable = 503,
29  #[strum(props(
30    Description = "The server, while acting as a gateway or proxy, did not receive a timely response from the upstream server. This could be due to network congestion or the upstream server being overloaded"
31  ))]
32  GatewayTimeout = 504,
33  #[strum(props(
34    Description = "The server does not support the HTTP protocol version used in the request. This prevents the server from processing the request"
35  ))]
36  HTTPVersionNotSupported = 505,
37  #[strum(props(
38    Description = "The server encountered a configuration error in transparent content negotiation. This resulted in a circular reference that prevents the server from serving the requested content"
39  ))]
40  VariantAlsoNegotiates = 506,
41  #[strum(props(
42    Description = "The server is unable to store the representation needed to complete the request. This could be due to storage limits being reached or allocation constraints"
43  ))]
44  InsufficientStorage = 507,
45  #[strum(props(
46    Description = "The server detected an infinite loop while processing a request. This is often due to circular references or recursive function calls in WebDAV configurations, RFC 5842"
47  ))]
48  LoopDetected = 508,
49  #[strum(props(
50    Description = "The server's bandwidth limit has been exceeded. This limit is typically set by the administrator and prevents further data transfer until the limit resets, often used by hosting providers to prevent abuse, apache, unofficial, Cpanel"
51  ))]
52  BandwidthLimitExceeded = 509,
53  #[strum(props(
54    Description = "The server requires further extensions to fulfill the request. This could mean additional client conditions or protocol extensions are necessary before the server can process the request"
55  ))]
56  NotExtended = 510,
57  #[strum(props(
58    Description = "The network connection requires authentication before accessing the requested resources. This is often used by captive portals to redirect users to a login page"
59  ))]
60  NetworkAuthenticationRequired = 511,
61  #[strum(props(
62    Description = "An unspecified error occurred, and the server was unable to provide more details. This is a catch-all for unexpected conditions"
63  ))]
64  UnknownError = 520,
65  #[strum(props(
66    Description = "Cloudflare, unofficial is currently unreachable, likely due to downtime or maintenance. This prevents the server from processing the request, and the client should try again later"
67  ))]
68  WebServerIsDown = 521,
69  #[strum(props(
70    Description = "The connection to the server timed out before a response could be received. This could be due to network issues or server overload"
71  ))]
72  ConnectionTimedOut = 522,
73  #[strum(props(
74    Description = "The origin server could not be contacted. This might be due to network issues or misconfiguration"
75  ))]
76  OriginIsUnreachable = 523,
77  #[strum(props(
78    Description = "The operation timed out while waiting for a response from the server. This could be due to network congestion or server overload"
79  ))]
80  TimeoutOccurred = 524,
81  #[strum(props(
82    Description = "The SSL/TLS handshake failed, preventing a secure connection from being established. This could be due to certificate issues or network problems"
83  ))]
84  SSLHandshakeFailed = 525,
85  #[strum(props(
86    Description = "The SSL/TLS certificate provided by the server is invalid, expired, or does not match the requested domain. This prevents the secure connection from being established"
87  ))]
88  InvalidSSLCertificate = 526,
89  #[strum(props(
90    Description = "An error occurred in the Railgun service, which accelerates connections between Cloudflare and the origin server. This may indicate a misconfiguration or temporary service unavailability"
91  ))]
92  RailgunError = 527,
93  #[strum(props(
94    Description = "Indicates the Qualys server cannot process the request, likely due to high traffic or resource constraints. This is a Qualys-specific status code, unofficial"
95  ))]
96  SiteIsOverloaded = 529,
97  #[strum(props(
98    Description = "Indicates the Pantheon server has been frozen due to inactivity, preventing further requests from being processed. This is a Pantheon-specific status code, unofficial"
99  ))]
100  SiteIsFrozen = 530,
101  #[strum(props(
102    Description = "The origin server encountered a DNS resolution error while attempting to process the request. This typically occurs when the domain name cannot be resolved to an IP address, possibly due to a misconfiguration or network issue"
103  ))]
104  OriginDNSError = 531,
105  #[strum(props(
106    Description = "This error is specific to certain hosting environments. For AWS, it indicates an HTTP Authentication failure, whereas for Pantheon, it means there is a problem with the site configuration"
107  ))]
108  NoSiteDetected = 561,
109  #[strum(props(
110    Description = "This unofficial status code indicates that the HTTP requests executed by the code failed because no local network was found or the HTTP connections to the local network returned read timeouts"
111  ))]
112  NetworkReadTimeoutError = 598,
113  #[strum(props(
114    Description = "This unofficial status code indicates that the HTTP requests executed by the code failed because no local network was found or the HTTP connections to the local network timed out"
115  ))]
116  NetworkConnectTimeoutError = 599,
117}
118
119impl ToU16 for ResponsesServerCodes {
120  fn to_u16(self) -> u16 {
121    self.into() // Conversion`Into<u16>`
122  }
123}
124
125impl FromU16 for ResponsesServerCodes {
126  fn from_u16(code: u16) -> Option<Self> {
127    Self::try_from(code).ok() // Conversion`TryFrom<u16>`
128  }
129}
130
131impl Into<(u16, &'static str)> for ResponsesServerCodes {
132  fn into(self) -> (u16, &'static str) {
133    let code: u16 = self.to_u16();
134    let description = self.get_str("Description").unwrap_or("No description");
135    (code, description) // Tuple
136  }
137}
138
139pub fn internal_server_error() -> (u16, &'static str) {
140  (500, "The server encountered an unexpected condition that prevented it from fulfilling the request. This could be due to a misconfiguration, an unhandled exception, or resource exhaustion")
141}
142
143pub fn not_implemented() -> (u16, &'static str) {
144  (501, "The server does not support the functionality required to fulfill the request. This might be because the server does not recognize the request method or lacks the capability to process it")
145}
146
147pub fn bad_gateway() -> (u16, &'static str) {
148  (502, "The server, while acting as a gateway or proxy, received an invalid response from an upstream server. This could be due to the upstream server being down or misconfigured")
149}
150
151pub fn service_unavailable() -> (u16, &'static str) {
152  (503, "The server is currently unable to handle the request due to temporary overloading or maintenance. This is usually a temporary state")
153}
154
155pub fn gateway_timeout() -> (u16, &'static str) {
156  (504, "The server, while acting as a gateway or proxy, did not receive a timely response from the upstream server. This could be due to network congestion or the upstream server being overloaded")
157}
158
159pub fn http_version_not_supported() -> (u16, &'static str) {
160  (505, "The server does not support the HTTP protocol version used in the request. This prevents the server from processing the request")
161}
162
163pub fn variant_also_negotiates() -> (u16, &'static str) {
164  (506, "The server encountered a configuration error in transparent content negotiation. This resulted in a circular reference that prevents the server from serving the requested content")
165}
166
167pub fn insufficient_storage() -> (u16, &'static str) {
168  (507, "The server is unable to store the representation needed to complete the request. This could be due to storage limits being reached or allocation constraints")
169}
170
171pub fn loop_detected() -> (u16, &'static str) {
172  (508, "The server detected an infinite loop while processing a request. This is often due to circular references or recursive function calls in WebDAV configurations, RFC 5842")
173}
174
175pub fn bandwidth_limit_exceeded() -> (u16, &'static str) {
176  (509, "The server's bandwidth limit has been exceeded. This limit is typically set by the administrator and prevents further data transfer until the limit resets, often used by hosting providers to prevent abuse, apache, unofficial, Cpanel")
177}
178
179pub fn not_extended() -> (u16, &'static str) {
180  (510, "The server requires further extensions to fulfill the request. This could mean additional client conditions or protocol extensions are necessary before the server can process the request")
181}
182
183pub fn network_authentication_required() -> (u16, &'static str) {
184  (511, "The network connection requires authentication before accessing the requested resources. This is often used by captive portals to redirect users to a login page")
185}
186
187pub fn unknown_error() -> (u16, &'static str) {
188  (520, "An unspecified error occurred, and the server was unable to provide more details. This is a catch-all for unexpected conditions")
189}
190
191pub fn web_server_is_down() -> (u16, &'static str) {
192  (521, "Cloudflare, unofficial is currently unreachable, likely due to downtime or maintenance. This prevents the server from processing the request, and the client should try again later")
193}
194
195pub fn connection_timed_out() -> (u16, &'static str) {
196  (522, "The connection to the server timed out before a response could be received. This could be due to network issues or server overload")
197}
198
199pub fn origin_is_unreachable() -> (u16, &'static str) {
200  (523, "The origin server could not be contacted. This might be due to network issues or misconfiguration")
201}
202
203pub fn timeout_occurred() -> (u16, &'static str) {
204  (524, "The operation timed out while waiting for a response from the server. This could be due to network congestion or server overload")
205}
206
207pub fn ssl_handshake_failed() -> (u16, &'static str) {
208  (525, "The SSL/TLS handshake failed, preventing a secure connection from being established. This could be due to certificate issues or network problems")
209}
210
211pub fn invalid_ssl_certificate() -> (u16, &'static str) {
212  (526, "The SSL/TLS certificate provided by the server is invalid, expired, or does not match the requested domain. This prevents the secure connection from being established")
213}
214
215pub fn railgun_error() -> (u16, &'static str) {
216  (527, "An error occurred in the Railgun service, which accelerates connections between Cloudflare and the origin server. This may indicate a misconfiguration or temporary service unavailability")
217}
218
219pub fn site_is_overloaded() -> (u16, &'static str) {
220  (529, "Indicates the Qualys server cannot process the request, likely due to high traffic or resource constraints. This is a Qualys-specific status code, unofficial")
221}
222
223pub fn site_is_frozen() -> (u16, &'static str) {
224  (530, "Indicates the Pantheon server has been frozen due to inactivity, preventing further requests from being processed. This is a Pantheon-specific status code, unofficial")
225}
226
227pub fn origin_dns_error() -> (u16, &'static str) {
228  (531, "The origin server encountered a DNS resolution error while attempting to process the request. This typically occurs when the domain name cannot be resolved to an IP address, possibly due to a misconfiguration or network issue")
229}
230
231pub fn no_site_detected() -> (u16, &'static str) {
232  (561, "This error is specific to certain hosting environments. For AWS, it indicates an HTTP Authentication failure, whereas for Pantheon, it means there is a problem with the site configuration")
233}
234
235pub fn network_read_timeout_error() -> (u16, &'static str) {
236  (598, "This unofficial status code indicates that the HTTP requests executed by the code failed because no local network was found or the HTTP connections to the local network returned read timeouts")
237}
238
239pub fn network_connect_timeout_error() -> (u16, &'static str) {
240  (599, "This unofficial status code indicates that the HTTP requests executed by the code failed because no local network was found or the HTTP connections to the local network timed out")
241}
242
243#[cfg(test)]
244mod tests {
245  use super::*;
246
247  #[test]
248  fn test_generated_function_internal_server_error() {
249    let response = ResponsesServerCodes::InternalServerError;
250    let (code, description) = response.into();
251    assert_eq!(code, 500);
252    assert_eq!(description, "The server encountered an unexpected condition that prevented it from fulfilling the request. This could be due to a misconfiguration, an unhandled exception, or resource exhaustion");
253  }
254
255  #[test]
256  fn test_to_16_not_implemented() {
257    let response = ResponsesServerCodes::NotImplemented;
258    let code = response.to_u16();
259    assert_eq!(code, 501);
260  }
261
262  #[test]
263  fn test_bad_gateway() {
264    assert_eq!(bad_gateway(), (502, "The server, while acting as a gateway or proxy, received an invalid response from an upstream server. This could be due to the upstream server being down or misconfigured"));
265  }
266
267  #[test]
268  fn test_from_u16_service_unavailable() {
269    let response = ResponsesServerCodes::from_u16(503);
270    assert_eq!(response, Some(ResponsesServerCodes::ServiceUnavailable));
271  }
272}