Skip to content

Commit 2c8121f

Browse files
authored
fix(client): HTTP/1 client "Transfer-Encoding" repair code would panic (#2410)
Closes #2409
1 parent 1928682 commit 2c8121f

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/headers.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ pub(super) fn add_chunked(mut entry: http::header::OccupiedEntry<'_, HeaderValue
117117
// + 2 for ", "
118118
let new_cap = line.as_bytes().len() + CHUNKED.len() + 2;
119119
let mut buf = BytesMut::with_capacity(new_cap);
120-
buf.copy_from_slice(line.as_bytes());
121-
buf.copy_from_slice(b", ");
122-
buf.copy_from_slice(CHUNKED.as_bytes());
120+
buf.extend_from_slice(line.as_bytes());
121+
buf.extend_from_slice(b", ");
122+
buf.extend_from_slice(CHUNKED.as_bytes());
123123

124124
*line = HeaderValue::from_maybe_shared(buf.freeze())
125125
.expect("original header value plus ascii is valid");

tests/client.rs

+30
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,36 @@ test! {
401401
body: None,
402402
}
403403

404+
test! {
405+
name: client_transfer_encoding_repair,
406+
407+
server:
408+
expected: "\
409+
GET / HTTP/1.1\r\n\
410+
transfer-encoding: foo, chunked\r\n\
411+
host: {addr}\r\n\
412+
\r\n\
413+
5\r\n\
414+
hello\r\n\
415+
0\r\n\r\n\
416+
",
417+
reply: REPLY_OK,
418+
419+
client:
420+
request: {
421+
method: GET,
422+
url: "http://{addr}/",
423+
headers: {
424+
"transfer-encoding" => "foo",
425+
},
426+
body: "hello", // not Body::empty
427+
},
428+
response:
429+
status: OK,
430+
headers: {},
431+
body: None,
432+
}
433+
404434
test! {
405435
name: client_get_req_body_chunked_http10,
406436

0 commit comments

Comments
 (0)