Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use different xhr object for local and for CORS ajax.

  • Loading branch information...
commit 9558c8d348da8f7884ac33dd444866457d2c27cf 1 parent b54d70f
@majek majek authored
View
40 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
View
9 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();
View
2  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();
View
2  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();
View
2  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();
View
4 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();
View
10 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])
Please sign in to comment.
Something went wrong with that request. Please try again.