| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 7d1cdd4 commit 6ff152c
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1786,6 +1786,9 @@ the request body should be sent. See the [`'checkContinue'`][] event on | |||
| 1786 | 1786 | <!-- YAML | |
| 1787 | 1787 | added: v0.1.30 | |
| 1788 | 1788 | changes: | |
| 1789 | + - version: REPLACEME | ||
| 1790 | + pr-url: https://github.com/nodejs/node/pull/35274 | ||
| 1791 | + description: Allow passing headers as an array. | ||
| 1789 | 1792 | - version: | |
| 1790 | 1793 | - v11.10.0 | |
| 1791 | 1794 | - v10.17.0 | |
@@ -1802,14 +1805,19 @@ changes: | |||
| 1802 | 1805 | ||
| 1803 | 1806 | * `statusCode` {number} | |
| 1804 | 1807 | * `statusMessage` {string} | |
| 1805 | - * `headers` {Object} | ||
| 1808 | + * `headers` {Object|Array} | ||
| 1806 | 1809 | * Returns: {http.ServerResponse} | |
| 1807 | 1810 | ||
| 1808 | 1811 | Sends a response header to the request. The status code is a 3-digit HTTP | |
| 1809 | 1812 | status code, like `404`. The last argument, `headers`, are the response headers. | |
| 1810 | 1813 | Optionally one can give a human-readable `statusMessage` as the second | |
| 1811 | 1814 | argument. | |
| 1812 | 1815 | ||
| 1816 | + `headers` may be an `Array` where the keys and values are in the same list. | ||
| 1817 | + It is *not* a list of tuples. So, the even-numbered offsets are key values, | ||
| 1818 | + and the odd-numbered offsets are the associated values. The array is in the same | ||
| 1819 | + format as `request.rawHeaders`. | ||
| 1820 | + | ||
| 1813 | 1821 | Returns a reference to the `ServerResponse`, so that calls can be chained. | |
| 1814 | 1822 | ||
| 1815 | 1823 | ```js | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -53,6 +53,7 @@ const { | |||
| 53 | 53 | ERR_HTTP_TRAILER_INVALID, | |
| 54 | 54 | ERR_INVALID_HTTP_TOKEN, | |
| 55 | 55 | ERR_INVALID_ARG_TYPE, | |
| 56 | + ERR_INVALID_ARG_VALUE, | ||
| 56 | 57 | ERR_INVALID_CHAR, | |
| 57 | 58 | ERR_METHOD_NOT_IMPLEMENTED, | |
| 58 | 59 | ERR_STREAM_CANNOT_PIPE, | |
@@ -376,8 +377,18 @@ function _storeHeader(firstLine, headers) { | |||
| 376 | 377 | processHeader(this, state, entry[0], entry[1], false); | |
| 377 | 378 | } | |
| 378 | 379 | } else if (ArrayIsArray(headers)) { | |
| 379 | - for (const entry of headers) { | ||
| 380 | - processHeader(this, state, entry[0], entry[1], true); | ||
| 380 | + if (headers.length && ArrayIsArray(headers[0])) { | ||
| 381 | + for (const entry of headers) { | ||
| 382 | + processHeader(this, state, entry[0], entry[1], true); | ||
| 383 | + } | ||
| 384 | + } else { | ||
| 385 | + if (headers.length % 2 !== 0) { | ||
| 386 | + throw new ERR_INVALID_ARG_VALUE('headers', headers); | ||
| 387 | + } | ||
| 388 | + | ||
| 389 | + for (let n = 0; n < headers.length; n += 2) { | ||
| 390 | + processHeader(this, state, headers[n + 0], headers[n + 1], true); | ||
| 391 | + } | ||
| 381 | 392 | } | |
| 382 | 393 | } else { | |
| 383 | 394 | for (const key in headers) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,6 +22,7 @@ | |||
| 22 | 22 | 'use strict'; | |
| 23 | 23 | ||
| 24 | 24 | const { | |
| 25 | + ArrayIsArray, | ||
| 25 | 26 | Error, | |
| 26 | 27 | ObjectKeys, | |
| 27 | 28 | ObjectSetPrototypeOf, | |
@@ -62,6 +63,7 @@ const { | |||
| 62 | 63 | ERR_HTTP_HEADERS_SENT, | |
| 63 | 64 | ERR_HTTP_INVALID_STATUS_CODE, | |
| 64 | 65 | ERR_INVALID_ARG_TYPE, | |
| 66 | + ERR_INVALID_ARG_VALUE, | ||
| 65 | 67 | ERR_INVALID_CHAR | |
| 66 | 68 | } = require('internal/errors').codes; | |
| 67 | 69 | const { | |
@@ -269,7 +271,16 @@ function writeHead(statusCode, reason, obj) { | |||
| 269 | 271 | if (this[kOutHeaders]) { | |
| 270 | 272 | // Slow-case: when progressive API and header fields are passed. | |
| 271 | 273 | let k; | |
| 272 | - if (obj) { | ||
| 274 | + if (ArrayIsArray(obj)) { | ||
| 275 | + if (obj.length % 2 !== 0) { | ||
| 276 | + throw new ERR_INVALID_ARG_VALUE('headers', obj); | ||
| 277 | + } | ||
| 278 | + | ||
| 279 | + for (let n = 0; n < obj.length; n += 2) { | ||
| 280 | + k = obj[n + 0]; | ||
| 281 | + if (k) this.setHeader(k, obj[n + 1]); | ||
| 282 | + } | ||
| 283 | + } else if (obj) { | ||
| 273 | 284 | const keys = ObjectKeys(obj); | |
| 274 | 285 | // Retain for(;;) loop for performance reasons | |
| 275 | 286 | // Refs: https://github.com/nodejs/node/pull/30958 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,61 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + const http = require('http'); | ||
| 5 | + | ||
| 6 | + // Verify that ServerResponse.writeHead() works with arrays. | ||
| 7 | + | ||
| 8 | + { | ||
| 9 | + const server = http.createServer(common.mustCall((req, res) => { | ||
| 10 | + res.setHeader('test', '1'); | ||
| 11 | + res.writeHead(200, [ 'test', '2', 'test2', '2' ]); | ||
| 12 | + res.end(); | ||
| 13 | + })); | ||
| 14 | + | ||
| 15 | + server.listen(0, common.mustCall(() => { | ||
| 16 | + http.get({ port: server.address().port }, common.mustCall((res) => { | ||
| 17 | + assert.strictEqual(res.headers.test, '2'); | ||
| 18 | + assert.strictEqual(res.headers.test2, '2'); | ||
| 19 | + res.resume().on('end', common.mustCall(() => { | ||
| 20 | + server.close(); | ||
| 21 | + })); | ||
| 22 | + })); | ||
| 23 | + })); | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + { | ||
| 27 | + const server = http.createServer(common.mustCall((req, res) => { | ||
| 28 | + res.writeHead(200, [ 'test', '1', 'test2', '2' ]); | ||
| 29 | + res.end(); | ||
| 30 | + })); | ||
| 31 | + | ||
| 32 | + server.listen(0, common.mustCall(function() { | ||
| 33 | + http.get({ port: server.address().port }, common.mustCall((res) => { | ||
| 34 | + assert.strictEqual(res.headers.test, '1'); | ||
| 35 | + assert.strictEqual(res.headers.test2, '2'); | ||
| 36 | + res.resume().on('end', common.mustCall(() => { | ||
| 37 | + server.close(); | ||
| 38 | + })); | ||
| 39 | + })); | ||
| 40 | + })); | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + | ||
| 44 | + { | ||
| 45 | + const server = http.createServer(common.mustCall((req, res) => { | ||
| 46 | + try { | ||
| 47 | + res.writeHead(200, [ 'test', '1', 'test2', '2', 'asd' ]); | ||
| 48 | + } catch (err) { | ||
| 49 | + assert.strictEqual(err.code, 'ERR_INVALID_ARG_VALUE'); | ||
| 50 | + } | ||
| 51 | + res.end(); | ||
| 52 | + })); | ||
| 53 | + | ||
| 54 | + server.listen(0, common.mustCall(function() { | ||
| 55 | + http.get({ port: server.address().port }, common.mustCall((res) => { | ||
| 56 | + res.resume().on('end', common.mustCall(() => { | ||
| 57 | + server.close(); | ||
| 58 | + })); | ||
| 59 | + })); | ||
| 60 | + })); | ||
| 61 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments