Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: allow passing array of key/val into writeHead #35274

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/_http_outgoing.js
Expand Up @@ -381,8 +381,14 @@ function _storeHeader(firstLine, headers) {
processHeader(this, state, entry[0], entry[1], false);
}
} else if (ArrayIsArray(headers)) {
for (const entry of headers) {
processHeader(this, state, entry[0], entry[1], true);
if (headers.length && ArrayIsArray(headers[0])) {
for (const entry of headers) {
processHeader(this, state, entry[0], entry[1], true);
}
} else {
for (let n = 0; n < headers.length; n += 2) {
processHeader(this, state, headers[n + 0], headers[n + 1], true);
}
}
} else {
for (const key in headers) {
Expand Down
8 changes: 7 additions & 1 deletion lib/_http_server.js
Expand Up @@ -22,6 +22,7 @@
'use strict';

const {
ArrayIsArray,
Error,
ObjectKeys,
ObjectSetPrototypeOf,
Expand Down Expand Up @@ -278,7 +279,12 @@ function writeHead(statusCode, reason, obj) {
if (this[kOutHeaders]) {
// Slow-case: when progressive API and header fields are passed.
let k;
if (obj) {
if (ArrayIsArray(obj)) {
ronag marked this conversation as resolved.
Show resolved Hide resolved
for (let n = 0; n < obj.length; n += 2) {
k = obj[n + 0];
if (k) this.setHeader(k, obj[n + 1]);
ronag marked this conversation as resolved.
Show resolved Hide resolved
}
} else if (obj) {
const keys = ObjectKeys(obj);
// Retain for(;;) loop for performance reasons
// Refs: https://github.com/nodejs/node/pull/30958
Expand Down
41 changes: 41 additions & 0 deletions test/parallel/test-http-write-head-2.js
@@ -0,0 +1,41 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');

// Verify that ServerResponse.writeHead() works with arrays.

{
const server = http.createServer(common.mustCall((req, res) => {
res.setHeader('test', '1');
res.writeHead(200, [ 'test', '2', 'test2', '2' ]);
res.end();
}));

server.listen(0, common.mustCall(() => {
http.get({ port: server.address().port }, common.mustCall((res) => {
assert.strictEqual(res.headers.test, '2');
assert.strictEqual(res.headers.test2, '2');
res.resume().on('end', () => {
server.close();
});
}));
}));
}

{
const server = http.createServer(common.mustCall((req, res) => {
res.writeHead(200, [ 'test', '1', 'test2', '2' ]);
res.end();
}));

server.listen(0, common.mustCall(function() {
http.get({ port: server.address().port }, common.mustCall((res) => {
assert.strictEqual(res.headers.test, '1');
assert.strictEqual(res.headers.test2, '2');
res.resume().on('end', () => {
server.close();
});
}));
}));
}