Skip to content

Commit

Permalink
fix(http1): properly end chunked bodies when it was known to be empty (
Browse files Browse the repository at this point in the history
…#3254)

Closes #3252
  • Loading branch information
seanmonstar committed Jun 20, 2023
1 parent 297dc4c commit 32422c4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
7 changes: 6 additions & 1 deletion src/proto/h1/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,12 @@ where
self.conn.end_body()?;
}
} else {
return Poll::Pending;
// If there's no body_rx, end the body
if self.conn.can_write_body() {
self.conn.end_body()?;
} else {
return Poll::Pending;
}
}
}
}
Expand Down
40 changes: 28 additions & 12 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ mod response_body_lengths {
}

fn run_test(case: TestCase) {
let _ = pretty_env_logger::try_init();
assert!(
case.version == 0 || case.version == 1,
"TestCase.version must 0 or 1"
Expand Down Expand Up @@ -157,18 +158,22 @@ mod response_body_lengths {
let n = body.find("\r\n\r\n").unwrap() + 4;

if case.expects_chunked {
let len = body.len();
assert_eq!(
&body[n + 1..n + 3],
"\r\n",
"expected body chunk size header"
);
assert_eq!(&body[n + 3..len - 7], body_str, "expected body");
assert_eq!(
&body[len - 7..],
"\r\n0\r\n\r\n",
"expected body final chunk size header"
);
if body_str.len() > 0 {
let len = body.len();
assert_eq!(
&body[n + 1..n + 3],
"\r\n",
"expected body chunk size header"
);
assert_eq!(&body[n + 3..len - 7], body_str, "expected body");
assert_eq!(
&body[len - 7..],
"\r\n0\r\n\r\n",
"expected body final chunk size header"
);
} else {
assert_eq!(&body[n..], "0\r\n\r\n");
}
} else {
assert_eq!(&body[n..], body_str, "expected body");
}
Expand Down Expand Up @@ -219,6 +224,17 @@ mod response_body_lengths {
});
}

#[test]
fn chunked_response_known_empty() {
run_test(TestCase {
version: 1,
headers: &[("transfer-encoding", "chunked")],
body: Bd::Known(""),
expects_chunked: true, // should still send chunked, and 0\r\n\r\n
expects_con_len: false,
});
}

#[test]
fn chunked_response_unknown() {
run_test(TestCase {
Expand Down

0 comments on commit 32422c4

Please sign in to comment.