Skip to content

Commit

Permalink
Adds ability to proxy requests insecurely
Browse files Browse the repository at this point in the history
  • Loading branch information
johntron committed Jan 17, 2016
1 parent c30a963 commit f266746
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -64,6 +64,8 @@ This will install `http-server` globally so that it may be run from the command

`-P` or `--proxy` Proxies all requests which can't be resolved locally to the given url. e.g.: -P http://someurl.com

`--proxy-secure` Verify SSL certificates for proxied requests (defaults to true)

`-S` or `--ssl` Enable https.

`-C` or `--cert` Path to ssl cert file (default: cert.pem).
Expand Down
12 changes: 10 additions & 2 deletions bin/http-server
Expand Up @@ -32,6 +32,7 @@ if (argv.h || argv.help) {
' -U --utc Use UTC time format in log messages.',
'',
' -P --proxy Fallback proxy if the request cannot be resolved. e.g.: http://someurl.com',
' --proxy-secure Verify proxied SSL certificates [true]',
'',
' -S --ssl Enable https.',
' -C --cert Path to ssl cert file (default: cert.pem).',
Expand Down Expand Up @@ -98,10 +99,17 @@ function listen(port) {
autoIndex: argv.i,
robots: argv.r || argv.robots,
ext: argv.e || argv.ext,
logFn: logger.request,
proxy: proxy
logFn: logger.request
};

if (proxy) {
options.proxy = {
target: proxy,
secure: argv['proxy-secure'] !== 'false',
changeOrigin: true
};
}

if (argv.cors) {
options.cors = true;
if (typeof argv.cors === 'string') {
Expand Down
9 changes: 3 additions & 6 deletions lib/http-server.js
Expand Up @@ -98,16 +98,13 @@ function HttpServer(options) {
autoIndex: this.autoIndex,
defaultExt: this.ext,
contentType: this.contentType,
handleError: typeof options.proxy !== 'string'
handleError: typeof options.proxy !== 'object'
}));

if (typeof options.proxy === 'string') {
if (options.proxy) {
var proxy = httpProxy.createProxyServer({});
before.push(function (req, res) {
proxy.web(req, res, {
target: options.proxy,
changeOrigin: true
});
proxy.web(req, res, options.proxy);
});
}

Expand Down

0 comments on commit f266746

Please sign in to comment.