Skip to content

Commit

Permalink
http: don't throw on Uint8Arrays for http.ServerResponse#write
Browse files Browse the repository at this point in the history
Don't throw errors on Uint8Arrays and added test for all
valid types.

Backport-PR-URL: #33488
PR-URL: #33155
Fixes: #33379
Refs: #29829
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Zeyu Yang <himself65@outlook.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
rexagod committed May 20, 2020
1 parent daf1d84 commit d2a920c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
19 changes: 16 additions & 3 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ const {
ERR_INVALID_CHAR,
ERR_METHOD_NOT_IMPLEMENTED,
ERR_STREAM_CANNOT_PIPE,
ERR_STREAM_NULL_VALUES,
ERR_STREAM_WRITE_AFTER_END
},
hideStackFrames
} = require('internal/errors');
const { validateString } = require('internal/validators');
const { isUint8Array } = require('internal/util/types');

const HIGH_WATER_MARK = getDefaultHighWaterMark();
const { CRLF, debug } = common;
Expand Down Expand Up @@ -625,6 +627,16 @@ OutgoingMessage.prototype.write = function write(chunk, encoding, callback) {
};

function write_(msg, chunk, encoding, callback, fromEnd) {
if (typeof callback !== 'function')
callback = function() {};

if (chunk === null)
throw new ERR_STREAM_NULL_VALUES();

if (typeof chunk !== 'string' && !isUint8Array(chunk))
throw new ERR_INVALID_ARG_TYPE(
'chunk', ['string', 'Buffer', 'Uint8Array'], chunk);

if (msg.finished) {
const err = new ERR_STREAM_WRITE_AFTER_END();
const triggerAsyncId = msg.socket ? msg.socket[async_id_symbol] : undefined;
Expand All @@ -649,9 +661,9 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
return true;
}

if (!fromEnd && typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
if (!fromEnd && typeof chunk !== 'string' && !isUint8Array(chunk)) {
throw new ERR_INVALID_ARG_TYPE('first argument',
['string', 'Buffer'], chunk);
['string', 'Buffer', 'Uint8Array'], chunk);
}

if (!fromEnd && msg.connection && !msg.connection.writableCorked) {
Expand Down Expand Up @@ -742,7 +754,8 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {

if (chunk) {
if (typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
throw new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer'], chunk);
throw new ERR_INVALID_ARG_TYPE(
'chunk', ['string', 'Buffer', 'Uint8Array'], chunk);
}
if (!this._header) {
if (typeof chunk === 'string')
Expand Down
18 changes: 13 additions & 5 deletions test/parallel/test-http-outgoing-proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,24 @@ assert.throws(() => {
outgoingMessage.write('');
}

assert(OutgoingMessage.prototype.write.call({ _header: 'test' }));
assert.throws(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.write.call({ _header: 'test' });
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "chunk" argument must be of type string or an instance of ' +
'Buffer or Uint8Array. Received undefined'
});

assert.throws(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.write.call({ _header: 'test', _hasBody: 'test' });
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The first argument must be of type string or an instance of ' +
'Buffer. Received undefined'
message: 'The "chunk" argument must be of type string or an instance of ' +
'Buffer or Uint8Array. Received undefined'
});

assert.throws(() => {
Expand All @@ -89,8 +97,8 @@ assert.throws(() => {
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The first argument must be of type string or an instance of ' +
'Buffer. Received type number (1)'
message: 'The "chunk" argument must be of type string or an instance of ' +
'Buffer or Uint8Array. Received type number (1)'
});

// addTrailers()
Expand Down
24 changes: 24 additions & 0 deletions test/parallel/test-http-outgoing-write-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');

const httpServer = http.createServer(common.mustCall(function(req, res) {
httpServer.close();
assert.throws(() => {
res.write(['Throws.']);
}, {
code: 'ERR_INVALID_ARG_TYPE'
});
// should not throw
res.write('1a2b3c');
// should not throw
res.write(new Uint8Array(1024));
// should not throw
res.write(Buffer.from('1'.repeat(1024)));
res.end();
}));

httpServer.listen(0, common.mustCall(function() {
http.get({ port: this.address().port });
}));

0 comments on commit d2a920c

Please sign in to comment.