Skip to content
This repository was archived by the owner on Jun 3, 2020. It is now read-only.

Commit 6bb1733

Browse files
committed
dat:// - look for file extensions based on Accept header
1 parent d97de04 commit 6bb1733

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

dat/protocol.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,10 @@ exports.electronHandler = async function (request, respond) {
213213
await tryStat(filepath)
214214
} else {
215215
await tryStat(filepath)
216-
await tryStat(filepath + '.html') // fallback to .html
216+
for (let ext of mime.acceptHeaderExtensions(request.headers.Accept)) {
217+
// fallback to different requested headers
218+
await tryStat(filepath + ext)
219+
}
217220
if (entry && entry.isDirectory()) {
218221
// unexpected directory, give the .html fallback a chance
219222
let dirEntry = entry

lib/mime.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ exports.isFileContentBinary = async function (fsInstance, filepath) {
6363
})
6464
}
6565

66+
// for a given HTTP accept header, provide a list of file-extensions to try
67+
exports.acceptHeaderExtensions = function (accept) {
68+
var exts = []
69+
accept = accept.split(',')
70+
if (accept.includes('text/html') || (accept.length === 1 && accept[0] === '*/*')) exts.push('.html')
71+
if (accept.includes('text/css')) exts.push('.css')
72+
if (accept.includes('image/*') || accept.includes('image/apng')) exts = exts.concat(['.png', '.jpg', '.jpeg', '.gif'])
73+
return exts
74+
}
75+
6676
// pulled from https://github.com/gjtorikian/isBinaryFile
6777
function isBinaryCheck (bytes) {
6878
var size = bytes.length

0 commit comments

Comments
 (0)