25
25
*/
26
26
27
27
var events = require ( 'events' ) ,
28
+ http = require ( 'http' ) ,
28
29
util = require ( 'util' ) ,
29
30
httpProxy = require ( '../node-http-proxy' ) ;
30
31
@@ -606,11 +607,13 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
606
607
//
607
608
outgoing . host = this . target . host ;
608
609
outgoing . port = this . target . port ;
610
+ outgoing . agent = agent ;
609
611
outgoing . method = 'GET' ;
610
612
outgoing . path = req . url ;
611
613
outgoing . headers = req . headers ;
614
+ outgoing . agent = agent ;
612
615
613
- var reverseProxy = agent . appendMessage ( outgoing ) ;
616
+ var reverseProxy = this . target . protocol . request ( outgoing ) ;
614
617
615
618
//
616
619
// On any errors from the `reverseProxy` emit the
@@ -632,7 +635,6 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
632
635
// available to the `upgrade` event. This bookkeeping is not tracked anywhere
633
636
// in nodejs core and is **very** specific to proxying WebSockets.
634
637
//
635
- reverseProxy . agent = agent ;
636
638
reverseProxy . incoming = {
637
639
request : req ,
638
640
socket : socket ,
@@ -647,24 +649,22 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
647
649
// In addition, it's important to note the closure scope here. Since
648
650
// there is no mapping of the socket to the request bound to it.
649
651
//
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
+ } ) ;
661
661
662
662
//
663
663
// If the reverseProxy connection has an underlying socket,
664
664
// then execute the WebSocket handshake.
665
665
//
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 ) {
668
668
//
669
669
// Ok, kind of harmfull part of code. Socket.IO sends a hash
670
670
// at the end of handshake if protocol === 76, but we need
@@ -697,12 +697,12 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
697
697
socket . write ( sdata ) ;
698
698
var flushed = socket . write ( data ) ;
699
699
if ( ! flushed ) {
700
- reverseProxy . socket . pause ( ) ;
700
+ revSocket . pause ( ) ;
701
701
socket . once ( 'drain' , function ( ) {
702
- try { reverseProxy . socket . resume ( ) }
702
+ try { revSocket . resume ( ) }
703
703
catch ( er ) { console . error ( "reverseProxy.socket.resume error: %s" , er . message ) }
704
704
} ) ;
705
-
705
+
706
706
//
707
707
// Force the `drain` event in 100ms if it hasn't
708
708
// happened on its own.
@@ -717,7 +717,7 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
717
717
// Remove data listener on socket error because the
718
718
// 'handshake' has failed.
719
719
//
720
- reverseProxy . socket . removeListener ( 'data' , handshake ) ;
720
+ revSocket . removeListener ( 'data' , handshake ) ;
721
721
return proxyError ( ex ) ;
722
722
}
723
723
@@ -727,9 +727,9 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
727
727
//
728
728
// Remove data listener now that the 'handshake' is complete
729
729
//
730
- reverseProxy . socket . removeListener ( 'data' , handshake ) ;
730
+ revSocket . removeListener ( 'data' , handshake ) ;
731
731
} ) ;
732
- }
732
+ } ) ;
733
733
734
734
reverseProxy . on ( 'error' , proxyError ) ;
735
735
@@ -768,9 +768,11 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
768
768
HttpProxy . prototype . close = function ( ) {
769
769
[ this . forward , this . target ] . forEach ( function ( proxy ) {
770
770
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
+ }
774
776
}
775
777
} ) ;
776
778
} ;
0 commit comments