Skip to content

Commit

Permalink
http: check statusCode early
Browse files Browse the repository at this point in the history
By enforcing the statusCode to be an SMI, it helps a bit
performance-wise when looking up the associated statusMessage.

PR-URL: nodejs#10558
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
  • Loading branch information
mscdex authored and italoacasas committed Jan 18, 2017
1 parent 55601b8 commit b8fc6b8
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit b8fc6b8

Please sign in to comment.