From cd78af5feaa67c5005df921a8d1a61575a58fca2 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Wed, 15 Sep 2010 14:19:10 +0700 Subject: [PATCH] 'end' event becomes 'close', added more try-catch handling --- lib/node-http-proxy.js | 49 +++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/lib/node-http-proxy.js b/lib/node-http-proxy.js index 2c6c386..15eec29 100644 --- a/lib/node-http-proxy.js +++ b/lib/node-http-proxy.js @@ -315,22 +315,36 @@ HttpProxy.prototype = { sdata = sdata .replace(remote_host, host) .replace(remote_host, host); + try { + // Write printable + socket.write(sdata); - // Write printable - socket.write(sdata); - - // Write non-printable - socket.write(data); - + // Write non-printable + socket.write(data); + } catch (e) { + request.end(); + socket.end(); + } + + // Catch socket errors + socket.on('error', function() { + request.end(); + }); + // Remove data listener request.socket.removeListener('data', t); }); // Write upgrade-head - request.write(head); + try { + request.write(head); + } catch(e) { + request.end(); + socket.end(); + } self.unwatch(socket); }); - + // Request function onUpgrade(reverse_proxy) { @@ -340,14 +354,23 @@ HttpProxy.prototype = { reverse_proxy.on('data', listeners._r_data = function(data) { // Pass data to client if (socket.writable) { - socket.write(data); + try { + socket.write(data); + } catch (e) { + socket.end(); + reverse_proxy.end(); + } } }); socket.on('data', listeners._data = function(data){ // Pass data from client to server - // Socket thougth that it isn't writable - reverse_proxy.write(data); + try { + reverse_proxy.write(data); + } catch (e) { + reverse_proxy.end(); + socket.end(); + } }); // Detach event listeners from reverse_proxy @@ -359,12 +382,12 @@ HttpProxy.prototype = { } // Hook disconnections - reverse_proxy.on('close', listeners._r_close = function() { + reverse_proxy.on('end', listeners._r_close = function() { socket.end(); detach(); }); - socket.on('close', listeners._close = function() { + socket.on('end', listeners._close = function() { reverse_proxy.end(); detach(); });