Skip to content

Commit 6f8ad3b

Browse files
committed
Merge branch '0.6-compatibility'
2 parents 8358ef8 + 3828616 commit 6f8ad3b

File tree

5 files changed

+129
-129
lines changed

5 files changed

+129
-129
lines changed

examples/websocket/latent-websocket-proxy.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
2525
*/
2626

27-
var sys = require('sys'),
27+
var util = require('util'),
2828
http = require('http'),
2929
colors = require('colors'),
3030
websocket = require('../../vendor/websocket'),
@@ -55,10 +55,10 @@ server.listen(8080);
5555
//
5656
var socket = io.listen(server);
5757
socket.on('connection', function (client) {
58-
sys.debug('Got websocket connection');
58+
util.debug('Got websocket connection');
5959

6060
client.on('message', function (msg) {
61-
sys.debug('Got message from client: ' + msg);
61+
util.debug('Got message from client: ' + msg);
6262
});
6363

6464
socket.broadcast('from server');
@@ -101,5 +101,5 @@ ws.on('open', function () {
101101
});
102102

103103
ws.on('message', function (msg) {
104-
sys.debug('Got message: ' + utils.decode(msg));
104+
util.debug('Got message: ' + utils.decode(msg));
105105
});

examples/websocket/standalone-websocket-proxy.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
2525
*/
2626

27-
var sys = require('sys'),
27+
var util = require('util'),
2828
http = require('http'),
2929
colors = require('colors'),
3030
websocket = require('../../vendor/websocket'),
@@ -55,10 +55,10 @@ server.listen(8080);
5555
//
5656
var socket = io.listen(server);
5757
socket.on('connection', function (client) {
58-
sys.debug('Got websocket connection');
58+
util.debug('Got websocket connection');
5959

6060
client.on('message', function (msg) {
61-
sys.debug('Got message from client: ' + msg);
61+
util.debug('Got message from client: ' + msg);
6262
});
6363

6464
socket.broadcast('from server');
@@ -97,5 +97,5 @@ ws.on('open', function () {
9797
});
9898

9999
ws.on('message', function (msg) {
100-
sys.debug('Got message: ' + utils.decode(msg));
100+
util.debug('Got message: ' + utils.decode(msg));
101101
});

examples/websocket/websocket-proxy.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
2525
*/
2626

27-
var sys = require('sys'),
27+
var util = require('util'),
2828
http = require('http'),
2929
colors = require('colors'),
3030
websocket = require('../../vendor/websocket'),
@@ -55,10 +55,10 @@ server.listen(8080);
5555
//
5656
var socket = io.listen(server);
5757
socket.on('connection', function (client) {
58-
sys.debug('Got websocket connection');
58+
util.debug('Got websocket connection');
5959

6060
client.on('message', function (msg) {
61-
sys.debug('Got message from client: ' + msg);
61+
util.debug('Got message from client: ' + msg);
6262
});
6363

6464
socket.broadcast('from server');
@@ -80,5 +80,5 @@ ws.on('open', function () {
8080
});
8181

8282
ws.on('message', function (msg) {
83-
sys.debug('Got message: ' + utils.decode(msg));
83+
util.debug('Got message: ' + utils.decode(msg));
8484
});

lib/node-http-proxy/http-proxy.js

+26-24
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
var events = require('events'),
28+
http = require('http'),
2829
util = require('util'),
2930
httpProxy = require('../node-http-proxy');
3031

@@ -606,11 +607,13 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
606607
//
607608
outgoing.host = this.target.host;
608609
outgoing.port = this.target.port;
610+
outgoing.agent = agent;
609611
outgoing.method = 'GET';
610612
outgoing.path = req.url;
611613
outgoing.headers = req.headers;
614+
outgoing.agent = agent;
612615

613-
var reverseProxy = agent.appendMessage(outgoing);
616+
var reverseProxy = this.target.protocol.request(outgoing);
614617

615618
//
616619
// On any errors from the `reverseProxy` emit the
@@ -632,7 +635,6 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
632635
// available to the `upgrade` event. This bookkeeping is not tracked anywhere
633636
// in nodejs core and is **very** specific to proxying WebSockets.
634637
//
635-
reverseProxy.agent = agent;
636638
reverseProxy.incoming = {
637639
request: req,
638640
socket: socket,
@@ -647,24 +649,22 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
647649
// In addition, it's important to note the closure scope here. Since
648650
// there is no mapping of the socket to the request bound to it.
649651
//
650-
if (!agent._events || agent._events['upgrade'].length === 0) {
651-
agent.on('upgrade', function (_, remoteSocket, head) {
652-
//
653-
// Prepare the socket for the reverseProxy request and begin to
654-
// stream data between the two sockets. Here it is important to
655-
// note that `remoteSocket._httpMessage === reverseProxy`.
656-
//
657-
_socket(remoteSocket, true);
658-
onUpgrade(remoteSocket._httpMessage, remoteSocket);
659-
});
660-
}
652+
reverseProxy.on('upgrade', function (_, remoteSocket, head) {
653+
//
654+
// Prepare the socket for the reverseProxy request and begin to
655+
// stream data between the two sockets. Here it is important to
656+
// note that `remoteSocket._httpMessage === reverseProxy`.
657+
//
658+
_socket(remoteSocket, true);
659+
onUpgrade(remoteSocket._httpMessage, remoteSocket);
660+
});
661661

662662
//
663663
// If the reverseProxy connection has an underlying socket,
664664
// then execute the WebSocket handshake.
665665
//
666-
if (typeof reverseProxy.socket !== 'undefined') {
667-
reverseProxy.socket.on('data', function handshake (data) {
666+
reverseProxy.once('socket', function (revSocket) {
667+
revSocket.on('data', function handshake (data) {
668668
//
669669
// Ok, kind of harmfull part of code. Socket.IO sends a hash
670670
// at the end of handshake if protocol === 76, but we need
@@ -697,12 +697,12 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
697697
socket.write(sdata);
698698
var flushed = socket.write(data);
699699
if (!flushed) {
700-
reverseProxy.socket.pause();
700+
revSocket.pause();
701701
socket.once('drain', function () {
702-
try { reverseProxy.socket.resume() }
702+
try { revSocket.resume() }
703703
catch (er) { console.error("reverseProxy.socket.resume error: %s", er.message) }
704704
});
705-
705+
706706
//
707707
// Force the `drain` event in 100ms if it hasn't
708708
// happened on its own.
@@ -717,7 +717,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
717717
// Remove data listener on socket error because the
718718
// 'handshake' has failed.
719719
//
720-
reverseProxy.socket.removeListener('data', handshake);
720+
revSocket.removeListener('data', handshake);
721721
return proxyError(ex);
722722
}
723723

@@ -727,9 +727,9 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
727727
//
728728
// Remove data listener now that the 'handshake' is complete
729729
//
730-
reverseProxy.socket.removeListener('data', handshake);
730+
revSocket.removeListener('data', handshake);
731731
});
732-
}
732+
});
733733

734734
reverseProxy.on('error', proxyError);
735735

@@ -768,9 +768,11 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
768768
HttpProxy.prototype.close = function () {
769769
[this.forward, this.target].forEach(function (proxy) {
770770
if (proxy && proxy.agent) {
771-
proxy.agent.sockets.forEach(function (socket) {
772-
socket.end();
773-
});
771+
for (var host in proxy.agent.sockets) {
772+
proxy.agent.sockets[host].forEach(function (socket) {
773+
socket.end();
774+
});
775+
}
774776
}
775777
});
776778
};

0 commit comments

Comments
 (0)