Skip to content

Commit df80728

Browse files
Made it so that you can send an argument to send that allows the user to ignore an invalid certificate. This is so you can write apps to connect to self signed cert servers.
1 parent e2ae9aa commit df80728

File tree

3 files changed

+135
-2
lines changed

3 files changed

+135
-2
lines changed

lib/XMLHttpRequest.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ exports.XMLHttpRequest = function() {
153153
* @param string user Username for basic authentication (optional)
154154
* @param string password Password for basic authentication (optional)
155155
*/
156-
this.open = function(method, url, async, user, password) {
156+
this.open = function(method, url, async, user, password, rejectUnauthorized) {
157157
this.abort();
158158
errorFlag = false;
159159

@@ -167,7 +167,8 @@ exports.XMLHttpRequest = function() {
167167
"url": url.toString(),
168168
"async": (typeof async !== "boolean" ? true : async),
169169
"user": user || null,
170-
"password": password || null
170+
"password": password || null,
171+
"rejectUnauthorized": (typeof rejectUnauthorized !== "boolean" ? true : rejectUnauthorized)
171172
};
172173

173174
setState(this.OPENED);
@@ -379,6 +380,7 @@ exports.XMLHttpRequest = function() {
379380
method: settings.method,
380381
headers: headers,
381382
agent: false,
383+
rejectUnauthorized: settings.rejectUnauthorized,
382384
withCredentials: self.withCredentials
383385
};
384386

@@ -416,6 +418,7 @@ exports.XMLHttpRequest = function() {
416418
path: url.path,
417419
method: response.statusCode === 303 ? "GET" : settings.method,
418420
headers: headers,
421+
rejectUnauthorized: settings.rejectUnauthorized,
419422
withCredentials: self.withCredentials
420423
};
421424

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
var sys = require("util")
2+
, assert = require("assert")
3+
, XMLHttpRequest = require("../lib/XMLHttpRequest").XMLHttpRequest
4+
, https = require("https")
5+
, fs = require('fs')
6+
, xhr;
7+
8+
var options = {
9+
key: fs.readFileSync('key.pem'),
10+
cert: fs.readFileSync('cert.pem')
11+
};
12+
13+
// Test server
14+
var server = https.createServer(options, function (req, res) {
15+
// Check request method and URL
16+
assert.equal(methods[curMethod], req.method);
17+
assert.equal("/" + methods[curMethod], req.url);
18+
19+
var body = (req.method != "HEAD" ? "Hello World" : "");
20+
21+
res.writeHead(200, {
22+
"Content-Type": "text/plain",
23+
"Content-Length": Buffer.byteLength(body)
24+
});
25+
// HEAD has no body
26+
if (req.method != "HEAD") {
27+
res.write(body);
28+
}
29+
res.end();
30+
31+
if (curMethod == methods.length - 1) {
32+
this.close();
33+
console.log("done");
34+
}
35+
}).listen(8000);
36+
37+
// Test standard methods
38+
var methods = ["GET", "POST", "HEAD", "PUT", "DELETE"];
39+
var curMethod = 0;
40+
41+
function start(method) {
42+
// Reset each time
43+
xhr = new XMLHttpRequest();
44+
45+
xhr.onreadystatechange = function() {
46+
if (this.readyState == 4) {
47+
if (method == "HEAD") {
48+
assert.equal("", this.responseText);
49+
} else {
50+
assert.equal("Hello World", this.responseText);
51+
}
52+
53+
curMethod++;
54+
55+
if (curMethod < methods.length) {
56+
console.log("Testing " + methods[curMethod]);
57+
start(methods[curMethod]);
58+
}
59+
}
60+
};
61+
62+
var url = "https://localhost:8000/" + method;
63+
xhr.open(method, url, true, 'foo', 'bar', false);
64+
xhr.send();
65+
}
66+
67+
console.log("Testing " + methods[curMethod]);
68+
start(methods[curMethod]);

tests/test-request-methods-https.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
var sys = require("util")
2+
, assert = require("assert")
3+
, XMLHttpRequest = require("../lib/XMLHttpRequest").XMLHttpRequest
4+
, https = require("https")
5+
, fs = require('fs')
6+
, xhr;
7+
8+
var options = {
9+
key: fs.readFileSync('key.pem'),
10+
cert: fs.readFileSync('cert.pem')
11+
};
12+
13+
// Test server
14+
var server = https.createServer(options, function (req, res) {
15+
// Check request method and URL
16+
assert.equal(methods[curMethod], req.method);
17+
assert.equal("/" + methods[curMethod], req.url);
18+
19+
var body = (req.method != "HEAD" ? "Hello World" : "");
20+
21+
res.writeHead(200, {
22+
"Content-Type": "text/plain",
23+
"Content-Length": Buffer.byteLength(body)
24+
});
25+
// HEAD has no body
26+
if (req.method != "HEAD") {
27+
res.write(body);
28+
}
29+
res.end();
30+
}).listen(8000);
31+
32+
// Test standard methods
33+
var methods = ["GET", "POST", "HEAD", "PUT", "DELETE"];
34+
var curMethod = 0;
35+
36+
function start(method) {
37+
// Reset each time
38+
xhr = new XMLHttpRequest();
39+
40+
xhr.onreadystatechange = function() {
41+
if (this.readyState == 4) {
42+
assert.equal(this.responseText.indexOf("Error: self") > -1, true);
43+
44+
curMethod++;
45+
46+
if (curMethod < methods.length) {
47+
console.log("Testing " + methods[curMethod]);
48+
start(methods[curMethod]);
49+
} else {
50+
server.close();
51+
console.log('done');
52+
}
53+
}
54+
};
55+
56+
var url = "https://localhost:8000/" + method;
57+
xhr.open(method, url);
58+
xhr.send();
59+
}
60+
61+
console.log("Testing " + methods[curMethod]);
62+
start(methods[curMethod]);

0 commit comments

Comments
 (0)