Skip to content

Commit 8d06737

Browse files
committed
Fix throwing error on request body without content-length and transfer-encoding
1 parent 9e5daea commit 8d06737

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

http-parser.js

+8
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,13 @@ HTTPParser.prototype.HEADER = function () {
296296
} else {
297297
var headers = info.headers;
298298
var hasContentLength = false;
299+
var hasTransferEncoding = false;
299300
var currentContentLengthValue;
300301
var hasUpgradeHeader = false;
301302
for (var i = 0; i < headers.length; i += 2) {
302303
switch (headers[i].toLowerCase()) {
303304
case 'transfer-encoding':
305+
hasTransferEncoding = true;
304306
this.isChunked = headers[i + 1].toLowerCase() === 'chunked';
305307
break;
306308
case 'content-length':
@@ -328,6 +330,12 @@ HTTPParser.prototype.HEADER = function () {
328330
}
329331
}
330332

333+
// Logic from https://github.com/nodejs/llhttp/blob/dc5dc9a018214ae767a86bb5b0c69983d272d21e/src/native/http.c#L142-L146
334+
// If there is no content length, no transfer encoding, and there is data in the body, throw.
335+
if (!hasContentLength && !hasTransferEncoding && (this.end - this.offset) > 0) {
336+
throw parseErrorCode('HPE_INVALID_METHOD');
337+
}
338+
331339
// if both isChunked and hasContentLength, isChunked wins
332340
// This is required so the body is parsed using the chunked method, and matches
333341
// Chrome's behavior. We could, maybe, ignore them both (would get chunked

0 commit comments

Comments
 (0)