Skip to content

Commit b5f52ff

Browse files
committed
fixuo:: simplify
1 parent b82c7e7 commit b5f52ff

File tree

2 files changed

+19
-47
lines changed

2 files changed

+19
-47
lines changed

lib/core/util.js

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -436,46 +436,6 @@ function parseHeaders (headers, obj) {
436436
return obj
437437
}
438438

439-
function normalizeHeaders (headers) {
440-
const ret = {}
441-
if (headers == null) {
442-
// Do nothing...
443-
} else if (Array.isArray(headers)) {
444-
for (let i = 0; i < headers.length; i += 2) {
445-
if (Array.isArray(headers[i]) && headers[i].length === 2) {
446-
const key = headerNameToString(headers[i][0])
447-
const val = ret[key]
448-
if (val == null) {
449-
ret[key] = String(headers[i][1])
450-
} else if (typeof val === 'string') {
451-
ret[key] = [val, String(headers[i][1])]
452-
} else {
453-
val.push(String(headers[i][1]))
454-
}
455-
} else {
456-
const key = headerNameToString(headers[i])
457-
const val = ret[key]
458-
if (val == null) {
459-
ret[key] = String(headers[i + 1])
460-
} else if (typeof val === 'string') {
461-
ret[key] = [val, String(headers[i + 1])]
462-
} else {
463-
val.push(String(headers[i + 1]))
464-
}
465-
}
466-
}
467-
} else if (typeof headers[Symbol.iterator] === 'function') {
468-
for (const [key, val] of headers) {
469-
ret[headerNameToString(key)] = Array.isArray(val) ? val.map(x => String(x)) : String(val)
470-
}
471-
} else {
472-
for (const [key, val] of Object.entries(headers)) {
473-
ret[headerNameToString(key)] = Array.isArray(val) ? val.map(x => String(x)) : String(val)
474-
}
475-
}
476-
return ret
477-
}
478-
479439
/**
480440
* @param {Buffer[]} headers
481441
* @returns {string[]}
@@ -943,6 +903,5 @@ module.exports = {
943903
nodeMajor,
944904
nodeMinor,
945905
safeHTTPMethods: Object.freeze(['GET', 'HEAD', 'OPTIONS', 'TRACE']),
946-
wrapRequestBody,
947-
normalizeHeaders
906+
wrapRequestBody
948907
}

lib/handler/redirect-handler.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,26 @@ function shouldRemoveHeader (name, removeContent, unknownOrigin) {
206206

207207
// https://tools.ietf.org/html/rfc7231#section-6.4
208208
function cleanRequestHeaders (headers, removeContent, unknownOrigin) {
209-
const ret = util.normalizeHeaders(headers)
210-
for (const name of Object.keys(ret)) {
211-
if (shouldRemoveHeader(name, removeContent, unknownOrigin)) {
212-
delete ret[name]
209+
let ret
210+
if (Array.isArray(headers)) {
211+
for (let i = 0; i < headers.length; i += 2) {
212+
const name = util.headerNameToString(headers[i])
213+
if (!shouldRemoveHeader(name, removeContent, unknownOrigin)) {
214+
ret ??= {}
215+
ret[name] = headers[i + 1]
216+
}
217+
}
218+
} else if (headers && typeof headers === 'object') {
219+
const entries = typeof headers[Symbol.iterator] === 'function' ? headers : Object.entries(headers)
220+
for (const [key, value] of entries) {
221+
const name = util.headerNameToString(key)
222+
if (!shouldRemoveHeader(name, removeContent, unknownOrigin)) {
223+
ret ??= {}
224+
ret[name] = value
225+
}
213226
}
214227
}
215-
return ret
228+
return ret ?? headers
216229
}
217230

218231
module.exports = RedirectHandler

0 commit comments

Comments
 (0)