From cd446a4e016a695f3d6243d0367034deb2793ff2 Mon Sep 17 00:00:00 2001 From: metsw24-max Date: Wed, 18 Nov 2026 09:08:45 +0530 Subject: [PATCH] drop a passed-through Content-Length when the client reframes the body (#2679) * drop a passed-through Content-Length when the client reframes the body send_with_content_provider_and_receiver() kept a Content-Length the caller passed through after the library reframed the body: it compresses a buffered body under set_compress(true), or streams a provider without a length as chunked. prepare_default_headers() only adds a Content-Length when none is present, so the stale value went out describing the original body. With compression the server reads only that many bytes of the longer gzip stream and the rest desyncs the next request on a kept-alive connection; with a chunked provider the client emits both Content-Length and Transfer-Encoding. Erase it once the library owns the framing so write_request() emits the matching length, or none for a chunked body. A plain, uncompressed body keeps the caller's value. * Document the dropped Content-Length and tidy the new tests Note in the README that a per-request Content-Length is dropped when the library compresses or chunks the body; a default header is not. Shorten the comments, use HOST like the neighbouring tests, and start the chunked test's Content-Length count at a value the handler has to overwrite. --------- Co-authored-by: yhirose --- README.md | 2 +- httplib.h | 5 +++ test/test.cc | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3316e6a3..16a3a778 100644 --- a/README.md +++ b/README.md @@ -1172,7 +1172,7 @@ cli.set_default_headers({ auto res = cli.Get("/hi"); ``` -A `Content-Length` among these headers is sent in place of the one the library would send, so it has to match the body as written. A `Transfer-Encoding` is sent in addition to the library's own framing, so leave it out. Leave both out when passing on the headers of another message, as a proxy does. +A `Content-Length` among these headers is sent in place of the one the library would send, so it has to match the body as written. One passed with the request is dropped when the library compresses the body or sends it chunked. A `Transfer-Encoding` is sent in addition to the library's own framing, so leave it out. Leave both out when passing on the headers of another message, as a proxy does. ### POST diff --git a/httplib.h b/httplib.h index 20d43218..e1c6eed1 100644 --- a/httplib.h +++ b/httplib.h @@ -16981,6 +16981,11 @@ ClientImpl::send_with_content_provider_and_receiver( if (enc.second) { req.set_header("Content-Encoding", enc.second); } + // A compressed or chunked body is framed by the library, not the caller + if (enc.second || content_provider_without_length) { + req.headers.erase("Content-Length"); + } + if (enc.first && !content_provider_without_length) { auto &compressor = enc.first; diff --git a/test/test.cc b/test/test.cc index 709b2207..39d36b34 100644 --- a/test/test.cc +++ b/test/test.cc @@ -14114,8 +14114,95 @@ TEST(DataSinkTest, CompressedRequestProviderThatFinishesEarlyFails) { ASSERT_FALSE(res); EXPECT_EQ(Error::Write, res.error()); } + +TEST(ClientRequestFramingTest, CompressedBodyReplacesCallerContentLength) { + Server svr; + + std::string received; + size_t content_length_count = 0; + bool has_transfer_encoding = true; + svr.Post("/echo", [&](const Request &req, Response &res) { + received = req.body; + content_length_count = req.get_header_value_count("Content-Length"); + has_transfer_encoding = req.has_header("Transfer-Encoding"); + res.set_content("ok", "text/plain"); + }); + svr.Get("/next", [](const Request &, Response &res) { + res.set_content("next", "text/plain"); + }); + + auto listen_thread = std::thread([&svr]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + cli.set_keep_alive(true); + cli.set_compress(true); + + const std::string body = "hello world"; + Headers headers = {{"Content-Length", std::to_string(body.size())}}; + auto res = cli.Post("/echo", headers, body, "text/plain"); + ASSERT_TRUE(res) << "Error: " << to_string(res.error()); + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ(body, received); + EXPECT_EQ(1u, content_length_count); + EXPECT_FALSE(has_transfer_encoding); + + // The rest of a truncated body would be read as this request + auto res2 = cli.Get("/next"); + ASSERT_TRUE(res2) << "Error: " << to_string(res2.error()); + EXPECT_EQ(StatusCode::OK_200, res2->status); + EXPECT_EQ("next", res2->body); +} #endif +TEST(ClientRequestFramingTest, ChunkedProviderDropsCallerContentLength) { + Server svr; + + std::string received; + size_t content_length_count = 1; + bool has_transfer_encoding = false; + svr.Post("/echo", [&](const Request &req, Response &res) { + received = req.body; + content_length_count = req.get_header_value_count("Content-Length"); + has_transfer_encoding = req.has_header("Transfer-Encoding"); + res.set_content("ok", "text/plain"); + }); + + auto listen_thread = std::thread([&svr]() { svr.listen(HOST, PORT); }); + auto se = detail::scope_exit([&] { + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); + }); + + svr.wait_until_ready(); + + Client cli(HOST, PORT); + cli.set_keep_alive(true); + + const std::string body = "hello world"; + Headers headers = {{"Content-Length", "5"}}; + auto res = cli.Post( + "/echo", headers, + [&](size_t /*offset*/, DataSink &sink) { + sink.write(body.data(), body.size()); + sink.done(); + return true; + }, + "text/plain"); + ASSERT_TRUE(res) << "Error: " << to_string(res.error()); + EXPECT_EQ(StatusCode::OK_200, res->status); + EXPECT_EQ(body, received); + EXPECT_EQ(0u, content_length_count); + EXPECT_TRUE(has_transfer_encoding); +} + TEST(ErrorHandlerWithContentProviderTest, ErrorHandler) { Server svr;