Skip to content

Commit

Permalink
Fix #1075
Browse files Browse the repository at this point in the history
  • Loading branch information
yhirose committed Nov 14, 2024
1 parent 7bd316f commit d1d4eee
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
12 changes: 12 additions & 0 deletions httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -7702,6 +7702,18 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req,

if (!req.has_header("Accept")) { req.set_header("Accept", "*/*"); }

if (!req.has_header("Accept-Encoding")) {
std::string accept_encoding;
#ifdef CPPHTTPLIB_BROTLI_SUPPORT
accept_encoding = "br";
#endif
#ifdef CPPHTTPLIB_ZLIB_SUPPORT
if (!accept_encoding.empty()) { accept_encoding += ", "; }
accept_encoding += "gzip, deflate";
#endif
req.set_header("Accept-Encoding", accept_encoding);
}

#ifndef CPPHTTPLIB_NO_DEFAULT_USER_AGENT
if (!req.has_header("User-Agent")) {
auto agent = std::string("cpp-httplib/") + CPPHTTPLIB_VERSION;
Expand Down
49 changes: 35 additions & 14 deletions test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2004,7 +2004,7 @@ TEST(ErrorHandlerTest, ContentLength) {
{
Client cli(HOST, PORT);

auto res = cli.Get("/hi");
auto res = cli.Get("/hi", {{"Accept-Encoding", ""}});
ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::OK_200, res->status);
EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
Expand Down Expand Up @@ -2087,7 +2087,7 @@ TEST(ExceptionTest, WithExceptionHandler) {
Client cli(HOST, PORT);

for (size_t j = 0; j < 100; j++) {
auto res = cli.Get("/hi");
auto res = cli.Get("/hi", {{"Accept-Encoding", ""}});
ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::InternalServerError_500, res->status);
EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
Expand All @@ -2098,7 +2098,7 @@ TEST(ExceptionTest, WithExceptionHandler) {
cli.set_keep_alive(true);

for (size_t j = 0; j < 100; j++) {
auto res = cli.Get("/hi");
auto res = cli.Get("/hi", {{"Accept-Encoding", ""}});
ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::InternalServerError_500, res->status);
EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
Expand Down Expand Up @@ -3803,7 +3803,10 @@ TEST_F(ServerTest, GetRangeWithMaxLongLength) {
}

TEST_F(ServerTest, GetRangeWithZeroToInfinite) {
auto res = cli_.Get("/with-range", {{"Range", "bytes=0-"}});
auto res = cli_.Get("/with-range", {
{"Range", "bytes=0-"},
{"Accept-Encoding", ""},
});
ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
EXPECT_EQ("7", res->get_header_value("Content-Length"));
Expand Down Expand Up @@ -3899,7 +3902,10 @@ TEST_F(ServerTest, ClientStop) {
}

TEST_F(ServerTest, GetWithRange1) {
auto res = cli_.Get("/with-range", {{make_range_header({{3, 5}})}});
auto res = cli_.Get("/with-range", {
make_range_header({{3, 5}}),
{"Accept-Encoding", ""},
});
ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
EXPECT_EQ("3", res->get_header_value("Content-Length"));
Expand All @@ -3909,7 +3915,10 @@ TEST_F(ServerTest, GetWithRange1) {
}

TEST_F(ServerTest, GetWithRange2) {
auto res = cli_.Get("/with-range", {{make_range_header({{1, -1}})}});
auto res = cli_.Get("/with-range", {
make_range_header({{1, -1}}),
{"Accept-Encoding", ""},
});
ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
EXPECT_EQ("6", res->get_header_value("Content-Length"));
Expand All @@ -3919,7 +3928,10 @@ TEST_F(ServerTest, GetWithRange2) {
}

TEST_F(ServerTest, GetWithRange3) {
auto res = cli_.Get("/with-range", {{make_range_header({{0, 0}})}});
auto res = cli_.Get("/with-range", {
make_range_header({{0, 0}}),
{"Accept-Encoding", ""},
});
ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
EXPECT_EQ("1", res->get_header_value("Content-Length"));
Expand All @@ -3929,7 +3941,10 @@ TEST_F(ServerTest, GetWithRange3) {
}

TEST_F(ServerTest, GetWithRange4) {
auto res = cli_.Get("/with-range", {{make_range_header({{-1, 2}})}});
auto res = cli_.Get("/with-range", {
make_range_header({{-1, 2}}),
{"Accept-Encoding", ""},
});
ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
EXPECT_EQ("2", res->get_header_value("Content-Length"));
Expand Down Expand Up @@ -4674,7 +4689,9 @@ TEST_F(ServerTest, Gzip) {
}

TEST_F(ServerTest, GzipWithoutAcceptEncoding) {
auto res = cli_.Get("/compress");
Headers headers;
headers.emplace("Accept-Encoding", "");
auto res = cli_.Get("/compress", headers);

ASSERT_TRUE(res);
EXPECT_TRUE(res->get_header_value("Content-Encoding").empty());
Expand Down Expand Up @@ -4723,12 +4740,16 @@ TEST_F(ServerTest, GzipWithoutDecompressing) {
}

TEST_F(ServerTest, GzipWithContentReceiverWithoutAcceptEncoding) {
Headers headers;
headers.emplace("Accept-Encoding", "");

std::string body;
auto res = cli_.Get("/compress", [&](const char *data, uint64_t data_length) {
EXPECT_EQ(100U, data_length);
body.append(data, data_length);
return true;
});
auto res = cli_.Get("/compress", headers,
[&](const char *data, uint64_t data_length) {
EXPECT_EQ(100U, data_length);
body.append(data, data_length);
return true;
});

ASSERT_TRUE(res);
EXPECT_TRUE(res->get_header_value("Content-Encoding").empty());
Expand Down

0 comments on commit d1d4eee

Please sign in to comment.