Skip to content

Commit

Permalink
http: optimize default method case
Browse files Browse the repository at this point in the history
PR-URL: #10654
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Fedor Indutny <[email protected]>
  • Loading branch information
mscdex committed Jan 13, 2017
1 parent 5c2ef14 commit dab22b5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
15 changes: 11 additions & 4 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,20 @@ function ClientRequest(options, cb) {
self.timeout = options.timeout;

var method = options.method;
if (method != null && typeof method !== 'string') {
var methodIsString = (typeof method === 'string');
if (method != null && !methodIsString) {
throw new TypeError('Method must be a string');
}
method = self.method = (method || 'GET').toUpperCase();
if (!common._checkIsHttpToken(method)) {
throw new TypeError('Method must be a valid HTTP token');

if (methodIsString && method) {
if (!common._checkIsHttpToken(method)) {
throw new TypeError('Method must be a valid HTTP token');
}
method = self.method = method.toUpperCase();
} else {
method = self.method = 'GET';
}

self.path = options.path || '/';
if (cb) {
self.once('response', cb);
Expand Down
2 changes: 0 additions & 2 deletions lib/_http_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,6 @@ var validTokens = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // ... 255
];
function checkIsHttpToken(val) {
if (typeof val !== 'string' || val.length === 0)
return false;
if (!validTokens[val.charCodeAt(0)])
return false;
if (val.length < 2)
Expand Down
18 changes: 9 additions & 9 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,21 +314,21 @@ function _storeHeader(firstLine, headers) {
if (state.expect) this._send('');
}

function storeHeader(self, state, field, value, validate) {
function storeHeader(self, state, key, value, validate) {
if (validate) {
if (!checkIsHttpToken(field)) {
if (typeof key !== 'string' || !key || !checkIsHttpToken(key)) {
throw new TypeError(
'Header name must be a valid HTTP Token ["' + field + '"]');
'Header name must be a valid HTTP Token ["' + key + '"]');
}
if (value === undefined) {
throw new Error('Header "%s" value must not be undefined', field);
throw new Error('Header "%s" value must not be undefined', key);
} else if (checkInvalidHeaderChar(value)) {
debug('Header "%s" contains invalid characters', field);
debug('Header "%s" contains invalid characters', key);
throw new TypeError('The header content contains invalid characters');
}
}
state.header += field + ': ' + escapeHeaderValue(value) + CRLF;
matchHeader(self, state, field, value);
state.header += key + ': ' + escapeHeaderValue(value) + CRLF;
matchHeader(self, state, key, value);
}

function matchConnValue(self, state, value) {
Expand Down Expand Up @@ -374,7 +374,7 @@ function matchHeader(self, state, field, value) {
}

function validateHeader(msg, name, value) {
if (!checkIsHttpToken(name))
if (typeof name !== 'string' || !name || !checkIsHttpToken(name))
throw new TypeError(
'Header name must be a valid HTTP Token ["' + name + '"]');
if (value === undefined)
Expand Down Expand Up @@ -568,7 +568,7 @@ OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
field = key;
value = headers[key];
}
if (!checkIsHttpToken(field)) {
if (typeof field !== 'string' || !field || !checkIsHttpToken(field)) {
throw new TypeError(
'Trailer name must be a valid HTTP Token ["' + field + '"]');
}
Expand Down

0 comments on commit dab22b5

Please sign in to comment.