Skip to content

Commit 7b87079

Browse files
committed
fix(headers): Support multiple Content-Length values on same line
1 parent eb0e718 commit 7b87079

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

src/headers.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,14 @@ pub(super) fn content_length_parse_all_values(values: ValueIter<'_, HeaderValue>
4242
// be alright if they all contain the same value, and all parse
4343
// correctly. If not, then it's an error.
4444

45-
let folded = values.fold(None, |prev, line| match prev {
46-
Some(Ok(prev)) => Some(
47-
line.to_str()
48-
.map_err(|_| ())
49-
.and_then(|s| s.parse().map_err(|_| ()))
50-
.and_then(|n| if prev == n { Ok(n) } else { Err(()) }),
51-
),
52-
None => Some(
53-
line.to_str()
54-
.map_err(|_| ())
55-
.and_then(|s| s.parse().map_err(|_| ())),
56-
),
57-
Some(Err(())) => Some(Err(())),
58-
});
45+
let folded = values.flat_map(content_length_parse_line)
46+
.fold(None, |prev, value| match prev {
47+
Some(Ok(prev)) => Some(
48+
value.and_then(|n| if prev == n { Ok(n) } else { Err(()) }),
49+
),
50+
None => Some(value),
51+
Some(Err(())) => Some(Err(())),
52+
});
5953

6054
if let Some(Ok(n)) = folded {
6155
Some(n)
@@ -64,6 +58,13 @@ pub(super) fn content_length_parse_all_values(values: ValueIter<'_, HeaderValue>
6458
}
6559
}
6660

61+
fn content_length_parse_line(value: &HeaderValue) -> Vec<Result<u64, ()>> {
62+
return match value.to_str() {
63+
Ok(s) => s.split(',').map(|a| a.trim().parse().map_err(|_| ())).collect(),
64+
Err(_) => vec![Err(())],
65+
}
66+
}
67+
6768
#[cfg(all(feature = "http2", feature = "client"))]
6869
pub(super) fn method_has_defined_payload_semantics(method: &Method) -> bool {
6970
match *method {

tests/client.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,29 @@ test! {
10431043
error: |err| err.to_string() == "request has unsupported HTTP version",
10441044
}
10451045

1046+
test! {
1047+
name: client_handles_contentlength_values_on_same_line,
1048+
1049+
server:
1050+
expected: "GET /foo HTTP/1.1\r\nhost: {addr}\r\n\r\n",
1051+
reply: "\
1052+
HTTP/1.1 200 OK\r\n\
1053+
Content-Length: 3,3\r\n\
1054+
\r\n\
1055+
abc\r\n",
1056+
1057+
client:
1058+
request: {
1059+
method: GET,
1060+
url: "http://{addr}/foo",
1061+
},
1062+
response:
1063+
status: OK,
1064+
headers: {
1065+
},
1066+
body: &b"abc"[..],
1067+
}
1068+
10461069
mod dispatch_impl {
10471070
use super::*;
10481071
use std::io::{self, Read, Write};

0 commit comments

Comments
 (0)