1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// In the name of Allah

//! List of HTTP response status codes

#![crate_name = "httpstatus"]

use std::convert::From;
use std::fmt::{Display, Error, Formatter};

/// Represents an HTTP status code
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Clone, Hash)]
pub enum StatusCode {
    /// 100 Continue (RFC 7231)
    Continue,

    /// 101 Switching Protocols (RFC 7231)
    SwitchingProtocols,

    /// 102 Processing (RFC 2518)
    Processing,

    /// 103 Early Hints (RFC 8297)
    EarlyHints,

    /// 200 OK (RFC 7231)
    Ok,

    /// 201 Created (RFC 7231)
    Created,

    /// 202 Accepted (RFC 7231)
    Accepted,

    /// 203 Non-Authoritative Information (RFC 7231)
    NonAuthoritativeInformation,

    /// 204 No Content (RFC 7231)
    NoContent,

    /// 205 Reset Content (RFC 7231)
    ResetContent,

    /// 206 Partial Content (RFC 7233)
    PartialContent,

    /// 207 Multi-Status (RFC 4918)
    MultiStatus,

    /// 208 Already Reported (RFC 5842)
    AlreadyReported,

    /// 226 IM Used (RFC 3229)
    IMUsed,

    /// 300 Multiple Choices (RFC 7231)
    MultipleChoices,

    /// 301 Moved Permanently (RFC 7231)
    MovedPermanently,

    /// 302 Found (RFC 7231)
    Found,

    /// 303 See Other (RFC 7231)
    SeeOther,

    /// 304 Not Modified (RFC 7232)
    NotModified,

    /// 305 Use Proxy (RFC 7231)
    UseProxy,

    /// 306 Switch Proxy (RFC 7231)
    SwitchProxy,

    /// 307 Temporary Redirect (RFC 7231)
    TemporaryRedirect,

    /// 308 Permanent Redirect (RFC 7538)
    PermanentRedirect,

    /// 400 Bad Request (RFC 7231)
    BadRequest,

    /// 401 Unauthorized (RFC 7235)
    Unauthorized,

    /// 402 Payment Required (RFC 7231)
    PaymentRequired,

    /// 403 Forbidden (RFC 7231)
    Forbidden,

    /// 404 Not Found (RFC 7231)
    NotFound,

    /// 405 Method Not Allowed (RFC 7231)
    MethodNotAllowed,

    /// 406 Not Acceptable (RFC 7231)
    NotAcceptable,

    /// 407 Proxy Authentication Required (RFC 7235)
    ProxyAuthenticationRequired,

    /// 408 Request Timeout (RFC 7231)
    RequestTimeout,

    /// 409 Conflict (RFC 7231)
    Conflict,

    /// 410 Gone (RFC 7231)
    Gone,

    /// 411 Length Required (RFC 7231)
    LengthRequired,

    /// 412 Precondition Failed (RFC 7232)
    PreconditionFailed,

    /// 413 Payload Too Large (RFC 7231)
    PayloadTooLarge,

    /// 414 URI Too Long (RFC 7231)
    UriTooLong,

    /// 415 Unsupported Media Type (RFC 7231)
    UnsupportedMediaType,

    /// 416 Range Not Satisfiable (RFC 7233)
    RangeNotSatisfiable,

    /// 417 Expectation Failed (RFC 7231)
    ExpectationFailed,

    /// 418 I'm a teapot (RFC 2324)
    ImATeapot,

    /// 421 Misdirected Request (RFC 7540)
    MisdirectedRequest,

    /// 422 Unprocessable Entity (RFC 4918)
    UnprocessableEntity,

    /// 423 Locked (RFC 4918)
    Locked,

    /// 424 Failed Dependency (RFC 4918)
    FailedDependency,

    /// 426 Upgrade Required (RFC 7231)
    UpgradeRequired,

    /// 428 Precondition Required (RFC 6585)
    PreconditionRequired,

    /// 429 Too Many Requests (RFC 6585)
    TooManyRequests,

    /// 431 Request Header Fields Too Large (RFC 6585)
    RequestHeaderFieldsTooLarge,

    /// 451 Unavailable For Legal Reasons (RFC 7725)
    UnavailableForLegalReasons,

