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
#[derive(Debug, PartialEq, Copy, Clone)]
#[allow(missing_docs)]
pub enum Status {
Continue,
SwitchingProtocol,
Ok,
Created,
Accepted,
NonAuthoritativeInformation,
NoContent,
ResetContent,
PartialContent,
MultipleChoices,
MovedPermanently,
Found,
SeeOther,
NotModified,
UseProxy,
TemporaryRedirect,
PermanentRedirect,
BadRequest,
Unauthorized,
PaymentRequired,
Forbidden,
NotFound,
MethodNotAllowed,
NotAcceptable,
ProxyAuthenticationRequired,
RequestTimeout,
Conflict,
Gone,
LengthRequired,
PreconditionFailed,
RequestEntityTooLarge,
RequestURITooLong,
UnsupportedMediaType,
RequestRangeNotSatisfiable,
ExpectationFailed,
UpgradeRequired,
TooManyRequests,
InternalServerError,
NotImplemented,
BadGateway,
ServiceUnavailable,
GatewayTimeout,
VersionNotSupported,
}
impl Status {
pub fn code(&self) -> u16 {
match *self {
Status::Continue => 100,
Status::SwitchingProtocol => 101,
Status::Ok => 200,
Status::Created => 201,
Status::Accepted => 202,
Status::NonAuthoritativeInformation => 203,
Status::NoContent => 204,
Status::ResetContent => 205,
Status::PartialContent => 206,
Status::MultipleChoices => 300,
Status::MovedPermanently => 301,
Status::Found => 302,
Status::SeeOther => 303,
Status::NotModified => 304,
Status::UseProxy => 305,
Status::TemporaryRedirect => 307,
Status::PermanentRedirect => 308,
Status::BadRequest => 400,
Status::Unauthorized => 401,
Status::PaymentRequired => 402,
Status::Forbidden => 403,
Status::NotFound => 404,
Status::MethodNotAllowed => 405,
Status::NotAcceptable => 406,
Status::ProxyAuthenticationRequired => 407,
Status::RequestTimeout => 408,
Status::Conflict => 409,
Status::Gone => 410,
Status::LengthRequired => 411,
Status::PreconditionFailed => 412,
Status::RequestEntityTooLarge => 413,
Status::RequestURITooLong => 414,
Status::UnsupportedMediaType => 415,
Status::RequestRangeNotSatisfiable => 416,
Status::ExpectationFailed => 417,
Status::UpgradeRequired => 426,
Status::TooManyRequests => 429,
Status::InternalServerError => 500,
Status::NotImplemented => 501,
Status::BadGateway => 502,
Status::ServiceUnavailable => 503,
Status::GatewayTimeout => 504,
Status::VersionNotSupported => 505,
}
}
pub fn reason(&self) -> &'static str {
match self.code() {
100 => "Continue",
101 => "Switching Protocol",
200 => "OK",
201 => "Created",
202 => "Accepted",
203 => "Non-Authoriative Information",
204 => "No Content",
205 => "Reset Content",
206 => "Partial Content",
300 => "Multiple Choice",
301 => "Moved Permanently",
302 => "Found",
303 => "See Other",
304 => "Not Modified",
305 => "Use Proxy",
307 => "Temporary Redirect",
308 => "Permanent Redirect",
400 => "Bad Request",
401 => "Unauthorized",
402 => "Payment Required",
403 => "Forbidden",
404 => "Not Found",
405 => "Method Not Allowed",
406 => "Not Acceptable",
407 => "Proxy Authentication Required",
408 => "Request Timeout",
409 => "Conflict",
410 => "Gone",
411 => "Length Required",
412 => "Precondition Failed",
413 => "Request Entity Too Large",
414 => "Request-URI Too Long",
415 => "Unsupported Media Type",
416 => "Request Range Not Satisfiable",
417 => "Expectation Failed",
426 => "Upgrade Required",
429 => "Too Many Requests",
500 => "Internal Server Error",
501 => "Not Implemented",
502 => "Bad Gateway",
503 => "Service Unavailable",
504 => "Gateway Timeout",
505 => "HTTP Version Not Supported",
_ => "Unknown",
}
}
pub fn response_has_body(&self) -> bool {
match self.code() {
100...199 | 204 | 304 => false,
_ => true,
}
}
pub fn from(code: u16) -> Option<Status> {
use self::Status::*;
let s = match code {
100 => Continue,
101 => SwitchingProtocol,
200 => Ok,
201 => Created,
202 => Accepted,
203 => NonAuthoritativeInformation,
204 => NoContent,
205 => ResetContent,
206 => PartialContent,
300 => MultipleChoices,
301 => MovedPermanently,
302 => Found,
303 => SeeOther,
304 => NotModified,
305 => UseProxy,
307 => TemporaryRedirect,
308 => PermanentRedirect,
400 => BadRequest,
401 => Unauthorized,
402 => PaymentRequired,
403 => Forbidden,
404 => NotFound,
405 => MethodNotAllowed,
406 => NotAcceptable,
407 => ProxyAuthenticationRequired,
408 => RequestTimeout,
409 => Conflict,
410 => Gone,
411 => LengthRequired,
412 => PreconditionFailed,
413 => RequestEntityTooLarge,
414 => RequestURITooLong,
415 => UnsupportedMediaType,
416 => RequestRangeNotSatisfiable,
417 => ExpectationFailed,
426 => UpgradeRequired,
429 => TooManyRequests,
500 => InternalServerError,
501 => NotImplemented,
502 => BadGateway,
503 => ServiceUnavailable,
504 => GatewayTimeout,
505 => VersionNotSupported,
_ => return None,
};
Some(s)
}
}