From 5860719ec7d9a4e62fd2a8a6fb89c99460906ed8 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Tue, 7 Feb 2023 16:08:29 +0100 Subject: [PATCH] Remove Content-Length header when chunked reply is used, only close connection when Content-Length is unknown. Change-Id: I12726fb8b08417f7e7b429f525ee2c7980f22375 --- lib/http_parser.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/http_parser.cpp b/lib/http_parser.cpp index 113fdb51..fe1b8eac 100644 --- a/lib/http_parser.cpp +++ b/lib/http_parser.cpp @@ -284,8 +284,18 @@ void HTTP::Parser::StartResponse(std::string code, std::string message, const HT protocol = prot; if (sendingChunks){ SetHeader("Transfer-Encoding", "chunked"); + //Chunked encoding does not allow a Content-Length, so convert to Content-Range instead + if (headers.count("Content-Length")){ + uint32_t len = atoi(headers["Content-Length"].c_str()); + if (len && !headers.count("Content-Range")){ + std::stringstream rangeReply; + rangeReply << "bytes 0-" << (len-1) << "/" << len; + SetHeader("Content-Range", rangeReply.str()); + } + headers.erase("Content-Length"); + } }else{ - SetHeader("Connection", "close"); + if (!headers.count("Content-Length")){SetHeader("Connection", "close");} } bufferChunks = bufferAllChunks; if (!bufferAllChunks){SendResponse(code, message, conn);}