fix(headers): Support multiple Content-Length values on same line (#2471)

Closes #2470
This commit is contained in:
Vincent Ricard
2021-03-19 18:38:58 +01:00
committed by GitHub
parent eb0e718696
commit 48fdaf1606
2 changed files with 43 additions and 19 deletions

View File

@@ -42,26 +42,26 @@ pub(super) fn content_length_parse_all_values(values: ValueIter<'_, HeaderValue>
// be alright if they all contain the same value, and all parse // be alright if they all contain the same value, and all parse
// correctly. If not, then it's an error. // correctly. If not, then it's an error.
let folded = values.fold(None, |prev, line| match prev { let mut content_length: Option<u64> = None;
Some(Ok(prev)) => Some( for h in values {
line.to_str() if let Ok(line) = h.to_str() {
.map_err(|_| ()) for v in line.split(',') {
.and_then(|s| s.parse().map_err(|_| ())) if let Some(n) = v.trim().parse().ok() {
.and_then(|n| if prev == n { Ok(n) } else { Err(()) }), if content_length.is_none() {
), content_length = Some(n)
None => Some( } else if content_length != Some(n) {
line.to_str() return None;
.map_err(|_| ())
.and_then(|s| s.parse().map_err(|_| ())),
),
Some(Err(())) => Some(Err(())),
});
if let Some(Ok(n)) = folded {
Some(n)
} else {
None
} }
} else {
return None
}
}
} else {
return None
}
}
return content_length
} }
#[cfg(all(feature = "http2", feature = "client"))] #[cfg(all(feature = "http2", feature = "client"))]

View File

@@ -1043,6 +1043,30 @@ test! {
error: |err| err.to_string() == "request has unsupported HTTP version", error: |err| err.to_string() == "request has unsupported HTTP version",
} }
test! {
name: client_handles_contentlength_values_on_same_line,
server:
expected: "GET /foo HTTP/1.1\r\nhost: {addr}\r\n\r\n",
reply: "\
HTTP/1.1 200 OK\r\n\
Content-Length: 3,3\r\n\
Content-Length: 3,3\r\n\
\r\n\
abc\r\n",
client:
request: {
method: GET,
url: "http://{addr}/foo",
},
response:
status: OK,
headers: {
},
body: &b"abc"[..],
}
mod dispatch_impl { mod dispatch_impl {
use super::*; use super::*;
use std::io::{self, Read, Write}; use std::io::{self, Read, Write};