From a52caef702f0d3a25885d9bff737797e4c9ced04 Mon Sep 17 00:00:00 2001 From: Viktor Hedefalk Date: Thu, 26 May 2016 16:10:54 +0200 Subject: [PATCH 1/2] added option for http response headers --- bin/http-server | 10 ++++++++++ lib/http-server.js | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/bin/http-server b/bin/http-server index a432dd3e..eabddb1c 100755 --- a/bin/http-server +++ b/bin/http-server @@ -36,6 +36,7 @@ if (argv.h || argv.help) { ' -S --ssl Enable https.', ' -C --cert Path to ssl cert file (default: cert.pem).', ' -K --key Path to ssl key file (default: key.pem).', + ' -H --header Add extra header to all responses, eg. "X-Frame-Options: DENY"', '', ' -r --robots Respond to /robots.txt [User-agent: *\\nDisallow: /]', ' -h --help Print this list and exit.' @@ -108,6 +109,15 @@ function listen(port) { options.corsHeaders = argv.cors; } } + + var extraHeaders = argv.H || argv.header; + if(extraHeaders) { + if (typeof extraHeaders === 'array') { + options.extraHeaders = extraHeaders; + } else { + options.extraHeaders = [extraHeaders]; + } + } if (ssl) { options.https = { diff --git a/lib/http-server.js b/lib/http-server.js index 8d849109..d729e5d7 100644 --- a/lib/http-server.js +++ b/lib/http-server.js @@ -75,6 +75,13 @@ function HttpServer(options) { requestHeaders: this.headers['Access-Control-Allow-Headers'].split(/\s*,\s*/) } : null)); } + + if(options.extraHeaders) { + options.extraHeaders.forEach(function (header) { + var split = header.split(":"); + this.headers[split[0]] = split[1]; + }, this); + } if (options.robots) { before.push(function (req, res) { From fe1f8397c95fc9ad176e5965b55825924b5bad37 Mon Sep 17 00:00:00 2001 From: Viktor Hedefalk Date: Thu, 26 May 2016 16:49:40 +0200 Subject: [PATCH 2/2] fixed lints --- bin/http-server | 9 +++++---- lib/http-server.js | 6 +++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/bin/http-server b/bin/http-server index eabddb1c..e12f48d0 100755 --- a/bin/http-server +++ b/bin/http-server @@ -109,12 +109,13 @@ function listen(port) { options.corsHeaders = argv.cors; } } - + var extraHeaders = argv.H || argv.header; - if(extraHeaders) { - if (typeof extraHeaders === 'array') { + if (extraHeaders) { + if (Array.isArray(extraHeaders)) { options.extraHeaders = extraHeaders; - } else { + } + else { options.extraHeaders = [extraHeaders]; } } diff --git a/lib/http-server.js b/lib/http-server.js index d729e5d7..5ba5459f 100644 --- a/lib/http-server.js +++ b/lib/http-server.js @@ -75,10 +75,10 @@ function HttpServer(options) { requestHeaders: this.headers['Access-Control-Allow-Headers'].split(/\s*,\s*/) } : null)); } - - if(options.extraHeaders) { + + if (options.extraHeaders) { options.extraHeaders.forEach(function (header) { - var split = header.split(":"); + var split = header.split(/:(.+)?/); this.headers[split[0]] = split[1]; }, this); }