Skip to content

Commit bef59cc

Browse files
authored
Merge pull request #16 from yoshuawuyts/normalize_headers
Issue 8: Normalize headers and make use of content-length value
2 parents a5e1037 + d3f0672 commit bef59cc

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

src/body.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ impl<R: AsyncRead> Body<R> {
3131
}
3232
}
3333

34+
/// Create a new instance from a reader with a known size.
35+
pub fn new_with_size(reader: R, size: usize) -> Self {
36+
Self {
37+
reader,
38+
length: Some(size),
39+
}
40+
}
41+
3442
/// Create a new empty body.
3543
pub fn empty(reader: R) -> Self {
3644
Self {

src/client.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,20 @@ where
126126
let body = match httparse_res
127127
.headers
128128
.iter()
129-
.find(|h| h.name == "Content-Length")
129+
.find(|h| h.name.eq_ignore_ascii_case("Content-Length"))
130130
{
131-
Some(_header) => Body::new(reader), // TODO: use the header value
131+
Some(content_length) => {
132+
let length = std::str::from_utf8(content_length.value)
133+
.ok()
134+
.map(|s| s.parse::<usize>().ok())
135+
.flatten();
136+
137+
if let Some(len) = length {
138+
Body::new_with_size(reader, len)
139+
} else {
140+
return Err("Invalid value for Content-Length".into());
141+
}
142+
}
132143
None => Body::empty(reader),
133144
};
134145

src/server.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,20 @@ where
210210
let body = match httparse_req
211211
.headers
212212
.iter()
213-
.find(|h| h.name == "Content-Length")
213+
.find(|h| h.name.eq_ignore_ascii_case("Content-Length"))
214214
{
215-
Some(_header) => Body::new(reader), // TODO: use the header value
215+
Some(content_length) => {
216+
let length = std::str::from_utf8(content_length.value)
217+
.ok()
218+
.map(|s| s.parse::<usize>().ok())
219+
.flatten();
220+
221+
if let Some(len) = length {
222+
Body::new_with_size(reader, len)
223+
} else {
224+
return Err("Invalid value for Content-Length".into());
225+
}
226+
}
216227
None => Body::empty(reader),
217228
};
218229

0 commit comments

Comments
 (0)