diff --git a/lib/transports/polling.js b/lib/transports/polling.js index c19cd03d..1fa27e39 100644 --- a/lib/transports/polling.js +++ b/lib/transports/polling.js @@ -71,34 +71,35 @@ Polling.prototype.onPollRequest = function (req, res) { // assert: this.res, '.req and .res should be (un)set together' this.onError('overlap from client'); res.writeHead(500); - } else { - debug('setting request'); + return; + } - this.req = req; - this.res = res; + debug('setting request'); - var self = this; + this.req = req; + this.res = res; - function onClose () { - self.onError('poll connection closed prematurely'); - } + var self = this; - function cleanup () { - req.removeListener('close', onClose); - self.req = self.res = null; - } + function onClose () { + self.onError('poll connection closed prematurely'); + } - req.cleanup = cleanup; - req.on('close', onClose); + function cleanup () { + req.removeListener('close', onClose); + self.req = self.res = null; + } - this.writable = true; - this.emit('drain'); + req.cleanup = cleanup; + req.on('close', onClose); - // if we're still writable but had a pending close, trigger an empty send - if (this.writable && this.shouldClose) { - debug('triggering empty send to append close packet'); - this.send([{ type: 'noop' }]); - } + this.writable = true; + this.emit('drain'); + + // if we're still writable but had a pending close, trigger an empty send + if (this.writable && this.shouldClose) { + debug('triggering empty send to append close packet'); + this.send([{ type: 'noop' }]); } }; @@ -113,54 +114,55 @@ Polling.prototype.onDataRequest = function (req, res) { // assert: this.dataRes, '.dataReq and .dataRes should be (un)set together' this.onError('data request overlap from client'); res.writeHead(500); - } else { - var isBinary = 'application/octet-stream' == req.headers['content-type']; - - this.dataReq = req; - this.dataRes = res; - - var chunks = isBinary ? new Buffer(0) : '' - , self = this - - function cleanup () { - chunks = isBinary ? new Buffer(0) : ''; - req.removeListener('data', onData); - req.removeListener('end', onEnd); - req.removeListener('close', onClose); - self.dataReq = self.dataRes = null; - }; - - function onClose () { - cleanup(); - self.onError('data request connection closed prematurely'); - }; - - function onData (data) { - if (typeof data == 'string') { - chunks += data; - } else { - chunks = Buffer.concat([chunks, data]); - } - }; - - function onEnd () { - self.onData(chunks); - res.writeHead(200, self.headers(req, { - 'Content-Length': 2 - // text/html is required instead of text/plain to avoid an - // unwanted download dialog on certain user-agents (GH-43) - , 'Content-Type': 'text/html' - })); - res.end('ok'); - cleanup(); - }; - - req.abort = cleanup; - req.on('close', onClose); - req.on('data', onData); - req.on('end', onEnd); - if (!isBinary) { req.setEncoding('utf8'); } + return; } + + var isBinary = 'application/octet-stream' == req.headers['content-type']; + + this.dataReq = req; + this.dataRes = res; + + var chunks = isBinary ? new Buffer(0) : ''; + var self = this; + + function cleanup () { + chunks = isBinary ? new Buffer(0) : ''; + req.removeListener('data', onData); + req.removeListener('end', onEnd); + req.removeListener('close', onClose); + self.dataReq = self.dataRes = null; + } + + function onClose () { + cleanup(); + self.onError('data request connection closed prematurely'); + } + + function onData (data) { + if (typeof data == 'string') { + chunks += data; + } else { + chunks = Buffer.concat([chunks, data]); + } + } + + function onEnd () { + self.onData(chunks); + res.writeHead(200, self.headers(req, { + 'Content-Length': 2 + // text/html is required instead of text/plain to avoid an + // unwanted download dialog on certain user-agents (GH-43) + , 'Content-Type': 'text/html' + })); + res.end('ok'); + cleanup(); + } + + req.abort = cleanup; + req.on('close', onClose); + req.on('data', onData); + req.on('end', onEnd); + if (!isBinary) req.setEncoding('utf8'); }; /**