Permalink
Browse files

Handle cases where res.write throws

  • Loading branch information...
1 parent 5d0bbb3 commit be3a0d84a1e75b45bc1fc63fe63cdabd9844eb59 @isaacs isaacs committed with indexzero Aug 27, 2011
Showing with 11 additions and 3 deletions.
  1. +11 −3 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();
}
-};
+};

0 comments on commit be3a0d8

Please sign in to comment.