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
use std::collections::HashMap;
use std::fmt::{Display, Error, Formatter};
use super::url::Tuple;
pub struct Response {
pub status: ResponseStatus,
pub body: String,
headers: Option<HashMap<String, String>>,
}
impl Response {
pub fn get_response_headers(&self) -> Option<impl Iterator<Item=(&str, &str)>> {
if self.headers.is_none() {
return None;
}
Some(self.headers.as_ref().unwrap().iter().map(|(k, v)| {
(k.as_str(), v.as_str())
}))
}
pub fn get_status_code(&self) -> Option<u16> {
self.status.0.get_code()
}
}
pub fn new_response_from_complete(response: String) -> Response {
let lines: Vec<&str> = response.splitn(2, "\r\n\r\n").collect();
let heads = (*lines.first().unwrap()).to_string();
let head_lines: Vec<&str> = heads.split("\r\n").collect();
let (resp_state, headers) = process_head_lines(head_lines);
let body = (*lines.last().unwrap()).to_string();
Response {
status: resp_state,
body,
headers,
}
}
fn process_head_lines(lines: Vec<&str>) -> (ResponseStatus, Option<HashMap<String, String>>) {
let head = *lines.get(0).unwrap();
let parts: Vec<&str> = head.split(' ').collect();
let status_code = StatusCode::from_code(parts.get(1).unwrap());
let reason = parts.get(2).map(|v| (*v).to_string());
let response_headers = process_response_headers(&lines[1..]);
(ResponseStatus(status_code, reason), response_headers)
}
fn process_response_headers(lines: &[&str]) -> Option<HashMap<String, String>> {
if lines.is_empty() {
None
} else {
let mut headers = HashMap::new();
for &line in lines {
if line.contains(':') {
let line_comp: Tuple<&str> = line.splitn(2, ':').collect();
headers.insert((*line_comp.left).to_string(), (*line_comp.right).trim().to_string());
} else {
continue;
}
}
Some(headers)
}
}
#[derive(Debug, Clone)]
pub struct ResponseStatus(pub StatusCode, pub Option<String>);
impl Display for ResponseStatus {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
if let Some(reason) = self.1.as_ref() {
write!(f, "{} - {}", &self.0, reason.as_str())
} else {
write!(f, "{}", self.0)
}
}
}
#[derive(Debug, Copy, Clone)]
pub enum StatusCode {
Informational(u16),
Success(u16),
Redirection(u16),
ClientError(u16),
ServerError(u16),
Ignore,
Failure,
}
impl Display for StatusCode {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
if let Some(code) = self.get_code().as_ref() {
write!(f, "HTTP Response Code: {}", *code)
} else {
write!(f, "HTTP Response Code: ERROR!")
}
}
}
impl StatusCode {
pub fn get_code(self) -> Option<u16> {
match self {
StatusCode::Informational(val) => Some(val),
StatusCode::ClientError(val) => Some(val),
StatusCode::ServerError(val) => Some(val),
StatusCode::Success(val) => Some(val),
StatusCode::Redirection(val) => Some(val),
_ => None,
}
}
fn from_code(code: &str) -> Self {
let code = code.trim();
if code.len() != 3 {
return StatusCode::Failure;
}
let code_num: u16 = code.parse().unwrap();
match code_num {
100..=199 => StatusCode::Informational(code_num),
200..=299 => StatusCode::Success(code_num),
300..=399 => StatusCode::Redirection(code_num),
400..=499 => StatusCode::ClientError(code_num),
500..=599 => StatusCode::ServerError(code_num),
_ => StatusCode::Failure,
}
}
}