From d55af578b454037c4224664f33e5e14f5054f2d2 Mon Sep 17 00:00:00 2001 From: Rowan Manning Date: Tue, 23 May 2017 12:17:55 +0100 Subject: [PATCH] Store the request object in proxy errors This would allow someone to differentiate between client errors and target server errors in their proxy error handling, by inspecting the error request property. This is a potential solution for #1017. --- lib/http-proxy/passes/web-incoming.js | 4 ++++ test/lib-http-proxy-passes-web-incoming-test.js | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/http-proxy/passes/web-incoming.js b/lib/http-proxy/passes/web-incoming.js index 5cb0b03d3..6b0f40df7 100644 --- a/lib/http-proxy/passes/web-incoming.js +++ b/lib/http-proxy/passes/web-incoming.js @@ -104,6 +104,7 @@ module.exports = { var forwardReq = (options.forward.protocol === 'https:' ? https : http).request( common.setupOutgoing(options.ssl || {}, options, req, 'forward') ); + forwardReq.proxyRequestType = 'forward'; // error handler (e.g. ECONNRESET, ECONNREFUSED) // Handle errors on incoming request as well as it makes sense to @@ -119,6 +120,7 @@ module.exports = { var proxyReq = (options.target.protocol === 'https:' ? https : http).request( common.setupOutgoing(options.ssl || {}, options, req) ); + proxyReq.proxyRequestType = 'target'; // Enable developers to modify the proxyReq before headers are sent proxyReq.on('socket', function(socket) { @@ -145,6 +147,8 @@ module.exports = { function createErrorHandler(proxyReq, url) { return function proxyError(err) { + err.request = proxyReq; + if (req.socket.destroyed && err.code === 'ECONNRESET') { server.emit('econnreset', err, req, res, url); return proxyReq.abort(); diff --git a/test/lib-http-proxy-passes-web-incoming-test.js b/test/lib-http-proxy-passes-web-incoming-test.js index 1996276d2..d0f219295 100644 --- a/test/lib-http-proxy-passes-web-incoming-test.js +++ b/test/lib-http-proxy-passes-web-incoming-test.js @@ -225,6 +225,10 @@ describe('#createProxyServer.web() using own http server', function () { expect(errRes).to.be.equal(res); expect(new Date().getTime() - started).to.be.greaterThan(99); expect(err.code).to.be('ECONNRESET'); + expect(err.request).to.be.an(Object); + expect(err.request.proxyRequestType).to.be('target'); + expect(err.request._headers.host).to.be('127.0.0.1:8084'); + expect(err.request.path).to.be('/'); done(); }); @@ -264,6 +268,10 @@ describe('#createProxyServer.web() using own http server', function () { expect(errReq).to.be.equal(req); expect(errRes).to.be.equal(res); expect(err.code).to.be('ECONNRESET'); + expect(err.request).to.be.an(Object); + expect(err.request.proxyRequestType).to.be('target'); + expect(err.request._headers.host).to.be('127.0.0.1:8085'); + expect(err.request.path).to.be('/'); doneOne(); });