    /// 500 Internal Server Error (RFC 7231)
    InternalServerError,

    /// 501 Not Implemented (RFC 7231)
    NotImplemented,

    /// 502 Bad Gateway (RFC 7231)
    BadGateway,

    /// 503 Service Unavailable (RFC 7231)
    ServiceUnavailable,

    /// 504 Gateway Timeout (RFC 7231)
    GatewayTimeout,

    /// 505 HTTP Version Not Supported (RFC 7231)
    HttpVersionNotSupported,

    /// 506 Variant Also Negotiates (RFC 2295)
    VariantAlsoNegotiates,

    /// 507 Insufficient Storage (RFC 4918)
    InsufficientStorage,

    /// 508 Loop Detected (RFC 5842)
    LoopDetected,

    /// 510 Not Extended (RFC 2774)
    NotExtended,

    /// 511 Network Authentication Required (RFC 6585)
    NetworkAuthenticationRequired,

    /// Unknown status code
    Unknown(u16),
}

/// Represents an HTTP status class
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Clone, Hash)]
pub enum StatusClass {
    /// 1xx Informational
    Informational,

    /// 2xx Success
    Success,

    /// 3xx Redirection
    Redirection,

    /// 4xx Client errors
    ClientError,

    /// 5xx Server errors
    ServerError,

    /// Unknown status class
    Unknown,
}

impl StatusCode {
    /// Returns the numeric status code
    #[inline]
    pub fn as_u16(&self) -> u16 {
        match *self {
            StatusCode::Continue                      => 100,
            StatusCode::SwitchingProtocols            => 101,
            StatusCode::Processing                    => 102,
            StatusCode::EarlyHints                    => 103,
            StatusCode::Ok                            => 200,
            StatusCode::Created                       => 201,
            StatusCode::Accepted                      => 202,
            StatusCode::NonAuthoritativeInformation   => 203,
            StatusCode::NoContent                     => 204,
            StatusCode::ResetContent                  => 205,
            StatusCode::PartialContent                => 206,
            StatusCode::MultiStatus                   => 207,
            StatusCode::AlreadyReported               => 208,
            StatusCode::IMUsed                        => 226,
            StatusCode::MultipleChoices               => 300,
            StatusCode::MovedPermanently              => 301,
            StatusCode::Found                         => 302,
            StatusCode::SeeOther                      => 303,
            StatusCode::NotModified                   => 304,
            StatusCode::UseProxy                      => 305,
            StatusCode::SwitchProxy                   => 306,
            StatusCode::TemporaryRedirect             => 307,
            StatusCode::PermanentRedirect             => 308,
            StatusCode::BadRequest                    => 400,
            StatusCode::Unauthorized                  => 401,
            StatusCode::PaymentRequired               => 402,
            StatusCode::Forbidden                     => 403,
            StatusCode::NotFound                      => 404,
            StatusCode::MethodNotAllowed              => 405,
            StatusCode::NotAcceptable                 => 406,
            StatusCode::ProxyAuthenticationRequired   => 407,
            StatusCode::RequestTimeout                => 408,
            StatusCode::Conflict                      => 409,
            StatusCode::Gone                          => 410,
            StatusCode::LengthRequired                => 411,
            StatusCode::PreconditionFailed            => 412,
            StatusCode::PayloadTooLarge               => 413,
            StatusCode::UriTooLong                    => 414,
            StatusCode::UnsupportedMediaType          => 415,
            StatusCode::RangeNotSatisfiable           => 416,
            StatusCode::ExpectationFailed             => 417,
            StatusCode::ImATeapot                     => 418,
            StatusCode::MisdirectedRequest            => 421,
            StatusCode::UnprocessableEntity           => 422,
            StatusCode::Locked                        => 423,
            StatusCode::FailedDependency              => 424,
            StatusCode::UpgradeRequired               => 426,
            StatusCode::PreconditionRequired          => 428,
            StatusCode::TooManyRequests               => 429,
            StatusCode::RequestHeaderFieldsTooLarge   => 431,
            StatusCode::UnavailableForLegalReasons    => 451,
            StatusCode::InternalServerError           => 500,
            StatusCode::NotImplemented                => 501,
            StatusCode::BadGateway                    => 502,
            StatusCode::ServiceUnavailable            => 503,
            StatusCode::GatewayTimeout                => 504,
            StatusCode::HttpVersionNotSupported       => 505,
            StatusCode::VariantAlsoNegotiates         => 506,
            StatusCode::InsufficientStorage           => 507,
            StatusCode::LoopDetected                  => 508,
            StatusCode::NotExtended                   => 510,
            StatusCode::NetworkAuthenticationRequired => 511,
            StatusCode::Unknown(code)                 => code,
        }
    }

