Fix #1975
This commit is contained in:
parent
7bd316f3d0
commit
bfef4b3e9b
12
httplib.h
12
httplib.h
@ -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")) { 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
|
#ifndef CPPHTTPLIB_NO_DEFAULT_USER_AGENT
|
||||||
if (!req.has_header("User-Agent")) {
|
if (!req.has_header("User-Agent")) {
|
||||||
auto agent = std::string("cpp-httplib/") + CPPHTTPLIB_VERSION;
|
auto agent = std::string("cpp-httplib/") + CPPHTTPLIB_VERSION;
|
||||||
|
41
test/test.cc
41
test/test.cc
@ -2004,7 +2004,7 @@ TEST(ErrorHandlerTest, ContentLength) {
|
|||||||
{
|
{
|
||||||
Client cli(HOST, PORT);
|
Client cli(HOST, PORT);
|
||||||
|
|
||||||
auto res = cli.Get("/hi");
|
auto res = cli.Get("/hi", {{"Accept-Encoding", ""}});
|
||||||
ASSERT_TRUE(res);
|
ASSERT_TRUE(res);
|
||||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||||
EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
|
EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
|
||||||
@ -2087,7 +2087,7 @@ TEST(ExceptionTest, WithExceptionHandler) {
|
|||||||
Client cli(HOST, PORT);
|
Client cli(HOST, PORT);
|
||||||
|
|
||||||
for (size_t j = 0; j < 100; j++) {
|
for (size_t j = 0; j < 100; j++) {
|
||||||
auto res = cli.Get("/hi");
|
auto res = cli.Get("/hi", {{"Accept-Encoding", ""}});
|
||||||
ASSERT_TRUE(res);
|
ASSERT_TRUE(res);
|
||||||
EXPECT_EQ(StatusCode::InternalServerError_500, res->status);
|
EXPECT_EQ(StatusCode::InternalServerError_500, res->status);
|
||||||
EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
|
EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
|
||||||
@ -2098,7 +2098,7 @@ TEST(ExceptionTest, WithExceptionHandler) {
|
|||||||
cli.set_keep_alive(true);
|
cli.set_keep_alive(true);
|
||||||
|
|
||||||
for (size_t j = 0; j < 100; j++) {
|
for (size_t j = 0; j < 100; j++) {
|
||||||
auto res = cli.Get("/hi");
|
auto res = cli.Get("/hi", {{"Accept-Encoding", ""}});
|
||||||
ASSERT_TRUE(res);
|
ASSERT_TRUE(res);
|
||||||
EXPECT_EQ(StatusCode::InternalServerError_500, res->status);
|
EXPECT_EQ(StatusCode::InternalServerError_500, res->status);
|
||||||
EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
|
EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
|
||||||
@ -3803,7 +3803,10 @@ TEST_F(ServerTest, GetRangeWithMaxLongLength) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(ServerTest, GetRangeWithZeroToInfinite) {
|
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);
|
ASSERT_TRUE(res);
|
||||||
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
||||||
EXPECT_EQ("7", res->get_header_value("Content-Length"));
|
EXPECT_EQ("7", res->get_header_value("Content-Length"));
|
||||||
@ -3899,7 +3902,10 @@ TEST_F(ServerTest, ClientStop) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(ServerTest, GetWithRange1) {
|
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);
|
ASSERT_TRUE(res);
|
||||||
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
||||||
EXPECT_EQ("3", res->get_header_value("Content-Length"));
|
EXPECT_EQ("3", res->get_header_value("Content-Length"));
|
||||||
@ -3909,7 +3915,10 @@ TEST_F(ServerTest, GetWithRange1) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(ServerTest, GetWithRange2) {
|
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);
|
ASSERT_TRUE(res);
|
||||||
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
||||||
EXPECT_EQ("6", res->get_header_value("Content-Length"));
|
EXPECT_EQ("6", res->get_header_value("Content-Length"));
|
||||||
@ -3919,7 +3928,10 @@ TEST_F(ServerTest, GetWithRange2) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(ServerTest, GetWithRange3) {
|
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);
|
ASSERT_TRUE(res);
|
||||||
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
||||||
EXPECT_EQ("1", res->get_header_value("Content-Length"));
|
EXPECT_EQ("1", res->get_header_value("Content-Length"));
|
||||||
@ -3929,7 +3941,10 @@ TEST_F(ServerTest, GetWithRange3) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(ServerTest, GetWithRange4) {
|
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);
|
ASSERT_TRUE(res);
|
||||||
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
EXPECT_EQ(StatusCode::PartialContent_206, res->status);
|
||||||
EXPECT_EQ("2", res->get_header_value("Content-Length"));
|
EXPECT_EQ("2", res->get_header_value("Content-Length"));
|
||||||
@ -4674,7 +4689,9 @@ TEST_F(ServerTest, Gzip) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(ServerTest, GzipWithoutAcceptEncoding) {
|
TEST_F(ServerTest, GzipWithoutAcceptEncoding) {
|
||||||
auto res = cli_.Get("/compress");
|
Headers headers;
|
||||||
|
headers.emplace("Accept-Encoding", "");
|
||||||
|
auto res = cli_.Get("/compress", headers);
|
||||||
|
|
||||||
ASSERT_TRUE(res);
|
ASSERT_TRUE(res);
|
||||||
EXPECT_TRUE(res->get_header_value("Content-Encoding").empty());
|
EXPECT_TRUE(res->get_header_value("Content-Encoding").empty());
|
||||||
@ -4723,8 +4740,12 @@ TEST_F(ServerTest, GzipWithoutDecompressing) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(ServerTest, GzipWithContentReceiverWithoutAcceptEncoding) {
|
TEST_F(ServerTest, GzipWithContentReceiverWithoutAcceptEncoding) {
|
||||||
|
Headers headers;
|
||||||
|
headers.emplace("Accept-Encoding", "");
|
||||||
|
|
||||||
std::string body;
|
std::string body;
|
||||||
auto res = cli_.Get("/compress", [&](const char *data, uint64_t data_length) {
|
auto res = cli_.Get("/compress", headers,
|
||||||
|
[&](const char *data, uint64_t data_length) {
|
||||||
EXPECT_EQ(100U, data_length);
|
EXPECT_EQ(100U, data_length);
|
||||||
body.append(data, data_length);
|
body.append(data, data_length);
|
||||||
return true;
|
return true;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user