From 8483e5931fb8a07ba8be078c59ee641eff661769 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 28 Mar 2019 19:55:43 -0400 Subject: [PATCH] Fix #136 --- httplib.h | 6 ++++-- test/test.cc | 13 +++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 115578e..97b41da 100644 --- a/httplib.h +++ b/httplib.h @@ -2119,8 +2119,10 @@ inline void Client::write_request(Stream& strm, Request& req) req.set_header("Content-Type", "text/plain"); } - auto length = std::to_string(req.body.size()); - req.set_header("Content-Length", length.c_str()); + if (!req.has_header("Content-Length")) { + auto length = std::to_string(req.body.size()); + req.set_header("Content-Length", length.c_str()); + } } detail::write_headers(bstrm, req); diff --git a/test/test.cc b/test/test.cc index 2d600cd..2e6e6fd 100644 --- a/test/test.cc +++ b/test/test.cc @@ -470,6 +470,10 @@ protected: EXPECT_EQ("value2", req.get_param_value("array", 1)); EXPECT_EQ("value3", req.get_param_value("array", 2)); }) + .Post("/validate-no-multiple-headers", [&](const Request& req, Response& res) { + EXPECT_EQ(1u, req.get_header_value_count("Content-Length")); + EXPECT_EQ("5", req.get_header_value("Content-Length")); + }) #ifdef CPPHTTPLIB_ZLIB_SUPPORT .Get("/gzip", [&](const Request& /*req*/, Response& res) { res.set_content("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", "text/plain"); @@ -989,6 +993,15 @@ TEST_F(ServerTest, ArrayParam) EXPECT_EQ(200, res->status); } +TEST_F(ServerTest, NoMultipleHeaders) +{ + Headers headers; + headers.emplace("Content-Length", "5"); + auto res = cli_.Post("/validate-no-multiple-headers", headers, "hello", "text/plain"); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(200, res->status); +} + #ifdef CPPHTTPLIB_ZLIB_SUPPORT TEST_F(ServerTest, Gzip) {