Skip to content

Commit eb5e89a

Browse files
committed
Merge pull request http-party#150 from MichalCz/master
Cors with additional headers
2 parents a9e160d + f21a2fe commit eb5e89a

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

bin/http-server

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ if (argv.h || argv.help) {
2424
' -i Display autoIndex [true]',
2525
' -e --ext Default file extension if none supplied [none]',
2626
' -s --silent Suppress log messages from output',
27-
' --cors Enable CORS via the "Access-Control-Allow-Origin" header',
27+
' --cors[=headers] Enable CORS via the "Access-Control-Allow-Origin" header',
28+
' Optionally provide CORS headers list separated by commas',
2829
' -o [path] Open browser window after starting the server',
2930
' -c Cache time (max-age) in seconds [3600], e.g. -c10 for 10 seconds.',
3031
' To disable caching, use -c-1.',
@@ -103,6 +104,9 @@ function listen(port) {
103104

104105
if (argv.cors) {
105106
options.cors = true;
107+
if (typeof argv.cors === 'string') {
108+
options.corsHeaders = argv.cors;
109+
}
106110
}
107111

108112
if (ssl) {

lib/http-server.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,13 @@ function HttpServer(options) {
6767
if (options.cors) {
6868
this.headers['Access-Control-Allow-Origin'] = '*';
6969
this.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Range';
70-
71-
before.push(corser.create());
70+
if (options.corsHeaders) {
71+
options.corsHeaders.split(/\s*,\s*/)
72+
.forEach(function (h) { this.headers['Access-Control-Allow-Headers'] += ', ' + h; }, this);
73+
}
74+
before.push(corser.create(options.corsHeaders ? {
75+
requestHeaders: this.headers['Access-Control-Allow-Headers'].split(/\s*,\s*/)
76+
} : null));
7277
}
7378

7479
if (options.robots) {

test/http-server-test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ vows.describe('http-server').addBatch({
129129
topic: function () {
130130
var server = httpServer.createServer({
131131
root: root,
132-
cors: true
132+
cors: true,
133+
corsHeaders: 'X-Test'
133134
});
134135
server.listen(8082);
135136
this.callback(null, server);
@@ -148,6 +149,9 @@ vows.describe('http-server').addBatch({
148149
},
149150
'status code should be 204': function (err, res) {
150151
assert.equal(res.statusCode, 204);
152+
},
153+
'response Access-Control-Allow-Headers should contain X-Test': function (err, res) {
154+
assert.ok(res.headers['access-control-allow-headers'].split(/\s*,\s*/g).indexOf('X-Test') >= 0, 204);
151155
}
152156
}
153157
}

0 commit comments

Comments
 (0)