Skip to content

Commit 38d08ad

Browse files
authored
Merge pull request #747 from http-party/tls-option
Make --ssl an alias for --tls
2 parents 35ff346 + 6f49089 commit 38d08ad

File tree

7 files changed

+60
-75
lines changed

7 files changed

+60
-75
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ This will install `http-server` globally so that it may be run from the command
6262

6363
|`--username` |Username for basic authentication | |
6464
|`--password` |Password for basic authentication | |
65-
|`-S` or `--ssl` |Enable https.| |
65+
|`-S`, `--tls` or `--ssl` |Enable secure request serving with TLS/SSL (HTTPS)|`false`|
6666
|`-C` or `--cert` |Path to ssl cert file |`cert.pem` |
6767
|`-K` or `--key` |Path to ssl key file |`key.pem` |
6868
|`-r` or `--robots` | Automatically provide a /robots.txt (The content of which defaults to `User-agent: *\nDisallow: /`) | `false` |

bin/http-server

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ var colors = require('colors/safe'),
77
httpServer = require('../lib/http-server'),
88
portfinder = require('portfinder'),
99
opener = require('opener'),
10-
fs = require('fs'),
11-
argv = require('minimist')(process.argv.slice(2));
10+
fs = require('fs');
11+
var argv = require('minimist')(process.argv.slice(2), {
12+
alias: {
13+
tls: 'ssl'
14+
}
15+
});
1216
var ifaces = os.networkInterfaces();
1317

