Skip to content

Commit 3eaa769

Browse files
committed
Fix #481, #483, #487
1 parent b915405 commit 3eaa769

File tree

3 files changed

+59
-20
lines changed

3 files changed

+59
-20
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,13 +559,21 @@ The server applies gzip compression to the following MIME type contents:
559559
* application/xml
560560
* application/xhtml+xml
561561
562-
### Compress content on client
562+
### Compress request body on client
563563
564564
```c++
565565
cli.set_compress(true);
566566
res = cli.Post("/resource/foo", "...", "text/plain");
567567
```
568568

569+
### Compress response body on client
570+
571+
```c++
572+
cli.set_decompress(false);
573+
res = cli.Get("/resource/foo", {{"Accept-Encoding", "gzip, deflate"}});
574+
res->body; // Compressed data
575+
```
576+
569577
Split httplib.h into .h and .cc
570578
-------------------------------
571579

httplib.h

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,8 @@ class Client {
778778

779779
void set_compress(bool on);
780780

781+
void set_decompress(bool on);
782+
781783
void set_interface(const char *intf);
782784

783785
void set_proxy(const char *host, int port);
@@ -821,6 +823,7 @@ class Client {
821823
bool follow_location_ = false;
822824

823825
bool compress_ = false;
826+
bool decompress_ = true;
824827

825828
std::string interface_;
826829

@@ -853,6 +856,7 @@ class Client {
853856
#endif
854857
follow_location_ = rhs.follow_location_;
855858
compress_ = rhs.compress_;
859+
decompress_ = rhs.decompress_;
856860
interface_ = rhs.interface_;
857861
proxy_host_ = rhs.proxy_host_;
858862
proxy_port_ = rhs.proxy_port_;
@@ -1281,6 +1285,11 @@ class Client2 {
12811285
return *this;
12821286
}
12831287

1288+
Client2 &set_decompress(bool on) {
1289+
cli_->set_decompress(on);
1290+
return *this;
1291+
}
1292+
12841293
Client2 &set_interface(const char *intf) {
12851294
cli_->set_interface(intf);
12861295
return *this;
@@ -2395,34 +2404,39 @@ inline bool is_chunked_transfer_encoding(const Headers &headers) {
23952404

23962405
template <typename T>
23972406
bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status,
2398-
Progress progress, ContentReceiver receiver) {
2407+
Progress progress, ContentReceiver receiver, bool decompress) {
23992408

24002409
ContentReceiver out = [&](const char *buf, size_t n) {
24012410
return receiver(buf, n);
24022411
};
24032412

24042413
#ifdef CPPHTTPLIB_ZLIB_SUPPORT
24052414
decompressor decompressor;
2415+
#endif
24062416

2407-
std::string content_encoding = x.get_header_value("Content-Encoding");
2408-
if (content_encoding.find("gzip") != std::string::npos ||
2409-
content_encoding.find("deflate") != std::string::npos) {
2410-
if (!decompressor.is_valid()) {
2411-
status = 500;
2412-
return false;
2413-
}
2417+
if (decompress) {
2418+
#ifdef CPPHTTPLIB_ZLIB_SUPPORT
2419+
std::string content_encoding = x.get_header_value("Content-Encoding");
2420+
if (content_encoding.find("gzip") != std::string::npos ||
2421+
content_encoding.find("deflate") != std::string::npos) {
2422+
if (!decompressor.is_valid()) {
2423+
status = 500;
2424+
return false;
2425+
}
24142426

2415-
out = [&](const char *buf, size_t n) {
2416-
return decompressor.decompress(
2417-
buf, n, [&](const char *buf, size_t n) { return receiver(buf, n); });
2418-
};
2419-
}
2427+
out = [&](const char *buf, size_t n) {
2428+
return decompressor.decompress(buf, n, [&](const char *buf, size_t n) {
2429+
return receiver(buf, n);
2430+
});
2431+
};
2432+
}
24202433
#else
2421-
if (x.get_header_value("Content-Encoding") == "gzip") {
2422-
status = 415;
2423-
return false;
2424-
}
2434+
if (x.get_header_value("Content-Encoding") == "gzip") {
2435+
status = 415;
2436+
return false;
2437+
}
24252438
#endif
2439+
}
24262440

24272441
auto ret = true;
24282442
auto exceed_payload_max_length = false;
@@ -3883,7 +3897,7 @@ inline bool Server::read_content_core(Stream &strm, Request &req, Response &res,
38833897
}
38843898

38853899
if (!detail::read_content(strm, req, payload_max_length_, res.status,
3886-
Progress(), out)) {
3900+
Progress(), out, true)) {
38873901
return false;
38883902
}
38893903

@@ -4663,7 +4677,7 @@ inline bool Client::process_request(Stream &strm, const Request &req,
46634677

46644678
int dummy_status;
46654679
if (!detail::read_content(strm, res, (std::numeric_limits<size_t>::max)(),
4666-
dummy_status, req.progress, out)) {
4680+
dummy_status, req.progress, out, decompress_)) {
46674681
return false;
46684682
}
46694683
}
@@ -5025,6 +5039,8 @@ inline void Client::set_follow_location(bool on) { follow_location_ = on; }
50255039

50265040
inline void Client::set_compress(bool on) { compress_ = on; }
50275041

5042+
inline void Client::set_decompress(bool on) { decompress_ = on; }
5043+
50285044
inline void Client::set_interface(const char *intf) { interface_ = intf; }
50295045

50305046
inline void Client::set_proxy(const char *host, int port) {

test/test.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2206,6 +2206,21 @@ TEST_F(ServerTest, GzipWithContentReceiver) {
22062206
EXPECT_EQ(200, res->status);
22072207
}
22082208

2209+
TEST_F(ServerTest, GzipWithoutDecompressing) {
2210+
Headers headers;
2211+
headers.emplace("Accept-Encoding", "gzip, deflate");
2212+
2213+
cli_.set_decompress(false);
2214+
auto res = cli_.Get("/gzip", headers);
2215+
2216+
ASSERT_TRUE(res != nullptr);
2217+
EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
2218+
EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
2219+
EXPECT_EQ("33", res->get_header_value("Content-Length"));
2220+
EXPECT_EQ(33, res->body.size());
2221+
EXPECT_EQ(200, res->status);
2222+
}
2223+
22092224
TEST_F(ServerTest, GzipWithContentReceiverWithoutAcceptEncoding) {
22102225
Headers headers;
22112226
std::string body;

0 commit comments

Comments
 (0)