Skip to content

Commit 48fdaf1

Browse files
authoredMar 19, 2021
fix(headers): Support multiple Content-Length values on same line (#2471)
Closes #2470
1 parent eb0e718 commit 48fdaf1

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed
 

‎src/headers.rs

+19-19
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,26 @@ 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-
});
59-
60-
if let Some(Ok(n)) = folded {
61-
Some(n)
62-
} else {
63-
None
45+
let mut content_length: Option<u64> = None;
46+
for h in values {
47+
if let Ok(line) = h.to_str() {
48+
for v in line.split(',') {
49+
if let Some(n) = v.trim().parse().ok() {
50+
if content_length.is_none() {
51+
content_length = Some(n)
52+
} else if content_length != Some(n) {
53+
return None;
54+
}
55+
} else {
56+
return None
57+
}
58+
}
59+
} else {
60+
return None
61+
}
6462
}
63+
64+
return content_length
6565
}
6666

6767
#[cfg(all(feature = "http2", feature = "client"))]

‎tests/client.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,30 @@ 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+
Content-Length: 3,3\r\n\
1055+
\r\n\
1056+
abc\r\n",
1057+
1058+
client:
1059+
request: {
1060+
method: GET,
1061+
url: "http://{addr}/foo",
1062+
},
1063+
response:
1064+
status: OK,
1065+
headers: {
1066+
},
1067+
body: &b"abc"[..],
1068+
}
1069+
10461070
mod dispatch_impl {
10471071
use super::*;
10481072
use std::io::{self, Read, Write};

0 commit comments

Comments
 (0)
Please sign in to comment.