Skip to content

Commit 171e2ba

Browse files
Fix timeout flag behavior (#826)
* Fix timeout flag behavior for 0 value, negative value, and empty value. Add timeout flag to README. * add docs --------- Co-authored-by: Eric Dubé <[email protected]>
1 parent 26a12e0 commit 171e2ba

File tree

4 files changed

+34
-29
lines changed

4 files changed

+34
-29
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ with the provided Dockerfile.
7676
|`-H` or `--header` |Add an extra response header (can be used several times) | |
7777
|`-o [path]` |Open browser window after starting the server. Optionally provide a URL path to open. e.g.: -o /other/dir/ | |
7878
|`-c` |Set cache time (in seconds) for cache-control max-age header, e.g. `-c10` for 10 seconds. To disable caching, use `-c-1`.|`3600` |
79+
|`-t` |Connection timeout in seconds, e.g. `-t60` for 1 minute. To disable timeout, use `-t0`.|`120` |
7980
|`-U` or `--utc` |Use UTC time format in log messages.| |
8081
|`--log-ip` |Enable logging of the client's IP address |`false` |
8182
|`-P` or `--proxy` |Proxies all requests which can't be resolved locally to the given url. e.g.: -P http://someurl.com | |

bin/http-server

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
'use strict';
44

55
var chalk = require('chalk'),
6-
os = require('os'),
7-
httpServer = require('../lib/http-server'),
8-
portfinder = require('portfinder'),
9-
opener = require('opener'),
6+
os = require('os'),
7+
httpServer = require('../lib/http-server'),
8+
portfinder = require('portfinder'),
9+
opener = require('opener'),
1010

11-
fs = require('fs'),
12-
url = require('url');
11+
fs = require('fs'),
12+
url = require('url');
1313
var argv = require('minimist')(process.argv.slice(2), {
1414
alias: {
1515
tls: 'ssl',
@@ -59,8 +59,8 @@ if (argv.h || argv.help) {
5959
' Optionally provide a URL path to open the browser window to.',
6060
' -c Cache time (max-age) in seconds [3600], e.g. -c10 for 10 seconds.',
6161
' To disable caching, use -c-1.',
62-
' -t Connections timeout in seconds [120], e.g. -t60 for 1 minute.',
63-
' To disable timeout, use -t0',
62+
' -t Connection timeout in seconds [120], e.g. -t60 for 1 minute.',
63+
' To disable timeout, use -t0.',
6464
' -U --utc Use UTC time format in log messages.',
6565
' --log-ip Enable logging of the client\'s IP address',
6666
'',
@@ -119,16 +119,15 @@ if (!argv.s && !argv.silent) {
119119
request: function (req, res, error) {
120120
var date = utc ? new Date().toUTCString() : new Date();
121121
var ip = argv['log-ip']
122-
? req.headers['x-forwarded-for'] || '' + req.connection.remoteAddress
123-
: '';
122+
? req.headers['x-forwarded-for'] || '' + req.connection.remoteAddress
123+
: '';
124124
if (error) {
125125
logger.info(
126126
'[%s] %s "%s %s" Error (%s): "%s"',
127127
date, ip, chalk.red(req.method), chalk.red(req.url),
128128
chalk.red(error.status.toString()), chalk.red(error.message)
129129
);
130-
}
131-
else {
130+
} else {
132131
logger.info(
133132
'[%s] %s "%s %s" "%s"',
134133
date, ip, chalk.cyan(req.method), chalk.cyan(req.url),
@@ -137,8 +136,7 @@ if (!argv.s && !argv.silent) {
137136
}
138137
}
139138
};
140-
}
141-
else if (chalk) {
139+
} else if (chalk) {
142140
logger = {
143141
info: function () {},
144142
request: function () {}
@@ -156,8 +154,7 @@ if (!port) {
156154
if (err) { throw err; }
157155
listen(port);
158156
});
159-
}
160-
else {
157+
} else {
161158
listen(port);
162159
}
163160

@@ -232,9 +229,8 @@ function listen(port) {
232229

233230
if (proxy) {
234231
try {
235-
new url.URL(proxy)
236-
}
237-
catch (err) {
232+
new url.URL(proxy);
233+
} catch (err) {
238234
logger.info(chalk.red('Error: Invalid proxy url'));
239235
process.exit(1);
240236
}
@@ -249,19 +245,17 @@ function listen(port) {
249245
options.https = {
250246
cert: argv.C || argv.cert || 'cert.pem',
251247
key: argv.K || argv.key || 'key.pem',
252-
passphrase: sslPassphrase,
248+
passphrase: sslPassphrase
253249
};
254250
try {
255251
fs.lstatSync(options.https.cert);
256-
}
257-
catch (err) {
252+
} catch (err) {
258253
logger.info(chalk.red('Error: Could not find certificate ' + options.https.cert));
259254
process.exit(1);
260255
}
261256
try {
262257
fs.lstatSync(options.https.key);
263-
}
264-
catch (err) {
258+
} catch (err) {
265259
logger.info(chalk.red('Error: Could not find private key ' + options.https.key));
266260
process.exit(1);
267261
}
@@ -285,7 +279,9 @@ function listen(port) {
285279
([chalk.yellow('COOP: '), argv.coop ? chalk.cyan(argv.coop) : chalk.red('disabled')].join('')),
286280
([chalk.yellow('CORS: '), argv.cors ? chalk.cyan(argv.cors) : chalk.red('disabled')].join('')),
287281
([chalk.yellow('Cache: '), argv.c ? (argv.c === '-1' ? chalk.red('disabled') : chalk.cyan(argv.c + ' seconds')) : chalk.cyan('3600 seconds')].join('')),
288-
([chalk.yellow('Connection Timeout: '), argv.t === '0' ? chalk.red('disabled') : (argv.t ? chalk.cyan(argv.t + ' seconds') : chalk.cyan('120 seconds'))].join('')),
282+
([chalk.yellow('Connection Timeout: '), Math.max(0, argv.t) === 0 ? chalk.red('disabled') :
283+
((!isNaN(argv.t) && !isNaN(parseFloat(argv.t))) ?
284+
chalk.cyan(Number(argv.t) + ' seconds') : chalk.cyan('120 seconds'))].join('')),
289285
([chalk.yellow('Directory Listings: '), argv.d ? chalk.red('not visible') : chalk.cyan('visible')].join('')),
290286
([chalk.yellow('AutoIndex: '), argv.i ? chalk.red('not visible') : chalk.cyan('visible')].join('')),
291287
([chalk.yellow('Serve GZIP Files: '), argv.g || argv.gzip ? chalk.cyan('true') : chalk.red('false')].join('')),
@@ -323,8 +319,7 @@ function listen(port) {
323319
if (typeof proxy === 'string') {
324320
if (proxyOptions) {
325321
logger.info('Unhandled requests will be served from: ' + proxy + '. Options: ' + JSON.stringify(proxyOptions));
326-
}
327-
else {
322+
} else {
328323
logger.info('Unhandled requests will be served from: ' + proxy);
329324
}
330325
}

doc/http-server.1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ Cache time (max-age) in seconds.
9696
To disable caching, use \-c \-1.
9797
Default is 3600.
9898

99+
.TP
100+
.BI \-t " " \fITIMEOUT\fR
101+
Connection timeout in seconds, e.g. -t60 for 1 minute.
102+
To disable timeout, use \-t0.
103+
Default is 120.
104+
99105
.TP
100106
.BI \-U ", " \-\-utc
101107
Use UTC time format in log messages.

lib/http-server.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,11 @@ function HttpServer(options) {
213213
? require('./shims/https-server-shim')(serverOptions)
214214
: union.createServer(serverOptions);
215215

216-
if (options.timeout !== undefined) {
217-
this.server.setTimeout(options.timeout);
216+
if (isNaN(options.timeout) || isNaN(parseFloat(options.timeout))) {
217+
this.server.setTimeout(120);
218+
} else {
219+
// set custom timeout only if options.timeout is a numeric string
220+
this.server.setTimeout(Math.max(0, Number(options.timeout)));
218221
}
219222

220223
if (typeof options.proxy === 'string' && options.websocket) {

0 commit comments

Comments
 (0)