aboutsummaryrefslogtreecommitdiff
path: root/cgi.lua
blob: 503398458fbfec5a7be82b5eb3a29b21b5c8815c (plain)
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
local cgi={}

cgi.newline=false

function cgi.tag(key,value,attrib)
    local ret_str="<"..key

    if (attrib) then
        for a_key,a_val in pairs(attrib) do
            ret_str=ret_str.." "..a_key..'="'..a_val..'"'
        end
    end

    ret_str=ret_str..">"
    if cgi.newline==true then ret_str=ret_str.."\n" end

    ret_str=ret_str..value

    if cgi.newline==true then ret_str=ret_str.."\n" end

    ret_str=ret_str.."</"..key..">"

    if cgi.newline==true then ret_str=ret_str.."\n" end

    return ret_str
end

local function get_phrase(code)
    local cases={
        [200]="OK",
        [401]="Unauthorized",
    }
    if(cases[code]) then return cases[code] else return "" end
end

function cgi.header(code,headers)
    print("Status: "..code.." "..get_phrase(code))
    for h_name,header in pairs(headers) do
        print(h_name..": "..header)
    end
    print()
end

function cgi.render(content,headers,code)
    headers["Content-Length"] = tostring(content:len()+1)
    cgi.header(code,headers)
    print(content);
end

_G.m = cgi.tag

return cgi