Skip to content

Commit 5ad4311

Browse files
committed
1 parent 76ea8dd commit 5ad4311

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

httplib.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,7 +1643,16 @@ inline void Server::write_response(Stream& strm, bool last_connection, const Req
16431643
res.set_header("Connection", "Keep-Alive");
16441644
}
16451645

1646-
if (!res.body.empty()) {
1646+
if (res.body.empty()) {
1647+
if (!res.has_header("Content-Length")) {
1648+
if (res.streamcb) {
1649+
// Streamed response
1650+
res.set_header("Transfer-Encoding", "chunked");
1651+
} else {
1652+
res.set_header("Content-Length", "0");
1653+
}
1654+
}
1655+
} else {
16471656
#ifdef CPPHTTPLIB_ZLIB_SUPPORT
16481657
// TODO: 'Accpet-Encoding' has gzip, not gzip;q=0
16491658
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
16601669

16611670
auto length = std::to_string(res.body.size());
16621671
res.set_header("Content-Length", length.c_str());
1663-
} else if (res.streamcb) {
1664-
// Streamed response
1665-
bool chunked_response = !res.has_header("Content-Length");
1666-
if (chunked_response)
1667-
res.set_header("Transfer-Encoding", "chunked");
16681672
}
16691673

16701674
detail::write_headers(strm, res);

0 commit comments

Comments
 (0)