From d0afe8590d18ebe29374cd9e9f98659d0ac5731f Mon Sep 17 00:00:00 2001 From: Daniel Howard Date: Wed, 20 Feb 2013 12:24:10 -0800 Subject: [PATCH] Support JSONP to allow Apache integration. --- client/cookie.html | 18 ++++++++++++++++++ client/index.html | 23 +++++++++++++++++++++++ client/js/im.js | 8 +++++--- client/js/jquery-1.4.1.js | 16 ++++++++-------- server/middleware/im.js | 22 ++++++++++++++++++++++ server/middleware/im/hub.js | 2 +- server/middleware/im/user.js | 23 +++++++++++++++-------- 7 files changed, 92 insertions(+), 20 deletions(-) create mode 100644 client/cookie.html create mode 100644 client/index.html diff --git a/client/cookie.html b/client/cookie.html new file mode 100644 index 0000000..27bd9e0 --- /dev/null +++ b/client/cookie.html @@ -0,0 +1,18 @@ + + + + + + +cookie set + + diff --git a/client/index.html b/client/index.html new file mode 100644 index 0000000..2d1288c --- /dev/null +++ b/client/index.html @@ -0,0 +1,23 @@ + + + +Ajax IM + + + + + + + + + +Hello. + + diff --git a/client/js/im.js b/client/js/im.js index 429e9c8..e7ecead 100644 --- a/client/js/im.js +++ b/client/js/im.js @@ -1387,20 +1387,22 @@ AjaxIM.request = function(url, type, data, successFunc, failureFunc) { if(typeof failureFunc != 'function') failureFunc = function(){}; + var jsonp = (url.substring(0, 1) !== '/'); + data['sessionid'] = cookies.get('sessionid'); $.ajax({ url: url, data: data, - dataType: 'json', + dataType: jsonp? 'jsonp': 'json', type: type, cache: false, timeout: 299000, success: function(json, textStatus, xhr) { - if('status' in xhr && xhr.status == '0') return; + if(!jsonp && 'status' in xhr && xhr.status == '0') return; _dbg(json); successFunc(json); }, complete: function(xhr, textStatus) { - if(~errorTypes.indexOf(textStatus) || xhr.status == '0') + if(!jsonp && (~errorTypes.indexOf(textStatus) || xhr.status == '0')) failureFunc(textStatus); } }); diff --git a/client/js/jquery-1.4.1.js b/client/js/jquery-1.4.1.js index 1097010..aadcc34 100644 --- a/client/js/jquery-1.4.1.js +++ b/client/js/jquery-1.4.1.js @@ -4855,15 +4855,15 @@ jQuery.extend({ success(); complete(); // Garbage collect - window[ jsonp ] = undefined; +// window[ jsonp ] = undefined; +// +// try { +// delete window[ jsonp ]; +// } catch(e) {} - try { - delete window[ jsonp ]; - } catch(e) {} - - if ( head ) { - head.removeChild( script ); - } +// if ( head ) { +// head.removeChild( script ); +// } }; } diff --git a/server/middleware/im.js b/server/middleware/im.js index ab75f6c..9d39607 100644 --- a/server/middleware/im.js +++ b/server/middleware/im.js @@ -14,7 +14,29 @@ module.exports = function setupHub(options) { return; } + if (!(options.authentication.cookie in req.cookies)) { + if (options.authentication.cookie in req.query) { + req.cookies[options.authentication.cookie] = req.query[options.authentication.cookie]; + } else if (options.authentication.cookie in req.body) { + req.cookies[options.authentication.cookie] = req.body[options.authentication.cookie]; + } + } + + if (!('callback' in req.cookies)) { + if ('callback' in req.query) { + req.cookies.callback = req.query.callback; + } else if ('callback' in req.body) { + req.cookies.callback = req.body.callback; + } + } + req.sessionID = req.cookies[options.authentication.cookie]; + req.jsonpCallback = req.cookies.callback; + + delete req.query[options.authentication.cookie]; + delete req.body[options.authentication.cookie]; + delete req.query.callback; + delete req.body.callback; if(req.dev) { next(); diff --git a/server/middleware/im/hub.js b/server/middleware/im/hub.js index 8fb722a..73e8598 100644 --- a/server/middleware/im/hub.js +++ b/server/middleware/im/hub.js @@ -56,7 +56,7 @@ Hub.prototype.get = function(req, fn) { } else { this.auth.authenticate(req, o_.bind(function(data) { if(data) { - var session = new User(req.sessionID, data); + var session = new User(req, data); this.set(req.sessionID, session); this.auth.friends(req, data, o_.bind(function(friends) { diff --git a/server/middleware/im/user.js b/server/middleware/im/user.js index c194b35..74d72fc 100644 --- a/server/middleware/im/user.js +++ b/server/middleware/im/user.js @@ -2,8 +2,9 @@ var events = require('events'), packages = require('../../libs/packages'), o_ = require('../../libs/utils'); -var User = module.exports = function(id, data) { - this.id = id; +var User = module.exports = function(req, data) { + this.req = req; + this.id = req.sessionID; this.connection = null; this.listeners = []; this.message_queue = []; @@ -60,6 +61,10 @@ User.prototype.send = function(code, message, callback) { this._send('listener', code, message, callback); }; +User.prototype.addCallback = function(message) { + return ((typeof this.req.jsonpCallback) != 'undefined')? this.req.jsonpCallback+'('+message+');': message; +}; + User.prototype._send = function(type, code, message, callback) { if(!message && typeof code != 'number') { callback = message; @@ -72,10 +77,11 @@ User.prototype._send = function(type, code, message, callback) { if(type == 'connection' && this.connection) { this.connection.writeHead(code || 200, { - 'Content-Type': 'application/json', - 'Content-Length': message.length +// 'Content-Type': 'application/json', + 'Content-Type': 'application/javascript', + 'Content-Length': this.addCallback(message).length }); - this.connection.end(message); + this.connection.end(this.addCallback(message)); } else { if(!this.listeners.length) return this.message_queue.push(arguments); @@ -84,10 +90,11 @@ User.prototype._send = function(type, code, message, callback) { this.listeners = []; while(conn = cx.shift()) { conn.writeHead(code || 200, { - 'Content-Type': 'application/json', - 'Content-Length': message.length +// 'Content-Type': 'application/json', + 'Content-Type': 'application/javascript', + 'Content-Length': this.addCallback(message).length }); - conn.end(message); + conn.end(this.addCallback(message)); } if(callback) callback(); }