Fix #1975
Some checks are pending
test / ubuntu (push) Waiting to run
test / macos (push) Waiting to run
test / windows (push) Waiting to run

This commit is contained in:
yhirose 2024-11-14 17:25:51 -05:00
parent 7bd316f3d0
commit bfef4b3e9b
2 changed files with 47 additions and 14 deletions

View file

@ -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;

View file

@ -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"));
@ -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"));
@ -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"));
@ -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"));
@ -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"));
@ -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"));
@ -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"));
@ -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"));
@ -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());
@ -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());