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
use hyper::header::Host;
use hyper::status::StatusCode;
pub fn get_name_by_http_code(code: u16) -> Option<&'static str> {
let status_code = get_status_from_code(code);
return status_code.canonical_reason();
}
pub fn get_content_type(mimetype: &str, charset: &str) -> String {
if mimetype.starts_with("text/") | (mimetype == "application/xml") |
(mimetype.starts_with("application/") & mimetype.ends_with("+xml")) {
if !mimetype.contains("charset") {
let mut content_type = mimetype.to_string();
content_type = content_type + "; charset=" + charset;
return content_type;
}
}
return mimetype.to_string();
}
pub fn get_host_value(host: &Host) -> String {
match host.port {
None | Some(80) | Some(443) => format!("{}", host.hostname),
Some(port) => format!("{}:{}", host.hostname, port),
}
}
pub fn get_status_from_code(code: u16) -> StatusCode {
match code {
100 => StatusCode::Continue,
101 => StatusCode::SwitchingProtocols,
102 => StatusCode::Processing,
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,
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::PermanentRedirect,
414 => StatusCode::UriTooLong,
415 => StatusCode::UnsupportedMediaType,
416 => StatusCode::RangeNotSatisfiable,
417 => StatusCode::ExpectationFailed,
418 => StatusCode::ImATeapot,
422 => StatusCode::UnprocessableEntity,
423 => StatusCode::Locked,
424 => StatusCode::FailedDependency,
426 => StatusCode::UpgradeRequired,
428 => StatusCode::PreconditionRequired,
429 => StatusCode::TooManyRequests,
431 => StatusCode::RequestHeaderFieldsTooLarge,
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::Unregistered(code),
}
}
#[test]
fn test_get_name_by_http_code() {
let status_name = get_name_by_http_code(200).unwrap();
assert!(status_name == "OK");
}