Skip to content

Commit

Permalink
http: allow passing array of key/val into writeHead
Browse files Browse the repository at this point in the history
Enables an optimization when the user already has the headers
in an array form, e.g. when proxying.
  • Loading branch information
ronag committed Sep 20, 2020
1 parent f4586c9 commit 7c3bcf5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)) {
for (let n = 0; n < obj.length; n += 2) {
k = obj[n + 0];
if (k) this.setHeader(k, obj[n + 1]);
}
} 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
Original file line number Diff line number Diff line change
@@ -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();
});
}));
}));
}

0 comments on commit 7c3bcf5

Please sign in to comment.