From 54caeae38a66841b7f7119649dc78ce55c8a7c52 Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Wed, 18 Apr 2018 15:45:45 +0200 Subject: [PATCH] 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 Reviewed-By: Luigi Pinca Reviewed-By: Trivikram Kamat Reviewed-By: James M Snell Reviewed-By: Colin Ihrig Reviewed-By: Yuta Hiroto Reviewed-By: Ruben Bridgewater --- lib/_http_outgoing.js | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index 8ed49401960..8406c9cd8b0 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -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; } }