Skip to content

Commit ca3613c

Browse files
committed
Make 'chunked' in Transfer-Encoding case-insensitive
1 parent c6a6530 commit ca3613c

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

httplib.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
#undef min
3535
#undef max
3636

37+
#ifndef strcasecmp
38+
#define strcasecmp _stricmp
39+
#endif
40+
3741
typedef SOCKET socket_t;
3842
#else
3943
#include <pthread.h>
@@ -860,7 +864,7 @@ bool read_content(Stream& strm, T& x, Progress progress = Progress())
860864
} else {
861865
const auto& encoding = get_header_value(x.headers, "Transfer-Encoding", "");
862866

863-
if (!strcmp(encoding, "chunked")) {
867+
if (!strcasecmp(encoding, "chunked")) {
864868
return read_content_chunked(strm, x);
865869
} else {
866870
return read_content_without_length(strm, x);

test/test.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ class ServerTest : public ::testing::Test {
292292
res.status = 404;
293293
}
294294
})
295+
.post("/chunked", [&](const Request& req, Response& /*res*/) {
296+
EXPECT_EQ(req.body, "dechunked post body");
297+
})
295298
.post("/multipart", [&](const Request& req, Response& /*res*/) {
296299
EXPECT_EQ(5u, req.files.size());
297300
ASSERT_TRUE(!req.has_file("???"));
@@ -660,6 +663,34 @@ TEST_F(ServerTest, CaseInsensitiveHeaderName)
660663
EXPECT_EQ("Hello World!", res->body);
661664
}
662665

666+
TEST_F(ServerTest, CaseInsensitiveTransferEncoding)
667+
{
668+
Request req;
669+
req.method = "POST";
670+
req.path = "/chunked";
671+
672+
std::string host_and_port;
673+
host_and_port += HOST;
674+
host_and_port += ":";
675+
host_and_port += std::to_string(PORT);
676+
677+
req.headers.emplace("Host", host_and_port.c_str());
678+
req.headers.emplace("Accept", "*/*");
679+
req.headers.emplace("User-Agent", "cpp-httplib/0.1");
680+
req.headers.emplace("Content-Type", "text/plain");
681+
req.headers.emplace("Content-Length", "0");
682+
req.headers.emplace("Transfer-Encoding", "Chunked"); // Note, "Chunked" rather than typical "chunked".
683+
684+
// Client does not chunk, so make a chunked body manually.
685+
req.body = "4\r\ndech\r\nf\r\nunked post body\r\n0\r\n\r\n";
686+
687+
auto res = std::make_shared<Response>();
688+
auto ret = cli_.send(req, *res);
689+
690+
ASSERT_TRUE(ret);
691+
EXPECT_EQ(200, res->status);
692+
}
693+
663694
TEST_F(ServerTest, GetMethodRemoteAddr)
664695
{
665696
auto res = cli_.get("/remote_addr");

0 commit comments

Comments
 (0)