mirror of
https://github.com/nodejs/node.git
synced 2025-12-28 16:07:39 +00:00
http: split set-cookie when using setHeaders
PR-URL: https://github.com/nodejs/node/pull/51649 Fixes: https://github.com/nodejs/node/issues/51599 Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Akhil Marsonya <akhil.marsonya27@gmail.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
This commit is contained in:
parent
c975384264
commit
9448c61e08
@ -728,8 +728,27 @@ OutgoingMessage.prototype.setHeaders = function setHeaders(headers) {
|
||||
throw new ERR_INVALID_ARG_TYPE('headers', ['Headers', 'Map'], headers);
|
||||
}
|
||||
|
||||
for (const key of headers.keys()) {
|
||||
this.setHeader(key, headers.get(key));
|
||||
// Headers object joins multiple cookies with a comma when using
|
||||
// the getter to retrieve the value,
|
||||
// unless iterating over the headers directly.
|
||||
// We also cannot safely split by comma.
|
||||
// To avoid setHeader overwriting the previous value we push
|
||||
// set-cookie values in array and set them all at once.
|
||||
const cookies = [];
|
||||
|
||||
for (const { 0: key, 1: value } of headers) {
|
||||
if (key === 'set-cookie') {
|
||||
if (ArrayIsArray(value)) {
|
||||
cookies.push(...value);
|
||||
} else {
|
||||
cookies.push(value);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
this.setHeader(key, value);
|
||||
}
|
||||
if (cookies.length) {
|
||||
this.setHeader('set-cookie', cookies);
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
@ -129,3 +129,46 @@ const assert = require('assert');
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
{
|
||||
const server = http.createServer({ requireHostHeader: false }, common.mustCall((req, res) => {
|
||||
const headers = new Headers();
|
||||
headers.append('Set-Cookie', 'a=b');
|
||||
headers.append('Set-Cookie', 'c=d');
|
||||
res.setHeaders(headers);
|
||||
res.end();
|
||||
}));
|
||||
|
||||
server.listen(0, common.mustCall(() => {
|
||||
http.get({ port: server.address().port }, (res) => {
|
||||
assert(Array.isArray(res.headers['set-cookie']));
|
||||
assert.strictEqual(res.headers['set-cookie'].length, 2);
|
||||
assert.strictEqual(res.headers['set-cookie'][0], 'a=b');
|
||||
assert.strictEqual(res.headers['set-cookie'][1], 'c=d');
|
||||
res.resume().on('end', common.mustCall(() => {
|
||||
server.close();
|
||||
}));
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
{
|
||||
const server = http.createServer({ requireHostHeader: false }, common.mustCall((req, res) => {
|
||||
const headers = new Map();
|
||||
headers.set('Set-Cookie', ['a=b', 'c=d']);
|
||||
res.setHeaders(headers);
|
||||
res.end();
|
||||
}));
|
||||
|
||||
server.listen(0, common.mustCall(() => {
|
||||
http.get({ port: server.address().port }, (res) => {
|
||||
assert(Array.isArray(res.headers['set-cookie']));
|
||||
assert.strictEqual(res.headers['set-cookie'].length, 2);
|
||||
assert.strictEqual(res.headers['set-cookie'][0], 'a=b');
|
||||
assert.strictEqual(res.headers['set-cookie'][1], 'c=d');
|
||||
res.resume().on('end', common.mustCall(() => {
|
||||
server.close();
|
||||
}));
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user