From b8fc6b8eb6f835462fa32a69ea48e1c37440c9c6 Mon Sep 17 00:00:00 2001 From: Brian White Date: Sat, 31 Dec 2016 16:04:00 -0500 Subject: [PATCH] http: check statusCode early By enforcing the statusCode to be an SMI, it helps a bit performance-wise when looking up the associated statusMessage. PR-URL: https://github.com/nodejs/node/pull/10558 Reviewed-By: Matteo Collina Reviewed-By: James M Snell Reviewed-By: Evan Lucas --- lib/_http_server.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/_http_server.js b/lib/_http_server.js index bd726f83e3e638..fe563977a635e3 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -162,6 +162,9 @@ ServerResponse.prototype._implicitHeader = function _implicitHeader() { ServerResponse.prototype.writeHead = writeHead; function writeHead(statusCode, reason, obj) { var headers; + statusCode |= 0; + if (statusCode < 100 || statusCode > 999) + throw new RangeError(`Invalid status code: ${statusCode}`); if (typeof reason === 'string') { // writeHead(statusCode, reasonPhrase[, headers]) @@ -190,17 +193,13 @@ function writeHead(statusCode, reason, obj) { headers = obj; } - statusCode |= 0; - if (statusCode < 100 || statusCode > 999) - throw new RangeError(`Invalid status code: ${statusCode}`); - if (common._checkInvalidHeaderChar(this.statusMessage)) throw new Error('Invalid character in statusMessage.'); var statusLine = 'HTTP/1.1 ' + statusCode + ' ' + this.statusMessage + CRLF; if (statusCode === 204 || statusCode === 304 || - (100 <= statusCode && statusCode <= 199)) { + (statusCode >= 100 && statusCode <= 199)) { // RFC 2616, 10.2.5: // The 204 response MUST NOT include a message-body, and thus is always // terminated by the first empty line after the header fields.