Skip to content

Commit cd1afb7

Browse files
authoredAug 5, 2021
Merge pull request #706 from zbynek/no-charset-binary
Remove charset from header of binary files
2 parents 46c0ce7 + 7830ac2 commit cd1afb7

9 files changed

+73
-70
lines changed
 

‎lib/core/index.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,9 @@ module.exports = function createMiddleware(_dir, _options) {
234234
const etag = generateEtag(stat, weakEtags);
235235
let cacheControl = cache;
236236
let stream = null;
237-
if (contentType) {
238-
charSet = mime.charsets.lookup(contentType, 'utf-8');
239-
if (charSet) {
240-
contentType += `; charset=${charSet}`;
241-
}
237+
if (contentType && isTextFile(contentType)) {
238+
// Assume text types are utf8
239+
contentType += '; charset=UTF-8';
242240
}
243241

244242
if (file === gzippedFile) { // is .gz picked up
@@ -400,6 +398,10 @@ module.exports = function createMiddleware(_dir, _options) {
400398
});
401399
}
402400

401+
function isTextFile(mimeType) {
402+
return (/^text\/|^application\/(javascript|json)/).test(mimeType);
403+
}
404+
403405
// serve gzip file if exists and is valid
404406
function tryServeWithGzip() {
405407
fs.stat(gzippedFile, (err, stat) => {

‎test/check-headers.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const request = require('request');
2+
3+
module.exports = (t, server, path, check) => {
4+
server.listen(() => {
5+
const port = server.address().port;
6+
const uri = `http://localhost:${port}/${path}`;
7+
8+
request.get({ uri }, (err, res) => {
9+
t.ifError(err);
10+
t.equal(res.statusCode, 200);
11+
check(t, res.headers);
12+
});
13+
});
14+
t.once('end', () => {
15+
server.close();
16+
});
17+
}

‎test/cli.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ test('setting mimeTypes via cli - .types file', (t) => {
9898
ecstatic.stdout.on('data', (msg) => {
9999
checkServerIsRunning(`${defaultUrl}:${port}/custom_mime_type.opml`, msg, t, (err, res) => {
100100
t.error(err);
101-
t.equal(res.headers['content-type'], 'application/secret; charset=utf-8');
101+
t.equal(res.headers['content-type'], 'application/secret');
102102
});
103103
});
104104
});
@@ -118,7 +118,7 @@ test('setting mimeTypes via cli - directly', (t) => {
118118
ecstatic.stdout.on('data', (msg) => {
119119
checkServerIsRunning(`${defaultUrl}:${port}/custom_mime_type.opml`, msg, t, (err, res) => {
120120
t.error(err);
121-
t.equal(res.headers['content-type'], 'application/x-my-type; charset=utf-8');
121+
t.equal(res.headers['content-type'], 'application/x-my-type');
122122
});
123123
});
124-
});
124+
});

‎test/content-type.test.js

+32-11
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
const test = require('tap').test;
44
const http = require('http');
5-
const request = require('request');
65
const ecstatic = require('../lib/core');
6+
const checkHeaders = require('./check-headers.js');
77

8-
test('default default contentType', (t) => {
8+
const root = `${__dirname}/public/`;
9+
10+
test('global default contentType', (t) => {
911
let server = null;
1012
try {
1113
server = http.createServer(ecstatic({
12-
root: `${__dirname}/public/`,
14+
root,
1315
contentType: 'text/plain',
1416
}));
1517
} catch (e) {
@@ -19,13 +21,32 @@ test('default default contentType', (t) => {
1921

2022
t.plan(3);
2123

22-
server.listen(0, () => {
23-
const port = server.address().port;
24-
request.get(`http://localhost:${port}/f_f`, (err, res) => {
25-
t.ifError(err);
26-
t.equal(res.statusCode, 200);
27-
t.equal(res.headers['content-type'], 'text/plain; charset=UTF-8');
28-
server.close(() => { t.end(); });
29-
});
24+
25+
checkHeaders(t, server, 'f_f', (t, headers) => {
26+
t.equal(headers['content-type'], 'text/plain; charset=UTF-8');
27+
});
28+
});
29+
30+
test('content type text', (t) => {
31+
t.plan(3);
32+
33+
const server = http.createServer(
34+
ecstatic({root})
35+
);
36+
37+
checkHeaders(t, server, 'subdir/e.html', (t, headers) => {
38+
t.equal(headers['content-type'], 'text/html; charset=UTF-8');
39+
});
40+
});
41+
42+
test('content type binary', (t) => {
43+
t.plan(3);
44+
45+
const server = http.createServer(
46+
ecstatic({root})
47+
);
48+
49+
checkHeaders(t, server, 'subdir/app.wasm', (t, headers) => {
50+
t.equal(headers['content-type'], 'application/wasm');
3051
});
3152
});

‎test/custom-content-type-file-secret.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ test('custom contentType via .types file', (t) => {
2424
request.get(`http://localhost:${port}/custom_mime_type.opml`, (err, res) => {
2525
t.ifError(err);
2626
t.equal(res.statusCode, 200, 'custom_mime_type.opml should be found');
27-
t.equal(res.headers['content-type'], 'application/secret; charset=utf-8');
27+
t.equal(res.headers['content-type'], 'application/secret');
2828
server.close(() => { t.end(); });
2929
});
3030
});

‎test/custom-content-type-file.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ test('custom contentType via .types file', (t) => {
4040
request.get(`http://localhost:${port}/custom_mime_type.opml`, (err, res) => {
4141
t.ifError(err);
4242
t.equal(res.statusCode, 200, 'custom_mime_type.opml should be found');
43-
t.equal(res.headers['content-type'], 'application/foo; charset=utf-8');
43+
t.equal(res.headers['content-type'], 'application/foo');
4444
server.close(() => { t.end(); });
4545
});
4646
});

‎test/custom-content-type.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ test('custom contentType', (t) => {
2626
request.get(`http://localhost:${port}/custom_mime_type.opml`, (err, res) => {
2727
t.ifError(err);
2828
t.equal(res.statusCode, 200, 'custom_mime_type.opml should be found');
29-
t.equal(res.headers['content-type'], 'application/jon; charset=utf-8');
29+
t.equal(res.headers['content-type'], 'application/jon');
3030
server.close(() => { t.end(); });
3131
});
3232
});

‎test/headers.test.js

+10-48
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const test = require('tap').test;
44
const ecstatic = require('../lib/core');
55
const http = require('http');
6-
const request = require('request');
6+
const checkHeaders = require('./check-headers.js');
77

88
const root = `${__dirname}/public`;
99

@@ -22,19 +22,9 @@ test('headers object', (t) => {
2222
})
2323
);
2424

25-
server.listen(() => {
26-
const port = server.address().port;
27-
const uri = `http://localhost:${port}/subdir`;
28-
29-
request.get({ uri }, (err, res) => {
30-
t.ifError(err);
31-
t.equal(res.statusCode, 200);
32-
t.equal(res.headers.wow, 'sweet');
33-
t.equal(res.headers.cool, 'beans');
34-
});
35-
});
36-
t.once('end', () => {
37-
server.close();
25+
checkHeaders(t, server, 'subdir', (t, headers) => {
26+
t.equal(headers.wow, 'sweet');
27+
t.equal(headers.cool, 'beans');
3828
});
3929
});
4030

@@ -50,18 +40,8 @@ test('header string', (t) => {
5040
})
5141
);
5242

53-
server.listen(() => {
54-
const port = server.address().port;
55-
const uri = `http://localhost:${port}/subdir`;
56-
57-
request.get({ uri }, (err, res) => {
58-
t.ifError(err);
59-
t.equal(res.statusCode, 200);
60-
t.equal(res.headers.beep, 'boop');
61-
});
62-
});
63-
t.once('end', () => {
64-
server.close();
43+
checkHeaders(t, server, 'subdir', (t, headers) => {
44+
t.equal(headers.beep, 'boop');
6545
});
6646
});
6747

@@ -80,17 +60,8 @@ test('header array', (t) => {
8060
})
8161
);
8262

83-
server.listen(() => {
84-
const port = server.address().port;
85-
const uri = `http://localhost:${port}/subdir`;
86-
request.get({ uri }, (err, res) => {
87-
t.ifError(err);
88-
t.equal(res.statusCode, 200);
89-
t.equal(res.headers.beep, 'boop');
90-
});
91-
});
92-
t.once('end', () => {
93-
server.close();
63+
checkHeaders(t, server, 'subdir', (t, headers) => {
64+
t.equal(headers.beep, 'boop');
9465
});
9566
});
9667

@@ -109,16 +80,7 @@ test('H array', (t) => {
10980
})
11081
);
11182

112-
server.listen(() => {
113-
const port = server.address().port;
114-
const uri = `http://localhost:${port}/subdir`;
115-
request.get({ uri }, (err, res) => {
116-
t.ifError(err);
117-
t.equal(res.statusCode, 200);
118-
t.equal(res.headers.beep, 'boop');
119-
});
120-
});
121-
t.once('end', () => {
122-
server.close();
83+
checkHeaders(t, server, 'subdir', (t, headers) => {
84+
t.equal(headers.beep, 'boop');
12385
});
12486
});

‎test/public/subdir/app.wasm

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fake WebAssemble file

0 commit comments

Comments
 (0)
Please sign in to comment.