    /// Returns the reason phrase of HTTP status code
    #[inline]
    pub fn reason_phrase(&self) -> &'static str {
        match *self {
            StatusCode::Continue                      => "Continue",
            StatusCode::SwitchingProtocols            => "Switching Protocols",
            StatusCode::Processing                    => "Processing",
            StatusCode::EarlyHints                    => "Early Hints",
            StatusCode::Ok                            => "OK",
            StatusCode::Created                       => "Created",
            StatusCode::Accepted                      => "Accepted",
            StatusCode::NonAuthoritativeInformation   => "Non-Authoritative Information",
            StatusCode::NoContent                     => "No Content",
            StatusCode::ResetContent                  => "Reset Content",
            StatusCode::PartialContent                => "Partial Content",
            StatusCode::MultiStatus                   => "Multi-Status",
            StatusCode::AlreadyReported               => "Already Reported",
            StatusCode::IMUsed                        => "IM Used",
            StatusCode::MultipleChoices               => "Multiple Choices",
            StatusCode::MovedPermanently              => "Moved Permanently",
            StatusCode::Found                         => "Found",
            StatusCode::SeeOther                      => "See Other",
            StatusCode::NotModified                   => "Not Modified",
            StatusCode::UseProxy                      => "Use Proxy",
            StatusCode::SwitchProxy                   => "Switch Proxy",
            StatusCode::TemporaryRedirect             => "Temporary Redirect",
            StatusCode::PermanentRedirect             => "Permanent Redirect",
            StatusCode::BadRequest                    => "Bad Request",
            StatusCode::Unauthorized                  => "Unauthorized",
            StatusCode::PaymentRequired               => "Payment Required",
            StatusCode::Forbidden                     => "Forbidden",
            StatusCode::NotFound                      => "Not Found",
            StatusCode::MethodNotAllowed              => "Method Not Allowed",
            StatusCode::NotAcceptable                 => "Not Acceptable",
            StatusCode::ProxyAuthenticationRequired   => "Proxy Authentication Required",
            StatusCode::RequestTimeout                => "Request Timeout",
            StatusCode::Conflict                      => "Conflict",
            StatusCode::Gone                          => "Gone",
            StatusCode::LengthRequired                => "Length Required",
            StatusCode::PreconditionFailed            => "Precondition Failed",
            StatusCode::PayloadTooLarge               => "Payload Too Large",
            StatusCode::UriTooLong                    => "URI Too Long",
            StatusCode::UnsupportedMediaType          => "Unsupported Media Type",
            StatusCode::RangeNotSatisfiable           => "Range Not Satisfiable",
            StatusCode::ExpectationFailed             => "Expectation Failed",
            StatusCode::ImATeapot                     => "I'm a teapot",
            StatusCode::MisdirectedRequest            => "Misdirected Request",
            StatusCode::UnprocessableEntity           => "Unprocessable Entity",
            StatusCode::Locked                        => "Locked",
            StatusCode::FailedDependency              => "Failed Dependency",
            StatusCode::UpgradeRequired               => "Upgrade Required",
            StatusCode::PreconditionRequired          => "Precondition Required",
            StatusCode::TooManyRequests               => "Too Many Requests",
            StatusCode::RequestHeaderFieldsTooLarge   => "Request Header Fields Too Large",
            StatusCode::UnavailableForLegalReasons    => "Unavailable For Legal Reasons",
            StatusCode::InternalServerError           => "Internal Server Error",
            StatusCode::NotImplemented                => "Not Implemented",
            StatusCode::BadGateway                    => "Bad Gateway",
            StatusCode::ServiceUnavailable            => "Service Unavailable",
            StatusCode::GatewayTimeout                => "Gateway Timeout",
            StatusCode::HttpVersionNotSupported       => "Http Version Not Supported",
            StatusCode::VariantAlsoNegotiates         => "Variant Also Negotiates",
            StatusCode::InsufficientStorage           => "Insufficient Storage",
            StatusCode::LoopDetected                  => "Loop Detected",
            StatusCode::NotExtended                   => "Not Extended",
            StatusCode::NetworkAuthenticationRequired => "Network Authentication Required",
            StatusCode::Unknown(_)                    => "",
        }
    }

    /// Returns the status class of status code
    #[inline]
    pub fn class(&self) -> StatusClass {
        match self.as_u16() {
            100...199 => StatusClass::Informational,
            200...299 => StatusClass::Success,
            300...399 => StatusClass::Redirection,
            400...499 => StatusClass::ClientError,
            500...599 => StatusClass::ServerError,
            _ => StatusClass::Unknown,
        }
    }
}

