From 8d65eed8fa96b70dfb2035e1bb02bcdd7062d5b1 Mon Sep 17 00:00:00 2001 From: Tom Atkinson Date: Fri, 14 Apr 2017 17:48:31 +0200 Subject: [PATCH 1/3] Set responseType based on response content-type header --- lib/transports/polling-xhr.js | 34 +++++++++------------------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/lib/transports/polling-xhr.js b/lib/transports/polling-xhr.js index dbf000f43..f181fb9be 100755 --- a/lib/transports/polling-xhr.js +++ b/lib/transports/polling-xhr.js @@ -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 { @@ -243,6 +238,12 @@ Request.prototype.create = function () { }; } else { xhr.onreadystatechange = function () { + if (xhr.readyState === 2) { + var contentType = xhr.getResponseHeader('Content-Type'); + if (contentType === 'application/octet-stream') { + xhr.responseType = 'arraybuffer'; + } + } if (4 !== xhr.readyState) return; if (200 === xhr.status || 1223 === xhr.status) { self.onLoad(); @@ -346,28 +347,11 @@ Request.prototype.cleanup = function (fromError) { Request.prototype.onLoad = function () { var data; try { - var contentType; - try { - contentType = this.xhr.getResponseHeader('Content-Type').split(';')[0]; - } catch (e) {} + var contentType = this.xhr.getResponseHeader('Content-Type'); if (contentType === 'application/octet-stream') { - data = this.xhr.response || this.xhr.responseText; + data = this.xhr.response; } 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); From 9218a4074f702f113ffb1d7e5e0465117996a17e Mon Sep 17 00:00:00 2001 From: Tom Atkinson Date: Mon, 24 Apr 2017 08:53:12 +0200 Subject: [PATCH 2/3] Disable utf8decode of polling strings --- lib/transports/polling.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/transports/polling.js b/lib/transports/polling.js index e600af601..22e0ed0e7 100644 --- a/lib/transports/polling.js +++ b/lib/transports/polling.js @@ -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) { From 9e29332518e064d9be5df9408f280bfcd32fa2e2 Mon Sep 17 00:00:00 2001 From: Tom Atkinson Date: Mon, 24 Apr 2017 09:57:29 +0200 Subject: [PATCH 3/3] Restore try catch for ie --- lib/transports/polling-xhr.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/transports/polling-xhr.js b/lib/transports/polling-xhr.js index f181fb9be..297bac59e 100755 --- a/lib/transports/polling-xhr.js +++ b/lib/transports/polling-xhr.js @@ -239,7 +239,10 @@ Request.prototype.create = function () { } else { xhr.onreadystatechange = function () { if (xhr.readyState === 2) { - var contentType = xhr.getResponseHeader('Content-Type'); + var contentType; + try { + contentType = xhr.getResponseHeader('Content-Type'); + } catch (e) {} if (contentType === 'application/octet-stream') { xhr.responseType = 'arraybuffer'; } @@ -347,9 +350,12 @@ Request.prototype.cleanup = function (fromError) { Request.prototype.onLoad = function () { var data; try { - var contentType = this.xhr.getResponseHeader('Content-Type'); + var contentType; + try { + contentType = this.xhr.getResponseHeader('Content-Type'); + } catch (e) {} if (contentType === 'application/octet-stream') { - data = this.xhr.response; + data = this.xhr.response || this.xhr.responseText; } else { data = this.xhr.responseText; }