Skip to content

Commit

Permalink
Fix allow override header host, connection and correct word uppercase…
Browse files Browse the repository at this point in the history
… of header name of undici
  • Loading branch information
tuanitfdev committed May 15, 2024
1 parent 903c667 commit 43a628b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 16 deletions.
10 changes: 7 additions & 3 deletions lib/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ function processHeader (request, key, val) {
}
} else if (request.contentType === null && headerName === 'content-type') {
request.contentType = val
request.headers.push(key, val)
// request.headers.push(key, val) // origin code
} else if (headerName === 'transfer-encoding' || headerName === 'keep-alive' || headerName === 'upgrade') {
throw new InvalidArgumentError(`invalid ${headerName} header`)
} else if (headerName === 'connection') {
Expand All @@ -386,9 +386,13 @@ function processHeader (request, key, val) {
}
} else if (headerName === 'expect') {
throw new NotSupportedError('expect header not supported')
} else {
request.headers.push(key, val)
}
// origin code
// else {
// request.headers.push(key, val)
// }

request.headers.push(key, val)
}

module.exports = Request
88 changes: 76 additions & 12 deletions lib/dispatcher/client-h1.js
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,34 @@ function shouldSendContentLength (method) {
function writeH1 (client, request) {
const { method, path, host, upgrade, blocking, reset } = request

function refineHeaderArray(headerArr) {
let headerObjsDict = {}

for (let i = 0; i < headerArr.length; i++) {
let originHeaderName, lcHeaderName, headerObjValue
if (i % 2 === 0) {
originHeaderName = headerArr[i]
lcHeaderName = originHeaderName.toLowerCase()
headerObjValue = {
arrayMappedIndex: i,
lowercaseName: lcHeaderName,
originName: originHeaderName,
}

headerObjsDict[lcHeaderName] = headerObjValue
} else {
originHeaderName = headerArr[i-1]
lcHeaderName = originHeaderName.toLowerCase()
headerObjValue = headerObjsDict[lcHeaderName]
headerObjValue.value = headerArr[i]
}
}

return headerObjsDict
}

let { body, headers, contentLength } = request
let refinedHeaderObjsDict = refineHeaderArray(headers)

// https://tools.ietf.org/html/rfc7231#section-4.3.1
// https://tools.ietf.org/html/rfc7231#section-4.3.2
Expand All @@ -843,21 +870,31 @@ function writeH1 (client, request) {
method === 'PATCH'
)

let contentTypeVal
if (util.isFormDataLike(body)) {
if (!extractBody) {
extractBody = require('../web/fetch/body.js').extractBody
}

const [bodyStream, contentType] = extractBody(body)
if (request.contentType == null) {
headers.push('content-type', contentType)
contentTypeVal = contentType
// headers.push('content-type', contentType) // origin code
}
body = bodyStream.stream
contentLength = bodyStream.length
} else if (util.isBlobLike(body) && request.contentType == null && body.type) {
headers.push('content-type', body.type)
contentTypeVal = body.type
// headers.push('content-type', body.type) // origin code
}

if (!refinedHeaderObjsDict.hasOwnProperty('content-type')
&& contentTypeVal) {
headers.unshift('Content-Type', contentTypeVal)
}



if (body && typeof body.read === 'function') {
// Try to read EOF in order to get length.
body.read(0)
Expand Down Expand Up @@ -943,18 +980,45 @@ function writeH1 (client, request) {

let header = `${method} ${path} HTTP/1.1\r\n`

if (typeof host === 'string') {
header += `host: ${host}\r\n`
} else {
header += client[kHostHeader]
// origin code
// if (typeof host === 'string') {
// header += `host: ${host}\r\n`
// } else {
// header += client[kHostHeader]
// }
if (!refinedHeaderObjsDict.hasOwnProperty('host')) {
if (typeof host === 'string') {
headers.unshift('Host', host)
} else {
header += client[kHostHeader]
}
}

if (upgrade) {
header += `connection: upgrade\r\nupgrade: ${upgrade}\r\n`
} else if (client[kPipelining] && !socket[kReset]) {
header += 'connection: keep-alive\r\n'
} else {
header += 'connection: close\r\n'
// origin code
// if (upgrade) {
// header += `connection: upgrade\r\nupgrade: ${upgrade}\r\n`
// } else if (client[kPipelining] && !socket[kReset]) {
// header += 'connection: keep-alive\r\n'
// } else {
// header += 'connection: close\r\n'
// }

if (!refinedHeaderObjsDict.hasOwnProperty('connection')) {
let hConnection = 'Connection'
let hUpgrade = 'Upgrade'
if (upgrade) {
headers.unshift(hConnection, 'upgrade')
if (!refinedHeaderObjsDict.hasOwnProperty('upgrade')) {
headers.unshift(hUpgrade, 'upgrade')
}
// header += `connection: upgrade\r\nupgrade: ${upgrade}\r\n` // origin code
} else if (client[kPipelining] && !socket[kReset]) {
headers.unshift(hConnection, 'keep-alive')
// header += 'connection: keep-alive\r\n' // origin code
} else {
headers.unshift(hConnection, 'close')
// header += 'connection: close\r\n' // origin code
}
}

if (Array.isArray(headers)) {
Expand Down
3 changes: 2 additions & 1 deletion lib/dispatcher/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ class Client extends DispatcherBase {
this[kLocalAddress] = localAddress != null ? localAddress : null
this[kResuming] = 0 // 0, idle, 1, scheduled, 2 resuming
this[kNeedDrain] = 0 // 0, idle, 1, scheduled, 2 resuming
this[kHostHeader] = `host: ${this[kUrl].hostname}${this[kUrl].port ? `:${this[kUrl].port}` : ''}\r\n`
// this[kHostHeader] = `host: ${this[kUrl].hostname}${this[kUrl].port ? `:${this[kUrl].port}` : ''}\r\n` // origin code
this[kHostHeader] = `Host: ${this[kUrl].hostname}${this[kUrl].port ? `:${this[kUrl].port}` : ''}\r\n`
this[kBodyTimeout] = bodyTimeout != null ? bodyTimeout : 300e3
this[kHeadersTimeout] = headersTimeout != null ? headersTimeout : 300e3
this[kStrictContentLength] = strictContentLength == null ? true : strictContentLength
Expand Down

0 comments on commit 43a628b

Please sign in to comment.