Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store the request object in proxy errors #1172

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/http-proxy/passes/web-incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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();
Expand Down
8 changes: 8 additions & 0 deletions test/lib-http-proxy-passes-web-incoming-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down Expand Up @@ -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();
});

Expand Down