Merge branch '0.6-compatibility'

This commit is contained in:
indexzero 2011-12-23 01:34:02 -05:00
commit 6f8ad3b8c3
5 changed files with 136 additions and 136 deletions

View File

@ -24,7 +24,7 @@
*/
var sys = require('sys'),
var util = require('util'),
http = require('http'),
colors = require('colors'),
websocket = require('../../vendor/websocket'),
@ -55,10 +55,10 @@ server.listen(8080);
//
var socket = io.listen(server);
socket.on('connection', function (client) {
sys.debug('Got websocket connection');
util.debug('Got websocket connection');
client.on('message', function (msg) {
sys.debug('Got message from client: ' + msg);
util.debug('Got message from client: ' + msg);
});
socket.broadcast('from server');
@ -101,5 +101,5 @@ ws.on('open', function () {
});
ws.on('message', function (msg) {
sys.debug('Got message: ' + utils.decode(msg));
util.debug('Got message: ' + utils.decode(msg));
});

View File

@ -24,7 +24,7 @@
*/
var sys = require('sys'),
var util = require('util'),
http = require('http'),
colors = require('colors'),
websocket = require('../../vendor/websocket'),
@ -55,10 +55,10 @@ server.listen(8080);
//
var socket = io.listen(server);
socket.on('connection', function (client) {
sys.debug('Got websocket connection');
util.debug('Got websocket connection');
client.on('message', function (msg) {
sys.debug('Got message from client: ' + msg);
util.debug('Got message from client: ' + msg);
});
socket.broadcast('from server');
@ -97,5 +97,5 @@ ws.on('open', function () {
});
ws.on('message', function (msg) {
sys.debug('Got message: ' + utils.decode(msg));
util.debug('Got message: ' + utils.decode(msg));
});

View File

@ -24,7 +24,7 @@
*/
var sys = require('sys'),
var util = require('util'),
http = require('http'),
colors = require('colors'),
websocket = require('../../vendor/websocket'),
@ -55,10 +55,10 @@ server.listen(8080);
//
var socket = io.listen(server);
socket.on('connection', function (client) {
sys.debug('Got websocket connection');
util.debug('Got websocket connection');
client.on('message', function (msg) {
sys.debug('Got message from client: ' + msg);
util.debug('Got message from client: ' + msg);
});
socket.broadcast('from server');
@ -80,5 +80,5 @@ ws.on('open', function () {
});
ws.on('message', function (msg) {
sys.debug('Got message: ' + utils.decode(msg));
util.debug('Got message: ' + utils.decode(msg));
});

View File

@ -25,6 +25,7 @@
*/
var events = require('events'),
http = require('http'),
util = require('util'),
httpProxy = require('../node-http-proxy');
@ -606,11 +607,13 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
//
outgoing.host = this.target.host;
outgoing.port = this.target.port;
outgoing.agent = agent;
outgoing.method = 'GET';
outgoing.path = req.url;
outgoing.headers = req.headers;
outgoing.agent = agent;
var reverseProxy = agent.appendMessage(outgoing);
var reverseProxy = this.target.protocol.request(outgoing);
//
// On any errors from the `reverseProxy` emit the
@ -632,7 +635,6 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
// available to the `upgrade` event. This bookkeeping is not tracked anywhere
// in nodejs core and is **very** specific to proxying WebSockets.
//
reverseProxy.agent = agent;
reverseProxy.incoming = {
request: req,
socket: socket,
@ -647,24 +649,22 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
// In addition, it's important to note the closure scope here. Since
// there is no mapping of the socket to the request bound to it.
//
if (!agent._events || agent._events['upgrade'].length === 0) {
agent.on('upgrade', function (_, remoteSocket, head) {
//
// Prepare the socket for the reverseProxy request and begin to
// stream data between the two sockets. Here it is important to
// note that `remoteSocket._httpMessage === reverseProxy`.
//
_socket(remoteSocket, true);
onUpgrade(remoteSocket._httpMessage, remoteSocket);
});
}
reverseProxy.on('upgrade', function (_, remoteSocket, head) {
//
// Prepare the socket for the reverseProxy request and begin to
// stream data between the two sockets. Here it is important to
// note that `remoteSocket._httpMessage === reverseProxy`.
//
_socket(remoteSocket, true);
onUpgrade(remoteSocket._httpMessage, remoteSocket);
});
//
// If the reverseProxy connection has an underlying socket,
// then execute the WebSocket handshake.
//
if (typeof reverseProxy.socket !== 'undefined') {
reverseProxy.socket.on('data', function handshake (data) {
reverseProxy.once('socket', function (revSocket) {
revSocket.on('data', function handshake (data) {
//
// Ok, kind of harmfull part of code. Socket.IO sends a hash
// at the end of handshake if protocol === 76, but we need
@ -697,12 +697,12 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
socket.write(sdata);
var flushed = socket.write(data);
if (!flushed) {
reverseProxy.socket.pause();
revSocket.pause();
socket.once('drain', function () {
try { reverseProxy.socket.resume() }
try { revSocket.resume() }
catch (er) { console.error("reverseProxy.socket.resume error: %s", er.message) }
});
//
// Force the `drain` event in 100ms if it hasn't
// happened on its own.
@ -717,7 +717,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
// Remove data listener on socket error because the
// 'handshake' has failed.
//
reverseProxy.socket.removeListener('data', handshake);
revSocket.removeListener('data', handshake);
return proxyError(ex);
}
@ -727,9 +727,9 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
//
// Remove data listener now that the 'handshake' is complete
//
reverseProxy.socket.removeListener('data', handshake);
revSocket.removeListener('data', handshake);
});
}
});
reverseProxy.on('error', proxyError);
@ -768,9 +768,11 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
HttpProxy.prototype.close = function () {
[this.forward, this.target].forEach(function (proxy) {
if (proxy && proxy.agent) {
proxy.agent.sockets.forEach(function (socket) {
socket.end();
});
for (var host in proxy.agent.sockets) {
proxy.agent.sockets[host].forEach(function (socket) {
socket.end();
});
}
}
});
};

198
vendor/websocket.js vendored
View File

@ -496,7 +496,7 @@ var WebSocket = function(url, proto, opts) {
if (u.protocol === 'ws:' || u.protocol === 'wss:') {
protocol = u.protocol === 'ws:' ? http : https;
port = u.protocol === 'ws:' ? 80 : 443;
agent = u.protocol === 'ws:' ? protocol.getAgent(u.hostname, u.port || port) : protocol.getAgent({
agent = u.protocol === new protocol.Agent({
host: u.hostname,
port: u.port || port
});
@ -514,105 +514,6 @@ var WebSocket = function(url, proto, opts) {
throw new Error('Invalid URL scheme \'' + urlScheme + '\' specified.');
}
if (!agent._events || agent._events['upgrade'].length === 0) {
agent.on('upgrade', (function() {
var data = undefined;
return function(res, s, head) {
stream = s;
//
// Emit the `wsupgrade` event to inspect the raw
// arguments returned from the websocket request.
//
self.emit('wsupgrade', httpHeaders, res, s, head);
stream.on('data', function(d) {
if (d.length <= 0) {
return;
}
if (!data) {
data = d;
} else {
var data2 = new buffer.Buffer(data.length + d.length);
data.copy(data2, 0, 0, data.length);
d.copy(data2, data.length, 0, d.length);
data = data2;
}
if (data.length >= 16) {
var expected = computeSecretKeySignature(key1, key2, challenge);
var actual = data.slice(0, 16).toString('binary');
// Handshaking fails; we're donezo
if (actual != expected) {
debug(
'expected=\'' + str2hex(expected) + '\'; ' +
'actual=\'' + str2hex(actual) + '\''
);
process.nextTick(function() {
// N.B. Emit 'wserror' here, as 'error' is a reserved word in the
// EventEmitter world, and gets thrown.
self.emit(
'wserror',
new Error('Invalid handshake from server:' +
'expected \'' + str2hex(expected) + '\', ' +
'actual \'' + str2hex(actual) + '\''
)
);
if (self.onerror) {
self.onerror();
}
finishClose();
});
}
//
// Un-register our data handler and add the one to be used
// for the normal, non-handshaking case. If we have extra
// data left over, manually fire off the handler on
// whatever remains.
//
stream.removeAllListeners('data');
stream.on('data', dataListener);
readyState = OPEN;
process.nextTick(function() {
self.emit('open');
if (self.onopen) {
self.onopen();
}
});
// Consume any leftover data
if (data.length > 16) {
stream.emit('data', data.slice(16, data.length));
}
}
});
stream.on('fd', fdListener);
stream.on('error', errorListener);
stream.on('close', function() {
errorListener(new Error('Stream closed unexpectedly.'));
});
stream.emit('data', head);
};
})());
}
agent.on('error', function (e) {
errorListener(e);
});
var httpReq = protocol.request({
host: u.hostname,
method: 'GET',
@ -622,6 +523,103 @@ var WebSocket = function(url, proto, opts) {
headers: httpHeaders
});
httpReq.on('error', function (e) {
errorListener(e);
});
httpReq.on('upgrade', (function() {
var data = undefined;
return function(res, s, head) {
stream = s;
//
// Emit the `wsupgrade` event to inspect the raw
// arguments returned from the websocket request.
//
self.emit('wsupgrade', httpHeaders, res, s, head);
stream.on('data', function(d) {
if (d.length <= 0) {
return;
}
if (!data) {
data = d;
} else {
var data2 = new buffer.Buffer(data.length + d.length);
data.copy(data2, 0, 0, data.length);
d.copy(data2, data.length, 0, d.length);
data = data2;
}
if (data.length >= 16) {
var expected = computeSecretKeySignature(key1, key2, challenge);
var actual = data.slice(0, 16).toString('binary');
// Handshaking fails; we're donezo
if (actual != expected) {
debug(
'expected=\'' + str2hex(expected) + '\'; ' +
'actual=\'' + str2hex(actual) + '\''
);
process.nextTick(function() {
// N.B. Emit 'wserror' here, as 'error' is a reserved word in the
// EventEmitter world, and gets thrown.
self.emit(
'wserror',
new Error('Invalid handshake from server:' +
'expected \'' + str2hex(expected) + '\', ' +
'actual \'' + str2hex(actual) + '\''
)
);
if (self.onerror) {
self.onerror();
}
finishClose();
});
}
//
// Un-register our data handler and add the one to be used
// for the normal, non-handshaking case. If we have extra
// data left over, manually fire off the handler on
// whatever remains.
//
stream.removeAllListeners('data');
stream.on('data', dataListener);
readyState = OPEN;
process.nextTick(function() {
self.emit('open');
if (self.onopen) {
self.onopen();
}
});
// Consume any leftover data
if (data.length > 16) {
stream.emit('data', data.slice(16, data.length));
}
}
});
stream.on('fd', fdListener);
stream.on('error', errorListener);
stream.on('close', function() {
errorListener(new Error('Stream closed unexpectedly.'));
});
stream.emit('data', head);
};
})());
httpReq.write(challenge, 'binary');
httpReq.end();
})();