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'); 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_VALUES = /(?:^|\W)close|upgrade(?:$|\W)/ig;
var RE_TE_CHUNKED = common.chunkExpression; var RE_TE_CHUNKED = common.chunkExpression;
@ -449,28 +447,27 @@ function matchConnValue(self, state, value) {
} }
function matchHeader(self, state, field, value) { function matchHeader(self, state, field, value) {
var m = RE_FIELDS.exec(field); if (field.length < 4 || field.length > 17)
if (!m)
return; return;
var len = m[0].length; field = field.toLowerCase();
if (len === 10) { switch (field) {
case 'connection':
state.connection = true; state.connection = true;
matchConnValue(self, state, value); matchConnValue(self, state, value);
} else if (len === 17) { break;
case 'transfer-encoding':
state.te = true; state.te = true;
if (RE_TE_CHUNKED.test(value)) self.chunkedEncoding = true; if (RE_TE_CHUNKED.test(value)) self.chunkedEncoding = true;
} else if (len === 14) { break;
case 'content-length':
state.contLen = true; state.contLen = true;
} else if (len === 4) { break;
state.date = true; case 'date':
} else if (len === 6) { case 'expect':
state.expect = true; case 'trailer':
} else if (len === 7) { case 'upgrade':
var ch = m[0].charCodeAt(0); state[field] = true;
if (ch === 85 || ch === 117) break;
state.upgrade = true;
else
state.trailer = true;
} }
} }