From be3a0d84a1e75b45bc1fc63fe63cdabd9844eb59 Mon Sep 17 00:00:00 2001 From: isaacs Date: Sat, 27 Aug 2011 11:52:05 -0700 Subject: [PATCH] Handle cases where res.write throws --- lib/node-http-proxy.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/node-http-proxy.js b/lib/node-http-proxy.js index 6aeee31..eb72268 100644 --- a/lib/node-http-proxy.js +++ b/lib/node-http-proxy.js @@ -513,9 +513,17 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) { // For each data `chunk` received from the `reverseProxy` // `response` write it to the outgoing `res`. + // If the res socket has been killed already, then write() + // will throw. Nevertheless, try our best to end it nicely. response.on('data', function (chunk) { - if (req.method !== 'HEAD') { - res.write(chunk); + if (req.method !== 'HEAD' && res.writable) { + try { + res.write(chunk); + } catch (er) { + try { + res.end(); + } catch (er) {} + } } }); @@ -909,4 +917,4 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options if (options.buffer && !errState) { options.buffer.resume(); } -}; \ No newline at end of file +};