6 var Transport = require('../transport');
7 var parser = require('engine.io-parser');
8 var zlib = require('zlib');
9 var accepts = require('accepts');
10 var util = require('util');
11 var debug = require('debug')('engine:polling');
13 var compressionMethods = {
14 gzip: zlib.createGzip,
15 deflate: zlib.createDeflate
19 * Exports the constructor.
22 module.exports = Polling;
25 * HTTP polling constructor.
30 function Polling (req) {
31 Transport.call(this, req);
33 this.closeTimeout = 30 * 1000;
34 this.maxHttpBufferSize = null;
35 this.httpCompression = null;
39 * Inherits from Transport.
44 util.inherits(Polling, Transport);
52 Polling.prototype.name = 'polling';
55 * Overrides onRequest.
57 * @param {http.IncomingMessage}
61 Polling.prototype.onRequest = function (req) {
64 if ('GET' === req.method) {
65 this.onPollRequest(req, res);
66 } else if ('POST' === req.method) {
67 this.onDataRequest(req, res);
75 * The client sends a request awaiting for us to send data.
80 Polling.prototype.onPollRequest = function (req, res) {
82 debug('request overlap');
83 // assert: this.res, '.req and .res should be (un)set together'
84 this.onError('overlap from client');
90 debug('setting request');
98 self.onError('poll connection closed prematurely');
101 function cleanup () {
102 req.removeListener('close', onClose);
103 self.req = self.res = null;
106 req.cleanup = cleanup;
107 req.on('close', onClose);
109 this.writable = true;
112 // if we're still writable but had a pending close, trigger an empty send
113 if (this.writable && this.shouldClose) {
114 debug('triggering empty send to append close packet');
115 this.send([{ type: 'noop' }]);
120 * The client sends a request with data.
125 Polling.prototype.onDataRequest = function (req, res) {
127 // assert: this.dataRes, '.dataReq and .dataRes should be (un)set together'
128 this.onError('data request overlap from client');
134 var isBinary = 'application/octet-stream' === req.headers['content-type'];
139 var chunks = isBinary ? Buffer.concat([]) : '';
142 function cleanup () {
143 req.removeListener('data', onData);
144 req.removeListener('end', onEnd);
145 req.removeListener('close', onClose);
146 self.dataReq = self.dataRes = chunks = null;
149 function onClose () {
151 self.onError('data request connection closed prematurely');
154 function onData (data) {
157 chunks = Buffer.concat([chunks, data]);
158 contentLength = chunks.length;
161 contentLength = Buffer.byteLength(chunks);
164 if (contentLength > self.maxHttpBufferSize) {
165 chunks = isBinary ? Buffer.concat([]) : '';
166 req.connection.destroy();
174 // text/html is required instead of text/plain to avoid an
175 // unwanted download dialog on certain user-agents (GH-43)
176 'Content-Type': 'text/html',
180 res.writeHead(200, self.headers(req, headers));
185 req.on('close', onClose);
186 if (!isBinary) req.setEncoding('utf8');
187 req.on('data', onData);
188 req.on('end', onEnd);
192 * Processes the incoming data payload.
194 * @param {String} encoded payload
198 Polling.prototype.onData = function (data) {
199 debug('received "%s"', data);
201 var callback = function (packet) {
202 if ('close' === packet.type) {
203 debug('got xhr close packet');
208 self.onPacket(packet);
211 parser.decodePayload(data, callback);
220 Polling.prototype.onClose = function () {
222 // close pending poll request
223 this.send([{ type: 'noop' }]);
225 Transport.prototype.onClose.call(this);
229 * Writes a packet payload.
231 * @param {Object} packet
235 Polling.prototype.send = function (packets) {
236 this.writable = false;
238 if (this.shouldClose) {
239 debug('appending close packet to payload');
240 packets.push({ type: 'close' });
242 this.shouldClose = null;
246 parser.encodePayload(packets, this.supportsBinary, function (data) {
247 var compress = packets.some(function (packet) {
248 return packet.options && packet.options.compress;
250 self.write(data, { compress: compress });
255 * Writes data as response to poll request.
257 * @param {String} data
258 * @param {Object} options
262 Polling.prototype.write = function (data, options) {
263 debug('writing "%s"', data);
265 this.doWrite(data, options, function () {
271 * Performs the write.
276 Polling.prototype.doWrite = function (data, options, callback) {
279 // explicit UTF-8 is required for pages not served under utf
280 var isString = typeof data === 'string';
281 var contentType = isString
282 ? 'text/plain; charset=UTF-8'
283 : 'application/octet-stream';
286 'Content-Type': contentType
289 if (!this.httpCompression || !options.compress) {
294 var len = isString ? Buffer.byteLength(data) : data.length;
295 if (len < this.httpCompression.threshold) {
300 var encoding = accepts(this.req).encodings(['gzip', 'deflate']);
306 this.compress(data, encoding, function (err, data) {
308 self.res.writeHead(500);
314 headers['Content-Encoding'] = encoding;
318 function respond (data) {
319 headers['Content-Length'] = 'string' === typeof data ? Buffer.byteLength(data) : data.length;
320 self.res.writeHead(200, self.headers(self.req, headers));
332 Polling.prototype.compress = function (data, encoding, callback) {
333 debug('compressing');
338 compressionMethods[encoding](this.httpCompression)
339 .on('error', callback)
340 .on('data', function (chunk) {
342 nread += chunk.length;
344 .on('end', function () {
345 callback(null, Buffer.concat(buffers, nread));
351 * Closes the transport.
356 Polling.prototype.doClose = function (fn) {
360 var closeTimeoutTimer;
363 debug('aborting ongoing data request');
364 this.dataReq.destroy();
368 debug('transport writable - closing right away');
369 this.send([{ type: 'close' }]);
371 } else if (this.discarded) {
372 debug('transport discarded - closing right away');
375 debug('transport not writable - buffering orderly close');
376 this.shouldClose = onClose;
377 closeTimeoutTimer = setTimeout(onClose, this.closeTimeout);
380 function onClose () {
381 clearTimeout(closeTimeoutTimer);
388 * Returns headers for a response.
390 * @param {http.IncomingMessage} request
391 * @param {Object} extra headers
395 Polling.prototype.headers = function (req, headers) {
396 headers = headers || {};
398 // prevent XSS warnings on IE
399 // https://github.com/LearnBoost/socket.io/pull/1333
400 var ua = req.headers['user-agent'];
401 if (ua && (~ua.indexOf(';MSIE') || ~ua.indexOf('Trident/'))) {
402 headers['X-XSS-Protection'] = '0';
405 this.emit('headers', headers);