diff options
| author | iceyrazor <iceyrazor@mailfence.com> | 2026-08-25 00:28:39 -0500 |
|---|---|---|
| committer | iceyrazor <iceyrazor@mailfence.com> | 2026-08-25 00:28:39 -0500 |
| commit | d302872f3ab4ec1870e93037b012019566fc554c (patch) | |
| tree | 4c355cc1c9e55a01535990822f6234b5b0198d20 /req-params.lua | |
| parent | 69c416c85bf92fca4b59aa78888a8aefbd0e0640 (diff) | |
Diffstat (limited to 'req-params.lua')
| -rwxr-xr-x | req-params.lua | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/req-params.lua b/req-params.lua new file mode 100755 index 0000000..077a7e4 --- /dev/null +++ b/req-params.lua @@ -0,0 +1,38 @@ +-- /var/www/cgi-bin/cgi.lua +-- Simple Lua CGI helper for GET and POST + +local params = {} + +-- Parse URL-encoded query string into a table +local function parse_query(qs) + local t = {} + for k, v in string.gmatch(qs or "", "([^&=]+)=([^&]*)") do + -- URL-decode + k = k:gsub("+", " "):gsub("%%(%x%x)", function(h) return string.char(tonumber(h,16)) end) + v = v:gsub("+", " "):gsub("%%(%x%x)", function(h) return string.char(tonumber(h,16)) end) + t[k] = v + end + return t +end + +-- GET parameters +params.get_params = parse_query(os.getenv("QUERY_STRING")) + +-- POST parameters +params.post_params = {} +local content_length = tonumber(os.getenv("CONTENT_LENGTH") or 0) +if content_length > 0 then + local body = io.read(content_length) + params.post_params = parse_query(body) +end + +-- Convenience functions +function params.get(key) return params.get_params[key] end +function params.post(key) return params.post_params[key] end + +-- Combine GET+POST if you want +function params.param(key) + return params.post(key) or params.get(key) +end + +return params |
