From 918c00e5ca9d9714c30ef2ba80d15ef1918b8984 Mon Sep 17 00:00:00 2001 From: Tony Kovanen Date: Sun, 12 Oct 2014 02:08:20 +0300 Subject: [PATCH 1/6] Add key, cert and ca options for ssl. --- lib/XMLHttpRequest.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/XMLHttpRequest.js b/lib/XMLHttpRequest.js index 878fce9..0b9469b 100644 --- a/lib/XMLHttpRequest.js +++ b/lib/XMLHttpRequest.js @@ -385,6 +385,12 @@ function XMLHttpRequest(opts) { agent: agent }; + if (ssl) { + options.key = opts.key; + options.cert = opts.cert; + options.ca = opts.ca; + } + // Reset error flag errorFlag = false; @@ -421,6 +427,12 @@ function XMLHttpRequest(opts) { headers: headers }; + if (ssl) { + newOptions.key = opts.key; + newOptions.cert = opts.cert; + newOptions.ca = opts.ca; + } + // Issue the new request request = doRequest(newOptions, responseHandler).on('error', errorHandler); request.end(); From a6b6f296e0a8278165c2d0270d9840b54d5eeadd Mon Sep 17 00:00:00 2001 From: Tony Kovanen Date: Sun, 12 Oct 2014 02:48:35 +0300 Subject: [PATCH 2/6] Add rest of the ssl related options supported by ws and https.request. --- lib/XMLHttpRequest.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/XMLHttpRequest.js b/lib/XMLHttpRequest.js index 0b9469b..52e36b3 100644 --- a/lib/XMLHttpRequest.js +++ b/lib/XMLHttpRequest.js @@ -386,9 +386,13 @@ function XMLHttpRequest(opts) { }; 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 @@ -428,9 +432,13 @@ function XMLHttpRequest(opts) { }; if (ssl) { - newOptions.key = opts.key; - newOptions.cert = opts.cert; - newOptions.ca = opts.ca; + 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 From df2aa1853d95ca8f6a9415ba0c2541469bbc6afc Mon Sep 17 00:00:00 2001 From: Michael de Wit Date: Wed, 27 May 2015 20:26:00 +0200 Subject: [PATCH 3/6] Tests are running now. --- README.md | 4 ++++ lib/XMLHttpRequest.js | 6 +++++- package.json | 11 +++++------ tests/test-exceptions.js | 5 +---- tests/test-request-protocols.js | 2 -- tests/testdata.txt | 2 +- 6 files changed, 16 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index b989434..73f8876 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/XMLHttpRequest.js b/lib/XMLHttpRequest.js index 52e36b3..bb52cce 100644 --- a/lib/XMLHttpRequest.js +++ b/lib/XMLHttpRequest.js @@ -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; }; /** diff --git a/package.json b/package.json index 5cd17b8..bd45157 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,9 @@ { - "name": "xmlhttprequest", + "name": "xmlhttprequest-ssl", "description": "XMLHttpRequest for Node", - "version": "1.5.0", + "version": "1.0.0", "author": { - "name": "Dan DeFelippi", - "url": "http://driverdan.com" + "name": "Michael de Wit" }, "keywords": [ "xhr", @@ -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" }, diff --git a/tests/test-exceptions.js b/tests/test-exceptions.js index f1edd71..10eaea2 100644 --- a/tests/test-exceptions.js +++ b/tests/test-exceptions.js @@ -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) { } } diff --git a/tests/test-request-protocols.js b/tests/test-request-protocols.js index cd4e174..543917d 100644 --- a/tests/test-request-protocols.js +++ b/tests/test-request-protocols.js @@ -8,7 +8,6 @@ xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (this.readyState == 4) { assert.equal("Hello World", this.responseText); - this.close(); runSync(); } }; @@ -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"); } }; diff --git a/tests/testdata.txt b/tests/testdata.txt index 557db03..5e1c309 100644 --- a/tests/testdata.txt +++ b/tests/testdata.txt @@ -1 +1 @@ -Hello World +Hello World \ No newline at end of file From 494938e2dd4ae963cd5972f242e8eaf7d6042331 Mon Sep 17 00:00:00 2001 From: Michael de Wit Date: Wed, 27 May 2015 20:29:14 +0200 Subject: [PATCH 4/6] Switched to correct version. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index bd45157..decfc43 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "xmlhttprequest-ssl", "description": "XMLHttpRequest for Node", - "version": "1.0.0", + "version": "1.5.1", "author": { "name": "Michael de Wit" }, From 9385365140be998c784fec94c1272740a4e719df Mon Sep 17 00:00:00 2001 From: Shuky Chen Date: Thu, 18 Jun 2015 00:22:41 +0300 Subject: [PATCH 5/6] Supporting explicit socket --- lib/XMLHttpRequest.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/XMLHttpRequest.js b/lib/XMLHttpRequest.js index 52e36b3..c44e2f6 100644 --- a/lib/XMLHttpRequest.js +++ b/lib/XMLHttpRequest.js @@ -382,7 +382,8 @@ function XMLHttpRequest(opts) { path: uri, method: settings.method, headers: headers, - agent: agent + agent: agent, + socket: options.socket }; if (ssl) { @@ -428,7 +429,8 @@ 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) { From f3929ac7311b64f762b61926d8b042c73b390106 Mon Sep 17 00:00:00 2001 From: Shuky Chen Date: Thu, 18 Jun 2015 01:09:40 +0300 Subject: [PATCH 6/6] --amend --- lib/XMLHttpRequest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/XMLHttpRequest.js b/lib/XMLHttpRequest.js index c44e2f6..e694b9d 100644 --- a/lib/XMLHttpRequest.js +++ b/lib/XMLHttpRequest.js @@ -383,7 +383,7 @@ function XMLHttpRequest(opts) { method: settings.method, headers: headers, agent: agent, - socket: options.socket + socket: opts.socket }; if (ssl) {