xmlhttprequest
Advanced tools
Comparing version 1.0.0 to 1.2.0
{ "name" : "xmlhttprequest" | ||
, "description" : "XMLHttpRequest for Node" | ||
, "version" : "1.0.0" | ||
, "version" : "1.2.0" | ||
, "author" : "Dan DeFelippi" | ||
@@ -16,2 +16,2 @@ , "licenses" : | ||
, "main" : "./XMLHttpRequest.js" | ||
} | ||
} |
@@ -12,3 +12,3 @@ var sys = require("util") | ||
assert.equal("/" + methods[curMethod], req.url); | ||
var body = (req.method != "HEAD" ? "Hello World" : ""); | ||
@@ -22,3 +22,3 @@ | ||
if (req.method != "HEAD") { | ||
res.write("Hello World"); | ||
res.write(body); | ||
} | ||
@@ -35,3 +35,2 @@ res.end(); | ||
var methods = ["GET", "POST", "HEAD", "PUT", "DELETE"]; | ||
var curMethod = 0; | ||
@@ -63,2 +62,5 @@ function start(method) { | ||
start(methods[curMethod]); | ||
for (var curMethod in methods) { | ||
sys.puts("Testing " + methods[curMethod]); | ||
start(methods[curMethod]); | ||
} |
@@ -11,7 +11,9 @@ /** | ||
* @author Dan DeFelippi <dan@driverdan.com> | ||
* @contributor David Ellis <d.f.ellis@ieee.org> | ||
* @license MIT | ||
*/ | ||
var Url = require("url") | ||
,sys = require("util"); | ||
var Url = require("url"), | ||
spawn = require("child_process").spawn, | ||
fs = require('fs'); | ||
@@ -58,3 +60,3 @@ exports.XMLHttpRequest = function() { | ||
// default ready state change handler in case one is not set or is set late | ||
this.onreadystatechange = function() {}; | ||
this.onreadystatechange = null; | ||
@@ -170,2 +172,11 @@ // Result & response | ||
this.setRequestHeader("Host", host); | ||
// Set Basic Auth if necessary | ||
if (settings.user) { | ||
if (typeof settings.password == "undefined") { | ||
settings.password = ""; | ||
} | ||
var authBuf = new Buffer(settings.user + ":" + settings.password); | ||
headers["Authorization"] = "Basic " + authBuf.toString("base64"); | ||
} | ||
@@ -182,6 +193,3 @@ // Set content length header | ||
} | ||
// Use the proper protocol | ||
var doRequest = ssl ? https.request : http.request; | ||
var options = { | ||
@@ -194,35 +202,83 @@ host: host, | ||
}; | ||
var req = doRequest(options, function(res) { | ||
response = res; | ||
response.setEncoding("utf8"); | ||
setState(self.HEADERS_RECEIVED); | ||
self.status = response.statusCode; | ||
response.on('data', function(chunk) { | ||
// Make sure there's some data | ||
if (chunk) { | ||
self.responseText += chunk; | ||
} | ||
setState(self.LOADING); | ||
if(!settings.hasOwnProperty("async") || settings.async) { //Normal async path | ||
// Use the proper protocol | ||
var doRequest = ssl ? https.request : http.request; | ||
var req = doRequest(options, function(response) { | ||
response.setEncoding("utf8"); | ||
setState(self.HEADERS_RECEIVED); | ||
self.status = response.statusCode; | ||
response.on('data', function(chunk) { | ||
// Make sure there's some data | ||
if (chunk) { | ||
self.responseText += chunk; | ||
} | ||
setState(self.LOADING); | ||
}); | ||
response.on('end', function() { | ||
setState(self.DONE); | ||
}); | ||
response.on('error', function(error) { | ||
self.handleError(error); | ||
}); | ||
}).on('error', function(error) { | ||
self.handleError(error); | ||
}); | ||
response.on('end', function() { | ||
// Node 0.4 and later won't accept empty data. Make sure it's needed. | ||
if (data) { | ||
req.write(data); | ||
} | ||
req.end(); | ||
} else { // Synchronous | ||
// Create a temporary file for communication with the other Node process | ||
var syncFile = ".node-xmlhttprequest-sync-" + process.pid; | ||
fs.writeFileSync(syncFile, "", "utf8"); | ||
// The async request the other Node process executes | ||
var execString = "var http = require('http'), https = require('https'), fs = require('fs');" | ||
+ "var doRequest = http" + (ssl?"s":"") + ".request;" | ||
+ "var options = " + JSON.stringify(options) + ";" | ||
+ "var responseText = '';" | ||
+ "var req = doRequest(options, function(response) {" | ||
+ "response.setEncoding('utf8');" | ||
+ "response.on('data', function(chunk) {" | ||
+ "responseText += chunk;" | ||
+ "});" | ||
+ "response.on('end', function() {" | ||
+ "fs.writeFileSync('" + syncFile + "', 'NODE-XMLHTTPREQUEST-STATUS:' + response.statusCode + ',' + responseText, 'utf8');" | ||
+ "});" | ||
+ "response.on('error', function(error) {" | ||
+ "fs.writeFileSync('" + syncFile + "', 'NODE-XMLHTTPREQUEST-ERROR:' + JSON.stringify(error), 'utf8');" | ||
+ "});" | ||
+ "}).on('error', function(error) {" | ||
+ "fs.writeFileSync('" + syncFile + "', 'NODE-XMLHTTPREQUEST-ERROR:' + JSON.stringify(error), 'utf8');" | ||
+ "});" | ||
+ (data ? "req.write('" + data.replace(/'/g, "\\'") + "');":"") | ||
+ "req.end();"; | ||
// Start the other Node Process, executing this string | ||
syncProc = spawn(process.argv[0], ["-e", execString]); | ||
while((self.responseText = fs.readFileSync(syncFile, 'utf8')) == "") { | ||
// Wait while the file is empty | ||
} | ||
// Kill the child process once the file has data | ||
syncProc.stdin.end(); | ||
// Remove the temporary file | ||
fs.unlinkSync(syncFile); | ||
if(self.responseText.match(/^NODE-XMLHTTPREQUEST-ERROR:/)) { | ||
// If the file returned an error, handle it | ||
var errorObj = self.responseText.replace(/^NODE-XMLHTTPREQUEST-ERROR:/, ""); | ||
self.handleError(errorObj); | ||
} else { | ||
// If the file returned okay, parse its data and move to the DONE state | ||
self.status = self.responseText.replace(/^NODE-XMLHTTPREQUEST-STATUS:([0-9]*),.*/, "$1"); | ||
self.responseText = self.responseText.replace(/^NODE-XMLHTTPREQUEST-STATUS:[0-9]*,(.*)/, "$1"); | ||
setState(self.DONE); | ||
}); | ||
response.on('error', function() { | ||
self.handleError(error); | ||
}); | ||
}).on('error', function(error) { | ||
self.handleError(error); | ||
}); | ||
// Node 0.4 and later won't accept empty data. Make sure it's needed. | ||
if (data) { | ||
req.write(data); | ||
} | ||
} | ||
req.end(); | ||
}; | ||
@@ -254,4 +310,6 @@ | ||
self.readyState = state; | ||
self.onreadystatechange(); | ||
if (typeof self.onreadystatechange === "function") { | ||
self.onreadystatechange(); | ||
} | ||
} | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
12554
372
8
1