From 2c8121f1735aa8efeb0d5e4ef595363c373ba470 Mon Sep 17 00:00:00 2001 From: Ivan Nikulin Date: Tue, 26 Jan 2021 18:36:58 +0000 Subject: [PATCH] fix(client): HTTP/1 client "Transfer-Encoding" repair code would panic (#2410) Closes #2409 --- src/headers.rs | 6 +++--- tests/client.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/headers.rs b/src/headers.rs index 897aa05d01..68c907f9db 100644 --- a/src/headers.rs +++ b/src/headers.rs @@ -117,9 +117,9 @@ pub(super) fn add_chunked(mut entry: http::header::OccupiedEntry<'_, HeaderValue // + 2 for ", " let new_cap = line.as_bytes().len() + CHUNKED.len() + 2; let mut buf = BytesMut::with_capacity(new_cap); - buf.copy_from_slice(line.as_bytes()); - buf.copy_from_slice(b", "); - buf.copy_from_slice(CHUNKED.as_bytes()); + buf.extend_from_slice(line.as_bytes()); + buf.extend_from_slice(b", "); + buf.extend_from_slice(CHUNKED.as_bytes()); *line = HeaderValue::from_maybe_shared(buf.freeze()) .expect("original header value plus ascii is valid"); diff --git a/tests/client.rs b/tests/client.rs index 409a1622c0..479500ab3e 100644 --- a/tests/client.rs +++ b/tests/client.rs @@ -401,6 +401,36 @@ test! { body: None, } +test! { + name: client_transfer_encoding_repair, + + server: + expected: "\ + GET / HTTP/1.1\r\n\ + transfer-encoding: foo, chunked\r\n\ + host: {addr}\r\n\ + \r\n\ + 5\r\n\ + hello\r\n\ + 0\r\n\r\n\ + ", + reply: REPLY_OK, + + client: + request: { + method: GET, + url: "http://{addr}/", + headers: { + "transfer-encoding" => "foo", + }, + body: "hello", // not Body::empty + }, + response: + status: OK, + headers: {}, + body: None, +} + test! { name: client_get_req_body_chunked_http10,