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

Support request timeouts > 2 minutes #372

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ If you have a suggestion for a feature currently not supported, feel free to ope
xforward: true // enables X-Forwarded-For
},
changeOrigin: false, // changes the origin of the host header to the target URL
timeout: 120000 // override the default 2 minute http socket timeout value in milliseconds
}
```

Expand Down
12 changes: 9 additions & 3 deletions lib/node-http-proxy/http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ var HttpProxy = exports.HttpProxy = function (options) {
//
this.forward = options.forward;
this.target = options.target;
this.timeout = options.timeout;

//
// Setup the necessary instances instance variables for
Expand Down Expand Up @@ -123,7 +124,6 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
errState = false,
outgoing = new(this.target.base),
reverseProxy;

//
// Add common proxy headers to the request so that they can
// be availible to the proxy target server. If the proxy is
Expand Down Expand Up @@ -158,6 +158,9 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
req.headers['x-forwarded-proto'] = getProto(req);
}
}
if(this.timeout) {
req.socket.setTimeout(this.timeout);
}

//
// Emit the `start` event indicating that we have begun the proxy operation.
Expand Down Expand Up @@ -309,13 +312,16 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
});

//
// Handle 'error' events from the `reverseProxy`.
// Handle 'error' events from the `reverseProxy`. Setup timeout override if needed
//
reverseProxy.once('error', proxyError);
reverseProxy.once('socket', function (socket) {
if(self.timeout) {
socket.setTimeout(self.timeout);
}
socket.once('error', proxyError);
});

//
// Handle 'error' events from the `req` (e.g. `Parse Error`).
//
Expand Down
9 changes: 5 additions & 4 deletions test/helpers/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ exports.createServer = function (options, callback) {
assert.equal(req.headers[key], options.headers[key]);
});
}

res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write(options.output || 'hello proxy');
res.end();
setTimeout(function() {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write(options.output || 'hello proxy');
res.end();
}, options.latency || 1);
}

var server = protocols.target === 'https'
Expand Down
5 changes: 5 additions & 0 deletions test/http/http-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ vows.describe(helpers.describe()).addBatch({
},
"and latency": macros.http.assertProxied({
latency: 2000
}),
"and timeout set": macros.http.assertProxied({
shouldRequestFail: true,
timeout: 2000,
requestLatency: 4000
})
},
"With a no valid target server": {
Expand Down
36 changes: 32 additions & 4 deletions test/macros/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ exports.assertRequest = function (options) {
};
};

//
// ### function assertFailedRequest (options)
// #### @options {Object} Options for this failed request assertion.
// #### @request {Object} Options to use for `request`.
// #### @assert {Object} Test assertions against the response.
//
// Makes a request using `options.request` and then asserts the response
// and body against anything in `options.assert`.
//
exports.assertFailedRequest = function (options) {
return {
topic: function () {
//
// Now make the HTTP request and assert.
//
options.request.rejectUnauthorized = false;
request(options.request, this.callback);
},
"should not succeed": function (err, res, body) {
assert.notStrictEqual(err,null);
}
};
};

//
// ### function assertProxied (options)
// #### @options {Object} Options for this test
Expand All @@ -60,7 +84,9 @@ exports.assertProxied = function (options) {
var ports = options.ports || helpers.nextPortPair,
output = options.output || 'hello world from ' + ports.target,
protocol = helpers.protocols.proxy,
req = options.request || {};
req = options.request || {},
shouldRequestFail = options.shouldRequestFail ? "assertFailedRequest" : "assertRequest",
timeout = options.timeout || null;

req.uri = req.uri || protocol + '://127.0.0.1:' + ports.proxy;

Expand All @@ -74,7 +100,8 @@ exports.assertProxied = function (options) {
target: {
output: output,
port: ports.target,
headers: req.headers
headers: req.headers,
latency: options.requestLatency
},
proxy: {
latency: options.latency,
Expand All @@ -85,12 +112,13 @@ exports.assertProxied = function (options) {
https: helpers.protocols.target === 'https',
host: '127.0.0.1',
port: ports.target
}
},
timeout: timeout
}
}
}, this.callback);
},
"the proxy request": exports.assertRequest({
"the proxy request": exports[shouldRequestFail]({
request: req,
assert: {
body: output
Expand Down