Skip to content

Commit 4fe1b60

Browse files
apapirovskijasnell
authored andcommitted
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: #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>
1 parent c449eb5 commit 4fe1b60

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

lib/_http_outgoing.js

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ const { utcDate } = internalHttp;
5252

5353
const kIsCorked = Symbol('isCorked');
5454

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

@@ -449,28 +447,27 @@ function matchConnValue(self, state, value) {
449447
}
450448

451449
function matchHeader(self, state, field, value) {
452-
var m = RE_FIELDS.exec(field);
453-
if (!m)
450+
if (field.length < 4 || field.length > 17)
454451
return;
455-
var len = m[0].length;
456-
if (len === 10) {
457-
state.connection = true;
458-
matchConnValue(self, state, value);
459-
} else if (len === 17) {
460-
state.te = true;
461-
if (RE_TE_CHUNKED.test(value)) self.chunkedEncoding = true;
462-
} else if (len === 14) {
463-
state.contLen = true;
464-
} else if (len === 4) {
465-
state.date = true;
466-
} else if (len === 6) {
467-
state.expect = true;
468-
} else if (len === 7) {
469-
var ch = m[0].charCodeAt(0);
470-
if (ch === 85 || ch === 117)
471-
state.upgrade = true;
472-
else
473-
state.trailer = true;
452+
field = field.toLowerCase();
453+
switch (field) {
454+
case 'connection':
455+
state.connection = true;
456+
matchConnValue(self, state, value);
457+
break;
458+
case 'transfer-encoding':
459+
state.te = true;
460+
if (RE_TE_CHUNKED.test(value)) self.chunkedEncoding = true;
461+
break;
462+
case 'content-length':
463+
state.contLen = true;
464+
break;
465+
case 'date':
466+
case 'expect':
467+
case 'trailer':
468+
case 'upgrade':
469+
state[field] = true;
470+
break;
474471
}
475472
}
476473

0 commit comments

Comments
 (0)