From 4098392e76d7053f8a23c4115911308dd84c2540 Mon Sep 17 00:00:00 2001 From: tsctx <91457664+tsctx@users.noreply.github.com> Date: Wed, 20 Dec 2023 22:44:11 +0900 Subject: [PATCH] perf: use tree --- lib/core/util.js | 6 +++--- lib/handler/RedirectHandler.js | 19 ++++++++++++------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/core/util.js b/lib/core/util.js index 6c2e1e55d21..a655047dd44 100644 --- a/lib/core/util.js +++ b/lib/core/util.js @@ -280,13 +280,13 @@ function parseRawHeaders (headers) { let contentDispositionIdx = -1 for (let n = 0; n < headers.length; n += 2) { - const key = headers[n + 0].toString() + const key = headerNameToString(headers[n + 0]) const val = headers[n + 1].toString('utf8') - if (key.length === 14 && (key === 'content-length' || key.toLowerCase() === 'content-length')) { + if (key === 'content-length') { ret.push(key, val) hasContentLength = true - } else if (key.length === 19 && (key === 'content-disposition' || key.toLowerCase() === 'content-disposition')) { + } else if (key === 'content-disposition') { contentDispositionIdx = ret.push(key, val) - 1 } else { ret.push(key, val) diff --git a/lib/handler/RedirectHandler.js b/lib/handler/RedirectHandler.js index 34bba185d96..d8e96ddd844 100644 --- a/lib/handler/RedirectHandler.js +++ b/lib/handler/RedirectHandler.js @@ -176,7 +176,7 @@ function parseLocation (statusCode, headers) { } for (let i = 0; i < headers.length; i += 2) { - if (headers[i].toString().toLowerCase() === 'location') { + if (headers[i].length === 8 && util.headerNameToString(headers[i]) === 'location') { return headers[i + 1] } } @@ -184,12 +184,17 @@ function parseLocation (statusCode, headers) { // https://tools.ietf.org/html/rfc7231#section-6.4.4 function shouldRemoveHeader (header, removeContent, unknownOrigin) { - return ( - (header.length === 4 && header.toString().toLowerCase() === 'host') || - (removeContent && header.toString().toLowerCase().indexOf('content-') === 0) || - (unknownOrigin && header.length === 13 && header.toString().toLowerCase() === 'authorization') || - (unknownOrigin && header.length === 6 && header.toString().toLowerCase() === 'cookie') - ) + if (header.length === 4) { + return util.headerNameToString(header) === 'host' + } + if (removeContent && util.headerNameToString(header).startsWith('content-')) { + return true + } + if (unknownOrigin && (header.length === 13 || header.length === 6)) { + const name = util.headerNameToString(header) + return name === 'authorization' || name === 'cookie' + } + return false } // https://tools.ietf.org/html/rfc7231#section-6.4