Skip to content

Commit ba86780

Browse files
tsctxcrysmags
authored andcommitted
perf: Improve processHeader (nodejs#2513)
* perf: Improve processHeader * rewrite * use if statement * fixup
1 parent 57472f0 commit ba86780

File tree

1 file changed

+18
-48
lines changed

1 file changed

+18
-48
lines changed

lib/core/request.js

+18-48
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,11 @@ const {
77
const assert = require('assert')
88
const { kHTTP2BuildRequest, kHTTP2CopyHeaders, kHTTP1BuildRequest } = require('./symbols')
99
const util = require('./util')
10+
const { headerNameLowerCasedRecord } = require('./constants')
1011

11-
// tokenRegExp and headerCharRegex have been lifted from
12+
// headerCharRegex have been lifted from
1213
// https://github.com/nodejs/node/blob/main/lib/_http_common.js
1314

14-
/**
15-
* Verifies that the given val is a valid HTTP token
16-
* per the rules defined in RFC 7230
17-
* See https://tools.ietf.org/html/rfc7230#section-3.2.6
18-
*/
19-
const tokenRegExp = /^[\^_`a-zA-Z\-0-9!#$%&'*+.|~]+$/
20-
2115
/**
2216
* Matches if val contains an invalid field-vchar
2317
* field-value = *( field-content / obs-fold )
@@ -416,65 +410,41 @@ function processHeader (request, key, val, skipAppend = false) {
416410
return
417411
}
418412

419-
if (
420-
request.host === null &&
421-
key.length === 4 &&
422-
key.toLowerCase() === 'host'
423-
) {
413+
let headerName = headerNameLowerCasedRecord[key]
414+
415+
if (headerName === undefined) {
416+
headerName = key.toLowerCase()
417+
if (headerNameLowerCasedRecord[headerName] === undefined && !util.isValidHTTPToken(headerName)) {
418+
throw new InvalidArgumentError('invalid header key')
419+
}
420+
}
421+
422+
if (request.host === null && headerName === 'host') {
424423
if (headerCharRegex.exec(val) !== null) {
425424
throw new InvalidArgumentError(`invalid ${key} header`)
426425
}
427426
// Consumed by Client
428427
request.host = val
429-
} else if (
430-
request.contentLength === null &&
431-
key.length === 14 &&
432-
key.toLowerCase() === 'content-length'
433-
) {
428+
} else if (request.contentLength === null && headerName === 'content-length') {
434429
request.contentLength = parseInt(val, 10)
435430
if (!Number.isFinite(request.contentLength)) {
436431
throw new InvalidArgumentError('invalid content-length header')
437432
}
438-
} else if (
439-
request.contentType === null &&
440-
key.length === 12 &&
441-
key.toLowerCase() === 'content-type'
442-
) {
433+
} else if (request.contentType === null && headerName === 'content-type') {
443434
request.contentType = val
444435
if (skipAppend) request.headers[key] = processHeaderValue(key, val, skipAppend)
445436
else request.headers += processHeaderValue(key, val)
446-
} else if (
447-
key.length === 17 &&
448-
key.toLowerCase() === 'transfer-encoding'
449-
) {
450-
throw new InvalidArgumentError('invalid transfer-encoding header')
451-
} else if (
452-
key.length === 10 &&
453-
key.toLowerCase() === 'connection'
454-
) {
437+
} else if (headerName === 'transfer-encoding' || headerName === 'keep-alive' || headerName === 'upgrade') {
438+
throw new InvalidArgumentError(`invalid ${headerName} header`)
439+
} else if (headerName === 'connection') {
455440
const value = typeof val === 'string' ? val.toLowerCase() : null
456441
if (value !== 'close' && value !== 'keep-alive') {
457442
throw new InvalidArgumentError('invalid connection header')
458443
} else if (value === 'close') {
459444
request.reset = true
460445
}
461-
} else if (
462-
key.length === 10 &&
463-
key.toLowerCase() === 'keep-alive'
464-
) {
465-
throw new InvalidArgumentError('invalid keep-alive header')
466-
} else if (
467-
key.length === 7 &&
468-
key.toLowerCase() === 'upgrade'
469-
) {
470-
throw new InvalidArgumentError('invalid upgrade header')
471-
} else if (
472-
key.length === 6 &&
473-
key.toLowerCase() === 'expect'
474-
) {
446+
} else if (headerName === 'expect') {
475447
throw new NotSupportedError('expect header not supported')
476-
} else if (tokenRegExp.exec(key) === null) {
477-
throw new InvalidArgumentError('invalid header key')
478448
} else {
479449
if (Array.isArray(val)) {
480450
for (let i = 0; i < val.length; i++) {

0 commit comments

Comments
 (0)