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

Proxy support #1

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# node-XMLHttpRequest #

Fork of [node-XMLHttpRequest](https://github.com/driverdan/node-XMLHttpRequest) by [driverdan](http://driverdan.com). Forked and published to npm because a [pull request](https://github.com/rase-/node-XMLHttpRequest/commit/a6b6f296e0a8278165c2d0270d9840b54d5eeadd) is not being created and merged. Changes made by [rase-](https://github.com/rase-/node-XMLHttpRequest/tree/add/ssl-support) are needed for [engine.io-client](https://github.com/Automattic/engine.io-client).

# Original README #

node-XMLHttpRequest is a wrapper for the built-in http client to emulate the
browser XMLHttpRequest object.

Expand Down
32 changes: 29 additions & 3 deletions lib/XMLHttpRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,23 @@ function XMLHttpRequest(opts) {
*
* @param string header Header name
* @param string value Header value
* @return boolean Header added
*/
this.setRequestHeader = function(header, value) {
if (this.readyState != this.OPENED) {
throw "INVALID_STATE_ERR: setRequestHeader can only be called when state is OPEN";
return false;
}
if (!isAllowedHttpHeader(header)) {
console.warn('Refused to set unsafe header "' + header + '"');
return;
return false;
}
if (sendFlag) {
throw "INVALID_STATE_ERR: send flag is true";
return false;
}
headers[header] = value;
return true;
};

/**
Expand Down Expand Up @@ -382,9 +386,20 @@ function XMLHttpRequest(opts) {
path: uri,
method: settings.method,
headers: headers,
agent: agent
agent: agent,
socket: opts.socket
};

if (ssl) {
options.pfx = opts.pfx;
options.key = opts.key;
options.passphrase = opts.passphrase;
options.cert = opts.cert;
options.ca = opts.ca;
options.ciphers = opts.ciphers;
options.rejectUnauthorized = opts.rejectUnauthorized;
}

// Reset error flag
errorFlag = false;

Expand Down Expand Up @@ -418,9 +433,20 @@ function XMLHttpRequest(opts) {
port: url.port,
path: url.path,
method: response.statusCode === 303 ? 'GET' : settings.method,
headers: headers
headers: headers,
socket: opts.socket
};

if (ssl) {
options.pfx = opts.pfx;
options.key = opts.key;
options.passphrase = opts.passphrase;
options.cert = opts.cert;
options.ca = opts.ca;
options.ciphers = opts.ciphers;
options.rejectUnauthorized = opts.rejectUnauthorized;
}

// Issue the new request
request = doRequest(newOptions, responseHandler).on('error', errorHandler);
request.end();
Expand Down
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"name": "xmlhttprequest",
"name": "xmlhttprequest-ssl",
"description": "XMLHttpRequest for Node",
"version": "1.5.0",
"version": "1.5.1",
"author": {
"name": "Dan DeFelippi",
"url": "http://driverdan.com"
"name": "Michael de Wit"
},
"keywords": [
"xhr",
Expand All @@ -18,9 +17,9 @@
],
"repository": {
"type": "git",
"url": "git://github.com/driverdan/node-XMLHttpRequest.git"
"url": "git://github.com/mjwwit/node-XMLHttpRequest.git"
},
"bugs": "http://github.com/driverdan/node-XMLHttpRequest/issues",
"bugs": "http://github.com/mjwwit/node-XMLHttpRequest/issues",
"engines": {
"node": ">=0.4.0"
},
Expand Down
5 changes: 1 addition & 4 deletions tests/test-exceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,12 @@ var forbiddenRequestHeaders = [
"trailer",
"transfer-encoding",
"upgrade",
"user-agent",
"via"
];

for (var i in forbiddenRequestHeaders) {
try {
xhr.setRequestHeader(forbiddenRequestHeaders[i], "Test");
if(xhr.setRequestHeader(forbiddenRequestHeaders[i], "Test") !== false) {
console.log("ERROR: " + forbiddenRequestHeaders[i] + " should have thrown exception");
} catch(e) {
}
}

Expand Down
2 changes: 0 additions & 2 deletions tests/test-request-protocols.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState == 4) {
assert.equal("Hello World", this.responseText);
this.close();
runSync();
}
};
Expand All @@ -25,7 +24,6 @@ var runSync = function() {
xhr.onreadystatechange = function() {
if (this.readyState == 4) {
assert.equal("Hello World", this.responseText);
this.close();
sys.puts("done");
}
};
Expand Down
2 changes: 1 addition & 1 deletion tests/testdata.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Hello World
Hello World