forked from cory/tildefriends
Move mime type shenanigans from JS => C.
This commit is contained in:
141
core/core.js
141
core/core.js
@ -8,116 +8,6 @@ let gStatsTimer = false;
|
||||
const k_content_security_policy =
|
||||
'sandbox allow-downloads allow-top-navigation-by-user-activation';
|
||||
|
||||
const k_mime_types = {
|
||||
css: 'text/css',
|
||||
html: 'text/html',
|
||||
js: 'text/javascript',
|
||||
json: 'text/json',
|
||||
map: 'application/json',
|
||||
svg: 'image/svg+xml',
|
||||
};
|
||||
|
||||
const k_magic_bytes = [
|
||||
{bytes: [0xff, 0xd8, 0xff, 0xdb], type: 'image/jpeg'},
|
||||
{
|
||||
bytes: [
|
||||
0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01,
|
||||
],
|
||||
type: 'image/jpeg',
|
||||
},
|
||||
{bytes: [0xff, 0xd8, 0xff, 0xee], type: 'image/jpeg'},
|
||||
{
|
||||
bytes: [
|
||||
0xff,
|
||||
0xd8,
|
||||
0xff,
|
||||
0xe1,
|
||||
null,
|
||||
null,
|
||||
0x45,
|
||||
0x78,
|
||||
0x69,
|
||||
0x66,
|
||||
0x00,
|
||||
0x00,
|
||||
],
|
||||
type: 'image/jpeg',
|
||||
},
|
||||
{bytes: [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a], type: 'image/png'},
|
||||
{bytes: [0x47, 0x49, 0x46, 0x38, 0x37, 0x61], type: 'image/gif'},
|
||||
{bytes: [0x47, 0x49, 0x46, 0x38, 0x39, 0x61], type: 'image/gif'},
|
||||
{
|
||||
bytes: [
|
||||
0x52,
|
||||
0x49,
|
||||
0x46,
|
||||
0x46,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
0x57,
|
||||
0x45,
|
||||
0x42,
|
||||
0x50,
|
||||
],
|
||||
type: 'image/webp',
|
||||
},
|
||||
{bytes: [0x3c, 0x73, 0x76, 0x67], type: 'image/svg+xml'},
|
||||
{
|
||||
bytes: [
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
0x66,
|
||||
0x74,
|
||||
0x79,
|
||||
0x70,
|
||||
0x6d,
|
||||
0x70,
|
||||
0x34,
|
||||
0x32,
|
||||
],
|
||||
type: 'audio/mpeg',
|
||||
},
|
||||
{
|
||||
bytes: [
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
0x66,
|
||||
0x74,
|
||||
0x79,
|
||||
0x70,
|
||||
0x69,
|
||||
0x73,
|
||||
0x6f,
|
||||
0x6d,
|
||||
],
|
||||
type: 'video/mp4',
|
||||
},
|
||||
{
|
||||
bytes: [
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
0x66,
|
||||
0x74,
|
||||
0x79,
|
||||
0x70,
|
||||
0x6d,
|
||||
0x70,
|
||||
0x34,
|
||||
0x32,
|
||||
],
|
||||
type: 'video/mp4',
|
||||
},
|
||||
{bytes: [0x4d, 0x54, 0x68, 0x64], type: 'audio/midi'},
|
||||
];
|
||||
|
||||
let k_static_files = [
|
||||
{uri: '/', path: 'index.html', type: 'text/html; charset=UTF-8'},
|
||||
];
|
||||
@ -941,29 +831,6 @@ function startsWithBytes(data, bytes) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* TODOC
|
||||
* @param {*} path
|
||||
* @returns
|
||||
*/
|
||||
function guessTypeFromName(path) {
|
||||
let extension = path.split('.').pop();
|
||||
return k_mime_types[extension];
|
||||
}
|
||||
|
||||
/**
|
||||
* TODOC
|
||||
* @param {*} data
|
||||
* @returns
|
||||
*/
|
||||
function guessTypeFromMagicBytes(data) {
|
||||
for (let magic of k_magic_bytes) {
|
||||
if (startsWithBytes(data, magic.bytes)) {
|
||||
return magic.type;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* TODOC
|
||||
* @param {*} response
|
||||
@ -979,7 +846,9 @@ function sendData(response, data, type, headers, status_code) {
|
||||
Object.assign(
|
||||
{
|
||||
'Content-Type':
|
||||
type || guessTypeFromMagicBytes(data) || 'application/binary',
|
||||
type ||
|
||||
httpd.mime_type_from_magic_bytes(data) ||
|
||||
'application/binary',
|
||||
'Content-Length': data.byteLength,
|
||||
},
|
||||
headers || {}
|
||||
@ -1348,7 +1217,9 @@ async function blobHandler(request, response, blobId, uri) {
|
||||
'Content-Security-Policy': k_content_security_policy,
|
||||
};
|
||||
data = await getBlobOrContent(id);
|
||||
let type = guessTypeFromName(uri) || guessTypeFromMagicBytes(data);
|
||||
let type =
|
||||
httpd.mime_type_from_extension(uri) ||
|
||||
httpd.mime_type_from_magic_bytes(data);
|
||||
sendData(response, data, type, headers);
|
||||
}
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user