Skip to content

Commit 465bc5d

Browse files
committed
fixup
1 parent b82c7e7 commit 465bc5d

File tree

2 files changed

+23
-55
lines changed

2 files changed

+23
-55
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: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ class RedirectHandler {
8383
}
8484

8585
onRequestStart (controller, context) {
86-
this.location = null
8786
this.handler.onRequestStart?.(controller, { ...context, history: this.history })
8887
}
8988

@@ -136,9 +135,7 @@ class RedirectHandler {
136135
// Remove headers referring to the original URL.
137136
// By default it is Host only, unless it's a 303 (see below), which removes also all Content-* headers.
138137
// https://tools.ietf.org/html/rfc7231#section-6.4
139-
this.opts.headers = this.opts.headers
140-
? cleanRequestHeaders(this.opts.headers, statusCode === 303, this.opts.origin !== origin)
141-
: this.opts.headers
138+
this.opts.headers = cleanRequestHeaders(this.opts.headers, statusCode === 303, this.opts.origin !== origin)
142139
this.opts.path = path
143140
this.opts.origin = origin
144141
this.opts.maxRedirections = 0
@@ -191,26 +188,38 @@ class RedirectHandler {
191188
}
192189

193190
// https://tools.ietf.org/html/rfc7231#section-6.4.4
194-
function shouldRemoveHeader (name, removeContent, unknownOrigin) {
195-
if (name.length === 4) {
196-
return name === 'host'
191+
function shouldRemoveHeader (header, removeContent, unknownOrigin) {
192+
if (header.length === 4) {
193+
return util.headerNameToString(header) === 'host'
197194
}
198-
if (removeContent && name.startsWith('content-')) {
195+
if (removeContent && util.headerNameToString(header).startsWith('content-')) {
199196
return true
200197
}
201-
if (unknownOrigin && (name.length === 13 || name.length === 6 || name.length === 19)) {
198+
if (unknownOrigin && (header.length === 13 || header.length === 6 || header.length === 19)) {
199+
const name = util.headerNameToString(header)
202200
return name === 'authorization' || name === 'cookie' || name === 'proxy-authorization'
203201
}
204202
return false
205203
}
206204

207205
// https://tools.ietf.org/html/rfc7231#section-6.4
208206
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]
207+
const ret = []
208+
if (Array.isArray(headers)) {
209+
for (let i = 0; i < headers.length; i += 2) {
210+
if (!shouldRemoveHeader(headers[i], removeContent, unknownOrigin)) {
211+
ret.push(headers[i], headers[i + 1])
212+
}
213+
}
214+
} else if (headers && typeof headers === 'object') {
215+
const entries = typeof headers[Symbol.iterator] === 'function' ? headers : Object.entries(headers)
216+
for (const [key, value] of entries) {
217+
if (!shouldRemoveHeader(key, removeContent, unknownOrigin)) {
218+
ret.push(key, value)
219+
}
213220
}
221+
} else {
222+
assert(headers == null, 'headers must be an object or an array')
214223
}
215224
return ret
216225
}

0 commit comments

Comments
 (0)