[Pkg-javascript-commits] [sockjs-client] 373/434: Use different xhr object for local and for CORS ajax.

Tonnerre Lombard tonnerre-guest at moszumanska.debian.org
Wed Jan 8 00:47:26 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 9558c8d348da8f7884ac33dd444866457d2c27cf
Author: Marek Majkowski <majek04 at gmail.com>
Date:   Mon Mar 26 16:21:01 2012 +0100

    Use different xhr object for local and for CORS ajax.
---
 lib/dom2.js                     | 40 ++++++++++++++++++++++++++++++----------
 lib/info.js                     |  9 ++++-----
 lib/trans-iframe-eventsource.js |  2 +-
 lib/trans-iframe-htmlfile.js    |  2 +-
 lib/trans-iframe-xhr-polling.js |  2 +-
 lib/trans-xhr.js                |  4 ++--
 tests/html/src/domtests.coffee  | 10 +++++-----
 7 files changed, 44 insertions(+), 25 deletions(-)

diff --git a/lib/dom2.js b/lib/dom2.js
index 2613e92..1f9052f 100644
--- a/lib/dom2.js
+++ b/lib/dom2.js
@@ -1,11 +1,7 @@
-var XHRObject = utils.XHRObject = function() {
-    var that = this, args = arguments;
-    utils.delay(function(){that._start.apply(that, args);});
-};
-
-XHRObject.prototype = new EventEmitter(['chunk', 'finish']);
+var AbstractXHRObject = function(){};
+AbstractXHRObject.prototype = new EventEmitter(['chunk', 'finish']);
 
