Skip to content

Instantly share code, notes, and snippets.

@stellaraccident
Created February 14, 2011 07:29
Show Gist options
  • Save stellaraccident/825591 to your computer and use it in GitHub Desktop.
Save stellaraccident/825591 to your computer and use it in GitHub Desktop.
diff --git a/lib/http.js b/lib/http.js
index 9506a8c..73b507e 100644
--- a/lib/http.js
+++ b/lib/http.js
@@ -884,8 +884,18 @@ function httpSocketSetup(socket) {
}
-function Server(requestListener) {
- if (!(this instanceof Server)) return new Server(requestListener);
+function Server(options, requestListener) {
+ if (!(this instanceof Server)) return new Server(options, requestListener);
+
+ // Detect arguments. Both options and requestListener are optional.
+ if (typeof arguments[0] == 'object') {
+ options = arguments[0];
+ requestListener = arguments[1];
+ } else if (typeof arguments[0] == 'function') {
+ options = {};
+ requestListener = arguments[0];
+ }
+
net.Server.call(this, { allowHalfOpen: true });
if (requestListener) {
@@ -898,15 +908,29 @@ function Server(requestListener) {
this.httpAllowHalfOpen = false;
this.addListener('connection', connectionListener);
+
+ this.setOptions(options||{});
}
util.inherits(Server, net.Server);
+Server.prototype.setOptions=function(options) {
+ if (typeof options.timeout === 'number') {
+ this.timeout = options.timeout;
+ } else {
+ // Default 2 minute timeout
+ this.timeout = 120 * 60 * 1000;
+ }
+};
+
+Server.prototype.setTimeout=function(timeout) {
+ this.timeout = timeout;
+};
exports.Server = Server;
-exports.createServer = function(requestListener) {
- return new Server(requestListener);
+exports.createServer = function(options, requestListener) {
+ return new Server(options, requestListener);
};
@@ -914,6 +938,7 @@ function connectionListener(socket) {
var self = this;
var outgoing = [];
var incoming = [];
+ var defaultTimeout = self.timeout;
function abortIncoming() {
while (incoming.length) {
@@ -926,11 +951,13 @@ function connectionListener(socket) {
debug('SERVER new http connection');
httpSocketSetup(socket);
-
- socket.setTimeout(2 * 60 * 1000); // 2 minute timeout
- socket.addListener('timeout', function() {
- socket.destroy();
- });
+
+ if (defaultTimeout>0) {
+ socket.setTimeout(defaultTimeout); // 2 minute timeout
+ socket.addListener('timeout', function() {
+ socket.destroy();
+ });
+ }
var parser = parsers.alloc();
parser.reinitialize('request');
diff --git a/lib/https.js b/lib/https.js
index 141eff7..03f3029 100644
--- a/lib/https.js
+++ b/lib/https.js
@@ -15,6 +15,21 @@ function Server(opts, requestListener) {
}
inherits(Server, tls.Server);
+Server.prototype.setOptions = function(options) {
+ // Call super
+ tls.Server.prototype.setOptions.call(this, options);
+
+ if (typeof options.timeout === 'number') {
+ this.timeout = options.timeout;
+ } else {
+ // Default 2 minute timeout
+ this.timeout = 120 * 60 * 1000;
+ }
+};
+
+Server.prototype.setTimeout=function(timeout) {
+ this.timeout = timeout;
+};
exports.Server = Server;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment