Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Feb 16, 2023
1 parent ab2c162 commit a1c9fc7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
45 changes: 20 additions & 25 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,20 +387,6 @@ function _writeRaw(data, encoding, callback, size) {
encoding = null;
}

if (size != null) {
if (
this.strictContentLength &&
this._hasBody &&
!this._removedContLen &&
!this.chunkedEncoding &&
this[kBytesWritten] + size > this._contentLength &&
!this.hasHeader('transfer-encoding')
) {
throw new ERR_HTTP_CONTENT_LENGTH_MISMATCH(size + this[kBytesWritten], this._contentLength);
}
this[kBytesWritten] += size;
}

if (conn && conn._httpMessage === this && conn.writable) {
// There might be pending data in the this.output buffer.
if (this.outputData.length) {
Expand Down Expand Up @@ -852,6 +838,17 @@ function emitErrorNt(msg, err, callback) {
}
}

function strictContentLength(msg) {
return (
msg._contentLength != null &&
msg.strictContentLength &&
msg._hasBody &&
!msg._removedContLen &&
!msg.chunkedEncoding &&
!msg.hasHeader('transfer-encoding')
)
}

function write_(msg, chunk, encoding, callback, fromEnd) {
if (typeof callback !== 'function')
callback = nop;
Expand Down Expand Up @@ -879,10 +876,15 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
return false;
}

let len;
let len = msg.strictContentLength ?
typeof chunk === 'string' ? Buffer.byteLength(chunk, encoding) : chunk.byteLength : null;

if (msg.strictContentLength) {
len = typeof chunk === 'string' ? Buffer.byteLength(chunk, encoding) : chunk.byteLength;
if (len != null) {
if (strictContentLength(msg) && (fromEnd ? msg[kBytesWritten] + len !== msg._contentLength : msg[kBytesWritten] + len > msg._contentLength)
) {
throw new ERR_HTTP_CONTENT_LENGTH_MISMATCH(len + msg[kBytesWritten], msg._contentLength);
}
msg[kBytesWritten] += len;
}

if (!msg._header) {
Expand Down Expand Up @@ -1019,14 +1021,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
if (typeof callback === 'function')
this.once('finish', callback);

if (
this.strictContentLength &&
this._hasBody &&
!this._removedContLen &&
!this.chunkedEncoding &&
this[kBytesWritten] !== this._contentLength &&
!this.hasHeader('transfer-encoding')
) {
if (strictContentLength(this) && this[kBytesWritten] !== this._contentLength) {
throw new ERR_HTTP_CONTENT_LENGTH_MISMATCH(this[kBytesWritten], this._contentLength);
}

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http-content-length-mismatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function shouldThrowOnFewerBytes() {
res.write('a');
res.statusCode = 200;
assert.throws(() => {
res.end();
res.end('aaa');
}, {
code: 'ERR_HTTP_CONTENT_LENGTH_MISMATCH'
});
Expand Down

0 comments on commit a1c9fc7

Please sign in to comment.