Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple Content-Length values on same line #2471

Merged
merged 1 commit into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions src/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
// correctly. If not, then it's an error.

let folded = values.fold(None, |prev, line| match prev {
Some(Ok(prev)) => Some(
line.to_str()
.map_err(|_| ())
.and_then(|s| s.parse().map_err(|_| ()))
.and_then(|n| if prev == n { Ok(n) } else { Err(()) }),
),
None => Some(
line.to_str()
.map_err(|_| ())
.and_then(|s| s.parse().map_err(|_| ())),
),
Some(Err(())) => Some(Err(())),
});

if let Some(Ok(n)) = folded {
Some(n)
} else {
None
let mut content_length: Option<u64> = None;
for h in values {
if let Ok(line) = h.to_str() {
for v in line.split(',') {
if let Some(n) = v.trim().parse().ok() {
if content_length.is_none() {
content_length = Some(n)
} else if content_length != Some(n) {
return None;
}
} else {
return None
}
}
} else {
return None
}
}

return content_length
}

#[cfg(all(feature = "http2", feature = "client"))]
Expand Down
24 changes: 24 additions & 0 deletions tests/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,30 @@ test! {
error: |err| err.to_string() == "request has unsupported HTTP version",
}

test! {
name: client_handles_contentlength_values_on_same_line,
seanmonstar marked this conversation as resolved.
Show resolved Hide resolved

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 {
use super::*;
use std::io::{self, Read, Write};
Expand Down