Skip to content
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
17 changes: 12 additions & 5 deletions lib/XMLHttpRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ exports.XMLHttpRequest = function() {
// Result & response
this.responseText = "";
this.responseXML = "";
this.response = Buffer.alloc(0);
this.status = null;
this.statusText = null;

Expand Down Expand Up @@ -429,15 +430,17 @@ exports.XMLHttpRequest = function() {
return;
}

response.setEncoding("utf8");
response.setEncoding('binary');

setState(self.HEADERS_RECEIVED);
self.status = response.statusCode;

response.on("data", function(chunk) {
// Make sure there's some data
if (chunk) {
self.responseText += chunk;
var data = Buffer.from(chunk, 'binary');
self.responseText += data.toString('utf8');
self.response = Buffer.concat([self.response, data]);
}
// Don't emit state changes if the connection has been aborted.
if (sendFlag) {
Expand Down Expand Up @@ -484,13 +487,16 @@ exports.XMLHttpRequest = function() {
+ "var doRequest = http" + (ssl ? "s" : "") + ".request;"
+ "var options = " + JSON.stringify(options) + ";"
+ "var responseText = '';"
+ "var responseData = Buffer.alloc(0);"
+ "var req = doRequest(options, function(response) {"
+ "response.setEncoding('utf8');"
+ "response.setEncoding('binary');"
+ "response.on('data', function(chunk) {"
+ " responseText += chunk;"
+ " var data = Buffer.from(chunk, 'binary');"
+ " responseText += data.toString('utf8');"
+ " responseData = Buffer.concat([responseData, data]);"
+ "});"
+ "response.on('end', function() {"
+ "fs.writeFileSync('" + contentFile + "', JSON.stringify({err: null, data: {statusCode: response.statusCode, headers: response.headers, text: responseText}}), 'utf8');"
+ "fs.writeFileSync('" + contentFile + "', JSON.stringify({err: null, data: {statusCode: response.statusCode, headers: response.headers, text: responseText, data: responseData.toString('base64')}}), 'utf8');"
+ "fs.unlinkSync('" + syncFile + "');"
+ "});"
+ "response.on('error', function(error) {"
Expand Down Expand Up @@ -520,6 +526,7 @@ exports.XMLHttpRequest = function() {
response = resp.data;
self.status = resp.data.statusCode;
self.responseText = resp.data.text;
self.response = Buffer.from(resp.data.data, 'base64');
setState(self.DONE);
}
}
Expand Down