mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2025-05-11 05:33:56 +00:00
parent
b91540514d
commit
3eaa769a2d
10
README.md
10
README.md
@ -559,13 +559,21 @@ The server applies gzip compression to the following MIME type contents:
|
|||||||
* application/xml
|
* application/xml
|
||||||
* application/xhtml+xml
|
* application/xhtml+xml
|
||||||
|
|
||||||
### Compress content on client
|
### Compress request body on client
|
||||||
|
|
||||||
```c++
|
```c++
|
||||||
cli.set_compress(true);
|
cli.set_compress(true);
|
||||||
res = cli.Post("/resource/foo", "...", "text/plain");
|
res = cli.Post("/resource/foo", "...", "text/plain");
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Compress response body on client
|
||||||
|
|
||||||
|
```c++
|
||||||
|
cli.set_decompress(false);
|
||||||
|
res = cli.Get("/resource/foo", {{"Accept-Encoding", "gzip, deflate"}});
|
||||||
|
res->body; // Compressed data
|
||||||
|
```
|
||||||
|
|
||||||
Split httplib.h into .h and .cc
|
Split httplib.h into .h and .cc
|
||||||
-------------------------------
|
-------------------------------
|
||||||
|
|
||||||
|
54
httplib.h
54
httplib.h
@ -778,6 +778,8 @@ public:
|
|||||||
|
|
||||||
void set_compress(bool on);
|
void set_compress(bool on);
|
||||||
|
|
||||||
|
void set_decompress(bool on);
|
||||||
|
|
||||||
void set_interface(const char *intf);
|
void set_interface(const char *intf);
|
||||||
|
|
||||||
void set_proxy(const char *host, int port);
|
void set_proxy(const char *host, int port);
|
||||||
@ -821,6 +823,7 @@ protected:
|
|||||||
bool follow_location_ = false;
|
bool follow_location_ = false;
|
||||||
|
|
||||||
bool compress_ = false;
|
bool compress_ = false;
|
||||||
|
bool decompress_ = true;
|
||||||
|
|
||||||
std::string interface_;
|
std::string interface_;
|
||||||
|
|
||||||
@ -853,6 +856,7 @@ protected:
|
|||||||
#endif
|
#endif
|
||||||
follow_location_ = rhs.follow_location_;
|
follow_location_ = rhs.follow_location_;
|
||||||
compress_ = rhs.compress_;
|
compress_ = rhs.compress_;
|
||||||
|
decompress_ = rhs.decompress_;
|
||||||
interface_ = rhs.interface_;
|
interface_ = rhs.interface_;
|
||||||
proxy_host_ = rhs.proxy_host_;
|
proxy_host_ = rhs.proxy_host_;
|
||||||
proxy_port_ = rhs.proxy_port_;
|
proxy_port_ = rhs.proxy_port_;
|
||||||
@ -1281,6 +1285,11 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Client2 &set_decompress(bool on) {
|
||||||
|
cli_->set_decompress(on);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
Client2 &set_interface(const char *intf) {
|
Client2 &set_interface(const char *intf) {
|
||||||
cli_->set_interface(intf);
|
cli_->set_interface(intf);
|
||||||
return *this;
|
return *this;
|
||||||
@ -2395,7 +2404,7 @@ inline bool is_chunked_transfer_encoding(const Headers &headers) {
|
|||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status,
|
bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status,
|
||||||
Progress progress, ContentReceiver receiver) {
|
Progress progress, ContentReceiver receiver, bool decompress) {
|
||||||
|
|
||||||
ContentReceiver out = [&](const char *buf, size_t n) {
|
ContentReceiver out = [&](const char *buf, size_t n) {
|
||||||
return receiver(buf, n);
|
return receiver(buf, n);
|
||||||
@ -2403,26 +2412,31 @@ bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status,
|
|||||||
|
|
||||||
#ifdef CPPHTTPLIB_ZLIB_SUPPORT
|
#ifdef CPPHTTPLIB_ZLIB_SUPPORT
|
||||||
decompressor decompressor;
|
decompressor decompressor;
|
||||||
|
#endif
|
||||||
|
|
||||||
std::string content_encoding = x.get_header_value("Content-Encoding");
|
if (decompress) {
|
||||||
if (content_encoding.find("gzip") != std::string::npos ||
|
#ifdef CPPHTTPLIB_ZLIB_SUPPORT
|
||||||
content_encoding.find("deflate") != std::string::npos) {
|
std::string content_encoding = x.get_header_value("Content-Encoding");
|
||||||
if (!decompressor.is_valid()) {
|
if (content_encoding.find("gzip") != std::string::npos ||
|
||||||
status = 500;
|
content_encoding.find("deflate") != std::string::npos) {
|
||||||
|
if (!decompressor.is_valid()) {
|
||||||
|
status = 500;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
out = [&](const char *buf, size_t n) {
|
||||||
|
return decompressor.decompress(buf, n, [&](const char *buf, size_t n) {
|
||||||
|
return receiver(buf, n);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
if (x.get_header_value("Content-Encoding") == "gzip") {
|
||||||
|
status = 415;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
out = [&](const char *buf, size_t n) {
|
|
||||||
return decompressor.decompress(
|
|
||||||
buf, n, [&](const char *buf, size_t n) { return receiver(buf, n); });
|
|
||||||
};
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
if (x.get_header_value("Content-Encoding") == "gzip") {
|
|
||||||
status = 415;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
auto ret = true;
|
auto ret = true;
|
||||||
auto exceed_payload_max_length = false;
|
auto exceed_payload_max_length = false;
|
||||||
@ -3883,7 +3897,7 @@ inline bool Server::read_content_core(Stream &strm, Request &req, Response &res,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!detail::read_content(strm, req, payload_max_length_, res.status,
|
if (!detail::read_content(strm, req, payload_max_length_, res.status,
|
||||||
Progress(), out)) {
|
Progress(), out, true)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -4663,7 +4677,7 @@ inline bool Client::process_request(Stream &strm, const Request &req,
|
|||||||
|
|
||||||
int dummy_status;
|
int dummy_status;
|
||||||
if (!detail::read_content(strm, res, (std::numeric_limits<size_t>::max)(),
|
if (!detail::read_content(strm, res, (std::numeric_limits<size_t>::max)(),
|
||||||
dummy_status, req.progress, out)) {
|
dummy_status, req.progress, out, decompress_)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -5025,6 +5039,8 @@ inline void Client::set_follow_location(bool on) { follow_location_ = on; }
|
|||||||
|
|
||||||
inline void Client::set_compress(bool on) { compress_ = on; }
|
inline void Client::set_compress(bool on) { compress_ = on; }
|
||||||
|
|
||||||
|
inline void Client::set_decompress(bool on) { decompress_ = on; }
|
||||||
|
|
||||||
inline void Client::set_interface(const char *intf) { interface_ = intf; }
|
inline void Client::set_interface(const char *intf) { interface_ = intf; }
|
||||||
|
|
||||||
inline void Client::set_proxy(const char *host, int port) {
|
inline void Client::set_proxy(const char *host, int port) {
|
||||||
|
15
test/test.cc
15
test/test.cc
@ -2206,6 +2206,21 @@ TEST_F(ServerTest, GzipWithContentReceiver) {
|
|||||||
EXPECT_EQ(200, res->status);
|
EXPECT_EQ(200, res->status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(ServerTest, GzipWithoutDecompressing) {
|
||||||
|
Headers headers;
|
||||||
|
headers.emplace("Accept-Encoding", "gzip, deflate");
|
||||||
|
|
||||||
|
cli_.set_decompress(false);
|
||||||
|
auto res = cli_.Get("/gzip", headers);
|
||||||
|
|
||||||
|
ASSERT_TRUE(res != nullptr);
|
||||||
|
EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
|
||||||
|
EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
|
||||||
|
EXPECT_EQ("33", res->get_header_value("Content-Length"));
|
||||||
|
EXPECT_EQ(33, res->body.size());
|
||||||
|
EXPECT_EQ(200, res->status);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_F(ServerTest, GzipWithContentReceiverWithoutAcceptEncoding) {
|
TEST_F(ServerTest, GzipWithContentReceiverWithoutAcceptEncoding) {
|
||||||
Headers headers;
|
Headers headers;
|
||||||
std::string body;
|
std::string body;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user