summaryrefslogtreecommitdiff
path: root/ipl/packs/icondb/cgi.icn
blob: 2b47f9dcc8ed376bca7efd91131ab2966219d7e8 (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

#everything needed for typical web form handling

procedure cgiparms() #returns a table, mapping names to lists of values
	local GET_data, POST_data, data, i, pname, pvalue, s
    static result
    initial {
        result := table()
        GET_data := trim(getenv("QUERY_STRING"))|""
        if *GET_data = 0 then GET_data := &null
        POST_data := reads(&input, getenv("CONTENT_LENGTH"))
        if \GET_data & \POST_data then
            data := GET_data || "&" || POST_data
        else
            data := \GET_data | \POST_data
        if /data then return result
        data ? every i := upto('&')|0 do {
            tab(i) ? {
                pname := _urldecode( tab(upto('=')) )
                move(1)
                pvalue := _urldecode( tab(0) )
                /result[pname] := []
                put( result[pname], pvalue )
            }
            if pos(0) then break
            move(1)
        }
    }
    return result
end

procedure _urldecode(url)
	local s
    s := ""
    url ? repeat {
        s ||:= tab(upto('%+')|0)
        if pos(0) then return s
        case move(1) of {
			"%" : s ||:= char("16r" || map(move(2)) )
			"+" : s ||:= " "
		}
    }
end