Skip to content

Commit ec8910b

Browse files
committed
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: #10558 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Evan Lucas <[email protected]>
1 parent f53a6fb commit ec8910b

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

lib/_http_server.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ ServerResponse.prototype._implicitHeader = function _implicitHeader() {
162162
ServerResponse.prototype.writeHead = writeHead;
163163
function writeHead(statusCode, reason, obj) {
164164
var headers;
165+
statusCode |= 0;
166+
if (statusCode < 100 || statusCode > 999)
167+
throw new RangeError(`Invalid status code: ${statusCode}`);
165168

166169
if (typeof reason === 'string') {
167170
// writeHead(statusCode, reasonPhrase[, headers])
@@ -190,17 +193,13 @@ function writeHead(statusCode, reason, obj) {
190193
headers = obj;
191194
}
192195

193-
statusCode |= 0;
194-
if (statusCode < 100 || statusCode > 999)
195-
throw new RangeError(`Invalid status code: ${statusCode}`);
196-
197196
if (common._checkInvalidHeaderChar(this.statusMessage))
198197
throw new Error('Invalid character in statusMessage.');
199198

200199
var statusLine = 'HTTP/1.1 ' + statusCode + ' ' + this.statusMessage + CRLF;
201200

202201
if (statusCode === 204 || statusCode === 304 ||
203-
(100 <= statusCode && statusCode <= 199)) {
202+
(statusCode >= 100 && statusCode <= 199)) {
204203
// RFC 2616, 10.2.5:
205204
// The 204 response MUST NOT include a message-body, and thus is always
206205
// terminated by the first empty line after the header fields.

0 commit comments

Comments
 (0)