rc-web@69: rc-web@69: /*! rc-web@69: * socket.io-node rc-web@69: * Copyright(c) 2011 LearnBoost rc-web@69: * MIT Licensed rc-web@69: */ rc-web@69: rc-web@69: /** rc-web@69: * Module requirements. rc-web@69: */ rc-web@69: rc-web@69: var HTTPPolling = require('./http-polling'); rc-web@69: var jsonpolling_re = /^\d+$/ rc-web@69: rc-web@69: /** rc-web@69: * Export the constructor. rc-web@69: */ rc-web@69: rc-web@69: exports = module.exports = JSONPPolling; rc-web@69: rc-web@69: /** rc-web@69: * JSON-P polling transport. rc-web@69: * rc-web@69: * @api public rc-web@69: */ rc-web@69: rc-web@69: function JSONPPolling (mng, data, req) { rc-web@69: HTTPPolling.call(this, mng, data, req); rc-web@69: rc-web@69: this.head = 'io.j[0]('; rc-web@69: this.foot = ');'; rc-web@69: rc-web@69: if (data.query.i && jsonpolling_re.test(data.query.i)) { rc-web@69: this.head = 'io.j[' + data.query.i + ']('; rc-web@69: } rc-web@69: }; rc-web@69: rc-web@69: /** rc-web@69: * Inherits from Transport. rc-web@69: */ rc-web@69: rc-web@69: JSONPPolling.prototype.__proto__ = HTTPPolling.prototype; rc-web@69: rc-web@69: /** rc-web@69: * Transport name rc-web@69: * rc-web@69: * @api public rc-web@69: */ rc-web@69: rc-web@69: JSONPPolling.prototype.name = 'jsonppolling'; rc-web@69: rc-web@69: /** rc-web@69: * Make sure POST are decoded. rc-web@69: */ rc-web@69: rc-web@69: JSONPPolling.prototype.postEncoded = true; rc-web@69: rc-web@69: /** rc-web@69: * Handles incoming data. rc-web@69: * Due to a bug in \n handling by browsers, we expect a JSONified string. rc-web@69: * rc-web@69: * @api private rc-web@69: */ rc-web@69: rc-web@69: JSONPPolling.prototype.onData = function (data) { rc-web@69: try { rc-web@69: data = JSON.parse(data); rc-web@69: } catch (e) { rc-web@69: this.error('parse', 'reconnect'); rc-web@69: return; rc-web@69: } rc-web@69: rc-web@69: HTTPPolling.prototype.onData.call(this, data); rc-web@69: }; rc-web@69: rc-web@69: /** rc-web@69: * Performs the write. rc-web@69: * rc-web@69: * @api private rc-web@69: */ rc-web@69: rc-web@69: JSONPPolling.prototype.doWrite = function (data) { rc-web@69: HTTPPolling.prototype.doWrite.call(this); rc-web@69: rc-web@69: var data = data === undefined rc-web@69: ? '' : this.head + JSON.stringify(data) + this.foot; rc-web@69: rc-web@69: this.response.writeHead(200, { rc-web@69: 'Content-Type': 'text/javascript; charset=UTF-8' rc-web@69: , 'Content-Length': Buffer.byteLength(data) rc-web@69: , 'Connection': 'Keep-Alive' rc-web@69: , 'X-XSS-Protection': '0' rc-web@69: }); rc-web@69: rc-web@69: this.response.write(data); rc-web@69: this.log.debug(this.name + ' writing', data); rc-web@69: };