Skip to content

Commit 7898f88

Browse files
committed
Exclude cookie headers from getAllResponseHeaders (tests added). Switch internal use of setRequestHeader to direct header array access to avoid exceptions.
1 parent ee95086 commit 7898f88

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

lib/XMLHttpRequest.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ exports.XMLHttpRequest = function() {
8080
/**
8181
* Constants
8282
*/
83+
8384
this.UNSENT = 0;
8485
this.OPENED = 1;
8586
this.HEADERS_RECEIVED = 2;
@@ -89,6 +90,7 @@ exports.XMLHttpRequest = function() {
8990
/**
9091
* Public vars
9192
*/
93+
9294
// Current state
9395
this.readyState = this.UNSENT;
9496

@@ -198,7 +200,7 @@ exports.XMLHttpRequest = function() {
198200
/**
199201
* Gets all the response headers.
200202
*
201-
* @return string
203+
* @return string A string with all response headers separated by CR+LF
202204
*/
203205
this.getAllResponseHeaders = function() {
204206
if (this.readyState < this.HEADERS_RECEIVED || errorFlag) {
@@ -207,7 +209,11 @@ exports.XMLHttpRequest = function() {
207209
var result = "";
208210

209211
for (var i in response.headers) {
210-
result += i + ": " + response.headers[i] + "\r\n";
212+
var headerName = i.toLowerCase();
213+
// Cookie headers are excluded
214+
if (headerName !== "set-cookie" && headerName !== "set-cookie2") {
215+
result += i + ": " + response.headers[i] + "\r\n";
216+
}
211217
}
212218
return result.substr(0, result.length - 2);
213219
};
@@ -254,7 +260,7 @@ exports.XMLHttpRequest = function() {
254260
var uri = url.pathname + (url.search ? url.search : '');
255261

256262
// Set the Host header or the server may reject the request
257-
this.setRequestHeader("Host", host);
263+
headers["Host"] = host;
258264

259265
// Set Basic Auth if necessary
260266
if (settings.user) {
@@ -269,10 +275,10 @@ exports.XMLHttpRequest = function() {
269275
if (settings.method == "GET" || settings.method == "HEAD") {
270276
data = null;
271277
} else if (data) {
272-
this.setRequestHeader("Content-Length", Buffer.byteLength(data));
278+
headers["Content-Length"] = Buffer.byteLength(data);
273279

274280
if (!headers["Content-Type"]) {
275-
this.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
281+
headers["Content-Type"] = "text/plain;charset=UTF-8";
276282
}
277283
}
278284

tests/test-headers.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ var server = http.createServer(function (req, res) {
1313
res.writeHead(200, {
1414
"Content-Type": "text/plain",
1515
"Content-Length": Buffer.byteLength(body),
16+
// Set cookie headers to see if they're correctly suppressed
17+
// Actual values don't matter
18+
"Set-Cookie": "foo=bar",
19+
"Set-Cookie2": "bar=baz",
1620
"Connection": "close"
1721
});
1822
res.write("Hello World");
@@ -43,6 +47,10 @@ xhr.onreadystatechange = function() {
4347
};
4448

4549
assert.equal(null, xhr.getResponseHeader("Content-Type"));
46-
xhr.open("GET", "http://localhost:8000/");
47-
xhr.setRequestHeader("X-Test", "Foobar");
48-
xhr.send();
50+
try {
51+
xhr.open("GET", "http://localhost:8000/");
52+
xhr.setRequestHeader("X-Test", "Foobar");
53+
xhr.send();
54+
} catch(e) {
55+
console.log("ERROR: Exception raised", e);
56+
}

0 commit comments

Comments
 (0)