Check that incoming requests have an output stream before doing anything with it.

This commit is contained in:
James Coglan
2012-07-08 20:30:53 +01:00
parent 59fd729a03
commit d071ec3acd
3 changed files with 15 additions and 13 deletions
+7 -7
View File
@@ -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() });
});
+7 -5
View File
@@ -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;
+1 -1
View File
@@ -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);