Skip to content

Commit 77a1cff

Browse files
committed
[fix] handle error on incoming request as well and properly abort proxy if client request is aborted
1 parent d908e2a commit 77a1cff

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

lib/http-proxy/passes/web-incoming.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ web_o = Object.keys(web_o).map(function(pass) {
9191

9292
function stream(req, res, options, _, server, clb) {
9393

94-
//
9594
// And we begin!
96-
//
9795
if (!clb) {
9896
server.emit('start', req, res, options.target)
9997
}
@@ -111,14 +109,24 @@ web_o = Object.keys(web_o).map(function(pass) {
111109
common.setupOutgoing(options.ssl || {}, options, req)
112110
);
113111

112+
// Ensure we abort proxy if request is aborted
113+
req.on('aborted', function () {
114+
proxyReq.abort();
115+
});
116+
117+
// Handle errors on incoming request as well as it makes sense to
118+
req.on('error', proxyError);
119+
114120
// Error Handler
115-
proxyReq.on('error', function(err){
121+
proxyReq.on('error', proxyError);
122+
123+
function proxyError (err){
116124
if (clb) {
117125
clb(err, req, res);
118126
} else {
119127
server.emit('error', err, req, res);
120128
}
121-
});
129+
}
122130

123131
(options.buffer || req).pipe(proxyReq);
124132

@@ -128,9 +136,7 @@ web_o = Object.keys(web_o).map(function(pass) {
128136
if(web_o[i](req, res, proxyRes)) { break; }
129137
}
130138

131-
//
132139
// Allow us to listen when the proxy has completed
133-
//
134140
proxyRes.on('end', function () {
135141
if (!clb) {
136142
server.emit('end', req, res, proxyRes);

0 commit comments

Comments
 (0)