Skip to content

Commit

Permalink
Merge pull request #747 from http-party/tls-option
Browse files Browse the repository at this point in the history
Make --ssl an alias for --tls
  • Loading branch information
thornjad committed Oct 11, 2021
2 parents 35ff346 + 6f49089 commit 38d08ad
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 75 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -62,7 +62,7 @@ This will install `http-server` globally so that it may be run from the command

|`--username` |Username for basic authentication | |
|`--password` |Password for basic authentication | |
|`-S` or `--ssl` |Enable https.| |
|`-S`, `--tls` or `--ssl` |Enable secure request serving with TLS/SSL (HTTPS)|`false`|
|`-C` or `--cert` |Path to ssl cert file |`cert.pem` |
|`-K` or `--key` |Path to ssl key file |`key.pem` |
|`-r` or `--robots` | Automatically provide a /robots.txt (The content of which defaults to `User-agent: *\nDisallow: /`) | `false` |
Expand Down
30 changes: 18 additions & 12 deletions bin/http-server
Expand Up @@ -7,8 +7,12 @@ var colors = require('colors/safe'),
httpServer = require('../lib/http-server'),
portfinder = require('portfinder'),
opener = require('opener'),
fs = require('fs'),
argv = require('minimist')(process.argv.slice(2));
fs = require('fs');
var argv = require('minimist')(process.argv.slice(2), {
alias: {
tls: 'ssl'
}
});
var ifaces = os.networkInterfaces();

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

var port = argv.p || argv.port || parseInt(process.env.PORT, 10),
host = argv.a || '0.0.0.0',
ssl = argv.S || argv.ssl,
tls = argv.S || argv.tls,
proxy = argv.P || argv.proxy,
proxyOptions = argv['proxy-options'],
utc = argv.U || argv.utc,
Expand Down Expand Up @@ -156,7 +160,7 @@ function listen(port) {
}
}

