Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix throwing error on request body without content-length and transfer-encoding #101

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (this.type === HTTPParser.REQUEST && !hasUpgradeHeader && !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
Loading