From f64ef3e898db4db515562789c4f9c492f7735281 Mon Sep 17 00:00:00 2001 From: Fabio Poloni Date: Tue, 1 Jun 2021 22:55:58 +0200 Subject: [PATCH] add custom headers --- README.md | 2 ++ bin/http-server | 19 +++++++++++++++++++ test/http-server-test.js | 26 ++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/README.md b/README.md index eeb3f9868..e983d4ff5 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,8 @@ Using `npx` you can run the script without installing it first: `-r` or `--robots` Provide a /robots.txt (whose content defaults to `User-agent: *\nDisallow: /`) `--no-dotfiles` Do not show dotfiles + +`-H` or `--header` Send the specified header with responses, e.g. `-H "Strict-Transport-Security: max-age=315360000; includeSubdomains"`. `-h` or `--help` Print this list and exit. diff --git a/bin/http-server b/bin/http-server index 42ebf651f..bca166205 100755 --- a/bin/http-server +++ b/bin/http-server @@ -51,6 +51,8 @@ if (argv.h || argv.help) { '', ' -r --robots Respond to /robots.txt [User-agent: *\\nDisallow: /]', ' --no-dotfiles Do not show dotfiles', + ' -H --header Send the specified header with responses,', + ' e.g. -H \'X-MY-HEADER: foo\'', ' -h --help Print this list and exit.', ' -v --version Print the version and exit.' ].join('\n')); @@ -113,6 +115,20 @@ else { listen(port); } +function addHeader(headers, headersList) { + if (!Array.isArray(headersList)) { + headersList = [headersList]; + } + + for (var idx in headersList) { + var idx = headersList[idx].indexOf(':'), + key = headersList[idx].substring(0, idx), + val = headersList[idx].substring(idx + 1).trim(); + + headers[key] = val; + } +} + function listen(port) { var options = { root: argv._[0], @@ -127,6 +143,7 @@ function listen(port) { logFn: logger.request, proxy: proxy, showDotfiles: argv.dotfiles, + headers: {}, username: argv.username || process.env.NODE_HTTP_SERVER_USERNAME, password: argv.password || process.env.NODE_HTTP_SERVER_PASSWORD }; @@ -159,6 +176,8 @@ function listen(port) { } } + addHeader(options.headers, argv.H || argv.header); + var server = httpServer.createServer(options); server.listen(port, host, function () { var canonicalHost = host === '0.0.0.0' ? '127.0.0.1' : host, diff --git a/test/http-server-test.js b/test/http-server-test.js index 6d5fce462..f206c4939 100644 --- a/test/http-server-test.js +++ b/test/http-server-test.js @@ -514,5 +514,31 @@ vows.describe('http-server').addBatch({ teardown: function (server) { server.close(); } + }, + 'When custom headers are set': { + topic: function () { + var server = httpServer.createServer({ + root: root, + headers: { + 'X-Custom': 'Test' + } + }); + server.listen(8087); + this.callback(null, server); + }, + requesting: { + topic: function () { + request('http://127.0.0.1:8087/', this.callback); + }, + 'should respond with status code 200': function (err, res) { + assert.equal(res.statusCode, 200); + }, + 'and response X-Custom header should contain 1': function (err, res) { + assert.ok(res.headers['x-custom']); + } + }, + teardown: function (server) { + server.close(); + } } }).export(module);