1418
process.title = 'http-server';
@@ -38,17 +42,17 @@ if (argv.h || argv.help) {
3842
' -U --utc Use UTC time format in log messages.',
3943
' --log-ip Enable logging of the client\'s IP address',
4044
'',
41-
' -P --proxy Fallback proxy if the request cannot be resolved. e.g.: http://someurl.com',
45+
' -P --proxy Fallback proxy if the request cannot be resolved. e.g.: http://someurl.com',
4246
' --proxy-options Pass options to proxy using nested dotted objects. e.g.: --proxy-options.secure false',
4347
'',
4448
' --username Username for basic authentication [none]',
4549
' Can also be specified with the env variable NODE_HTTP_SERVER_USERNAME',
4650
' --password Password for basic authentication [none]',
4751
' Can also be specified with the env variable NODE_HTTP_SERVER_PASSWORD',
4852
'',
49-
' -S --ssl Enable https.',
50-
' -C --cert Path to ssl cert file (default: cert.pem).',
51-
' -K --key Path to ssl key file (default: key.pem).',
53+
' -S --tls --ssl Enable secure request serving with TLS/SSL (HTTPS)',
54+
' -C --cert Path to TLS cert file (default: cert.pem)',
55+
' -K --key Path to TLS key file (default: key.pem)',
5256
'',
5357
' -r --robots Respond to /robots.txt [User-agent: *\\nDisallow: /]',
5458
' --no-dotfiles Do not show dotfiles',
@@ -61,7 +65,7 @@ if (argv.h || argv.help) {
6165

6266
var port = argv.p || argv.port || parseInt(process.env.PORT, 10),
6367
host = argv.a || '0.0.0.0',
64-
ssl = argv.S || argv.ssl,
68+
tls = argv.S || argv.tls,
6569
proxy = argv.P || argv.proxy,
6670
proxyOptions = argv['proxy-options'],
6771
utc = argv.U || argv.utc,
@@ -156,7 +160,7 @@ function listen(port) {
156160
}
157161
}
158162

159-
if (ssl) {
163+
if (tls) {
160164
options.https = {
161165
cert: argv.C || argv.cert || 'cert.pem',
162166
key: argv.K || argv.key || 'key.pem'
@@ -179,16 +183,18 @@ function listen(port) {
179183

180184
var server = httpServer.createServer(options);
181185
server.listen(port, host, function () {
182-
var protocol = ssl ? 'https://' : 'http://';
186+
var protocol = tls ? 'https://' : 'http://';
183187

184-
logger.info([colors.yellow('Starting up http-server, serving '),
188+
logger.info([
189+
colors.yellow('Starting up http-server, serving '),
185190
colors.cyan(server.root),
186-
ssl ? (colors.yellow(' through') + colors.cyan(' https')) : ''
191+
tls ? (colors.yellow(' through') + colors.cyan(' https')) : ''
187192
].join(''));
188193

189194
logger.info([colors.yellow('\nhttp-server version: '), colors.cyan(require('../package.json').version)].join(''));
190195

191-
logger.info([colors.yellow('\nhttp-server settings: '),
196+
logger.info([
197+
colors.yellow('\nhttp-server settings: '),
192198
([colors.yellow('CORS: '), argv.cors ? colors.cyan(argv.cors) : colors.red('disabled')].join('')),
193199
([colors.yellow('Cache: '), argv.c ? (argv.c === '-1' ? colors.red('disabled') : colors.cyan(argv.c + ' seconds')) : colors.cyan('3600 seconds')].join('')),
194200
([colors.yellow('Connection Timeout: '), argv.t === '0' ? colors.red('disabled') : (argv.t ? colors.cyan(argv.t + ' seconds') : colors.cyan('120 seconds'))].join('')),

doc/http-server.1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ Can also be specified with the environment variable NODE_HTTP_SERVER_PASSWORD.
102102
Defaults to none.
103103

104104
.TP
105-
.BI \-S ", " \-\-ssl
105+
.BI \-S ", " \-\-tls ", " \-\-ssl
106106
Enable https.
107107

108108
.TP

lib/http-server.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ function HttpServer(options) {
3333

3434
if (options.root) {
3535
this.root = options.root;
36-
}
37-
else {
36+
} else {
3837
try {
3938
fs.lstatSync('./public');
4039
this.root = './public';

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/cli.test.js

Lines changed: 36 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,12 @@ const test = require('tap').test;
66
const request = require('request');
77
const spawn = require('child_process').spawn;
88
const path = require('path');
9+
const portfinder = require('portfinder');
910

1011
const node = process.execPath;
11-
const defaultUrl = 'http://localhost';
1212
const defaultPort = 8080;
1313

14-
function getRandomInt(min, max) {
15-
return Math.floor(Math.random() * ((max - min) + 1)) + min;
16-
}
17-
18-
function startEcstatic(args) {
14+
function startServer(args) {
1915
return spawn(node, [require.resolve('../bin/http-server')].concat(args));
2016
}
2117

@@ -43,82 +39,65 @@ function tearDown(ps, t) {
4339
});
4440
}
4541

46-
const getRandomPort = (() => {
47-
const usedPorts = [];
48-
return () => {
49-
const port = getRandomInt(1025, 65536);
50-
if (usedPorts.indexOf(port) > -1) {
51-
return getRandomPort();
52-
}
53-
54-
usedPorts.push(port);
55-
return port;
56-
};
57-
})();
58-
59-
test('setting port via cli - default port', (t) => {
60-
t.plan(2);
61-
62-
const port = defaultPort;
63-
const options = ['.'];
64-
const ecstatic = startEcstatic(options);
65-
66-
tearDown(ecstatic, t);
67-
68-
ecstatic.stdout.on('data', (msg) => {
69-
checkServerIsRunning(`${defaultUrl}:${port}`, msg, t);
42+
const getPort = () => new Promise((resolve, reject) => {
43+
portfinder.getPort((err, port) => {
44+
if (err) reject(err);
45+
resolve(port);
7046
});
7147
});
7248

7349
test('setting port via cli - custom port', (t) => {
7450
t.plan(2);
7551

76-
const port = getRandomPort();
77-
const options = ['.', '--port', port];
78-
const ecstatic = startEcstatic(options);
52+
getPort().then((port) => {
53+
const options = ['.', '--port', port];
54+
const server = startServer(options);
7955

80-
tearDown(ecstatic, t);
56+
tearDown(server, t);
8157

82-
ecstatic.stdout.on('data', (msg) => {
83-
checkServerIsRunning(`${defaultUrl}:${port}`, msg, t);
58+
server.stdout.on('data', (msg) => {
59+
checkServerIsRunning(`http://localhost:${port}`, msg, t);
60+
});
8461
});
8562
});
8663

8764
test('setting mimeTypes via cli - .types file', (t) => {
8865
t.plan(4);
8966

90-
const port = getRandomPort();
91-
const root = path.resolve(__dirname, 'public/');
92-
const pathMimetypeFile = path.resolve(__dirname, 'fixtures/custom_mime_type.types');
93-
const options = [root, '--port', port, '--mimetypes', pathMimetypeFile];
94-
const ecstatic = startEcstatic(options);
67+
getPort().then((port) => {
68+
const root = path.resolve(__dirname, 'public/');
69+
const pathMimetypeFile = path.resolve(__dirname, 'fixtures/custom_mime_type.types');
70+
const options = [root, '--port', port, '--mimetypes', pathMimetypeFile];
71+
const server = startServer(options);
9572

96-
tearDown(ecstatic, t);
73+
tearDown(server, t);
9774

98-
ecstatic.stdout.on('data', (msg) => {
99-
checkServerIsRunning(`${defaultUrl}:${port}/custom_mime_type.opml`, msg, t, (err, res) => {
100-
t.error(err);
101-
t.equal(res.headers['content-type'], 'application/secret');
75+
server.stdout.on('data', (msg) => {
76+
checkServerIsRunning(`http://localhost:${port}/custom_mime_type.opml`, msg, t, (err, res) => {
77+
t.error(err);
78+
t.equal(res.headers['content-type'], 'application/secret');
79+
});
10280
});
10381
});
10482
});
10583

10684
test('setting mimeTypes via cli - directly', (t) => {
10785
t.plan(4);
10886

109-
const port = getRandomPort();
110-
const root = path.resolve(__dirname, 'public/');
111-
const mimeType = ['--mimetypes', '{ "application/x-my-type": ["opml"] }'];
112-
const options = [root, '--port', port].concat(mimeType);
113-
const ecstatic = startEcstatic(options);
87+
getPort().then((port) => {
88+
const root = path.resolve(__dirname, 'public/');
89+
const mimeType = ['--mimetypes', '{ "application/x-my-type": ["opml"] }'];
90+
const options = [root, '--port', port].concat(mimeType);
91+
const server = startServer(options);
11492

115-
// TODO: remove error handler
116-
tearDown(ecstatic, t);
93+
// TODO: remove error handler
94+
tearDown(server, t);
11795

118-
ecstatic.stdout.on('data', (msg) => {
119-
checkServerIsRunning(`${defaultUrl}:${port}/custom_mime_type.opml`, msg, t, (err, res) => {
120-
t.error(err);
121-
t.equal(res.headers['content-type'], 'application/x-my-type');
96+
server.stdout.on('data', (msg) => {
97+
checkServerIsRunning(`http://localhost:${port}/custom_mime_type.opml`, msg, t, (err, res) => {
98+
t.error(err);
99+
t.equal(res.headers['content-type'], 'application/x-my-type');
100+
});
122101
});
123102
});
124103
});

test/proxy-options.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ test('proxy options', (t) => {
3535
brotli: true,
3636
gzip: true
3737
})
38+
// TODO #723 we should use portfinder
3839
server.listen(8080, async () => {
3940
try {
4041

4142
// Another server proxies 8081 to 8080
4243
const proxyServer = httpServer.createServer({
4344
proxy: 'http://localhost:8080',
4445
root: path.join(__dirname, 'fixtures'),
45-
ssl: true,
46+
tls: true,
4647
https: httpsOpts,
4748
proxyOptions: {
4849
secure: false

0 commit comments

Comments
 (0)