Skip to content

Commit be3a0d8

Browse files
isaacsindexzero
authored andcommitted
Handle cases where res.write throws
1 parent 5d0bbb3 commit be3a0d8

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

Diff for: lib/node-http-proxy.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -513,9 +513,17 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) {
513513

514514
// For each data `chunk` received from the `reverseProxy`
515515
// `response` write it to the outgoing `res`.
516+
// If the res socket has been killed already, then write()
517+
// will throw. Nevertheless, try our best to end it nicely.
516518
response.on('data', function (chunk) {
517-
if (req.method !== 'HEAD') {
518-
res.write(chunk);
519+
if (req.method !== 'HEAD' && res.writable) {
520+
try {
521+
res.write(chunk);
522+
} catch (er) {
523+
try {
524+
res.end();
525+
} catch (er) {}
526+
}
519527
}
520528
});
521529

@@ -909,4 +917,4 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options
909917
if (options.buffer && !errState) {
910918
options.buffer.resume();
911919
}
912-
};
920+
};

0 commit comments

Comments
 (0)