diff --git a/httplib.h b/httplib.h index 4607342..9bf7365 100644 --- a/httplib.h +++ b/httplib.h @@ -34,6 +34,10 @@ #undef min #undef max +#ifndef strcasecmp +#define strcasecmp _stricmp +#endif + typedef SOCKET socket_t; #else #include @@ -860,7 +864,7 @@ bool read_content(Stream& strm, T& x, Progress progress = Progress()) } else { const auto& encoding = get_header_value(x.headers, "Transfer-Encoding", ""); - if (!strcmp(encoding, "chunked")) { + if (!strcasecmp(encoding, "chunked")) { return read_content_chunked(strm, x); } else { return read_content_without_length(strm, x); diff --git a/test/test.cc b/test/test.cc index 2e91ad7..46a9c5e 100644 --- a/test/test.cc +++ b/test/test.cc @@ -292,6 +292,9 @@ protected: res.status = 404; } }) + .post("/chunked", [&](const Request& req, Response& /*res*/) { + EXPECT_EQ(req.body, "dechunked post body"); + }) .post("/multipart", [&](const Request& req, Response& /*res*/) { EXPECT_EQ(5u, req.files.size()); ASSERT_TRUE(!req.has_file("???")); @@ -660,6 +663,34 @@ TEST_F(ServerTest, CaseInsensitiveHeaderName) EXPECT_EQ("Hello World!", res->body); } +TEST_F(ServerTest, CaseInsensitiveTransferEncoding) +{ + Request req; + req.method = "POST"; + req.path = "/chunked"; + + std::string host_and_port; + host_and_port += HOST; + host_and_port += ":"; + host_and_port += std::to_string(PORT); + + req.headers.emplace("Host", host_and_port.c_str()); + req.headers.emplace("Accept", "*/*"); + req.headers.emplace("User-Agent", "cpp-httplib/0.1"); + req.headers.emplace("Content-Type", "text/plain"); + req.headers.emplace("Content-Length", "0"); + req.headers.emplace("Transfer-Encoding", "Chunked"); // Note, "Chunked" rather than typical "chunked". + + // Client does not chunk, so make a chunked body manually. + req.body = "4\r\ndech\r\nf\r\nunked post body\r\n0\r\n\r\n"; + + auto res = std::make_shared(); + auto ret = cli_.send(req, *res); + + ASSERT_TRUE(ret); + EXPECT_EQ(200, res->status); +} + TEST_F(ServerTest, GetMethodRemoteAddr) { auto res = cli_.get("/remote_addr");