Remove Content-Length header when chunked reply is used, only close connection when Content-Length is unknown.

Change-Id: I12726fb8b08417f7e7b429f525ee2c7980f22375
This commit is contained in:
Thulinma 2023-02-07 16:08:29 +01:00
parent 9cd08e3a77
commit 5860719ec7

View file

@ -284,8 +284,18 @@ void HTTP::Parser::StartResponse(std::string code, std::string message, const HT
protocol = prot; protocol = prot;
if (sendingChunks){ if (sendingChunks){
SetHeader("Transfer-Encoding", "chunked"); 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{ }else{
SetHeader("Connection", "close"); if (!headers.count("Content-Length")){SetHeader("Connection", "close");}
} }
bufferChunks = bufferAllChunks; bufferChunks = bufferAllChunks;
if (!bufferAllChunks){SendResponse(code, message, conn);} if (!bufferAllChunks){SendResponse(code, message, conn);}