yhirose 7 years ago
parent
commit
5ad4311fb0
1 changed files with 10 additions and 6 deletions
  1. 10 6
      httplib.h

+ 10 - 6
httplib.h

@@ -1643,7 +1643,16 @@ inline void Server::write_response(Stream& strm, bool last_connection, const Req
         res.set_header("Connection", "Keep-Alive");
         res.set_header("Connection", "Keep-Alive");
     }
     }
 
 
-    if (!res.body.empty()) {
+    if (res.body.empty()) {
+        if (!res.has_header("Content-Length")) {
+            if (res.streamcb) {
+                // Streamed response
+                res.set_header("Transfer-Encoding", "chunked");
+            } else {
+                res.set_header("Content-Length", "0");
+            }
+        }
+    } else {
 #ifdef CPPHTTPLIB_ZLIB_SUPPORT
 #ifdef CPPHTTPLIB_ZLIB_SUPPORT
         // TODO: 'Accpet-Encoding' has gzip, not gzip;q=0
         // TODO: 'Accpet-Encoding' has gzip, not gzip;q=0
         const auto& encodings = req.get_header_value("Accept-Encoding");
         const auto& encodings = req.get_header_value("Accept-Encoding");
@@ -1660,11 +1669,6 @@ inline void Server::write_response(Stream& strm, bool last_connection, const Req
 
 
         auto length = std::to_string(res.body.size());
         auto length = std::to_string(res.body.size());
         res.set_header("Content-Length", length.c_str());
         res.set_header("Content-Length", length.c_str());
-    } else if (res.streamcb) {
-        // Streamed response
-        bool chunked_response = !res.has_header("Content-Length");
-        if (chunked_response)
-            res.set_header("Transfer-Encoding", "chunked");
     }
     }
 
 
     detail::write_headers(strm, res);
     detail::write_headers(strm, res);