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
pub mod statuses {

    pub fn status(status: u16) -> u16 {
        let code_list = [100, 101, 102, 200, 201, 202, 203, 204, 205, 206, 207, 208, 226, 300, 301, 302, 303, 304, 305, 306, 307, 308, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 421, 422, 423, 424, 425, 426, 428, 429, 431, 451, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511];
        let is_valid = code_list.contains(&status);
        if is_valid{
            status
        }else{
            panic!("invalid status code:{}", &status);
        }
    }

    pub fn code(code: u16) -> &'static str {
        match code {
            100 => "Continue",
            101 => "Switching Protocols",
            102 => "Processing",
            200 => "OK",
            201 => "Created",
            202 => "Accepted",
            203 => "Non-Authoritative Information",
            204 => "No Content",
            205 => "Reset Content",
            206 => "Partial Content",
            207 => "Multi-Status",
            208 => "Already Reported",
            226 => "IM Used",
            300 => "Multiple Choices",
            301 => "Moved Permanently",
            302 => "Found",
            303 => "See Other",
            304 => "Not Modified",
            305 => "Use Proxy",
            306 => "(Unused)",
            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 => "Payload Too Large",
            414 => "URI Too Long",
            415 => "Unsupported Media Type",
            416 => "Range Not Satisfiable",
            417 => "Expectation Failed",
            418 => "I'm a teapot",
            421 => "Misdirected Request",
            422 => "Unprocessable Entity",
            423 => "Locked",
            424 => "Failed Dependency",
            425 => "Unordered Collection",
            426 => "Upgrade Required",
            428 => "Precondition Required",
            429 => "Too Many Requests",
            431 => "Request Header Fields Too Large",
            451 => "Unavailable For Legal Reasons",
            500 => "Internal Server Error",
            501 => "Not Implemented",
            502 => "Bad Gateway",
            503 => "Service Unavailable",
            504 => "Gateway Timeout",
            505 => "HTTP Version Not Supported",
            506 => "Variant Also Negotiates",
            507 => "Insufficient Storage",
            508 => "Loop Detected",
            509 => "Bandwidth Limit Exceeded",
            510 => "Not Extended",
            511 => "Network Authentication Required",
            _ => panic!("invalid status code:{}", &code),
        }
    }

    pub fn message(message: &str) -> u16 {
        match message {
            "Continue" => 100,
            "continue" => 100,
            "Switching Protocols" => 101,
            "switching protocols" => 101,
            "Processing" => 102,
            "processing" => 102,
            "OK" => 200,
            "ok" => 200,
            "Created" => 201,
            "created" => 201,
            "Accepted" => 202,
            "accepted" => 202,
            "Non-Authoritative Information" => 203,
            "non-authoritative information" => 203,
            "No Content" => 204,
            "no content" => 204,
            "Reset Content" => 205,
            "reset content" => 205,
            "Partial Content" => 206,
            "partial content" => 206,
            "Multi-Status" => 207,
            "multi-status" => 207,
            "Already Reported" => 208,
            "already reported" => 208,
            "IM Used" => 226,
            "im used" => 226,
            "Multiple Choices" => 300,
            "multiple choices" => 300,
            "Moved Permanently" => 301,
            "moved permanently" => 301,
            "Found" => 302,
            "found" => 302,
            "See Other" => 303,
            "see other" => 303,
            "Not Modified" => 304,
            "not modified" => 304,
            "Use Proxy" => 305,
            "use proxy" => 305,
            "(Unused)" => 306,
            "(unused)" => 306,
            "Temporary Redirect" => 307,
            "temporary redirect" => 307,
            "Permanent Redirect" => 308,
            "permanent redirect" => 308,
            "Bad Request" => 400,
            "bad request" => 400,
            "Unauthorized" => 401,
            "unauthorized" => 401,
            "Payment Required" => 402,
            "payment required" => 402,
            "Forbidden" => 403,
            "forbidden" => 403,
            "Not Found" => 404,
            "not found" => 404,
            "Method Not Allowed" => 405,
            "method not allowed" => 405,
            "Not Acceptable" => 406,
            "not acceptable" => 406,
            "Proxy Authentication Required" => 407,
            "proxy authentication required" => 407,
            "Request Timeout" => 408,
            "request timeout" => 408,
            "Conflict" => 409,
            "conflict" => 409,
            "Gone" => 410,
            "gone" => 410,
            "Length Required" => 411,
            "length required" => 411,
            "Precondition Failed" => 412,
            "precondition failed" => 412,
            "Payload Too Large" => 413,
            "payload too large" => 413,
            "URI Too Long" => 414,
            "uri too long" => 414,
            "Unsupported Media Type" => 415,
            "unsupported media type" => 415,
            "Range Not Satisfiable" => 416,
            "range not satisfiable" => 416,
            "Expectation Failed" => 417,
            "expectation failed" => 417,
            "I'm a teapot" => 418,
            "i'm a teapot" => 418,
            "Misdirected Request" => 421,
            "misdirected request" => 421,
            "Unprocessable Entity" => 422,
            "unprocessable entity" => 422,
            "Locked" => 423,
            "locked" => 423,
            "Failed Dependency" => 424,
            "failed dependency" => 424,
            "Unordered Collection" => 425,
            "unordered collection" => 425,
            "Upgrade Required" => 426,
            "upgrade required" => 426,
            "Precondition Required" => 428,
            "precondition required" => 428,
            "Too Many Requests" => 429,
            "too many requests" => 429,
            "Request Header Fields Too Large" => 431,
            "request header fields too large" => 431,
            "Unavailable For Legal Reasons" => 451,
            "unavailable for legal reasons" => 451,
            "Internal Server Error" => 500,
            "internal server error" => 500,
            "Not Implemented" => 501,
            "not implemented" => 501,
            "Bad Gateway" => 502,
            "bad gateway" => 502,
            "Service Unavailable" => 503,
            "service unavailable" => 503,
            "Gateway Timeout" => 504,
            "gateway timeout" => 504,
            "HTTP Version Not Supported" => 505,
            "http version not supported" => 505,
            "Variant Also Negotiates" => 506,
            "variant also negotiates" => 506,
            "Insufficient Storage" => 507,
            "insufficient storage" => 507,
            "Loop Detected" => 508,
            "loop detected" => 508,
            "Bandwidth Limit Exceeded" => 509,
            "bandwidth limit exceeded" => 509,
            "Not Extended" => 510,
            "not extended" => 510,
            "Network Authentication Required" => 511,
            "network authentication required" => 511,
            _ => panic!("invalid status message:{}", &message),
        }
    }

    pub fn redirect(code: u16) -> bool {
        match code {
            300 => true,
            301 => true,
            302 => true,
            303 => true,
            305 => true,
            307 => true,
            308 => true,
            _ => false,
        }
    }

    pub fn empty(code: u16) -> bool {
        let valid = status(code);
        match valid {
            204 => true,
            205 => true,
            304 => true,
            _ => false,
        }
    }

    pub fn retry(code: u16) -> bool {
        let valid = status(code);
        match valid {
            502 => true,
            503 => true,
            504 => true,
            _ => false,
        }
    }
}