-XHRObject.prototype._start = function(method, url, payload, opts) {
+AbstractXHRObject.prototype._start = function(method, url, payload, opts) {
     var that = this;
 
     try {
@@ -34,9 +30,16 @@ XHRObject.prototype._start = function(method, url, payload, opts) {
         return;
     };
 
-    if ('withCredentials' in that.xhr && (!opts || !opts.no_credentials)) {
+    if (!opts || !opts.no_credentials) {
+        // Mozilla docs says https://developer.mozilla.org/en/XMLHttpRequest :
+        // "This never affects same-site requests."
         that.xhr.withCredentials = 'true';
     }
+    if (opts && opts.headers) {
+        for(var key in opts.headers) {
+            that.xhr.setRequestHeader(key, opts.headers[key]);
+        }
+    }
 
     that.xhr.onreadystatechange = function() {
         if (that.xhr) {
@@ -64,7 +67,7 @@ XHRObject.prototype._start = function(method, url, payload, opts) {
     that.xhr.send(payload);
 };
 
-XHRObject.prototype._cleanup = function(abort) {
+AbstractXHRObject.prototype._cleanup = function(abort) {
     var that = this;
     if (!that.xhr) return;
     utils.unload_del(that.unload_ref);
@@ -80,12 +83,29 @@ XHRObject.prototype._cleanup = function(abort) {
     that.unload_ref = that.xhr = null;
 };
 
-XHRObject.prototype.close = function() {
+AbstractXHRObject.prototype.close = function() {
     var that = this;
     that.nuke();
     that._cleanup(true);
 };
 
+var XHRCorsObject = utils.XHRCorsObject = function() {
+    var that = this, args = arguments;
+    utils.delay(function(){that._start.apply(that, args);});
+};
+XHRCorsObject.prototype = new AbstractXHRObject();
+
+var XHRLocalObject = utils.XHRLocalObject = function(method, url, payload) {
+    var that = this;
+    utils.delay(function(){
+        that._start(method, url, payload, {
+            no_credentials: true
+        });
+    });
+};
+XHRLocalObject.prototype = new AbstractXHRObject();
+
+
 
 // References:
 //   http://ajaxian.com/archives/100-line-ajax-wrapper
diff --git a/lib/info.js b/lib/info.js
index aa85764..92aa2c8 100644
--- a/lib/info.js
+++ b/lib/info.js
@@ -8,8 +8,7 @@ InfoReceiver.prototype = new EventEmitter(['finish']);
 InfoReceiver.prototype.doXhr = function(base_url, AjaxObject) {
     var that = this;
     var t0 = (new Date()).getTime();
-    var xo = new AjaxObject('GET', base_url + '/info', null,
-                            {no_credentials: true});
+    var xo = new AjaxObject('GET', base_url + '/info');
 
     var tref = utils.delay(8000,
                            function(){xo.ontimeout();});
@@ -79,11 +78,11 @@ var createInfoReceiver = function(base_url) {
     if (utils.isSameOriginUrl(base_url)) {
         // If, for some reason, we have SockJS locally - there's no
         // need to start up the complex machinery. Just use ajax.
-        return new InfoReceiver(base_url, utils.XHRObject);
+        return new InfoReceiver(base_url, utils.XHRLocalObject);
     }
     switch (utils.isXHRCorsCapable()) {
     case 1:
-        return new InfoReceiver(base_url, utils.XHRObject);
+        return new InfoReceiver(base_url, utils.XHRCorsObject);
     case 2:
         return new InfoReceiver(base_url, utils.XDRObject);
     case 3:
@@ -97,7 +96,7 @@ var createInfoReceiver = function(base_url) {
 
 
 var WInfoReceiverIframe = FacadeJS['w-iframe-info-receiver'] = function(ri, _trans_url, base_url) {
-    var ir = new InfoReceiver(base_url, utils.XHRObject);
+    var ir = new InfoReceiver(base_url, utils.XHRLocalObject);
     ir.onfinish = function(info, rtt) {
         ri._didMessage('m'+JSON.stringify([info, rtt]));
         ri._didClose();
diff --git a/lib/trans-iframe-eventsource.js b/lib/trans-iframe-eventsource.js
index a9309cf..f326710 100644
--- a/lib/trans-iframe-eventsource.js
+++ b/lib/trans-iframe-eventsource.js
@@ -16,6 +16,6 @@ EventSourceIframeTransport.roundTrips = 3; // html, javascript, eventsource
 
 // w-iframe-eventsource
 var EventSourceTransport = FacadeJS['w-iframe-eventsource'] = function(ri, trans_url) {
-    this.run(ri, trans_url, '/eventsource', EventSourceReceiver, utils.XHRObject);
+    this.run(ri, trans_url, '/eventsource', EventSourceReceiver, utils.XHRLocalObject);
 }
 EventSourceTransport.prototype = new AjaxBasedTransport();
diff --git a/lib/trans-iframe-htmlfile.js b/lib/trans-iframe-htmlfile.js
index befe258..1169901 100644
--- a/lib/trans-iframe-htmlfile.js
+++ b/lib/trans-iframe-htmlfile.js
@@ -22,6 +22,6 @@ HtmlFileIframeTransport.roundTrips = 3; // html, javascript, htmlfile
 
 // w-iframe-htmlfile
 var HtmlFileTransport = FacadeJS['w-iframe-htmlfile'] = function(ri, trans_url) {
-    this.run(ri, trans_url, '/htmlfile', HtmlfileReceiver, utils.XHRObject);
+    this.run(ri, trans_url, '/htmlfile', HtmlfileReceiver, utils.XHRLocalObject);
 };
 HtmlFileTransport.prototype = new AjaxBasedTransport();
diff --git a/lib/trans-iframe-xhr-polling.js b/lib/trans-iframe-xhr-polling.js
index 126e8e4..f82a46e 100644
--- a/lib/trans-iframe-xhr-polling.js
+++ b/lib/trans-iframe-xhr-polling.js
@@ -16,7 +16,7 @@ XhrPollingIframeTransport.roundTrips = 3; // html, javascript, xhr
 
 // w-iframe-xhr-polling
 var XhrPollingITransport = FacadeJS['w-iframe-xhr-polling'] = function(ri, trans_url) {
-    this.run(ri, trans_url, '/xhr', XhrReceiver, utils.XHRObject);
+    this.run(ri, trans_url, '/xhr', XhrReceiver, utils.XHRLocalObject);
 };
 
 XhrPollingITransport.prototype = new AjaxBasedTransport();
diff --git a/lib/trans-xhr.js b/lib/trans-xhr.js
index 1f8e262..6ddf1a8 100644
--- a/lib/trans-xhr.js
+++ b/lib/trans-xhr.js
@@ -21,7 +21,7 @@ AjaxBasedTransport.prototype.doCleanup = function() {
 
 // xhr-streaming
 var XhrStreamingTransport = SockJS['xhr-streaming'] = function(ri, trans_url) {
-    this.run(ri, trans_url, '/xhr_streaming', XhrReceiver, utils.XHRObject);
+    this.run(ri, trans_url, '/xhr_streaming', XhrReceiver, utils.XHRCorsObject);
 };
 
 XhrStreamingTransport.prototype = new AjaxBasedTransport();
@@ -53,7 +53,7 @@ XdrStreamingTransport.roundTrips = 2; // preflight, ajax
 
 // xhr-polling
 var XhrPollingTransport = SockJS['xhr-polling'] = function(ri, trans_url) {
-    this.run(ri, trans_url, '/xhr', XhrReceiver, utils.XHRObject);
+    this.run(ri, trans_url, '/xhr', XhrReceiver, utils.XHRCorsObject);
 };
 
 XhrPollingTransport.prototype = new AjaxBasedTransport();
diff --git a/tests/html/src/domtests.coffee b/tests/html/src/domtests.coffee
index 2894217..2a72281 100644
--- a/tests/html/src/domtests.coffee
+++ b/tests/html/src/domtests.coffee
@@ -130,23 +130,23 @@ ajax_wrong_port_factory = (name) ->
             test_wrong_url(name, 'http://localhost:'+port+'/wrong_url_indeed.txt', [0])
 
 
-ajax_simple_factory('XHRObject')
+ajax_simple_factory('XHRLocalObject')
 if window.XDomainRequest
     ajax_simple_factory('XDRObject')
 
 if not window.ActiveXObject
     # Ajax streaming is not working in ie.
-    ajax_streaming_factory('XHRObject')
+    ajax_streaming_factory('XHRLocalObject')
 if window.XDomainRequest
     ajax_streaming_factory('XDRObject')
 
-ajax_wrong_port_factory('XHRObject')
+ajax_wrong_port_factory('XHRLocalObject')
 if window.XDomainRequest
     ajax_wrong_port_factory('XDRObject')
 
-asyncTest 'XHRObject wrong url', ->
+asyncTest 'XHRLocalObject wrong url', ->
     # Opera responds with 0, all other browsers with 404
-    test_wrong_url('XHRObject', '/wrong_url_indeed.txt', [0, 404])
+    test_wrong_url('XHRLocalObject', '/wrong_url_indeed.txt', [0, 404])
 if window.XDomainRequest
     asyncTest 'XDRObject wrong url', ->
         test_wrong_url('XDRObject', '/wrong_url_indeed.txt', [0])

-- 
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