[Pkg-javascript-commits] [sockjs-client] 35/434: XHR cross domain - XDR. IE implements it differently.

Tonnerre Lombard tonnerre-guest at moszumanska.debian.org
Wed Jan 8 00:47:00 UTC 2014


This is an automated email from the git hooks/post-receive script.

tonnerre-guest pushed a commit to branch master
in repository sockjs-client.

commit 755e508858b7dd569b0af1d9c1f943ed5c7db6ba
Author: Marek Majkowski <majek04 at gmail.com>
Date:   Mon Aug 1 16:10:50 2011 +0100

    XHR cross domain - XDR. IE implements it differently.
---
 lib/trans-jsonp-sender.js | 10 +++++++++
 lib/trans-xhrpolling.js   | 11 ++++++----
 lib/utils.js              | 54 ++++++++++++++++++++++++++++++++++++++++++++++-
 tests-src/test-run.coffee |  2 +-
 4 files changed, 71 insertions(+), 6 deletions(-)

diff --git a/lib/trans-jsonp-sender.js b/lib/trans-jsonp-sender.js
index d0e2e82..5843b45 100644
--- a/lib/trans-jsonp-sender.js
+++ b/lib/trans-jsonp-sender.js
@@ -91,3 +91,13 @@ var ajaxSender = function(url, payload, callback) {
     return utils.createXHR('POST', url + '/xhr_send', payload, orsc);
 };
 
+var xdrSender = function(url, payload, callback) {
+    var orsc = function (xhr, e, abort_reason) {
+        if(xhr.readyState === 4 || abort_reason) {
+            callback(xhr.status, abort_reason);
+        }
+    };
+    var fun = window.XDomainRequest ? utils.createXDR : utils.createXHR;
+    return fun('POST', url + '/xhr_send', payload, orsc);
+};
+
diff --git a/lib/trans-xhrpolling.js b/lib/trans-xhrpolling.js
index 5590edb..70139f4 100644
--- a/lib/trans-xhrpolling.js
+++ b/lib/trans-xhrpolling.js
@@ -4,7 +4,7 @@ var XhrTransport = SockJS.xhrpolling = function(ri, trans_url){
     var that = this;
     that.ri = ri;
     that.trans_url = trans_url;
-    that.send_constructor(ajaxSender);
+    that.send_constructor(xdrSender);
     that._schedule_recv();
 };
 
@@ -51,14 +51,17 @@ var xhrPoll = function(url, user_callback) {
         }
     };
     // Using POST can save us from caching issues.
-    return utils.createXHR('POST', url, null, orsc);
+    var fun = window.XDomainRequest ? utils.createXDR : utils.createXHR;
+    return fun('POST', url, null, orsc);
 };
 
 // According to:
 //   http://stackoverflow.com/questions/1641507/detect-browser-support-for-cross-domain-xmlhttprequests
 //   http://hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors/
 XhrTransport.enabled = function() {
-    if (!('XMLHttpRequest' in window)) return false;
-    return ('withCredentials' in new XMLHttpRequest());
+    if (window.XDomainRequest) return true;
+    if (window.XMLHttpRequest &&
+        'withCredentials' in new XMLHttpRequest()) return true;
+    return false;
 };
 
diff --git a/lib/utils.js b/lib/utils.js
index d81ec54..245f2e2 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -80,9 +80,61 @@ if (navigator &&
     delete xhrDefaultHeaders['User-Agent'];
 }
 
+// References:
+//   http://ajaxian.com/archives/100-line-ajax-wrapper
+//   http://msdn.microsoft.com/en-us/library/cc288060(v=VS.85).aspx
+utils.createXDR = function(method, url, payload, callback) {
+    var mock_xhr = {status: null, responseText:'', readyState:1};
+    var xdr = new XDomainRequest();
+    // IE caches POSTs
+    url += ((url.indexOf('?') === -1) ? '?' : '&') + 't='+(+new Date);
+    var cleanup = function() {
+        onerror = xdr.onerror = xdr.ontimeout = xdr.onprogress =
+            xdr.onload = null;
+        try {
+            xdr.abort();
+        } catch (x) {}
+        xdr = callback = null;
+    };
+    var onerror = xdr.ontimeout = xdr.onerror = function() {
+        mock_xhr.status = 500;
+        mock_xhr.readyState = 4;
+        callback(mock_xhr);
+        cleanup();
+    };
+    xdr.onload = function() {
+        mock_xhr.status = 200;
+        mock_xhr.readyState = 4;
+        mock_xhr.responseText = xdr.responseText;
+        callback(mock_xhr);
+        cleanup();
+    };
+    xdr.onprogress = function() {
+        mock_xhr.status = 200;
+        mock_xhr.readyState = 3;
+        mock_xhr.responseText = xdr.responseText;
+        callback(mock_xhr);
+    };
+    try {
+        // Fails with AccessDenied if port number is bogus
+        xdr.open(method, url);
+        xdr.send(payload);
+    } catch (x) {
+        onerror();
+    }
+    return function (abort_reason) {
+        if (callback) {
+            callback(mock_xhr, null, abort_reason);
+            cleanup();
+        }
+    };
+};
+
 utils.createXHR = function(method, url, payload, callback) {
     var xhr;
     if (_window.ActiveXObject) {
+        // IE caches POSTs
+        url += ((url.indexOf('?') === -1) ? '?' : '&') + 't='+(+new Date);
         try {
             xhr = new ActiveXObject('Microsoft.XMLHTTP');
         } catch(x) {}
@@ -191,7 +243,7 @@ utils.userSetStatus = function (status) {
 };
 
 utils.log = function() {
-    if (_window.console && console.log) {
+    if (_window.console && console.log && console.log.apply) {
         console.log.apply(console, arguments);
     }
 };
diff --git a/tests-src/test-run.coffee b/tests-src/test-run.coffee
index 01bfe56..60d858e 100644
--- a/tests-src/test-run.coffee
+++ b/tests-src/test-run.coffee
@@ -19,5 +19,5 @@ test_protocol = (protocol) ->
         asyncTest("invalid url port", test_invalid_url_port(protocol))
 
 
-for protocol in ['websocket', 'jsonp', 'iframe-eventsource', 'iframe-htmlfile']
+for protocol in ['websocket', 'xhrpolling', 'jsonp', 'iframe-eventsource', 'iframe-htmlfile']
     test_protocol(protocol)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/sockjs-client.git



More information about the Pkg-javascript-commits mailing list