Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: simplify various RegExp and related checks #20131

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 27 additions & 55 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,14 @@ const { utcDate } = internalHttp;

const kIsCorked = Symbol('isCorked');

var RE_FIELDS =
/^(?:Connection|Transfer-Encoding|Content-Length|Date|Expect|Trailer|Upgrade)$/i;
var RE_CONN_VALUES = /(?:^|\W)close|upgrade(?:$|\W)/ig;
var RE_CONN_CLOSE = /(?:^|\W)close(?:$|\W)/i;
var RE_TE_CHUNKED = common.chunkExpression;

// isCookieField performs a case-insensitive comparison of a provided string
// against the word "cookie." This method (at least as of V8 5.4) is faster than
// the equivalent case-insensitive regexp, even if isCookieField does not get
// inlined.
// against the word "cookie." As of V8 6.6 this is faster than handrolling or
// using a case-insensitive RegExp.
function isCookieField(s) {
if (s.length !== 6) return false;
var ch = s.charCodeAt(0);
if (ch !== 99 && ch !== 67) return false;
ch = s.charCodeAt(1);
if (ch !== 111 && ch !== 79) return false;
ch = s.charCodeAt(2);
if (ch !== 111 && ch !== 79) return false;
ch = s.charCodeAt(3);
if (ch !== 107 && ch !== 75) return false;
ch = s.charCodeAt(4);
if (ch !== 105 && ch !== 73) return false;
ch = s.charCodeAt(5);
if (ch !== 101 && ch !== 69) return false;
return true;
return s.length === 6 && s.toLowerCase() === 'cookie';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one change alone makes the whole PR worth it :-)

}

function noopPendingOutput(amount) {}
Expand Down Expand Up @@ -448,43 +432,31 @@ function storeHeader(self, state, key, value, validate) {
matchHeader(self, state, key, value);
}

function matchConnValue(self, state, value) {
var sawClose = false;
var m = RE_CONN_VALUES.exec(value);
while (m) {
if (m[0].length === 5)
sawClose = true;
m = RE_CONN_VALUES.exec(value);
}
if (sawClose)
self._last = true;
else
self.shouldKeepAlive = true;
}

function matchHeader(self, state, field, value) {
var m = RE_FIELDS.exec(field);
if (!m)
if (field.length < 4 || field.length > 17)
return;
var len = m[0].length;
if (len === 10) {
state.connection = true;
matchConnValue(self, state, value);
} else if (len === 17) {
state.te = true;
if (RE_TE_CHUNKED.test(value)) self.chunkedEncoding = true;
} else if (len === 14) {
state.contLen = true;
} else if (len === 4) {
state.date = true;
} else if (len === 6) {
state.expect = true;
} else if (len === 7) {
var ch = m[0].charCodeAt(0);
if (ch === 85 || ch === 117)
state.upgrade = true;
else
state.trailer = true;
field = field.toLowerCase();
switch (field) {
case 'connection':
state.connection = true;
if (RE_CONN_CLOSE.test(value))
self._last = true;
else
self.shouldKeepAlive = true;
break;
case 'transfer-encoding':
state.te = true;
if (RE_TE_CHUNKED.test(value)) self.chunkedEncoding = true;
break;
case 'content-length':
state.contLen = true;
break;
case 'date':
case 'expect':
case 'trailer':
case 'upgrade':
state[field] = true;
break;
}
}

Expand Down