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
  • Loading branch information
seanmonstar authored and 0xE282B0 committed Jan 12, 2024
1 parent ec50c06 commit 56265f5
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 @@ -368,7 +368,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 @@ -92,6 +92,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 @@ -155,18 +156,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 @@ -217,6 +222,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 56265f5

Please sign in to comment.