Skip to content

Commit dab22b5

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 5c2ef14 commit dab22b5

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

lib/_http_client.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,20 @@ function ClientRequest(options, cb) {
105105
self.timeout = options.timeout;
106106

107107
var method = options.method;
108-
if (method != null && typeof method !== 'string') {
108+
var methodIsString = (typeof method === 'string');
109+
if (method != null && !methodIsString) {
109110
throw new TypeError('Method must be a string');
110111
}
111-
method = self.method = (method || 'GET').toUpperCase();
112-
if (!common._checkIsHttpToken(method)) {
113-
throw new TypeError('Method must be a valid HTTP token');
112+
113+
if (methodIsString && method) {
114+
if (!common._checkIsHttpToken(method)) {
115+
throw new TypeError('Method must be a valid HTTP token');
116+
}
117+
method = self.method = method.toUpperCase();
118+
} else {
119+
method = self.method = 'GET';
114120
}
121+
115122
self.path = options.path || '/';
116123
if (cb) {
117124
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

+9-9
Original file line numberDiff line numberDiff line change
@@ -314,21 +314,21 @@ function _storeHeader(firstLine, headers) {
314314
if (state.expect) this._send('');
315315
}
316316

317-
function storeHeader(self, state, field, value, validate) {
317+
function storeHeader(self, state, key, value, validate) {
318318
if (validate) {
319-
if (!checkIsHttpToken(field)) {
319+
if (typeof key !== 'string' || !key || !checkIsHttpToken(key)) {
320320
throw new TypeError(
321-
'Header name must be a valid HTTP Token ["' + field + '"]');
321+
'Header name must be a valid HTTP Token ["' + key + '"]');
322322
}
323323
if (value === undefined) {
324-
throw new Error('Header "%s" value must not be undefined', field);
324+
throw new Error('Header "%s" value must not be undefined', key);
325325
} else if (checkInvalidHeaderChar(value)) {
326-
debug('Header "%s" contains invalid characters', field);
326+
debug('Header "%s" contains invalid characters', key);
327327
throw new TypeError('The header content contains invalid characters');
328328
}
329329
}
330-
state.header += field + ': ' + escapeHeaderValue(value) + CRLF;
331-
matchHeader(self, state, field, value);
330+
state.header += key + ': ' + escapeHeaderValue(value) + CRLF;
331+
matchHeader(self, state, key, value);
332332
}
333333

334334
function matchConnValue(self, state, value) {
@@ -374,7 +374,7 @@ function matchHeader(self, state, field, value) {
374374
}
375375

376376
function validateHeader(msg, name, value) {
377-
if (!checkIsHttpToken(name))
377+
if (typeof name !== 'string' || !name || !checkIsHttpToken(name))
378378
throw new TypeError(
379379
'Header name must be a valid HTTP Token ["' + name + '"]');
380380
if (value === undefined)
@@ -568,7 +568,7 @@ OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
568568
field = key;
569569
value = headers[key];
570570
}
571-
if (!checkIsHttpToken(field)) {
571+
if (typeof field !== 'string' || !field || !checkIsHttpToken(field)) {
572572
throw new TypeError(
573573
'Trailer name must be a valid HTTP Token ["' + field + '"]');
574574
}

0 commit comments

Comments
 (0)