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
use crate::{
HttpError,
data::{
Data,
encodings::{ Ascii, HeaderFieldKey, Uri, Integer }
},
helpers::{
iter_ext::IterExt,
io_ext::{ ReadExt, WriteExt },
slice_ext::{ ByteSliceExt, SliceExt }
}
};
use std::{
collections::HashMap,
io::{ self, Read, Write, Cursor },
convert::{ TryFrom, TryInto }
};
pub fn read(mut source: impl Read, buf: &mut[u8]) -> Result<Option<usize>, io::Error> {
const END: &[u8] = b"\r\n\r\n";
source.read_until(buf, &END)
}
pub fn parse_request<'a, 'b: 'a>(bytes: &'b[u8])
-> Result<(RequestHeader<'a>, &'b[u8]), HttpError>
{
let (header, body) = Header::parse(bytes)?;
Ok((RequestHeader(header), body))
}
pub fn parse_response<'a, 'b: 'a>(bytes: &'b[u8])
-> Result<(ResponseHeader<'a>, &'b[u8]), HttpError>
{
let (header, body) = Header::parse(bytes)?;
Ok((ResponseHeader(header), body))
}
#[derive(Debug)]
pub(in crate::header) struct Header<'a> {
pub header_line: (&'a[u8], &'a[u8], &'a[u8]),
pub header_fields: HashMap<Data<'a, HeaderFieldKey>, Data<'a, Ascii>>
}
impl<'a> Header<'a> {
fn parse(bytes: &'a[u8]) -> Result<(Self, &'a[u8]), HttpError> {
const SPACE: &[u8] = b" ";
const SEPARATOR: &[u8] = b":";
const NEWLINE: &[u8] = b"\r\n";
const END: &[u8] = b"\r\n\r\n";
let header_body = bytes.as_ref().splitn_pat(2, &END)
.collect_min(2).ok_or(HttpError::TruncatedData)?;
let mut header = header_body[0].split_pat(&NEWLINE);
let body = header_body[1];
let status_line = header.next().ok_or(HttpError::ProtocolViolation)?
.trim().split_pat(&SPACE)
.collect_exact(3).ok_or(HttpError::ProtocolViolation)?;
let status_line = (status_line[0], status_line[1], status_line[2]);
let mut header_fields = HashMap::new();
while let Some(line) = header.next() {
let key_value = line.splitn_pat(2, &SEPARATOR)
.collect_min(2).ok_or(HttpError::ProtocolViolation)?;
header_fields.insert(
Data::try_from(key_value[0])?,
Data::try_from(key_value[1].trim())?
);
}
Ok((Self{ header_line: status_line, header_fields }, body))
}
fn serialize(&self, mut sink: impl WriteExt) -> Result<usize, io::Error> {
const SPACE: &[u8] = b" ";
const SEPARATOR: &[u8] = b": ";
const NEWLINE: &[u8] = b"\r\n";
let mut written = 0;
sink.write(self.header_line.0)?.write(SPACE)?
.write(self.header_line.1)?.write(SPACE)?
.write(self.header_line.2)?.write(NEWLINE)?;
written += self.header_line.0.len() + SPACE.len()
+ self.header_line.1.len() + SPACE.len()
+ self.header_line.2.len() + NEWLINE.len();
for (k, v) in self.header_fields.iter() {
sink.write(k)?.write(SEPARATOR)?.write(v)?.write(NEWLINE)?;
written += k.len() + SEPARATOR.len() + v.len() + NEWLINE.len();
}
sink.write(NEWLINE)?;
written += NEWLINE.len();
Ok(written)
}
}
#[derive(Debug)]
pub struct RequestHeader<'a>(pub(in crate::header) Header<'a>);
impl<'a> RequestHeader<'a> {
pub fn method(&'a self) -> Result<Data<'a, Ascii>, HttpError> {
self.0.header_line.0.try_into()
}
pub fn uri(&'a self) -> Result<Data<'a, Uri>, HttpError> {
self.0.header_line.1.try_into()
}
pub fn version(&'a self) -> Result<Data<'a, Ascii>, HttpError> {
self.0.header_line.2.try_into()
}
pub fn field(&self, key: Data<'a, HeaderFieldKey>) -> Option<&Data<'a, Ascii>> {
self.0.header_fields.get(&key)
}
pub fn fields(&self) -> &HashMap<Data<'a, HeaderFieldKey>, Data<'a, Ascii>> {
&self.0.header_fields
}
pub fn write(&self, sink: &mut Write) -> Result<usize, io::Error> {
self.0.serialize(sink)
}
pub fn to_vec(&self) -> Vec<u8> {
let mut sink = Cursor::new(Vec::new());
self.write(&mut sink).unwrap();
sink.into_inner()
}
}
#[derive(Debug)]
pub struct ResponseHeader<'a>(pub(in crate::header) Header<'a>);
impl<'a> ResponseHeader<'a> {
pub fn version(&'a self) -> Result<Data<'a, Ascii>, HttpError> {
self.0.header_line.0.try_into()
}
pub fn status(&'a self) -> Result<u16, HttpError> {
let status = Data::<Integer>::try_from(self.0.header_line.1)?;
Ok(u16::try_from(status).map_err(|_| HttpError::ProtocolViolation)?)
}
pub fn reason(&'a self) -> Result<Data<'a, Ascii>, HttpError> {
self.0.header_line.2.try_into()
}
pub fn field(&self, key: Data<'a, HeaderFieldKey>) -> Option<&Data<'a, Ascii>> {
self.0.header_fields.get(&key)
}
pub fn fields(&self) -> &HashMap<Data<'a, HeaderFieldKey>, Data<'a, Ascii>> {
&self.0.header_fields
}
pub fn write(&self, sink: &mut Write) -> Result<usize, io::Error> {
self.0.serialize(sink)
}
pub fn to_vec(&self) -> Vec<u8> {
let mut sink = Cursor::new(Vec::new());
self.write(&mut sink).unwrap();
sink.into_inner()
}
}