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

[issue 20] Storing lower-cased header keys #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions lib/XMLHttpRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ exports.XMLHttpRequest = function() {

// Set some default headers
var defaultHeaders = {
"User-Agent": "node-XMLHttpRequest",
"Accept": "*/*",
"user-agent": "node-XMLHttpRequest",
"accept": "*/*",
};

var headers = defaultHeaders;
Expand Down Expand Up @@ -195,7 +195,7 @@ exports.XMLHttpRequest = function() {
if (sendFlag) {
throw "INVALID_STATE_ERR: send flag is true";
}
headers[header] = value;
headers[header.toLowerCase()] = value;
};

/**
Expand Down Expand Up @@ -230,6 +230,7 @@ exports.XMLHttpRequest = function() {
var result = "";

for (var i in response.headers) {
i = i.toLowerCase();
// Cookie headers are excluded
if (i !== "set-cookie" && i !== "set-cookie2") {
result += i + ": " + response.headers[i] + "\r\n";
Expand All @@ -245,9 +246,11 @@ exports.XMLHttpRequest = function() {
* @return string Returns the request header or empty string if not set
*/
this.getRequestHeader = function(name) {
// @TODO Make this case insensitive
if (typeof name === "string" && headers[name]) {
return headers[name];
if (typeof name === "string") {
name = name.toLowerCase();
if (headers[name]) {
return headers[name];
}
}

return "";
Expand Down Expand Up @@ -328,9 +331,9 @@ exports.XMLHttpRequest = function() {
var uri = url.pathname + (url.search ? url.search : "");

// Set the Host header or the server may reject the request
headers.Host = host;
headers.host = host;
if (!((ssl && port === 443) || port === 80)) {
headers.Host += ":" + url.port;
headers.host += ":" + url.port;
}

// Set Basic Auth if necessary
Expand All @@ -339,22 +342,22 @@ exports.XMLHttpRequest = function() {
settings.password = "";
}
var authBuf = new Buffer(settings.user + ":" + settings.password);
headers.Authorization = "Basic " + authBuf.toString("base64");
headers.authorization = "Basic " + authBuf.toString("base64");
}

// Set content length header
if (settings.method === "GET" || settings.method === "HEAD") {
data = null;
} else if (data) {
headers["Content-Length"] = Buffer.isBuffer(data) ? data.length : Buffer.byteLength(data);
headers["content-length"] = Buffer.isBuffer(data) ? data.length : Buffer.byteLength(data);

if (!headers["Content-Type"]) {
headers["Content-Type"] = "text/plain;charset=UTF-8";
if (!this.getRequestHeader("Content-Type")) {
headers["content-type"] = "text/plain;charset=UTF-8";
}
} else if (settings.method === "POST") {
// For a post with no data set Content-Length: 0.
// This is required by buggy servers that don't meet the specs.
headers["Content-Length"] = 0;
headers["content-length"] = 0;
}

var options = {
Expand Down