yhirose 6 years ago
parent
commit
8483e5931f
2 changed files with 17 additions and 2 deletions
  1. 4 2
      httplib.h
  2. 13 0
      test/test.cc

+ 4 - 2
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);

+ 13 - 0
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)
 {