Skip to content

Commit

Permalink
use if statement
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx committed Dec 8, 2023
1 parent 3029aa2 commit 198ee21
Showing 1 changed file with 36 additions and 55 deletions.
91 changes: 36 additions & 55 deletions lib/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,70 +414,51 @@ function processHeader (request, key, val, skipAppend = false) {

if (headerName === undefined) {
headerName = key.toLowerCase()
if (!util.isValidHTTPToken(headerName)) {
if (headerNameLowerCasedRecord[headerName] === undefined && !util.isValidHTTPToken(headerName)) {
throw new InvalidArgumentError('invalid header key')
}
}

switch (headerName) {
case 'host': {
if (request.host !== null) break
if (headerCharRegex.exec(val) !== null) {
throw new InvalidArgumentError(`invalid ${key} header`)
}
// Consumed by Client
request.host = val
return
if (request.host !== null && headerName === 'host') {
if (headerCharRegex.exec(val) !== null) {
throw new InvalidArgumentError(`invalid ${key} header`)
}
case 'content-length': {
if (request.contentLength !== null) break
request.contentLength = parseInt(val, 10)
if (!Number.isFinite(request.contentLength)) {
throw new InvalidArgumentError('invalid content-length header')
}
return
// Consumed by Client
request.host = val
} else if (request.contentLength !== null && headerName === 'content-length') {
request.contentLength = parseInt(val, 10)
if (!Number.isFinite(request.contentLength)) {
throw new InvalidArgumentError('invalid content-length header')
}
case 'content-type': {
if (request.contentType !== null) break
request.contentType = val
} else if (request.contentType !== null && headerName === 'content-type') {
request.contentType = val
if (skipAppend) request.headers[key] = processHeaderValue(key, val, skipAppend)
else request.headers += processHeaderValue(key, val)
} else if (headerName === 'transfer-encoding' || headerName === 'keep-alive' || headerName === 'upgrade') {
throw new InvalidArgumentError(`invalid ${headerName} header`)
} else if (headerName === 'connection') {
const value = typeof val === 'string' ? val.toLowerCase() : null
if (value !== 'close' && value !== 'keep-alive') {
throw new InvalidArgumentError('invalid connection header')
} else if (value === 'close') {
request.reset = true
}
} else if (headerName === 'expect') {
throw new NotSupportedError('expect header not supported')
} else {
if (Array.isArray(val)) {
for (let i = 0; i < val.length; i++) {
if (skipAppend) {
if (request.headers[key]) request.headers[key] += `,${processHeaderValue(key, val[i], skipAppend)}`
else request.headers[key] = processHeaderValue(key, val[i], skipAppend)
} else {
request.headers += processHeaderValue(key, val[i])
}
}
} else {
if (skipAppend) request.headers[key] = processHeaderValue(key, val, skipAppend)
else request.headers += processHeaderValue(key, val)
return
}
case 'transfer-encoding': {
throw new InvalidArgumentError('invalid transfer-encoding header')
}
case 'connection': {
const value = typeof val === 'string' ? val.toLowerCase() : null
if (value !== 'close' && value !== 'keep-alive') {
throw new InvalidArgumentError('invalid connection header')
} else if (value === 'close') {
request.reset = true
}
return
}
case 'keep-alive': {
throw new InvalidArgumentError('invalid keep-alive header')
}
case 'upgrade': {
throw new InvalidArgumentError('invalid upgrade header')
}
case 'expect': {
throw new NotSupportedError('expect header not supported')
}
}
if (Array.isArray(val)) {
for (let i = 0; i < val.length; i++) {
if (skipAppend) {
if (request.headers[key]) request.headers[key] += `,${processHeaderValue(key, val[i], skipAppend)}`
else request.headers[key] = processHeaderValue(key, val[i], skipAppend)
} else {
request.headers += processHeaderValue(key, val[i])
}
}
} else {
if (skipAppend) request.headers[key] = processHeaderValue(key, val, skipAppend)
else request.headers += processHeaderValue(key, val)
}
}

Expand Down

0 comments on commit 198ee21

Please sign in to comment.