1
2
3
4
5
6
7
8
9
10
11
..
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
..
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
-- vim: ft=terra
local m = {}
local util = dofile('common.lua')
struct m.header {
key: rawstring
value: rawstring
}
struct m.page {
respcode: uint16
body: lib.mem.ptr(int8)
................................................................................
local resps = {
[200] = 'OK';
[201] = 'Created';
[301] = 'Moved Permanently';
[302] = 'Found';
[303] = 'See Other';
[307] = 'Temporary Redirect';
[404] = 'Not Found';
[401] = 'Unauthorized';
[403] = 'Forbidden';
[418] = 'I\'m a teapot';
[405] = 'Method Not Allowed';
[500] = 'Internal Server Error';
}
local resptext = symbol(rawstring)
local resplen = symbol(intptr)
local respbranches = {}
................................................................................
end
end
m.codestr = terra(code: uint16)
var [resptext] var [resplen]
switch code do [respbranches] end
return resptext, resplen
end
m.page.methods = {
free = terra(self: &m.page)
self.body:free()
self.headers:free()
end;
send = terra(self: &m.page, con: &lib.net.mg_connection)
var code: rawstring
|
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
..
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
..
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
-- vim: ft=terra
local m = {}
local util = dofile('common.lua')
m.method = lib.enum { 'get', 'post', 'head', 'options', 'put', 'delete' }
m.findheader = terralib.externfunction('mg_http_get_header', {&lib.net.mg_http_message, rawstring} -> &lib.mem.ref(int8)) -- unfortunately necessary to access this function, as its return type conflicts with a function name
struct m.header {
key: rawstring
value: rawstring
}
struct m.page {
respcode: uint16
body: lib.mem.ptr(int8)
................................................................................
local resps = {
[200] = 'OK';
[201] = 'Created';
[301] = 'Moved Permanently';
[302] = 'Found';
[303] = 'See Other';
[307] = 'Temporary Redirect';
[400] = 'Bad Request';
[401] = 'Unauthorized';
[402] = 'Payment Required';
[403] = 'Forbidden';
[404] = 'Not Found';
[405] = 'Method Not Allowed';
[406] = 'Not Acceptable';
[418] = 'I\'m a teapot';
[405] = 'Method Not Allowed';
[500] = 'Internal Server Error';
}
local resptext = symbol(rawstring)
local resplen = symbol(intptr)
local respbranches = {}
................................................................................
end
end
m.codestr = terra(code: uint16)
var [resptext] var [resplen]
switch code do [respbranches] end
return resptext, resplen
end
terra m.hier(uri: lib.mem.ptr(int8)): lib.mem.ptr(lib.mem.ref(int8))
if uri.ct == 0 then return [lib.mem.ptr(lib.mem.ref(int8))] { ptr = nil, ct = 0 } end
var sz = 1
var start = 0 if uri.ptr[0] == @'/' then start = 1 end
for i = start, uri.ct do if uri.ptr[i] == @'/' then sz = sz + 1 end end
var lst = lib.mem.heapa([lib.mem.ref(int8)], sz)
if sz == 0 then
lst.ptr[0].ptr = uri.ptr
lst.ptr[0].ct = uri.ct
return lst
end
var idx: intptr = 0
var len: intptr = 0
lst.ptr[0].ptr = uri.ptr
for i = 0, uri.ct do
if uri.ptr[i] == @'/' then
if len == 0 then
lst.ptr[idx].ptr = lst.ptr[idx].ptr + 1
goto skip
end
lst.ptr[idx].ct = len
idx = idx + 1
lst.ptr[idx].ptr = uri.ptr + i + 1
len = 0
else
len = len + 1
end
::skip::end
lst.ptr[idx].ct = len
return lst
end
m.page.methods = {
free = terra(self: &m.page)
self.body:free()
self.headers:free()
end;
send = terra(self: &m.page, con: &lib.net.mg_connection)
var code: rawstring
|