Skip to content

Commit

Permalink
add custom headers
Browse files Browse the repository at this point in the history
  • Loading branch information
einfallstoll committed Jun 1, 2021
1 parent d1b7739 commit f64ef3e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
19 changes: 19 additions & 0 deletions bin/http-server
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down Expand Up @@ -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],
Expand All @@ -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
};
Expand Down Expand Up @@ -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,
Expand Down
26 changes: 26 additions & 0 deletions test/http-server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit f64ef3e

Please sign in to comment.