Skip to content

Commit

Permalink
[refactor] Set responseType based on 'Content-Type' header (#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Atkinson authored and darrachequesne committed Apr 24, 2017
1 parent 51d7529 commit 3e03346
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
32 changes: 11 additions & 21 deletions lib/transports/polling-xhr.js
Expand Up @@ -205,11 +205,6 @@ Request.prototype.create = function () {
}
}
} catch (e) {}
if (this.supportsBinary) {
// This has to be done after open because Firefox is stupid
// http://stackoverflow.com/questions/13216903/get-binary-data-with-xmlhttprequest-in-a-firefox-extension
xhr.responseType = 'arraybuffer';
}

if ('POST' === this.method) {
try {
Expand Down Expand Up @@ -243,6 +238,15 @@ Request.prototype.create = function () {
};
} else {
xhr.onreadystatechange = function () {
if (xhr.readyState === 2) {
var contentType;
try {
contentType = xhr.getResponseHeader('Content-Type');
} catch (e) {}
if (contentType === 'application/octet-stream') {
xhr.responseType = 'arraybuffer';
}
}
if (4 !== xhr.readyState) return;
if (200 === xhr.status || 1223 === xhr.status) {
self.onLoad();
Expand Down Expand Up @@ -348,26 +352,12 @@ Request.prototype.onLoad = function () {
try {
var contentType;
try {
contentType = this.xhr.getResponseHeader('Content-Type').split(';')[0];
contentType = this.xhr.getResponseHeader('Content-Type');
} catch (e) {}
if (contentType === 'application/octet-stream') {
data = this.xhr.response || this.xhr.responseText;
} else {
if (!this.supportsBinary) {
data = this.xhr.responseText;
} else {
try {
data = String.fromCharCode.apply(null, new Uint8Array(this.xhr.response));
} catch (e) {
var ui8Arr = new Uint8Array(this.xhr.response);
var dataArray = [];
for (var idx = 0, length = ui8Arr.length; idx < length; idx++) {
dataArray.push(ui8Arr[idx]);
}

data = String.fromCharCode.apply(null, dataArray);
}
}
data = this.xhr.responseText;
}
} catch (e) {
this.onError(e);
Expand Down
2 changes: 1 addition & 1 deletion lib/transports/polling.js
Expand Up @@ -145,7 +145,7 @@ Polling.prototype.onData = function (data) {
};

// decode payload
parser.decodePayload(data, this.socket.binaryType, this.supportsBinary, callback);
parser.decodePayload(data, this.socket.binaryType, false, callback);

// if an event did not trigger closing
if ('closed' !== this.readyState) {
Expand Down

0 comments on commit 3e03346

Please sign in to comment.