Browse Source

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 <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
v6
Brian White 8 years ago
parent
commit
ec8910bcea
No known key found for this signature in database GPG Key ID: 606D7358F94DA209
  1. 9
      lib/_http_server.js

9
lib/_http_server.js

@ -162,6 +162,9 @@ ServerResponse.prototype._implicitHeader = function _implicitHeader() {
ServerResponse.prototype.writeHead = writeHead; ServerResponse.prototype.writeHead = writeHead;
function writeHead(statusCode, reason, obj) { function writeHead(statusCode, reason, obj) {
var headers; var headers;
statusCode |= 0;
if (statusCode < 100 || statusCode > 999)
throw new RangeError(`Invalid status code: ${statusCode}`);
if (typeof reason === 'string') { if (typeof reason === 'string') {
// writeHead(statusCode, reasonPhrase[, headers]) // writeHead(statusCode, reasonPhrase[, headers])
@ -190,17 +193,13 @@ function writeHead(statusCode, reason, obj) {
headers = obj; headers = obj;
} }
statusCode |= 0;
if (statusCode < 100 || statusCode > 999)
throw new RangeError(`Invalid status code: ${statusCode}`);
if (common._checkInvalidHeaderChar(this.statusMessage)) if (common._checkInvalidHeaderChar(this.statusMessage))
throw new Error('Invalid character in statusMessage.'); throw new Error('Invalid character in statusMessage.');
var statusLine = 'HTTP/1.1 ' + statusCode + ' ' + this.statusMessage + CRLF; var statusLine = 'HTTP/1.1 ' + statusCode + ' ' + this.statusMessage + CRLF;
if (statusCode === 204 || statusCode === 304 || if (statusCode === 204 || statusCode === 304 ||
(100 <= statusCode && statusCode <= 199)) { (statusCode >= 100 && statusCode <= 199)) {
// RFC 2616, 10.2.5: // RFC 2616, 10.2.5:
// The 204 response MUST NOT include a message-body, and thus is always // The 204 response MUST NOT include a message-body, and thus is always
// terminated by the first empty line after the header fields. // terminated by the first empty line after the header fields.

Loading…
Cancel
Save