From 5ad4311fb0257c1ced9d2f8e4fe32b0c478120da Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 22 Nov 2018 20:50:54 -0500 Subject: [PATCH] fix #109 --- httplib.h | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/httplib.h b/httplib.h index 6c6ad0e..e494b84 100644 --- a/httplib.h +++ b/httplib.h @@ -1643,7 +1643,16 @@ inline void Server::write_response(Stream& strm, bool last_connection, const Req 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 // TODO: 'Accpet-Encoding' has gzip, not gzip;q=0 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()); 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);