Browse Source

Send multiple header lines in ServerResponse#writeHead when header values are arrays

v0.7.4-release
Sam Stephenson 14 years ago
committed by Ryan Dahl
parent
commit
6560ab9073
  1. 45
      lib/http.js
  2. 8
      test/simple/test-http-proxy.js

45
lib/http.js

@ -374,39 +374,52 @@ OutgoingMessage.prototype._storeHeader = function (firstLine, headers) {
// in the case of response it is: "HTTP/1.1 200 OK\r\n"
var messageHeader = firstLine;
var field, value;
var self = this;
if (headers) {
var keys = Object.keys(headers);
var isArray = (Array.isArray(headers));
for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i];
if (isArray) {
field = headers[key][0];
value = headers[key][1];
} else {
field = key;
value = headers[key];
}
function store(field, value) {
messageHeader += field + ": " + value + CRLF;
if (connectionExpression.test(field)) {
sentConnectionHeader = true;
if (closeExpression.test(value)) {
this._last = true;
self._last = true;
} else {
this.shouldKeepAlive = true;
self.shouldKeepAlive = true;
}
} else if (transferEncodingExpression.test(field)) {
sentTransferEncodingHeader = true;
if (chunkExpression.test(value)) this.chunkedEncoding = true;
if (chunkExpression.test(value)) self.chunkedEncoding = true;
} else if (contentLengthExpression.test(field)) {
sentContentLengthHeader = true;
}
}
if (headers) {
var keys = Object.keys(headers);
var isArray = (Array.isArray(headers));
var i, l;
for (i = 0, l = keys.length; i < l; i++) {
var key = keys[i];
if (isArray) {
field = headers[key][0];
value = headers[key][1];
} else {
field = key;
value = headers[key];
}
if (Array.isArray(value)) {
for (i = 0, l = value.length; i < l; i++) {
store(field, value[i]);
}
} else {
store(field, value);
}
}
}
// keep-alive logic

8
test/simple/test-http-proxy.js

@ -6,9 +6,14 @@ url = require("url");
var PROXY_PORT = common.PORT;
var BACKEND_PORT = common.PORT+1;
var cookies = [
"session_token=; path=/; expires=Sun, 15-Sep-2030 13:48:52 GMT",
"prefers_open_id=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"
];
var backend = http.createServer(function (req, res) {
common.debug("backend request");
res.writeHead(200, {"content-type": "text/plain"});
res.writeHead(200, {"content-type": "text/plain", "set-cookie": cookies});
res.write("hello world\n");
res.end();
});
@ -43,6 +48,7 @@ function startReq () {
req.addListener('response', function (res) {
common.debug("got res");
assert.equal(200, res.statusCode);
assert.deepEqual(cookies, res.headers["set-cookie"]);
res.setEncoding("utf8");
res.addListener('data', function (chunk) { body += chunk; });
res.addListener('end', function () {

Loading…
Cancel
Save