From d071ec3acd7a1f375fdb428d640b25436cfd043f Mon Sep 17 00:00:00 2001 From: James Coglan Date: Sun, 8 Jul 2012 20:30:53 +0100 Subject: [PATCH] Check that incoming requests have an output stream before doing anything with it. --- lib/faye/eventsource.js | 14 +++++++------- lib/faye/websocket.js | 12 +++++++----- lib/faye/websocket/api.js | 2 +- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/faye/eventsource.js b/lib/faye/eventsource.js index 6ed792b..b2039a6 100644 --- a/lib/faye/eventsource.js +++ b/lib/faye/eventsource.js @@ -19,9 +19,6 @@ var EventSource = function(request, response, options) { this._ping = options.ping || this.DEFAULT_PING; this._retry = options.retry || this.DEFAULT_RETRY; - this._stream.setTimeout(0); - this._stream.setNoDelay(true); - var scheme = isSecureConnection(request) ? 'https:' : 'http:'; this.url = scheme + '//' + request.headers.host + request.url; @@ -39,15 +36,18 @@ var EventSource = function(request, response, options) { '\r\n\r\n' + 'retry: ' + Math.floor(this._retry * 1000) + '\r\n\r\n'; - try { - this._stream.write(handshake, 'utf8'); - } catch (e) {} - this.readyState = API.OPEN; if (this._ping) this._pingLoop = setInterval(function() { self.ping() }, this._ping * 1000); + if (!this._stream || !this._stream.writable) return; + + this._stream.setTimeout(0); + this._stream.setNoDelay(true); + + try { this._stream.write(handshake, 'utf8') } catch (e) {} + ['close', 'end', 'error'].forEach(function(event) { self._stream.addListener(event, function() { self.close() }); }); diff --git a/lib/faye/websocket.js b/lib/faye/websocket.js index add1c79..943b2c0 100644 --- a/lib/faye/websocket.js +++ b/lib/faye/websocket.js @@ -37,9 +37,6 @@ var WebSocket = function(request, socket, head, supportedProtos, options) { this._ping = options && options.ping; this._pingId = 0; - this._stream.setTimeout(0); - this._stream.setNoDelay(true); - var scheme = isSecureConnection(request) ? 'wss:' : 'ws:'; this.url = scheme + '//' + request.headers.host + request.url; this.readyState = API.CONNECTING; @@ -53,8 +50,6 @@ var WebSocket = function(request, socket, head, supportedProtos, options) { process.nextTick(function() { self._open() }); var handshake = this._parser.handshakeResponse(head); - try { this._stream.write(handshake, 'binary') } catch (e) {} - if (this._parser.isOpen()) this.readyState = API.OPEN; if (this._ping) @@ -66,6 +61,13 @@ var WebSocket = function(request, socket, head, supportedProtos, options) { this.protocol = this._parser.protocol || ''; this.version = this._parser.getVersion(); + if (!this._stream || !this._stream.writable) return; + + this._stream.setTimeout(0); + this._stream.setNoDelay(true); + + try { this._stream.write(handshake, 'binary') } catch (e) {} + this._stream.addListener('data', function(data) { var response = self._parser.parse(data); if (!response) return; diff --git a/lib/faye/websocket/api.js b/lib/faye/websocket/api.js index 4108bfd..05a662b 100644 --- a/lib/faye/websocket/api.js +++ b/lib/faye/websocket/api.js @@ -63,7 +63,7 @@ var API = { var close = function() { this.readyState = API.CLOSED; if (this._pingLoop) clearInterval(this._pingLoop); - this._stream.end(); + if (this._stream) this._stream.end(); var event = new Event('close', {code: code || 1000, reason: reason || ''}); event.initEvent('close', false, false); this.dispatchEvent(event);