Skip to content

Commit e7859c2

Browse files
committed
http: optimize default method case
PR-URL: #10654 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Fedor Indutny <[email protected]>
1 parent c9bff04 commit e7859c2

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

lib/_http_client.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,21 @@ function ClientRequest(options, cb) {
102102
self.socketPath = options.socketPath;
103103
self.timeout = options.timeout;
104104

105-
var method = self.method = (options.method || 'GET').toUpperCase();
106-
if (!common._checkIsHttpToken(method)) {
107-
throw new TypeError('Method must be a valid HTTP token');
105+
var method = options.method;
106+
var methodIsString = (typeof method === 'string');
107+
if (method != null && !methodIsString) {
108+
throw new TypeError('Method must be a string');
108109
}
110+
111+
if (methodIsString && method) {
112+
if (!common._checkIsHttpToken(method)) {
113+
throw new TypeError('Method must be a valid HTTP token');
114+
}
115+
method = self.method = method.toUpperCase();
116+
} else {
117+
method = self.method = 'GET';
118+
}
119+
109120
self.path = options.path || '/';
110121
if (cb) {
111122
self.once('response', cb);

lib/_http_common.js

-2
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,6 @@ var validTokens = [
265265
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // ... 255
266266
];
267267
function checkIsHttpToken(val) {
268-
if (typeof val !== 'string' || val.length === 0)
269-
return false;
270268
if (!validTokens[val.charCodeAt(0)])
271269
return false;
272270
if (val.length < 2)

lib/_http_outgoing.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -319,18 +319,18 @@ function _storeHeader(firstLine, headers) {
319319
if (state.sentExpect) this._send('');
320320
}
321321

322-
function storeHeader(self, state, field, value) {
323-
if (!checkIsHttpToken(field)) {
322+
function storeHeader(self, state, key, value) {
323+
if (typeof key !== 'string' || !key || !checkIsHttpToken(key)) {
324324
throw new TypeError(
325-
'Header name must be a valid HTTP Token ["' + field + '"]');
325+
'Header name must be a valid HTTP Token ["' + key + '"]');
326326
}
327327
if (checkInvalidHeaderChar(value)) {
328-
debug('Header "%s" contains invalid characters', field);
328+
debug('Header "%s" contains invalid characters', key);
329329
throw new TypeError('The header content contains invalid characters');
330330
}
331-
state.messageHeader += field + ': ' + escapeHeaderValue(value) + CRLF;
331+
state.messageHeader += key + ': ' + escapeHeaderValue(value) + CRLF;
332332

333-
if (connectionExpression.test(field)) {
333+
if (connectionExpression.test(key)) {
334334
state.sentConnectionHeader = true;
335335
if (connCloseExpression.test(value)) {
336336
self._last = true;
@@ -339,26 +339,26 @@ function storeHeader(self, state, field, value) {
339339
}
340340
if (connUpgradeExpression.test(value))
341341
state.sentConnectionUpgrade = true;
342-
} else if (transferEncodingExpression.test(field)) {
342+
} else if (transferEncodingExpression.test(key)) {
343343
state.sentTransferEncodingHeader = true;
344344
if (trfrEncChunkExpression.test(value)) self.chunkedEncoding = true;
345345

346-
} else if (contentLengthExpression.test(field)) {
346+
} else if (contentLengthExpression.test(key)) {
347347
state.sentContentLengthHeader = true;
348-
} else if (dateExpression.test(field)) {
348+
} else if (dateExpression.test(key)) {
349349
state.sentDateHeader = true;
350-
} else if (expectExpression.test(field)) {
350+
} else if (expectExpression.test(key)) {
351351
state.sentExpect = true;
352-
} else if (trailerExpression.test(field)) {
352+
} else if (trailerExpression.test(key)) {
353353
state.sentTrailer = true;
354-
} else if (upgradeExpression.test(field)) {
354+
} else if (upgradeExpression.test(key)) {
355355
state.sentUpgrade = true;
356356
}
357357
}
358358

359359

360360
OutgoingMessage.prototype.setHeader = function setHeader(name, value) {
361-
if (!checkIsHttpToken(name))
361+
if (typeof name !== 'string' || !name || !checkIsHttpToken(name))
362362
throw new TypeError(
363363
'Header name must be a valid HTTP Token ["' + name + '"]');
364364
if (value === undefined)
@@ -538,7 +538,7 @@ OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
538538
field = key;
539539
value = headers[key];
540540
}
541-
if (!checkIsHttpToken(field)) {
541+
if (typeof field !== 'string' || !field || !checkIsHttpToken(field)) {
542542
throw new TypeError(
543543
'Trailer name must be a valid HTTP Token ["' + field + '"]');
544544
}

0 commit comments

Comments
 (0)