From 014e9a11f50b1fb0e03c3b9a4583701c5b2af18f Mon Sep 17 00:00:00 2001 From: Aral Balkan Date: Fri, 24 Jan 2020 20:37:55 +0000 Subject: [PATCH 1/2] Do not carry out proxy-related web output if the response is 404 --- lib/http-proxy/passes/web-incoming.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/http-proxy/passes/web-incoming.js b/lib/http-proxy/passes/web-incoming.js index 781b32692..ad13d689a 100644 --- a/lib/http-proxy/passes/web-incoming.js +++ b/lib/http-proxy/passes/web-incoming.js @@ -170,7 +170,7 @@ module.exports = { proxyReq.on('response', function(proxyRes) { if(server) { server.emit('proxyRes', proxyRes, req, res); } - if(!res.headersSent && !options.selfHandleResponse) { + if(!res.headersSent && !options.selfHandleResponse && (proxyRes.statusCode !== 404)) { for(var i=0; i < web_o.length; i++) { if(web_o[i](req, res, proxyRes, options)) { break; } } @@ -182,7 +182,9 @@ module.exports = { if (server) server.emit('end', req, res, proxyRes); }); // We pipe to the response unless its expected to be handled by the user - if (!options.selfHandleResponse) proxyRes.pipe(res); + if (!options.selfHandleResponse && (proxyRes.statusCode !== 404)) { + proxyRes.pipe(res); + } } else { if (server) server.emit('end', req, res, proxyRes); } From 435806c127c3141c752bc287eed31d8d9ae6c6a6 Mon Sep 17 00:00:00 2001 From: Aral Balkan Date: Sun, 26 Jan 2020 23:15:02 +0000 Subject: [PATCH 2/2] Skip entire default proxyRes behaviour if fallthrough is requested --- lib/http-proxy/passes/web-incoming.js | 28 ++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/http-proxy/passes/web-incoming.js b/lib/http-proxy/passes/web-incoming.js index ad13d689a..8c4caabd4 100644 --- a/lib/http-proxy/passes/web-incoming.js +++ b/lib/http-proxy/passes/web-incoming.js @@ -170,23 +170,25 @@ module.exports = { proxyReq.on('response', function(proxyRes) { if(server) { server.emit('proxyRes', proxyRes, req, res); } - if(!res.headersSent && !options.selfHandleResponse && (proxyRes.statusCode !== 404)) { - for(var i=0; i < web_o.length; i++) { - if(web_o[i](req, res, proxyRes, options)) { break; } + if (!(options.fallthrough === true && proxyRes.statusCode === 404)) { + if(!res.headersSent && !options.selfHandleResponse) { + for(var i=0; i < web_o.length; i++) { + if(web_o[i](req, res, proxyRes, options)) { break; } + } } - } - if (!res.finished) { - // Allow us to listen when the proxy has completed - proxyRes.on('end', function () { + if (!res.finished) { + // Allow us to listen when the proxy has completed + proxyRes.on('end', function () { + if (server) server.emit('end', req, res, proxyRes); + }); + // We pipe to the response unless its expected to be handled by the user + if (!options.selfHandleResponse) { + proxyRes.pipe(res); + } + } else { if (server) server.emit('end', req, res, proxyRes); - }); - // We pipe to the response unless its expected to be handled by the user - if (!options.selfHandleResponse && (proxyRes.statusCode !== 404)) { - proxyRes.pipe(res); } - } else { - if (server) server.emit('end', req, res, proxyRes); } }); }