@@ -1165,18 +1165,8 @@ inline bool can_compress(const std::string& content_type) {
11651165 content_type == " application/xhtml+xml" ;
11661166}
11671167
1168- inline void compress (const Request& req, Response& res )
1168+ inline void compress (std::string& content )
11691169{
1170- // TODO: Server version is HTTP/1.1 and 'Accpet-Encoding' has gzip, not gzip;q=0
1171- const auto & encodings = req.get_header_value (" Accept-Encoding" );
1172- if (encodings.find (" gzip" ) == std::string::npos) {
1173- return ;
1174- }
1175-
1176- if (!can_compress (res.get_header_value (" Content-Type" ))) {
1177- return ;
1178- }
1179-
11801170 z_stream strm;
11811171 strm.zalloc = Z_NULL;
11821172 strm.zfree = Z_NULL;
@@ -1187,8 +1177,8 @@ inline void compress(const Request& req, Response& res)
11871177 return ;
11881178 }
11891179
1190- strm.avail_in = res. body .size ();
1191- strm.next_in = (Bytef *)res. body .data ();
1180+ strm.avail_in = content .size ();
1181+ strm.next_in = (Bytef *)content .data ();
11921182
11931183 std::string compressed;
11941184
@@ -1201,17 +1191,13 @@ inline void compress(const Request& req, Response& res)
12011191 compressed.append (buff, bufsiz - strm.avail_out );
12021192 } while (strm.avail_out == 0 );
12031193
1204- res.set_header (" Content-Encoding" , " gzip" );
1205- res.body .swap (compressed);
1194+ content.swap (compressed);
12061195
12071196 deflateEnd (&strm);
12081197}
12091198
1210- inline void decompress_request_body (Request& req )
1199+ inline void decompress (std::string& content )
12111200{
1212- if (req.get_header_value (" Content-Encoding" ) != " gzip" )
1213- return ;
1214-
12151201 z_stream strm;
12161202 strm.zalloc = Z_NULL;
12171203 strm.zfree = Z_NULL;
@@ -1225,8 +1211,8 @@ inline void decompress_request_body(Request& req)
12251211 return ;
12261212 }
12271213
1228- strm.avail_in = req. body .size ();
1229- strm.next_in = (Bytef *)req. body .data ();
1214+ strm.avail_in = content .size ();
1215+ strm.next_in = (Bytef *)content .data ();
12301216
12311217 std::string decompressed;
12321218
@@ -1239,7 +1225,7 @@ inline void decompress_request_body(Request& req)
12391225 decompressed.append (buff, bufsiz - strm.avail_out );
12401226 } while (strm.avail_out == 0 );
12411227
1242- req. body .swap (decompressed);
1228+ content .swap (decompressed);
12431229
12441230 inflateEnd (&strm);
12451231}
@@ -1531,7 +1517,13 @@ inline void Server::write_response(Stream& strm, bool last_connection, const Req
15311517
15321518 if (!res.body .empty ()) {
15331519#ifdef CPPHTTPLIB_ZLIB_SUPPORT
1534- detail::compress (req, res);
1520+ // TODO: Server version is HTTP/1.1 and 'Accpet-Encoding' has gzip, not gzip;q=0
1521+ const auto & encodings = req.get_header_value (" Accept-Encoding" );
1522+ if (encodings.find (" gzip" ) != std::string::npos &&
1523+ detail::can_compress (res.get_header_value (" Content-Type" ))) {
1524+ detail::compress (res.body );
1525+ res.set_header (" Content-Encoding" , " gzip" );
1526+ }
15351527#endif
15361528
15371529 if (!res.has_header (" Content-Type" )) {
@@ -1743,15 +1735,15 @@ inline bool Server::process_request(Stream& strm, bool last_connection)
17431735
17441736 const auto & content_type = req.get_header_value (" Content-Type" );
17451737
1738+ if (req.get_header_value (" Content-Encoding" ) == " gzip" ) {
17461739#ifdef CPPHTTPLIB_ZLIB_SUPPORT
1747- detail::decompress_request_body (req);
1740+ detail::decompress (req. body );
17481741#else
1749- if (req.get_header_value (" Content-Encoding" ) == " gzip" ) {
17501742 res.status = 415 ;
17511743 write_response (strm, last_connection, req, res);
17521744 return ret;
1753- }
17541745#endif
1746+ }
17551747
17561748 if (!content_type.find (" application/x-www-form-urlencoded" )) {
17571749 detail::parse_query_text (req.body , req.params );
@@ -1936,6 +1928,14 @@ inline bool Client::process_request(Stream& strm, Request& req, Response& res)
19361928 if (!detail::read_content (strm, res, req.progress )) {
19371929 return false ;
19381930 }
1931+
1932+ if (res.get_header_value (" Content-Encoding" ) == " gzip" ) {
1933+ #ifdef CPPHTTPLIB_ZLIB_SUPPORT
1934+ detail::decompress (res.body );
1935+ #else
1936+ return false ;
1937+ #endif
1938+ }
19391939 }
19401940
19411941 return true ;
0 commit comments