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

Feat/--dir-overrides-404 flag #894

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 @@ -47,6 +47,7 @@ This will install `http-server` globally so that it may be run from the command
|`-p` or `--port` |Port to use. Use `-p 0` to look for an open port, starting at 8080. It will also read from `process.env.PORT`. |8080 |
|`-a` |Address to use |0.0.0.0|
|`-d` |Show directory listings |`true` |
|`-dir-overrides-404` | Whether `-d` should override magic `404.html` | `false`
|`-i` | Display autoIndex | `true` |
|`-g` or `--gzip` |When enabled it will serve `./public/some-file.js.gz` in place of `./public/some-file.js` when a gzipped version of the file exists and the request accepts gzip encoding. If brotli is also enabled, it will try to serve brotli first.|`false`|
|`-b` or `--brotli`|When enabled it will serve `./public/some-file.js.br` in place of `./public/some-file.js` when a brotli compressed version of the file exists and the request accepts `br` encoding. If gzip is also enabled, it will try to serve brotli first. |`false`|
Expand Down
2 changes: 2 additions & 0 deletions bin/http-server
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ if (argv.h || argv.help) {
' -p --port Port to use. If 0, look for open port. [8080]',
' -a Address to use [0.0.0.0]',
' -d Show directory listings [true]',
' --dir-overrides-404 Whether -d should override magic 404.html [false]',
' -i Display autoIndex [true]',
' -g --gzip Serve gzip files when possible [false]',
' -b --brotli Serve brotli files when possible [false]',
Expand Down Expand Up @@ -142,6 +143,7 @@ function listen(port) {
cache: argv.c,
timeout: argv.t,
showDir: argv.d,
dirOverrides404: argv['dir-overrides-404'],
autoIndex: argv.i,
gzip: argv.g || argv.gzip,
brotli: argv.b || argv.brotli,
Expand Down
5 changes: 5 additions & 0 deletions doc/http-server.1
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ Default is 0.0.0.0.
Show directory listings.
Default is true.

.TP
.BI \-d
Whether -d should override magic 404.html
Default is false.

.TP
.BI \-i
Display autoIndex.
Expand Down
7 changes: 7 additions & 0 deletions lib/core/aliases.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
{
"autoIndex": [ "autoIndex", "autoindex" ],
"showDir": [ "showDir", "showdir" ],
"dirOverrides404": [
"dirOverrides404",
"diroverrides404",
"dir-overrides-404",
"listingsOverride404",
"listings-override-404"
],
"showDotfiles": ["showDotfiles", "showdotfiles"],
"humanReadable": [ "humanReadable", "humanreadable", "human-readable" ],
"hidePermissions": ["hidePermissions", "hidepermissions", "hide-permissions"],
Expand Down
1 change: 1 addition & 0 deletions lib/core/defaults.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"autoIndex": true,
"showDir": true,
"dirOverrides404": false,
"showDotfiles": true,
"humanReadable": true,
"hidePermissions": false,
Expand Down
6 changes: 5 additions & 1 deletion lib/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,11 @@ module.exports = function createMiddleware(_dir, _options) {
url: `${parsed.pathname}.${defaultExt}${(parsed.search) ? parsed.search : ''}`,
headers: req.headers,
}, res, next);
} else if (opts.showDir && opts.dirOverrides404) {
// If showDir and dirOverrides404 are true, show the directory instead of 404.html
req.url = path.dirname(req.url);
showDir(opts, stat)(req, res);
return;
} else {
// Try to serve default ./404.html
const rawUrl = (handleError ? `/${path.join(baseDir, `404.${defaultExt}`)}` : req.url);
Expand Down Expand Up @@ -406,7 +411,6 @@ module.exports = function createMiddleware(_dir, _options) {
showDir(opts, stat)(req, res);
return;
}

status[403](res, next);
});
return;
Expand Down
10 changes: 10 additions & 0 deletions lib/core/opts.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const aliases = require('./aliases.json');
module.exports = (opts) => {
let autoIndex = defaults.autoIndex;
let showDir = defaults.showDir;
let dirOverrides404 = defaults.dirOverrides404;
let showDotfiles = defaults.showDotfiles;
let humanReadable = defaults.humanReadable;
let hidePermissions = defaults.hidePermissions;
Expand Down Expand Up @@ -55,6 +56,14 @@ module.exports = (opts) => {
return false;
});

aliases.dirOverrides404.some((k) => {
if (isDeclared(k)) {
dirOverrides404 = opts[k];
return true;
}
return false;
});

aliases.showDotfiles.some((k) => {
if (isDeclared(k)) {
showDotfiles = opts[k];
Expand Down Expand Up @@ -184,6 +193,7 @@ module.exports = (opts) => {
cache,
autoIndex,
showDir,
dirOverrides404,
showDotfiles,
humanReadable,
hidePermissions,
Expand Down
2 changes: 2 additions & 0 deletions lib/http-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ function HttpServer(options) {
options.cache // in seconds.
);
this.showDir = options.showDir !== 'false';
this.dirOverrides404 = options.dirOverrides404;
this.autoIndex = options.autoIndex !== 'false';
this.showDotfiles = options.showDotfiles;
this.gzip = options.gzip === true;
Expand Down Expand Up @@ -129,6 +130,7 @@ function HttpServer(options) {
root: this.root,
cache: this.cache,
showDir: this.showDir,
dirOverrides404: this.dirOverrides404,
showDotfiles: this.showDotfiles,
autoIndex: this.autoIndex,
defaultExt: this.ext,
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@
{
"name": "Jade Michael Thornton",
"email": "[email protected]"
},
{
"name": "Jorens Merenjanu",
"email": "[email protected]"
}
],
"dependencies": {
Expand Down
63 changes: 63 additions & 0 deletions test/dir-overrides-404.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';

const test = require('tap').test;
const http = require('http');
const ecstatic = require('../lib/core');
const request = require('request');
const showDir = require('../lib/core/show-dir');

const root = `${__dirname}/public/dir-overrides-404`;

test('server should display directory if -d and --dir-overrides--404 flags are specified', (t) => {
// require('portfinder').getPort((err, port) => {
try {
const server = http.createServer(ecstatic({root, showDir: true, dirOverrides404: true}));
// t.plan(2);
// t.on('end', () => { server.close(); });
server.listen(0, () => {
const port = server.address().port;
request.get(`http://localhost:${port}/directory/`, (err, res, body) => {
if(err) {
t.error(err);
}
// console.log(body);
// console.log(res.statusCode);
t.equal(res.statusCode, 200);
console.log(body);
t.equal(body.includes('Index of /directory/'), true);
server.close(() => { t.end(); });
});
})
console.log('d');

} catch (e) {
t.fail(e.message);
t.end();
}
// });
});

test('server should display 404.html if -d flag is specified but not --dir-overrides-404', (t) => {
try {
const server = http.createServer(ecstatic({root, showDir: true, dirOverrides404: false}));
// t.plan(2);
// t.on('end', () => { server.close(); });
server.listen(0, () => {
const port = server.address().port;
request.get(`http://localhost:${port}/directory/`, (err, res, body) => {
if(err) {
t.error(err);
}
t.equal(res.statusCode, 404);
t.equal(body.includes('404file'), true);
server.close(() => { t.end(); });
});
})
console.log('d');

} catch (e) {
t.fail(e.message);
t.end();
}

});
1 change: 1 addition & 0 deletions test/public/dir-overrides-404/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
404file
Empty file.