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;