From b6d3a8fe43726d4968893e44f0a04034de6493df Mon Sep 17 00:00:00 2001 From: kipcode66 Date: Fri, 9 Aug 2024 21:05:31 -0400 Subject: [PATCH 1/2] Added option for custom headers --- bin/http-server | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/bin/http-server b/bin/http-server index 7c597fa8..aed88262 100755 --- a/bin/http-server +++ b/bin/http-server @@ -59,6 +59,7 @@ if (argv.h || argv.help) { ' -r --robots Respond to /robots.txt [User-agent: *\\nDisallow: /]', ' --no-dotfiles Do not show dotfiles', ' --mimetypes Path to a .types file for custom mimetype definition', + ' -H --headers Semi-colon separated list of headers to use', ' -h --help Print this list and exit.', ' -v --version Print the version and exit.' ].join('\n')); @@ -156,6 +157,13 @@ function listen(port) { password: argv.password || process.env.NODE_HTTP_SERVER_PASSWORD }; + if (argv.H || argv.headers) { + options.headers = {}; + for (let h of (argv.H || argv.headers).split(/\s*;\s*/).map((x) => {x = x.split(/\s*:\s*/); x.push(''); return x.slice(0, 2);})) { + options.headers[h[0]] = h[1]; + } + } + if (argv.cors) { options.cors = true; if (typeof argv.cors === 'string') { @@ -211,6 +219,7 @@ function listen(port) { chalk.yellow('\nhttp-server settings: '), ([chalk.yellow('CORS: '), argv.cors ? chalk.cyan(argv.cors) : chalk.red('disabled')].join('')), ([chalk.yellow('Cache: '), argv.c ? (argv.c === '-1' ? chalk.red('disabled') : chalk.cyan(argv.c + ' seconds')) : chalk.cyan('3600 seconds')].join('')), + ([chalk.yellow('Headers: ', argv.H || argv.headers ? chalk.cyan(argv.H || argv.headers) : chalk.red('none'))]), ([chalk.yellow('Connection Timeout: '), argv.t === '0' ? chalk.red('disabled') : (argv.t ? chalk.cyan(argv.t + ' seconds') : chalk.cyan('120 seconds'))].join('')), ([chalk.yellow('Directory Listings: '), argv.d ? chalk.red('not visible') : chalk.cyan('visible')].join('')), ([chalk.yellow('AutoIndex: '), argv.i ? chalk.red('not visible') : chalk.cyan('visible')].join('')), From 4dfa549d43e1176439d1605c31b4745ed09796b4 Mon Sep 17 00:00:00 2001 From: kipcode66 Date: Sat, 10 Aug 2024 00:35:14 -0400 Subject: [PATCH 2/2] fixed docs --- README.md | 1 + doc/http-server.1 | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/README.md b/README.md index 0ffbbc1f..2f9b98f1 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ This will install `http-server` globally so that it may be run from the command |`-r` or `--robots` | Automatically provide a /robots.txt (The content of which defaults to `User-agent: *\nDisallow: /`) | `false` | |`--no-dotfiles` |Do not show dotfiles| | |`--mimetypes` |Path to a .types file for custom mimetype definition| | +|`-H` or `--headers` |Semi-colon separated list of headers to use (Ex.: `Cross-Origin-Resource-Policy:same-site;Cross-Origin-Opener-Policy:same-origin`)| | |`-h` or `--help` |Print this list and exit. | | |`-v` or `--version`|Print the version and exit. | | diff --git a/doc/http-server.1 b/doc/http-server.1 index 8e2796e6..abf05733 100644 --- a/doc/http-server.1 +++ b/doc/http-server.1 @@ -125,6 +125,10 @@ If not specified, uses "User-agent: *\\nDisallow: /]" .BI \-\-no\-dotfiles Do not show dotfiles. +.TP +.BI \-H ", " \-\-headers " " [\fIHEADERS\fR] +Semi-colon separated list of custom headers. + .TP .BI \-h ", " \-\-help Print usage and exit.