Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Nov 25, 2024
1 parent b82c7e7 commit 395ff5f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 51 deletions.
43 changes: 1 addition & 42 deletions lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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[]}
Expand Down Expand Up @@ -943,6 +903,5 @@ module.exports = {
nodeMajor,
nodeMinor,
safeHTTPMethods: Object.freeze(['GET', 'HEAD', 'OPTIONS', 'TRACE']),
wrapRequestBody,
normalizeHeaders
wrapRequestBody
}
30 changes: 21 additions & 9 deletions lib/handler/redirect-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,26 +191,38 @@ 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
}

// 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
}
Expand Down

0 comments on commit 395ff5f

Please sign in to comment.