Skip to content

Commit 06cea34

Browse files
committed
fix: 1xx 204 304 no need Content-Length (#569)
1 parent a1baf87 commit 06cea34

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

http/HttpMessage.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,19 @@ void HttpMessage::FillContentType() {
401401
return;
402402
}
403403

404+
bool HttpMessage::NeedContentLength() {
405+
if (type == HTTP_RESPONSE) {
406+
HttpResponse* res = (HttpResponse*)(this);
407+
if (res->status_code / 100 == 1 ||
408+
res->status_code == HTTP_STATUS_NO_CONTENT ||
409+
res->status_code == HTTP_STATUS_NOT_MODIFIED) {
410+
return false;
411+
}
412+
return true;
413+
}
414+
return false;
415+
}
416+
404417
void HttpMessage::FillContentLength() {
405418
auto iter = headers.find("Content-Length");
406419
if (iter != headers.end()) {
@@ -411,7 +424,7 @@ void HttpMessage::FillContentLength() {
411424
content_length = body.size();
412425
}
413426
if (iter == headers.end() && !IsChunked() && content_type != TEXT_EVENT_STREAM) {
414-
if (content_length != 0 || type == HTTP_RESPONSE) {
427+
if (content_length != 0 || NeedContentLength()) {
415428
headers["Content-Length"] = hv::to_string(content_length);
416429
}
417430
}

http/HttpMessage.h

+1
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ class HV_EXPORT HttpMessage {
253253
void FillContentType();
254254
// body.size -> content_length <-> headers["Content-Length"]
255255
void FillContentLength();
256+
bool NeedContentLength();
256257

257258
bool IsChunked();
258259
bool IsKeepAlive();

0 commit comments

Comments
 (0)