MCPcopy Create free account
hub / github.com/nodejs/undici / processHeader

Function processHeader

lib/core/request.js:446–544  ·  view source on GitHub ↗
(request, key, val)

Source from the content-addressed store, hash-verified

444}
445
446function processHeader (request, key, val) {
447 if (val && (typeof val === 'object' && !Array.isArray(val))) {
448 throw new InvalidArgumentError(`invalid ${key} header`)
449 } else if (val === undefined) {
450 return
451 }
452
453 let headerName = headerNameLowerCasedRecord[key]
454
455 if (headerName === undefined) {
456 headerName = key.toLowerCase()
457 if (headerNameLowerCasedRecord[headerName] === undefined && !isValidHTTPToken(headerName)) {
458 throw new InvalidArgumentError('invalid header key')
459 }
460 }
461
462 if (Array.isArray(val)) {
463 const arr = []
464 for (let i = 0; i < val.length; i++) {
465 if (typeof val[i] === 'string') {
466 if (!isValidHeaderValue(val[i])) {
467 throw new InvalidArgumentError(`invalid ${key} header`)
468 }
469 arr.push(val[i])
470 } else if (val[i] === null) {
471 arr.push('')
472 } else if (typeof val[i] === 'object') {
473 throw new InvalidArgumentError(`invalid ${key} header`)
474 } else {
475 // Coerce primitives (and reject unsafe coercions such as functions
476 // with a crafted toString/Symbol.toPrimitive).
477 const str = `${val[i]}`
478 if (!isValidHeaderValue(str)) {
479 throw new InvalidArgumentError(`invalid ${key} header`)
480 }
481 arr.push(str)
482 }
483 }
484 val = arr
485 } else if (typeof val === 'string') {
486 if (!isValidHeaderValue(val)) {
487 throw new InvalidArgumentError(`invalid ${key} header`)
488 }
489 } else if (val === null) {
490 val = ''
491 } else {
492 // Coerce primitives (and reject unsafe coercions such as functions
493 // with a crafted toString/Symbol.toPrimitive).
494 val = `${val}`
495 if (!isValidHeaderValue(val)) {
496 throw new InvalidArgumentError(`invalid ${key} header`)
497 }
498 }
499
500 if (headerName === 'host') {
501 if (request.host !== null) {
502 throw new InvalidArgumentError('duplicate host header')
503 }

Callers 2

constructorMethod · 0.85
addHeaderMethod · 0.85

Calls 4

isValidHTTPTokenFunction · 0.85
isValidHeaderValueFunction · 0.70
pushMethod · 0.45

Tested by

no test coverage detected