MCPcopy Index your code
hub / github.com/nodejs/node / writeHead

Function writeHead

lib/_http_server.js:408–499  ·  view source on GitHub ↗
(statusCode, reason, obj)

Source from the content-addressed store, hash-verified

406
407ServerResponse.prototype.writeHead = writeHead;
408function writeHead(statusCode, reason, obj) {
409
410 if (this._header) {
411 throw new ERR_HTTP_HEADERS_SENT('write');
412 }
413
414 const originalStatusCode = statusCode;
415
416 statusCode |= 0;
417 if (statusCode < 100 || statusCode > 999) {
418 throw new ERR_HTTP_INVALID_STATUS_CODE(originalStatusCode);
419 }
420
421
422 if (typeof reason === 'string') {
423 // writeHead(statusCode, reasonPhrase[, headers])
424 this.statusMessage = reason;
425 } else {
426 // writeHead(statusCode[, headers])
427 this.statusMessage ||= STATUS_CODES[statusCode] || 'unknown';
428 obj ??= reason;
429 }
430 this.statusCode = statusCode;
431
432 let headers;
433 if (this[kOutHeaders]) {
434 // Slow-case: when progressive API and header fields are passed.
435 let k;
436 if (ArrayIsArray(obj)) {
437 if (obj.length % 2 !== 0) {
438 throw new ERR_INVALID_ARG_VALUE('headers', obj);
439 }
440
441 // Headers in obj should override previous headers but still
442 // allow explicit duplicates. To do so, we first remove any
443 // existing conflicts, then use appendHeader.
444
445 for (let n = 0; n < obj.length; n += 2) {
446 k = obj[n + 0];
447 this.removeHeader(k);
448 }
449
450 for (let n = 0; n < obj.length; n += 2) {
451 k = obj[n + 0];
452 if (k) this.appendHeader(k, obj[n + 1]);
453 }
454 } else if (obj) {
455 const keys = ObjectKeys(obj);
456 // Retain for(;;) loop for performance reasons
457 // Refs: https://github.com/nodejs/node/pull/30958
458 for (let i = 0; i < keys.length; i++) {
459 k = keys[i];
460 if (k) this.setHeader(k, obj[k]);
461 }
462 }
463 // Only progressive api is used
464 headers = this[kOutHeaders];
465 } else {

Callers

nothing calls this directly

Calls 4

checkInvalidHeaderCharFunction · 0.85
removeHeaderMethod · 0.80
appendHeaderMethod · 0.80
setHeaderMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…