Skip to content

Commit

Permalink
Fix throwing error on request body without content-length and transfe…
Browse files Browse the repository at this point in the history
…r-encoding
  • Loading branch information
kylecarbs committed Jan 22, 2025
1 parent 9e5daea commit 8d06737
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions http-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,13 @@ HTTPParser.prototype.HEADER = function () {
} else {
var headers = info.headers;
var hasContentLength = false;
var hasTransferEncoding = false;
var currentContentLengthValue;
var hasUpgradeHeader = false;
for (var i = 0; i < headers.length; i += 2) {
switch (headers[i].toLowerCase()) {
case 'transfer-encoding':
hasTransferEncoding = true;
this.isChunked = headers[i + 1].toLowerCase() === 'chunked';
break;
case 'content-length':
Expand Down Expand Up @@ -328,6 +330,12 @@ HTTPParser.prototype.HEADER = function () {
}
}

// Logic from https://github.com/nodejs/llhttp/blob/dc5dc9a018214ae767a86bb5b0c69983d272d21e/src/native/http.c#L142-L146
// If there is no content length, no transfer encoding, and there is data in the body, throw.
if (!hasContentLength && !hasTransferEncoding && (this.end - this.offset) > 0) {
throw parseErrorCode('HPE_INVALID_METHOD');
}

// if both isChunked and hasContentLength, isChunked wins
// This is required so the body is parsed using the chunked method, and matches
// Chrome's behavior. We could, maybe, ignore them both (would get chunked
Expand Down

0 comments on commit 8d06737

Please sign in to comment.