forked from lpereira/lwan
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.lua
70 lines (60 loc) · 1.55 KB
/
test.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
function handle_get_hello(req)
local name = req:query_param[[name]]
if name then
req:set_response("Hello, " .. name .. "!")
else
req:set_response("Hello, World!")
end
end
function handle_get_cookie(req)
req:set_headers({
["Set-Cookie"] = {
"SESSION_ID=1234; HttpOnly",
"LANG=pt_BR"
},
['Other-Header'] = 'some random value',
['Yet-Another-Header'] = '42'
})
local foo = req:cookie[[FOO]]
if foo then
req:set_response("Cookie FOO has value: " .. foo)
else
req:set_response("Cookie FOO not set")
end
end
function handle_get_sse(req)
for i = 0, 10 do
req:send_event("counter-changed", "event" .. i)
end
end
function handle_get_chunked(req)
for i = 0, 10 do
req:say("Chunk #" .. i .. "\n")
end
end
function handle_get_random(req)
req:set_response("Random number: " .. math.random())
end
function handle_get_brew_coffee(req)
return 418
end
function handle_get_invalid_code(req)
return 42
end
function string.starts(String, Start)
-- From http://lua-users.org/wiki/StringRecipes
return string.sub(String, 1, string.len(Start)) == Start
end
function is_get_handler(s, func)
if string.starts(s, "handle_get_") then
return type(func) == "function"
end
return false
end
function handle_get_root(req)
for key, value in pairs(_G) do
if is_get_handler(key, value) then
req:say("<li><a href=" .. string.sub(key, 12) .. ">" .. key .. "</a></li>\n")
end
end
end