http: use switch in matchHeader

Using a switch improves clarity of the code but also performance for
all but a few edge cases which remain on par with the old code.

PR-URL: https://github.com/nodejs/node/pull/20131
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Yuta Hiroto <hello@hiroppy.me>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Anatoli Papirovski 2018-04-18 15:45:45 +02:00
parent db0174bbc8
commit 54caeae38a
No known key found for this signature in database
GPG Key ID: 614E2E1ABEB4B2C0

View File

@ -52,8 +52,6 @@ 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_TE_CHUNKED = common.chunkExpression;
@ -449,28 +447,27 @@ function matchConnValue(self, state, value) {
}
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;
matchConnValue(self, state, value);
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;
}
}