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

Add support for Private Network Access via Access-Control-Allow-Private-Network header #801

Open
wants to merge 1 commit 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 @@ -53,6 +53,7 @@ This will install `http-server` globally so that it may be run from the command
|`-e` or `--ext` |Default file extension if none supplied |`html` |
|`-s` or `--silent` |Suppress log messages from output | |
|`--cors` |Enable CORS via the `Access-Control-Allow-Origin` header | |
|`--private-network-access` |Enable Private Network Access via the `Access-Control-Allow-Private-Network` header | |
|`-o [path]` |Open browser window after starting the server. Optionally provide a URL path to open. e.g.: -o /other/dir/ | |
|`-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` |
|`-U` or `--utc` |Use UTC time format in log messages.| |
Expand Down
7 changes: 7 additions & 0 deletions bin/http-server
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ if (argv.h || argv.help) {
' -s --silent Suppress log messages from output',
' --cors[=headers] Enable CORS via the "Access-Control-Allow-Origin" header',
' Optionally provide CORS headers list separated by commas',
' --private-network-access Enable Private Network Access via the',
' "Access-Control-Allow-Private-Network" header',
' -o [path] Open browser window after starting the server.',
' Optionally provide a URL path to open the browser window to.',
' -c Cache time (max-age) in seconds [3600], e.g. -c10 for 10 seconds.',
Expand Down Expand Up @@ -163,6 +165,10 @@ function listen(port) {
}
}

if (argv['private-network-access']) {
options.privateNetworkAccess = true;
}

if (proxy) {
try {
new url.URL(proxy)
Expand Down Expand Up @@ -210,6 +216,7 @@ function listen(port) {
logger.info([
chalk.yellow('\nhttp-server settings: '),
([chalk.yellow('CORS: '), argv.cors ? chalk.cyan(argv.cors) : chalk.red('disabled')].join('')),
([chalk.yellow('Private Network Access: '), argv['private-network-access'] ? chalk.cyan(argv['private-network-access']) : 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('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('')),
Expand Down
4 changes: 4 additions & 0 deletions doc/http-server.1
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ Suppress log messages from output.
Enable CORS via the "Access-Control-Allow-Origin" header.
Optionally provide CORS headers list separated by commas.

.TP
.BI \-\-private-network-access
Enable Private Network Access via the "Access-Control-Allow-Private-Network" header.

.TP
.BI \-o " " [\fIPATH\fR]
Open default browser window after starting the server.
Expand Down
1 change: 1 addition & 0 deletions lib/core/aliases.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"si": [ "si", "index" ],
"handleError": [ "handleError", "handleerror" ],
"cors": [ "cors", "CORS" ],
"privateNetworkAccess": [ "privateNetworkAccess", "privatenetworkaccess", "private-network-access" ],
"headers": [ "H", "header", "headers" ],
"contentType": [ "contentType", "contenttype", "content-type" ],
"mimeType": [
Expand Down
1 change: 1 addition & 0 deletions lib/core/defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"si": false,
"cache": "max-age=3600",
"cors": false,
"privateNetworkAccess": false,
"gzip": true,
"brotli": false,
"defaultExt": ".html",
Expand Down
6 changes: 6 additions & 0 deletions lib/core/opts.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ module.exports = (opts) => {
}
});

aliases.privateNetworkAccess.forEach((k) => {
if (isDeclared(k) && opts[k]) {
headers['Access-Control-Allow-Private-Network'] = 'true';
}
});

aliases.headers.forEach((k) => {
if (isDeclared(k)) {
if (Array.isArray(opts[k])) {
Expand Down
4 changes: 4 additions & 0 deletions lib/http-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ function HttpServer(options) {
} : null));
}

if (options.privateNetworkAccess) {
this.headers['Access-Control-Allow-Private-Network'] = true;
}

if (options.robots) {
before.push(function (req, res) {
if (req.url === '/robots.txt') {
Expand Down
88 changes: 88 additions & 0 deletions test/private-network-access.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
'use strict';

const test = require('tap').test;
const server = require('../lib/core');
const http = require('http');
const path = require('path');
const request = require('request');

const root = path.join(__dirname, 'public');

test('private-network-access defaults to false', (t) => {
t.plan(3);

const httpServer = http.createServer(
server({
root,
autoIndex: true,
defaultExt: 'html',
})
);

httpServer.listen(() => {
const port = httpServer.address().port;
const uri = `http://localhost:${port}/subdir/index.html`;

request.get({ uri }, (err, res) => {
t.ifError(err);
t.equal(res.statusCode, 200);
t.type(res.headers['access-control-allow-private-network'], 'undefined');
});
});
t.once('end', () => {
httpServer.close();
});
});

test('privateNetworkAccess set to false', (t) => {
t.plan(3);

const httpServer = http.createServer(
server({
root,
privateNetworkAccess: false,
autoIndex: true,
defaultExt: 'html',
})
);

httpServer.listen(() => {
const port = httpServer.address().port;
const uri = `http://localhost:${port}/subdir/index.html`;

request.get({ uri }, (err, res) => {
t.ifError(err);
t.equal(res.statusCode, 200);
t.type(res.headers['access-control-allow-private-network'], 'undefined');
});
});
t.once('end', () => {
httpServer.close();
});
});

test('privateNetworkAccess set to true', (t) => {
t.plan(3);

const httpServer = http.createServer(
server({
root,
privateNetworkAccess: true,
autoIndex: true,
defaultExt: 'html',
})
);

httpServer.listen(() => {
const port = httpServer.address().port;
const uri = `http://localhost:${port}/subdir/index.html`;
request.get({ uri }, (err, res) => {
t.ifError(err);
t.equal(res.statusCode, 200);
t.equal(res.headers['access-control-allow-private-network'], 'true');
});
});
t.once('end', () => {
httpServer.close();
});
});