Skip to content

Commit

Permalink
Use different xhr object for local and for CORS ajax.
Browse files Browse the repository at this point in the history
  • Loading branch information
majek committed Mar 26, 2012
1 parent b54d70f commit 9558c8d
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 25 deletions.
40 changes: 30 additions & 10 deletions lib/dom2.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down
9 changes: 4 additions & 5 deletions lib/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();});
Expand Down Expand Up @@ -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:
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion lib/trans-iframe-eventsource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
2 changes: 1 addition & 1 deletion lib/trans-iframe-htmlfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
2 changes: 1 addition & 1 deletion lib/trans-iframe-xhr-polling.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
4 changes: 2 additions & 2 deletions lib/trans-xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
10 changes: 5 additions & 5 deletions tests/html/src/domtests.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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])

0 comments on commit 9558c8d

Please sign in to comment.