impl From<u16> for StatusCode {
    fn from(code: u16) -> Self {
        match code {
            100 => StatusCode::Continue,
            101 => StatusCode::SwitchingProtocols,
            102 => StatusCode::Processing,
            103 => StatusCode::EarlyHints,
            200 => StatusCode::Ok,
            201 => StatusCode::Created,
            202 => StatusCode::Accepted,
            203 => StatusCode::NonAuthoritativeInformation,
            204 => StatusCode::NoContent,
            205 => StatusCode::ResetContent,
            206 => StatusCode::PartialContent,
            207 => StatusCode::MultiStatus,
            208 => StatusCode::AlreadyReported,
            226 => StatusCode::IMUsed,
            300 => StatusCode::MultipleChoices,
            301 => StatusCode::MovedPermanently,
            302 => StatusCode::Found,
            303 => StatusCode::SeeOther,
            304 => StatusCode::NotModified,
            305 => StatusCode::UseProxy,
            306 => StatusCode::SwitchProxy,
            307 => StatusCode::TemporaryRedirect,
            308 => StatusCode::PermanentRedirect,
            400 => StatusCode::BadRequest,
            401 => StatusCode::Unauthorized,
            402 => StatusCode::PaymentRequired,
            403 => StatusCode::Forbidden,
            404 => StatusCode::NotFound,
            405 => StatusCode::MethodNotAllowed,
            406 => StatusCode::NotAcceptable,
            407 => StatusCode::ProxyAuthenticationRequired,
            408 => StatusCode::RequestTimeout,
            409 => StatusCode::Conflict,
            410 => StatusCode::Gone,
            411 => StatusCode::LengthRequired,
            412 => StatusCode::PreconditionFailed,
            413 => StatusCode::PayloadTooLarge,
            414 => StatusCode::UriTooLong,
            415 => StatusCode::UnsupportedMediaType,
            416 => StatusCode::RangeNotSatisfiable,
            417 => StatusCode::ExpectationFailed,
            418 => StatusCode::ImATeapot,
            421 => StatusCode::MisdirectedRequest,
            422 => StatusCode::UnprocessableEntity,
            423 => StatusCode::Locked,
            424 => StatusCode::FailedDependency,
            426 => StatusCode::UpgradeRequired,
            428 => StatusCode::PreconditionRequired,
            429 => StatusCode::TooManyRequests,
            431 => StatusCode::RequestHeaderFieldsTooLarge,
            451 => StatusCode::UnavailableForLegalReasons,
            500 => StatusCode::InternalServerError,
            501 => StatusCode::NotImplemented,
            502 => StatusCode::BadGateway,
            503 => StatusCode::ServiceUnavailable,
            504 => StatusCode::GatewayTimeout,
            505 => StatusCode::HttpVersionNotSupported,
            506 => StatusCode::VariantAlsoNegotiates,
            507 => StatusCode::InsufficientStorage,
            508 => StatusCode::LoopDetected,
            510 => StatusCode::NotExtended,
            511 => StatusCode::NetworkAuthenticationRequired,
            _   => StatusCode::Unknown(code),
        }
    }
}

impl Display for StatusCode {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        write!(f, "{}{}{}", self.as_u16(), if let StatusCode::Unknown(_) = *self {
            ""
        } else {
            " "
        }, self.reason_phrase())
    }
}