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
|
use std::vec::Vec;
#[derive(Copy, Clone)]
pub enum Status {
Input = 10,
Success = 20,
Redirect = 30,
TemporaryFailure = 40,
PermanentFailure = 50,
ClientCertificateRequired = 60,
}
pub struct Header {
status: Status,
meta: String,
}
pub struct Response {
header: Header,
data: Vec<u8>,
}
impl Header {
pub fn new(status: Status, meta: &str) -> Header {
Header {
status: status,
meta: meta.to_string(),
}
}
pub fn format(&self) -> String {
format!("{} {}\r\n", self.status as u8, self.meta)
}
}
impl Response {
pub fn new(header: Header, data: Vec<u8>) -> Response {
Response {
header: header,
data: data,
}
}
pub fn new_empty(header: Header) -> Response {
Response {
header: header,
data: Vec::new(),
}
}
pub fn format(&self) -> Vec<u8> {
let mut resp: Vec<u8> = self.header.format().as_bytes().to_vec();
resp.extend(&self.data);
return resp;
}
}
pub fn invalid_protocol() -> Response {
Response::new_empty(Header::new(
Status::PermanentFailure,
"this protocol is not supported",
))
}
pub fn not_understood() -> Response {
Response::new_empty(Header::new(
Status::PermanentFailure,
"request not understood",
))
}
pub fn internal_error() -> Response {
Response::new_empty(Header::new(
Status::PermanentFailure,
"internal server error",
))
}
|