From a4bb9fdb893c8f2c36a6b77862e29b2608e080c2 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Tue, 7 Feb 2017 12:47:11 -0500 Subject: [PATCH] http: include provided status code in range error ServerResponse#writeHead() coerces the user provided status code to a number and then performs a range check. If the check fails, a range error is thrown. The coerced status code is included in the error message. This commit uses the user provided status code instead. PR-URL: https://github.com/nodejs/node/pull/11221 Reviewed-By: James M Snell --- lib/_http_server.js | 4 +++- .../parallel/test-http-response-statuscode.js | 20 +++++++++---------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/_http_server.js b/lib/_http_server.js index 80880be46bb9e5..73e0b2c14ae532 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -161,9 +161,11 @@ ServerResponse.prototype._implicitHeader = function _implicitHeader() { ServerResponse.prototype.writeHead = writeHead; function writeHead(statusCode, reason, obj) { + var originalStatusCode = statusCode; + statusCode |= 0; if (statusCode < 100 || statusCode > 999) - throw new RangeError(`Invalid status code: ${statusCode}`); + throw new RangeError(`Invalid status code: ${originalStatusCode}`); if (typeof reason === 'string') { // writeHead(statusCode, reasonPhrase[, headers]) diff --git a/test/parallel/test-http-response-statuscode.js b/test/parallel/test-http-response-statuscode.js index ed3676b03e1f0c..1b2d4d598e1841 100644 --- a/test/parallel/test-http-response-statuscode.js +++ b/test/parallel/test-http-response-statuscode.js @@ -20,17 +20,17 @@ const server = http.Server(common.mustCall(function(req, res) { case 1: assert.throws(common.mustCall(() => { res.writeHead(Infinity); - }), createErrorMessage(0)); + }), createErrorMessage(Infinity)); break; case 2: assert.throws(common.mustCall(() => { res.writeHead(NaN); - }), createErrorMessage(0)); + }), createErrorMessage(NaN)); break; case 3: assert.throws(common.mustCall(() => { res.writeHead({}); - }), createErrorMessage(0)); + }), createErrorMessage('\\[object Object\\]')); break; case 4: assert.throws(common.mustCall(() => { @@ -45,37 +45,37 @@ const server = http.Server(common.mustCall(function(req, res) { case 6: assert.throws(common.mustCall(() => { res.writeHead('1000'); - }), createErrorMessage(1000)); + }), createErrorMessage('1000')); break; case 7: assert.throws(common.mustCall(() => { res.writeHead(null); - }), createErrorMessage(0)); + }), createErrorMessage(null)); break; case 8: assert.throws(common.mustCall(() => { res.writeHead(true); - }), createErrorMessage(1)); + }), createErrorMessage(true)); break; case 9: assert.throws(common.mustCall(() => { res.writeHead([]); - }), createErrorMessage(0)); + }), createErrorMessage([])); break; case 10: assert.throws(common.mustCall(() => { res.writeHead('this is not valid'); - }), createErrorMessage(0)); + }), createErrorMessage('this is not valid')); break; case 11: assert.throws(common.mustCall(() => { res.writeHead('404 this is not valid either'); - }), createErrorMessage(0)); + }), createErrorMessage('404 this is not valid either')); break; case 12: assert.throws(common.mustCall(() => { res.writeHead(); - }), createErrorMessage(0)); + }), createErrorMessage(undefined)); this.close(); break; default: