Skip to content

Commit dea1508

Browse files
committed
Merge branch 'qr' of github.com:HARDY8118/http-server into qr
2 parents e3bc05f + 3983e59 commit dea1508

21 files changed

+1430
-1571
lines changed

.github/ISSUE_TEMPLATE

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
---
5+
6+
<!-- Describe the issue briefly here. -->
7+
8+
#### Environment Versions
9+
10+
1. OS Type
11+
1. Node version: `$ node --version`
12+
1. http-server version: `$ http-server --version`
13+
14+
#### Steps to reproduce
15+
16+
<!-- Include the actual command -->
17+
18+
1. ...
19+
2. ...
20+
3. ...
21+
22+
#### Expected result
23+
24+
...
25+
26+
#### Actual result
27+
28+
<!-- Include full output and/or stack trace -->
29+
30+
...
31+
32+
#### Other information
33+
34+
<!-- Include related issues, suggestions for a fix or further debug information, etc. -->
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
---
5+
6+
#### What's the problem this feature will solve?
7+
8+
<!-- What are you trying to do, that you are unable to achieve with http-server as it currently stands? -->
9+
10+
#### Describe the solution you'd like
11+
12+
<!-- A clear and concise description of what you want to happen. -->
13+
14+
<!-- Provide examples of real-world use cases that this would enable and how it solves the problem described above. -->
15+
16+
#### Alternative Solutions
17+
18+
<!-- Have you tried to workaround the problem using http-server or other tools? Or a different approach to solving this issue? Please elaborate here. -->
19+
20+
#### Additional context
21+
22+
<!-- Add any other context, links, etc. about the feature here. -->

.github/PULL_REQUEST_TEMPLATE

Lines changed: 0 additions & 23 deletions
This file was deleted.

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!--- Describe the changes here. --->
2+
3+
##### Relevant issues
4+
5+
<!--
6+
Link to the issue(s) this pull request fixes here, if applicable: "Fixes #xxx" or "Resolves #xxx"
7+
8+
If your PR fixes multiple issues, list them individually like "Fixes #xx1, fixes #xx2, fixes #xx3". This is a quirk of how GitHub links issues.
9+
-->
10+
11+
##### Contributor checklist
12+
13+
- [ ] Provide tests for the changes (unless documentation-only)
14+
- [ ] Documented any new features, CLI switches, etc. (if applicable)
15+
- [ ] Server `--help` output
16+
- [ ] README.md
17+
- [ ] doc/http-server.1 (use the same format as other entries)
18+
- [ ] The pull request is being made against the `master` branch
19+
20+
##### Maintainer checklist
21+
22+
- [ ] Assign a version triage tag
23+
- [ ] Approve tests if applicable

.github/workflows/node.js.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
strategy:
1818
fail-fast: false
1919
matrix:
20-
node-version: [10.x, 12.x, 14.x, 16.x]
20+
node-version: [12.x, 14.x, 16.x]
2121
os: [ubuntu-latest, macOS-latest, windows-latest]
2222

2323
steps:

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@ This will install `http-server` globally so that it may be run from the command
5858
|`-U` or `--utc` |Use UTC time format in log messages.| |
5959
|`--log-ip` |Enable logging of the client's IP address |`false` |
6060
|`-P` or `--proxy` |Proxies all requests which can't be resolved locally to the given url. e.g.: -P http://someurl.com | |
61+
|`--proxy-options` Pass proxy [options](https://github.com/http-party/node-http-proxy#options) using nested dotted objects. e.g.: --proxy-options.secure false
62+
6163
|`--username` |Username for basic authentication | |
6264
|`--password` |Password for basic authentication | |
63-
|`-S` or `--ssl` |Enable https.| |
65+
|`-S`, `--tls` or `--ssl` |Enable secure request serving with TLS/SSL (HTTPS)|`false`|
6466
|`-C` or `--cert` |Path to ssl cert file |`cert.pem` |
6567
|`-K` or `--key` |Path to ssl key file |`key.pem` |
6668
|`--qr-code` |Show QR code for public IP | |,

bin/http-server

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
'use strict';
44

5-
var colors = require('colors/safe'),
6-
os = require('os'),
7-
httpServer = require('../lib/http-server'),
8-
portfinder = require('portfinder'),
9-
opener = require('opener'),
10-
fs = require('fs'),
11-
argv = require('minimist')(process.argv.slice(2)),
12-
qrcode = require('qrcode-terminal');
5+
var colors = require('colors/safe'),
6+
os = require('os'),
7+
httpServer = require('../lib/http-server'),
8+
portfinder = require('portfinder'),
9+
opener = require('opener'),
10+
fs = require('fs');
11+
var argv = require('minimist')(process.argv.slice(2), {
12+
alias: {
13+
tls: 'ssl'
14+
}
15+
});
1316
var ifaces = os.networkInterfaces();
1417
var plainIp;
1518

@@ -40,7 +43,8 @@ if (argv.h || argv.help) {
4043
' -U --utc Use UTC time format in log messages.',
4144
' --log-ip Enable logging of the client\'s IP address',
4245
'',
43-
' -P --proxy Fallback proxy if the request cannot be resolved. e.g.: http://someurl.com',
46+
' -P --proxy Fallback proxy if the request cannot be resolved. e.g.: http://someurl.com',
47+
' --proxy-options Pass options to proxy using nested dotted objects. e.g.: --proxy-options.secure false',
4448
'',
4549
' --username Username for basic authentication [none]',
4650
' Can also be specified with the env variable NODE_HTTP_SERVER_USERNAME',
@@ -62,12 +66,26 @@ if (argv.h || argv.help) {
6266
}
6367

6468
var port = argv.p || argv.port || parseInt(process.env.PORT, 10),
65-
host = argv.a || '0.0.0.0',
66-
ssl = argv.S || argv.ssl,
67-
proxy = argv.P || argv.proxy,
68-
utc = argv.U || argv.utc,
69-
version = argv.v || argv.version,
70-
logger;
69+
host = argv.a || '0.0.0.0',
70+
tls = argv.S || argv.tls,
71+
proxy = argv.P || argv.proxy,
72+
proxyOptions = argv['proxy-options'],
73+
utc = argv.U || argv.utc,
74+
version = argv.v || argv.version,
75+
logger;
76+
77+
var proxyOptionsBooleanProps = [
78+
'ws', 'xfwd', 'secure', 'toProxy', 'prependPath', 'ignorePath', 'changeOrigin',
79+
'preserveHeaderKeyCase', 'followRedirects', 'selfHandleResponse'
80+
];
81+
82+
if (proxyOptions) {
83+
Object.keys(proxyOptions).forEach(function (key) {
84+
if (proxyOptionsBooleanProps.indexOf(key) > -1) {
85+
proxyOptions[key] = proxyOptions[key].toLowerCase() === 'true';
86+
}
87+
});
88+
}
7189

7290
if (!argv.s && !argv.silent) {
7391
logger = {
@@ -130,6 +148,7 @@ function listen(port) {
130148
ext: argv.e || argv.ext,
131149
logFn: logger.request,
132150
proxy: proxy,
151+
proxyOptions: proxyOptions,
133152
showDotfiles: argv.dotfiles,
134153
mimetypes: argv.mimetypes,
135154
username: argv.username || process.env.NODE_HTTP_SERVER_USERNAME,
@@ -144,7 +163,7 @@ function listen(port) {
144163
}
145164
}
146165

147-
if (ssl) {
166+
if (tls) {
148167
options.https = {
149168
cert: argv.C || argv.cert || 'cert.pem',
150169
key: argv.K || argv.key || 'key.pem'
@@ -167,24 +186,26 @@ function listen(port) {
167186

168187
var server = httpServer.createServer(options);
169188
server.listen(port, host, function () {
170-
var protocol = ssl ? 'https://' : 'http://';
189+
var protocol = tls ? 'https://' : 'http://';
171190

172-
logger.info([colors.yellow('Starting up http-server, serving '),
173-
colors.cyan(server.root),
174-
ssl ? (colors.yellow(' through') + colors.cyan(' https')) : ''
191+
logger.info([
192+
colors.yellow('Starting up http-server, serving '),
193+
colors.cyan(server.root),
194+
tls ? (colors.yellow(' through') + colors.cyan(' https')) : ''
175195
].join(''));
176196

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

179-
logger.info([colors.yellow('\nhttp-server settings: '),
180-
([colors.yellow('CORS: '), argv.cors ? colors.cyan(argv.cors) : colors.red('disabled')].join('')),
181-
([colors.yellow('Cache: '), argv.c ? (argv.c === '-1' ? colors.red('disabled') : colors.cyan(argv.c + ' seconds')) : colors.cyan('3600 seconds')].join('')),
182-
([colors.yellow('Connection Timeout: '), argv.t === '0' ? colors.red('disabled') : (argv.t ? colors.cyan(argv.t + ' seconds') : colors.cyan('120 seconds'))].join('')),
183-
([colors.yellow('Directory Listings: '), argv.d ? colors.red('not visible') : colors.cyan('visible')].join('')),
184-
([colors.yellow('AutoIndex: '), argv.i ? colors.red('not visible') : colors.cyan('visible')].join('')),
185-
([colors.yellow('Serve GZIP Files: '), argv.g || argv.gzip ? colors.cyan('true') : colors.red('false')].join('')),
186-
([colors.yellow('Serve Brotli Files: '), argv.b || argv.brotli ? colors.cyan('true') : colors.red('false')].join('')),
187-
([colors.yellow('Default File Extension: '), argv.e ? colors.cyan(argv.e) : (argv.ext ? colors.cyan(argv.ext) : colors.red('none'))].join(''))
199+
logger.info([
200+
colors.yellow('\nhttp-server settings: '),
201+
([colors.yellow('CORS: '), argv.cors ? colors.cyan(argv.cors) : colors.red('disabled')].join('')),
202+
([colors.yellow('Cache: '), argv.c ? (argv.c === '-1' ? colors.red('disabled') : colors.cyan(argv.c + ' seconds')) : colors.cyan('3600 seconds')].join('')),
203+
([colors.yellow('Connection Timeout: '), argv.t === '0' ? colors.red('disabled') : (argv.t ? colors.cyan(argv.t + ' seconds') : colors.cyan('120 seconds'))].join('')),
204+
([colors.yellow('Directory Listings: '), argv.d ? colors.red('not visible') : colors.cyan('visible')].join('')),
205+
([colors.yellow('AutoIndex: '), argv.i ? colors.red('not visible') : colors.cyan('visible')].join('')),
206+
([colors.yellow('Serve GZIP Files: '), argv.g || argv.gzip ? colors.cyan('true') : colors.red('false')].join('')),
207+
([colors.yellow('Serve Brotli Files: '), argv.b || argv.brotli ? colors.cyan('true') : colors.red('false')].join('')),
208+
([colors.yellow('Default File Extension: '), argv.e ? colors.cyan(argv.e) : (argv.ext ? colors.cyan(argv.ext) : colors.red('none'))].join(''))
188209
].join('\n'));
189210

190211
logger.info(colors.yellow('\nAvailable on:'));
@@ -203,7 +224,12 @@ function listen(port) {
203224
}
204225

205226
if (typeof proxy === 'string') {
206-
logger.info('Unhandled requests will be served from: ' + proxy);
227+
if (proxyOptions) {
228+
logger.info('Unhandled requests will be served from: ' + proxy + '. Options: ' + JSON.stringify(proxyOptions));
229+
}
230+
else {
231+
logger.info('Unhandled requests will be served from: ' + proxy);
232+
}
207233
}
208234
if (options.showQR) {
209235
qrcode.generate(plainIp, { small: true }, function (qr) {

doc/http-server.1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ Enable logging of the client IP address.
8585
.BI \-P ", " \-\-proxy
8686
Fallback proxy if the request cannot be resolved.
8787

88+
.TP
89+
.BI \-\-proxy\-options
90+
Pass proxy options using nested dotted objects.
91+
8892
.TP
8993
.BI \-\-username " " \fIUSERNAME\fR
9094
Username for basic authentication.
@@ -98,7 +102,7 @@ Can also be specified with the environment variable NODE_HTTP_SERVER_PASSWORD.
98102
Defaults to none.
99103

100104
.TP
101-
.BI \-S ", " \-\-ssl
105+
.BI \-S ", " \-\-tls ", " \-\-ssl
102106
Enable https.
103107

104108
.TP

lib/core/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function shouldCompressBrotli(req) {
5555

5656
function hasGzipId12(gzipped, cb) {
5757
const stream = fs.createReadStream(gzipped, { start: 0, end: 1 });
58-
let buffer = Buffer('');
58+
let buffer = Buffer.from('');
5959
let hasBeenCalled = false;
6060

6161
stream.on('data', (chunk) => {
@@ -318,6 +318,9 @@ module.exports = function createMiddleware(_dir, _options) {
318318
stream.on('error', (err) => {
319319
status['500'](res, next, { error: err });
320320
});
321+
stream.on('close', () => {
322+
stream.destroy();
323+
})
321324
}
322325

323326

0 commit comments

Comments
 (0)