/*
node-http-proxy.js: http proxy for node.js
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Marak Squires, Fedor Indutny
@@ -407,44 +407,44 @@ @req {ServerRequest} Incoming HTTP Request to proxy.
}
}
- function onUpgrade ( out , reverseProxy ) {
- if ( ! out ) {
- reverseProxy . end ();
+ function onUpgrade ( reverseProxy , proxySocket ) {
+ if ( ! reverseProxy ) {
+ proxySocket . end ();
socket . end ();
return ;
}
var listeners = {};
- We're now connected to the server, so lets change server socket
reverseProxy . on ( 'data' , listeners . _r_data = function ( data ) { Pass data to client
if ( out . incoming . socket . writable ) {
+ We're now connected to the server, so lets change server socket
proxySocket . on ( 'data' , listeners . _r_data = function ( data ) { Pass data to client
if ( reverseProxy . incoming . socket . writable ) {
try {
- out . incoming . socket . write ( data );
+ reverseProxy . incoming . socket . write ( data );
}
catch ( e ) {
- out . incoming . socket . end ();
- reverseProxy . end ();
+ reverseProxy . incoming . socket . end ();
+ proxySocket . end ();
}
}
});
- out . incoming . socket . on ( 'data' , listeners . _data = function ( data ) { Pass data from client to server
try {
- reverseProxy . write ( data );
+ reverseProxy . incoming . socket . on ( 'data' , listeners . _data = function ( data ) { Pass data from client to server
try {
+ proxySocket . write ( data );
}
catch ( e ) {
- reverseProxy . end ();
+ proxySocket . end ();
socket . end ();
}
}); Detach event listeners from reverseProxy
function detach () {
- reverseProxy . removeListener ( 'close' , listeners . _r_close );
- reverseProxy . removeListener ( 'data' , listeners . _r_data );
- out . incoming . socket . removeListener ( 'data' , listeners . _data );
- out . incoming . socket . removeListener ( 'close' , listeners . _close );
- } Hook disconnections
reverseProxy . on ( 'end' , listeners . _r_close = function () {
- out . incoming . socket . end ();
+ proxySocket . removeListener ( 'end' , listeners . _r_close );
+ proxySocket . removeListener ( 'data' , listeners . _r_data );
+ reverseProxy . incoming . socket . removeListener ( 'data' , listeners . _data );
+ reverseProxy . incoming . socket . removeListener ( 'end' , listeners . _close );
+ } Hook disconnections
proxySocket . on ( 'end' , listeners . _r_close = function () {
+ reverseProxy . incoming . socket . end ();
detach ();
});
- socket . on ( 'end' , listeners . _close = function () {
- reverseProxy . end ();
+ reverseProxy . incoming . socket . on ( 'end' , listeners . _close = function () {
+ proxySocket . end ();
detach ();
});
}; Client socket
_socket ( socket );
@@ -461,11 +461,11 @@ @req {ServerRequest} Incoming HTTP Request to proxy.
method : 'GET' ,
path : req . url ,
headers : req . headers ,
- }; Make the outgoing WebSocket request
var request = agent . appendMessage ( outgoing ); Here we set the incoming req
, socket
and head
data to the outgoing
+ };
Make the outgoing WebSocket request
var reverseProxy = agent . appendMessage ( outgoing ); Here we set the incoming req
, socket
and head
data to the outgoing
request so that we can reuse this data later on in the closure scope
available to the upgrade
event. This bookkeeping is not tracked anywhere
-in nodejs core and is very specific to proxying WebSockets.
request . agent = agent ;
- request . incoming = {
+in nodejs core and is very specific to proxying WebSockets. reverseProxy . agent = agent ;
+ reverseProxy . incoming = {
request : req ,
socket : socket ,
head : head
@@ -476,37 +476,46 @@ @req {ServerRequest} Incoming HTTP Request to proxy.
In addition, it's important to note the closure scope here. Since
there is no mapping of the
if ( ! agent . _events || agent . _events [ 'upgrade' ]. length === 0 ) {
- agent . on ( 'upgrade' , function ( out , remoteSocket , head ) { Prepare socket
_socket ( remoteSocket , true );
- Emit event
onUpgrade ( remoteSocket . _httpMessage , remoteSocket );
+ agent . on ( 'upgrade' , function ( _ , remoteSocket , head ) { Prepare the socket for the reverseProxy request and begin to
+stream data between the two sockets
_socket ( remoteSocket , true );
+ onUpgrade ( remoteSocket . _httpMessage , remoteSocket );
});
}
-
- if ( typeof request . socket !== 'undefined' ) {
- request . socket . on ( 'data' , function handshake ( data ) { Handshaking
Ok, kind of harmfull part of code
-Socket.IO is sending hash at the end of handshake
-If protocol = 76
-But we need to replace 'host' and 'origin' in response
-So we split data to printable data and to non-printable
-(Non-printable will come after double-CRLF)
var sdata = data . toString (); Get Printable
sdata = sdata . substr ( 0 , sdata . search ( CRLF + CRLF )); Get Non-Printable
data = data . slice ( Buffer . byteLength ( sdata ), data . length ); Replace host and origin
sdata = sdata . replace ( remoteHost , options . host )
+ If the reverseProxy connection has an underlying socket,
+then behing the handshake.
if ( typeof reverseProxy . socket !== 'undefined' ) {
+ reverseProxy . socket . 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
+to replace 'host' and 'origin' in response so we split
+data to printable data and to non-printable. (Non-printable
+will come after double-CRLF).
var sdata = data . toString (); Get the Printable data
sdata = sdata . substr ( 0 , sdata . search ( CRLF + CRLF )); Get the Non-Printable data
data = data . slice ( Buffer . byteLength ( sdata ), data . length ); Replace the host and origin headers in the Printable data
sdata = sdata . replace ( remoteHost , options . host )
. replace ( remoteHost , options . host );
- try { Write printable
Write non-printable
socket . write ( data );
+ try { Write the printable and non-printable data to the socket
+from the original incoming request.
socket . write ( sdata );
+ socket . write ( data );
}
catch ( e ) {
- request . end ();
+ reverseProxy . end ();
+ socket . end ();
+ } Catch socket errors
socket . on ( 'error' , function () {
+ reverseProxy . end ();
socket . end ();
- } Catch socket errors
socket . on ( 'error' , function () {
- request . end ();
- }); Remove data listener now that the 'handshake' is complete
request . socket . removeListener ( 'data' , handshake );
+ }); Remove data listener now that the 'handshake' is complete
reverseProxy . socket . removeListener ( 'data' , handshake );
});
- } Write upgrade-head
try {
- request . write ( head );
+ }
+
+ reverseProxy . on ( 'error' , function ( err ) {
+ reverseProxy . end ();
+ socket . end ();
+ });
+
+ try { Attempt to write the upgrade-head to the reverseProxy request.
reverseProxy . write ( head );
}
catch ( ex ) {
- request . end ();
+ reverseProxy . end ();
socket . end ();
}
- If we have been passed buffered data, resume it.
if ( options . buffer && ! errState ) {
+ If we have been passed buffered data, resume it.
if ( options . buffer && ! errState ) {
options . buffer . resume ();
}
};
diff --git a/docs/proxy-table.html b/docs/proxy-table.html
index dac6eeae7..d03364740 100644
--- a/docs/proxy-table.html
+++ b/docs/proxy-table.html
@@ -1,5 +1,5 @@
- proxy-table.js
proxy-table.js /*
- proxy-table.js: Lookup table for proxy targets in node.js
+ proxy-table.js
proxy-table.js /*
+ node-http-proxy.js: Lookup table for proxy targets in node.js
Copyright (c) 2010 Charlie Robbins