Skip to content

Commit 8aa6a8f

Browse files
committed
Add support for Access-Control-Expose-Headers and default CORS headers to CORS-safelisted headers
Similar to this (stale) PR: http-party#546 Resolves http-party#545 Usable for cruise-automation/webviz#247
1 parent 8f7fcb0 commit 8aa6a8f

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

bin/http-server

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ if (argv.h || argv.help) {
3434
' -e --ext Default file extension if none supplied [none]',
3535
' -s --silent Suppress log messages from output',
3636
' --cors[=headers] Enable CORS via the "Access-Control-Allow-Origin" header',
37-
' Optionally provide CORS headers list separated by commas',
37+
' Optionally provide CORS request/response headers list separated by commas',
38+
' headers defaults to CORS-safelisted request/response headers',
3839
' -o [path] Open browser window after starting the server.',
3940
' Optionally provide a URL path to open the browser window to.',
4041
' -c Cache time (max-age) in seconds [3600], e.g. -c10 for 10 seconds.',

lib/http-server.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,20 @@ function HttpServer(options) {
100100

101101
if (options.cors) {
102102
this.headers['Access-Control-Allow-Origin'] = '*';
103-
this.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Range';
103+
// Default allowed headers to CORS-safelisted request headers:
104+
// https://fetch.spec.whatwg.org/#cors-safelisted-request-header
105+
this.headers['Access-Control-Allow-Headers'] = 'Accept, Accept-Language, Content-Language, Content-Type';
106+
// Default exposed headers to CORS-safelisted response headers:
107+
// https://fetch.spec.whatwg.org/#cors-safelisted-response-header-name
108+
this.headers['Access-Control-Expose-Headers'] = 'Cache-Control, Content-Language, Content-Length, Content-Type, Expires, Last-Modified, Pragma';
104109
if (options.corsHeaders) {
105110
options.corsHeaders.split(/\s*,\s*/)
106-
.forEach(function (h) { this.headers['Access-Control-Allow-Headers'] += ', ' + h; }, this);
111+
.forEach(function (h) {
112+
this.headers['Access-Control-Allow-Headers'] += ', ' + h;
113+
this.headers['Access-Control-Expose-Headers'] += ', ' + h;
114+
}, this);
107115
}
116+
console.log('headers', this.headers);
108117
before.push(corser.create(options.corsHeaders ? {
109118
requestHeaders: this.headers['Access-Control-Allow-Headers'].split(/\s*,\s*/)
110119
} : null));

0 commit comments

Comments
 (0)