Skip to content

Commit

Permalink
Merge 379a545 into 33e5ac9
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-Valentine committed Apr 29, 2020
2 parents 33e5ac9 + 379a545 commit 9dac54b
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/ponse.js
Expand Up @@ -8,6 +8,7 @@ const {unescape} = require('querystring');
const {lstat} = require('fs').promises;

const type = require('itype');
const filetype = require('file-type');
const tryToCatch = require('try-to-catch');
const files = require('files-io');

Expand Down Expand Up @@ -217,6 +218,11 @@ async function sendFile(params) {
if (!isGzip)
p.length = length;

const [, type] = await tryToCatch(filetype.fromFile, p.name);

if (type)
p.mime = type.mime;

setHeader(params);

const options = {
Expand Down Expand Up @@ -359,4 +365,3 @@ function getStatic(options, request, response) {
response,
});
}

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -38,6 +38,7 @@
},
"dependencies": {
"debug": "^4.1.0",
"file-type": "^14.2.0",
"files-io": "^3.0.0",
"itype": "^3.0.1",
"try-to-catch": "^3.0.0"
Expand Down
Binary file added test/fixtures/mimetype/294px-Railroad1860
Binary file not shown.
Binary file added test/fixtures/mimetype/294px-Railroad1860.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/mimetype/320px-Floppy_disk_2009_G1
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/fixtures/mimetype/hello.txt
@@ -0,0 +1 @@
Hello, world!
26 changes: 26 additions & 0 deletions test/ponse.js
Expand Up @@ -13,6 +13,8 @@ const {

const {request} = require('serve-once')(ponse.static);

const {keys} = Object;

test('ponse: path traversal: status', async (t) => {
const {status} = await request.get('/../../../../../../etc/passwd', {
options: {
Expand Down Expand Up @@ -112,3 +114,27 @@ test('ponse: send', (t) => {
t.end();
});

test('ponse: get: content-type', async (t) => {
const filesToMimes = {
'320px-Floppy_disk_2009_G1' : 'image/jpeg',
'320px-Floppy_disk_2009_G1.jpg' : 'image/jpeg',
'294px-Railroad1860.png' : 'image/png',
'294px-Railroad1860' : 'image/png',
'hello.txt' : 'text/plain; charset=UTF-8',
};

for (const file of keys(filesToMimes)) {
const {headers} = await request.get('/' + file, {
options: {
root: __dirname + '/../test/fixtures/mimetype/',
},
});

const reportedMime = headers.get('content-type');
const expect = filesToMimes[file];

t.equal(reportedMime, expect, 'should equal');
}

t.end();
});

0 comments on commit 9dac54b

Please sign in to comment.