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
|
use std::str::FromStr;
use crate::{status::{Status, self}, version::Version, header::{HeaderMap, Header}, parse::TryParse, error::HTTPError};
pub struct Response<T: ToString + FromStr> {
version: Version,
status: Status,
headers: HeaderMap,
body: Option<T>
}
impl<T: ToString + FromStr> Response<T> {
pub fn new() -> Self {
Self {
version: Version::HTTP11,
status: status::SUCCESS,
headers: HeaderMap::new(),
body: None
}
}
pub fn set_version(&mut self, version: impl Into<Version>) -> &mut Self {
self.version = version.into();
self
}
pub fn set_status(&mut self, status: impl Into<Status>) -> &mut Self {
self.status = status.into();
self
}
pub fn add_header(&mut self, header: impl Into<Header>) -> &mut Self {
self.headers.insert(header.into());
self
}
pub fn set_body(&mut self, body: impl Into<T>) -> &mut Self {
self.body = Some(body.into());
self
}
}
impl<T: ToString + FromStr> TryParse for Response<T> {
fn try_parse(s: impl Into<String>) -> Result<Self, HTTPError> {
let s = s.into();
let mut lines = s.lines();
let Some(header_str) = lines.next() else {
return Err(HTTPError::MissingHeader)
};
let mut header_parts = header_str.split(" ");
let Some(version_str) = header_parts.next() else {
return Err(HTTPError::InvalidHeader(header_str.into()))
};
let version = version_str.into();
let status_str: String = header_parts.collect();
let status = status_str.try_into()?;
let mut headers = HeaderMap::new();
loop {
let Some(next) = lines.next() else {
return Err(HTTPError::MissingHeaderBreak)
};
if next.len() < 1 { break }
let header = Header::try_parse(next)?;
headers.insert(header);
}
let rest: String = lines.collect();
let body;
if rest.len() < 1 {
body = None
} else {
body = match T::from_str(&rest) {
Ok(body) => Some(body),
Err(_) => return Err(HTTPError::InvalidBody)
}
}
Ok(Self { version, status, headers, body })
}
}
impl<T: ToString + FromStr> ToString for Response<T> {
fn to_string(&self) -> String {
let mut s = String::new();
s.push_str(self.version.as_str());
s.push(' ');
s.push_str(&self.status.to_string());
s.push_str("\r\n");
let iter = self.headers.iter();
for headers in iter {
for header in headers {
s.push_str(header.name.as_str());
s.push_str(": ");
s.push_str(header.value.as_str());
s.push_str("\r\n");
}
}
s.push_str("\r\n");
if let Some(body) = &self.body {
s.push_str(&body.to_string());
}
s
}
}
|