Skip to content

Commit 3e03346

Browse files
Tom Atkinsondarrachequesne
Tom Atkinson
authored andcommitted
[refactor] Set responseType based on 'Content-Type' header (#562)
1 parent 51d7529 commit 3e03346

File tree

2 files changed

+12
-22
lines changed

2 files changed

+12
-22
lines changed

lib/transports/polling-xhr.js

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,6 @@ Request.prototype.create = function () {
205205
}
206206
}
207207
} catch (e) {}
208-
if (this.supportsBinary) {
209-
// This has to be done after open because Firefox is stupid
210-
// http://stackoverflow.com/questions/13216903/get-binary-data-with-xmlhttprequest-in-a-firefox-extension
211-
xhr.responseType = 'arraybuffer';
212-
}
213208

214209
if ('POST' === this.method) {
215210
try {
@@ -243,6 +238,15 @@ Request.prototype.create = function () {
243238
};
244239
} else {
245240
xhr.onreadystatechange = function () {
241+
if (xhr.readyState === 2) {
242+
var contentType;
243+
try {
244+
contentType = xhr.getResponseHeader('Content-Type');
245+
} catch (e) {}
246+
if (contentType === 'application/octet-stream') {
247+
xhr.responseType = 'arraybuffer';
248+
}
249+
}
246250
if (4 !== xhr.readyState) return;
247251
if (200 === xhr.status || 1223 === xhr.status) {
248252
self.onLoad();
@@ -348,26 +352,12 @@ Request.prototype.onLoad = function () {
348352
try {
349353
var contentType;
350354
try {
351-
contentType = this.xhr.getResponseHeader('Content-Type').split(';')[0];
355+
contentType = this.xhr.getResponseHeader('Content-Type');
352356
} catch (e) {}
353357
if (contentType === 'application/octet-stream') {
354358
data = this.xhr.response || this.xhr.responseText;
355359
} else {
356-
if (!this.supportsBinary) {
357-
data = this.xhr.responseText;
358-
} else {
359-
try {
360-
data = String.fromCharCode.apply(null, new Uint8Array(this.xhr.response));
361-
} catch (e) {
362-
var ui8Arr = new Uint8Array(this.xhr.response);
363-
var dataArray = [];
364-
for (var idx = 0, length = ui8Arr.length; idx < length; idx++) {
365-
dataArray.push(ui8Arr[idx]);
366-
}
367-
368-
data = String.fromCharCode.apply(null, dataArray);
369-
}
370-
}
360+
data = this.xhr.responseText;
371361
}
372362
} catch (e) {
373363
this.onError(e);

lib/transports/polling.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ Polling.prototype.onData = function (data) {
145145
};
146146

147147
// decode payload
148-
parser.decodePayload(data, this.socket.binaryType, this.supportsBinary, callback);
148+
parser.decodePayload(data, this.socket.binaryType, false, callback);
149149

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

0 commit comments

Comments
 (0)