Differences From
Artifact [b6a24abaaf]:
1 +-- vim: ft=terra
1 2 local knowntypes = {
2 - ['text/csrc'] = {
3 - ext = 'c', lang = 'c';
4 - };
5 - ['text/html'] = {
6 - ext = 'html', lang = 'html';
7 - unsafe = true;
3 + html = {
4 + ext = 'html', kind = 'markup', unsafe = true, id = {
5 + 'text/html';
6 + 'application/xhtml+xml';
7 + 'application/vnd.wap.xhtml+xml';
8 + };
8 9 };
9 - ['text/x-lua'] = {
10 - ext = 'lua', lang = 'lua';
11 - };
12 - ['text/markdown'] = {
10 + flash = { ext = 'swf', kind = 'vm_prog', id = 'application/x-shockwave-flash', unsafe = true, binary = true };
11 + java = { ext = 'java', kind = 'vm_prog', id = 'application/java', unsafe = true, binary = true };
12 + css = { ext = 'css', kind = 'lang', id = 'text/css'};
13 + text = { ext = 'txt', kind = 'text', id = 'text/plain' };
14 + c = { ext = 'c', kind = 'prog_lang', id = 'text/csrc' };
15 + xml = { ext = 'xml', kind = 'markup', unsafe = true, id = 'text/xml' };
16 + lua = { ext = 'lua', kind = 'prog_lang', id = 'text/x-lua' };
17 + ansi = { ext = 'ans', kind = 'text', id = 'text/x-ansi', doc = true, binary = true};
18 + mkdown = { ext = 'md', kind = 'text', doc = true; id = 'text/markdown';
13 19 formatter = 'smackdown';
14 - ext = 'md', doc = true;
20 + };
21 + json = {
22 + ext = 'json', kind = 'lang', id = {
23 + 'application/json';
24 + 'application/activity+json';
25 + 'application/ld+json';
26 + 'application/jrd+json';
27 + };
15 28 };
29 + svg = { ext = 'svg', kind = 'image', id = 'image/svg+xml' };
30 + webp = { ext = 'webp', kind = 'image', id = 'image/webp', binary = true };
31 + png = { ext = 'png', kind = 'image', id = 'image/png', binary = true };
32 + jpeg = { ext = 'jpg', kind = 'image', id = 'image/jpeg', binary = true };
33 +
34 + -- wildcard
35 + none = { id = '*/*' };
36 +}
37 +
38 +local idcache = {}
39 +
40 +
41 +local pstr = lib.str.t
42 +local filekind = lib.enum [[none image text lang prog_lang markup vm_prog]]
43 +local struct mime {
44 + key: pstr
45 + canonical: pstr
46 + safe: bool
47 + binary: bool
48 + ext: pstr
49 + kind: filekind.t
50 + output: lib.http.mime.t
51 +}
52 +
53 +local typestore = {}
54 +for typecode, ty in pairs(knowntypes) do
55 + ty.key = typecode
56 + if type(ty.id) == 'string' then ty.id = {ty.id} end
57 + for i, mime in ipairs(ty.id) do
58 + idcache[mime] = ty
59 + end
60 +
61 + local op = lib.http.mime[typecode]
62 + if op == nil then op = lib.http.mime.none end
63 + print(typecode,op)
64 +
65 + ty.offset = #typestore
66 + typestore[#typestore + 1] = `mime {
67 + key = typecode;
68 + canonical = [ty.id[1]];
69 + safe = [not ty.unsafe];
70 + ext = [ty.ext or `pstr{nil,0}];
71 + kind = [ty.kind and filekind[ty.kind] or filekind.none];
72 + binary = [ty.binary or false];
73 + output = [op];
74 + }
75 +
76 +end
77 +
78 +local typedex = global(`array([typestore]))
79 +local struct mimemapping {
80 + string: pstr
81 + type: &mime
82 +}
83 +
84 +local typemap_l = {}
85 +for mime, ty in pairs(idcache) do
86 + typemap_l[#typemap_l + 1] = `mimemapping {
87 + string = mime;
88 + type = &typedex[ [ty.offset] ];
89 + }
90 +
91 +end
92 +local typemap = global(`array([typemap_l]));
93 +
94 +
95 +return {
96 + type = mime;
97 + types = knowntypes;
98 + tbl = idcache;
99 + typedex = typedex;
100 + lookup = terra(m: pstr): &mime
101 + for i=0, [#typemap_l] do
102 + if m:cmp(typemap[i].string) then
103 + lib.io.fmt('returning type %s %u\n', typemap[i].type.key, typemap[i].type.output)
104 + return typemap[i].type
105 + end
106 + end
107 + return nil
108 + end;
16 109 }