if (ssl) {
if (tls) {
options.https = {
cert: argv.C || argv.cert || 'cert.pem',
key: argv.K || argv.key || 'key.pem'
Expand All @@ -179,16 +183,18 @@ function listen(port) {

var server = httpServer.createServer(options);
server.listen(port, host, function () {
var protocol = ssl ? 'https://' : 'http://';
var protocol = tls ? 'https://' : 'http://';

logger.info([colors.yellow('Starting up http-server, serving '),
logger.info([
colors.yellow('Starting up http-server, serving '),
colors.cyan(server.root),
ssl ? (colors.yellow(' through') + colors.cyan(' https')) : ''
tls ? (colors.yellow(' through') + colors.cyan(' https')) : ''
].join(''));

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

logger.info([colors.yellow('\nhttp-server settings: '),
logger.info([
colors.yellow('\nhttp-server settings: '),
([colors.yellow('CORS: '), argv.cors ? colors.cyan(argv.cors) : colors.red('disabled')].join('')),
([colors.yellow('Cache: '), argv.c ? (argv.c === '-1' ? colors.red('disabled') : colors.cyan(argv.c + ' seconds')) : colors.cyan('3600 seconds')].join('')),
([colors.yellow('Connection Timeout: '), argv.t === '0' ? colors.red('disabled') : (argv.t ? colors.cyan(argv.t + ' seconds') : colors.cyan('120 seconds'))].join('')),
Expand Down
2 changes: 1 addition & 1 deletion doc/http-server.1
Expand Up @@ -102,7 +102,7 @@ Can also be specified with the environment variable NODE_HTTP_SERVER_PASSWORD.
Defaults to none.

.TP
.BI \-S ", " \-\-ssl
.BI \-S ", " \-\-tls ", " \-\-ssl
Enable https.

.TP
Expand Down
3 changes: 1 addition & 2 deletions lib/http-server.js
Expand Up @@ -33,8 +33,7 @@ function HttpServer(options) {

if (options.root) {
this.root = options.root;
}
else {
} else {
try {
fs.lstatSync('./public');
this.root = './public';
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

93 changes: 36 additions & 57 deletions test/cli.test.js
Expand Up @@ -6,16 +6,12 @@ const test = require('tap').test;
const request = require('request');
const spawn = require('child_process').spawn;
const path = require('path');
const portfinder = require('portfinder');

const node = process.execPath;
const defaultUrl = 'http://localhost';
const defaultPort = 8080;

function getRandomInt(min, max) {
return Math.floor(Math.random() * ((max - min) + 1)) + min;
}

function startEcstatic(args) {
function startServer(args) {
return spawn(node, [require.resolve('../bin/http-server')].concat(args));
}

Expand Down Expand Up @@ -43,82 +39,65 @@ function tearDown(ps, t) {
});
}

const getRandomPort = (() => {
const usedPorts = [];
return () => {
const port = getRandomInt(1025, 65536);
if (usedPorts.indexOf(port) > -1) {
return getRandomPort();
}

usedPorts.push(port);
return port;
};
})();

test('setting port via cli - default port', (t) => {
t.plan(2);

const port = defaultPort;
const options = ['.'];
const ecstatic = startEcstatic(options);

tearDown(ecstatic, t);

ecstatic.stdout.on('data', (msg) => {
checkServerIsRunning(`${defaultUrl}:${port}`, msg, t);
const getPort = () => new Promise((resolve, reject) => {
portfinder.getPort((err, port) => {
if (err) reject(err);
resolve(port);
});
});

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

const port = getRandomPort();
const options = ['.', '--port', port];
const ecstatic = startEcstatic(options);
getPort().then((port) => {
const options = ['.', '--port', port];
const server = startServer(options);

tearDown(ecstatic, t);
tearDown(server, t);

ecstatic.stdout.on('data', (msg) => {
checkServerIsRunning(`${defaultUrl}:${port}`, msg, t);
server.stdout.on('data', (msg) => {
checkServerIsRunning(`http://localhost:${port}`, msg, t);
});
});
});

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

const port = getRandomPort();
const root = path.resolve(__dirname, 'public/');
const pathMimetypeFile = path.resolve(__dirname, 'fixtures/custom_mime_type.types');
const options = [root, '--port', port, '--mimetypes', pathMimetypeFile];
const ecstatic = startEcstatic(options);
getPort().then((port) => {
const root = path.resolve(__dirname, 'public/');
const pathMimetypeFile = path.resolve(__dirname, 'fixtures/custom_mime_type.types');
const options = [root, '--port', port, '--mimetypes', pathMimetypeFile];
const server = startServer(options);

tearDown(ecstatic, t);
tearDown(server, t);

ecstatic.stdout.on('data', (msg) => {
checkServerIsRunning(`${defaultUrl}:${port}/custom_mime_type.opml`, msg, t, (err, res) => {
t.error(err);
t.equal(res.headers['content-type'], 'application/secret');
server.stdout.on('data', (msg) => {
checkServerIsRunning(`http://localhost:${port}/custom_mime_type.opml`, msg, t, (err, res) => {
t.error(err);
t.equal(res.headers['content-type'], 'application/secret');
});
});
});
});

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

const port = getRandomPort();
const root = path.resolve(__dirname, 'public/');
const mimeType = ['--mimetypes', '{ "application/x-my-type": ["opml"] }'];
const options = [root, '--port', port].concat(mimeType);
const ecstatic = startEcstatic(options);
getPort().then((port) => {
const root = path.resolve(__dirname, 'public/');
const mimeType = ['--mimetypes', '{ "application/x-my-type": ["opml"] }'];
const options = [root, '--port', port].concat(mimeType);
const server = startServer(options);

// TODO: remove error handler
tearDown(ecstatic, t);
// TODO: remove error handler
tearDown(server, t);

ecstatic.stdout.on('data', (msg) => {
checkServerIsRunning(`${defaultUrl}:${port}/custom_mime_type.opml`, msg, t, (err, res) => {
t.error(err);
t.equal(res.headers['content-type'], 'application/x-my-type');
server.stdout.on('data', (msg) => {
checkServerIsRunning(`http://localhost:${port}/custom_mime_type.opml`, msg, t, (err, res) => {
t.error(err);
t.equal(res.headers['content-type'], 'application/x-my-type');
});
});
});
});
3 changes: 2 additions & 1 deletion test/proxy-options.test.js
Expand Up @@ -35,14 +35,15 @@ test('proxy options', (t) => {
brotli: true,
gzip: true
})
// TODO #723 we should use portfinder
server.listen(8080, async () => {
try {

// Another server proxies 8081 to 8080
const proxyServer = httpServer.createServer({
proxy: 'http://localhost:8080',
root: path.join(__dirname, 'fixtures'),
ssl: true,
tls: true,
https: httpsOpts,
proxyOptions: {
secure: false
Expand Down

0 comments on commit 38d08ad

Please sign in to comment.