Skip to content

Commit 1880d26

Browse files
freeinternet865freeinternet865
andauthored
domain_fronter: drain chunked response trailers (#22)
Consume the final CRLF and any trailer lines after a zero-length chunk before returning a pooled upstream connection. Without this, leftover bytes from a chunked response could remain on the socket and corrupt the next response read on the reused connection. Add a regression test that reads a chunked response with a trailer and then successfully parses a second response from the same stream. Co-authored-by: freeinternet865 <free@internet865.com>
1 parent e0dcf1e commit 1880d26

1 file changed

Lines changed: 56 additions & 13 deletions

File tree

‎src/domain_fronter.rs‎

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,20 +1036,10 @@ where
10361036
let mut out: Vec<u8> = Vec::new();
10371037
let mut tmp = [0u8; 16384];
10381038
loop {
1039-
while !buf.windows(2).any(|w| w == b"\r\n") {
1040-
let n = timeout(Duration::from_secs(20), stream.read(&mut tmp)).await
1041-
.map_err(|_| FronterError::Timeout)??;
1042-
if n == 0 {
1043-
return Ok(out);
1044-
}
1045-
buf.extend_from_slice(&tmp[..n]);
1046-
}
1047-
let idx = buf.windows(2).position(|w| w == b"\r\n").unwrap();
1048-
let size_line_owned = std::str::from_utf8(&buf[..idx])
1039+
let size_line_owned = std::str::from_utf8(&read_crlf_line(stream, &mut buf, &mut tmp).await?)
10491040
.map_err(|_| FronterError::BadResponse("bad chunk size".into()))?
10501041
.trim()
10511042
.to_string();
1052-
buf.drain(..idx + 2);
10531043
if size_line_owned.is_empty() {
10541044
continue;
10551045
}
@@ -1059,7 +1049,11 @@ where
10591049
)
10601050
.map_err(|_| FronterError::BadResponse(format!("bad chunk size '{}'", size_line_owned)))?;
10611051
if size == 0 {
1062-
break;
1052+
loop {
1053+
if read_crlf_line(stream, &mut buf, &mut tmp).await?.is_empty() {
1054+
return Ok(out);
1055+
}
1056+
}
10631057
}
10641058
while buf.len() < size + 2 {
10651059
let n = timeout(Duration::from_secs(20), stream.read(&mut tmp)).await
@@ -1073,7 +1067,31 @@ where
10731067
out.extend_from_slice(&buf[..size]);
10741068
buf.drain(..size + 2);
10751069
}
1076-
Ok(out)
1070+
}
1071+
1072+
async fn read_crlf_line<S>(
1073+
stream: &mut S,
1074+
buf: &mut Vec<u8>,
1075+
tmp: &mut [u8],
1076+
) -> Result<Vec<u8>, FronterError>
1077+
where
1078+
S: tokio::io::AsyncRead + Unpin,
1079+
{
1080+
loop {
1081+
if let Some(idx) = buf.windows(2).position(|w| w == b"\r\n") {
1082+
let line = buf[..idx].to_vec();
1083+
buf.drain(..idx + 2);
1084+
return Ok(line);
1085+
}
1086+
let n = timeout(Duration::from_secs(20), stream.read(tmp)).await
1087+
.map_err(|_| FronterError::Timeout)??;
1088+
if n == 0 {
1089+
return Err(FronterError::BadResponse(
1090+
"connection closed mid-chunked response".into(),
1091+
));
1092+
}
1093+
buf.extend_from_slice(&tmp[..n]);
1094+
}
10771095
}
10781096

10791097
fn decode_gzip(data: &[u8]) -> Result<Vec<u8>, std::io::Error> {
@@ -1344,6 +1362,7 @@ impl ServerCertVerifier for NoVerify {
13441362
#[cfg(test)]
13451363
mod tests {
13461364
use super::*;
1365+
use tokio::io::{duplex, AsyncWriteExt};
13471366

13481367
#[test]
13491368
fn normalize_x_graphql_trims_after_variables() {
@@ -1508,4 +1527,28 @@ mod tests {
15081527
assert!(s.contains("Set-Cookie: a=1\r\n"));
15091528
assert!(s.contains("Set-Cookie: b=2\r\n"));
15101529
}
1530+
1531+
#[tokio::test(flavor = "current_thread")]
1532+
async fn chunked_reader_consumes_final_crlf_and_trailers() {
1533+
let (mut client, mut server) = duplex(1024);
1534+
client
1535+
.write_all(
1536+
b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nHello\r\n0\r\nX-Test: 1\r\n\r\n",
1537+
)
1538+
.await
1539+
.unwrap();
1540+
1541+
let (status, _headers, body) = read_http_response(&mut server).await.unwrap();
1542+
assert_eq!(status, 200);
1543+
assert_eq!(body, b"Hello");
1544+
1545+
client
1546+
.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK")
1547+
.await
1548+
.unwrap();
1549+
1550+
let (status2, _headers2, body2) = read_http_response(&mut server).await.unwrap();
1551+
assert_eq!(status2, 200);
1552+
assert_eq!(body2, b"OK");
1553+
}
15111554
}

0 commit comments

Comments
 (0)