Skip to content

Commit 7bda25b

Browse files
committed
Add guards to every throw-able res.end call
1 parent 62201a0 commit 7bda25b

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

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

+27-6
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,13 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) {
404404
// then respond with `404` since we do not have a valid proxy target.
405405
//
406406
if (!location) {
407-
res.writeHead(404);
408-
return res.end();
407+
try {
408+
res.writeHead(404);
409+
res.end();
410+
} catch (er) {
411+
console.error("res.writeHead/res.end error: %s", er.message);
412+
}
413+
return;
409414
}
410415

411416
//
@@ -480,7 +485,11 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) {
480485
}
481486
}
482487

483-
res.end();
488+
try {
489+
res.end();
490+
} catch (er) {
491+
console.error("res.end error: %s", er.message);
492+
}
484493
}
485494

486495
outgoing = {
@@ -508,7 +517,12 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) {
508517

509518
// `response.statusCode === 304`: No 'data' event and no 'end'
510519
if (response.statusCode === 304) {
511-
return res.end();
520+
try {
521+
res.end();
522+
} catch (er) {
523+
console.error("res.end error: %s", er.message)
524+
}
525+
return;
512526
}
513527

514528
// For each data `chunk` received from the `reverseProxy`
@@ -520,9 +534,12 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) {
520534
try {
521535
res.write(chunk);
522536
} catch (er) {
537+
console.error("res.write error: %s", er.message);
523538
try {
524539
res.end();
525-
} catch (er) {}
540+
} catch (er) {
541+
console.error("res.end error: %s", er.message);
542+
}
526543
}
527544
}
528545
});
@@ -535,7 +552,11 @@ HttpProxy.prototype.proxyRequest = function (req, res, options) {
535552
response.on('end', function () {
536553
if (!errState) {
537554
reverseProxy.removeListener('error', proxyError);
538-
res.end();
555+
try {
556+
res.end();
557+
} catch (er) {
558+
console.error("res.end error: %s", er.message);
559+
}
539560

540561
// Emit the `end` event now that we have completed proxying
541562
self.emit('end', req, res);

0 commit comments

Comments
 (0)