From 395ff5fa3d6d8ac386ceaa6813c2383da33ea088 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Mon, 25 Nov 2024 19:39:39 +0100 Subject: [PATCH] fixup --- lib/core/util.js | 43 +-------------------------------- lib/handler/redirect-handler.js | 30 ++++++++++++++++------- 2 files changed, 22 insertions(+), 51 deletions(-) diff --git a/lib/core/util.js b/lib/core/util.js index ab397f89e74..dfefac6d15c 100644 --- a/lib/core/util.js +++ b/lib/core/util.js @@ -436,46 +436,6 @@ function parseHeaders (headers, obj) { return obj } -function normalizeHeaders (headers) { - const ret = {} - if (headers == null) { - // Do nothing... - } else if (Array.isArray(headers)) { - for (let i = 0; i < headers.length; i += 2) { - if (Array.isArray(headers[i]) && headers[i].length === 2) { - const key = headerNameToString(headers[i][0]) - const val = ret[key] - if (val == null) { - ret[key] = String(headers[i][1]) - } else if (typeof val === 'string') { - ret[key] = [val, String(headers[i][1])] - } else { - val.push(String(headers[i][1])) - } - } else { - const key = headerNameToString(headers[i]) - const val = ret[key] - if (val == null) { - ret[key] = String(headers[i + 1]) - } else if (typeof val === 'string') { - ret[key] = [val, String(headers[i + 1])] - } else { - val.push(String(headers[i + 1])) - } - } - } - } else if (typeof headers[Symbol.iterator] === 'function') { - for (const [key, val] of headers) { - ret[headerNameToString(key)] = Array.isArray(val) ? val.map(x => String(x)) : String(val) - } - } else { - for (const [key, val] of Object.entries(headers)) { - ret[headerNameToString(key)] = Array.isArray(val) ? val.map(x => String(x)) : String(val) - } - } - return ret -} - /** * @param {Buffer[]} headers * @returns {string[]} @@ -943,6 +903,5 @@ module.exports = { nodeMajor, nodeMinor, safeHTTPMethods: Object.freeze(['GET', 'HEAD', 'OPTIONS', 'TRACE']), - wrapRequestBody, - normalizeHeaders + wrapRequestBody } diff --git a/lib/handler/redirect-handler.js b/lib/handler/redirect-handler.js index c8c9d60663c..64c078de87b 100644 --- a/lib/handler/redirect-handler.js +++ b/lib/handler/redirect-handler.js @@ -191,14 +191,15 @@ class RedirectHandler { } // https://tools.ietf.org/html/rfc7231#section-6.4.4 -function shouldRemoveHeader (name, removeContent, unknownOrigin) { - if (name.length === 4) { - return name === 'host' +function shouldRemoveHeader (header, removeContent, unknownOrigin) { + if (header.length === 4) { + return util.headerNameToString(header) === 'host' } - if (removeContent && name.startsWith('content-')) { + if (removeContent && util.headerNameToString(header).startsWith('content-')) { return true } - if (unknownOrigin && (name.length === 13 || name.length === 6 || name.length === 19)) { + if (unknownOrigin && (header.length === 13 || header.length === 6 || header.length === 19)) { + const name = util.headerNameToString(header) return name === 'authorization' || name === 'cookie' || name === 'proxy-authorization' } return false @@ -206,11 +207,22 @@ function shouldRemoveHeader (name, removeContent, unknownOrigin) { // https://tools.ietf.org/html/rfc7231#section-6.4 function cleanRequestHeaders (headers, removeContent, unknownOrigin) { - const ret = util.normalizeHeaders(headers) - for (const name of Object.keys(ret)) { - if (shouldRemoveHeader(name, removeContent, unknownOrigin)) { - delete ret[name] + const ret = [] + if (Array.isArray(headers)) { + for (let i = 0; i < headers.length; i += 2) { + if (!shouldRemoveHeader(headers[i], removeContent, unknownOrigin)) { + ret.push(headers[i], headers[i + 1]) + } + } + } else if (headers && typeof headers === 'object') { + const entries = typeof headers[Symbol.iterator] === 'function' ? headers : Object.entries(headers) + for (const [key, value] of entries) { + if (!shouldRemoveHeader(key, removeContent, unknownOrigin)) { + ret.push(key, value) + } } + } else { + assert(headers == null, 'headers must be an object or an array') } return ret }