# HG changeset patch # User jbe # Date 1434319628 -7200 # Node ID 625ab06babe9e91a8af82d3996ede71fbf6b4889 # Parent 41a44ae5c293a88cd64489ca4986a9a6eeda626e Use old system for GET/POST param tables in new HTTP module implementation diff -r 41a44ae5c293 -r 625ab06babe9 moonbridge_http.lua --- a/moonbridge_http.lua Sat Jun 13 20:34:50 2015 +0200 +++ b/moonbridge_http.lua Mon Jun 15 00:07:08 2015 +0200 @@ -83,20 +83,69 @@ ["304"] = true } --- parses URL encoded form data: -local function read_urlencoded_form(data) - local tbl = {} - for rawkey, rawvalue in string.gmatch(data, "([^?=&]*)=([^?=&]*)") do - local key = decode_uri(rawkey) - local value = decode_uri(rawvalue) - local subtbl = tbl[key] - if subtbl then - subtbl[#subtbl+1] = value - else - tbl[key] = {value} +-- handling of GET/POST param tables: +local new_params_list -- defined later +do + local params_list_mapping = setmetatable({}, {__mode="k"}) + local function nextnonempty(tbl, key) + while true do + key = next(tbl, key) + if key == nil then + return nil + end + local value = tbl[key] + if #value > 0 then + return key, value + end end end - return tbl + local function nextvalue(tbl, key) + key = next(tbl, key) + if key == nil then + return nil + end + return key, tbl[key][1] + end + local params_list_metatable = { + __index = function(self, key) + local tbl = {} + self[key] = tbl + return tbl + end, + __pairs = function(self) + return nextnonempty, self, nil + end + } + local params_metatable = { + __index = function(self, key) + return params_list_mapping[self][key][1] + end, + __newindex = function(self, key, value) + params_list_mapping[self][key] = {value} + end, + __pairs = function(self) + return nextvalue, params_list_mapping[self], nil + end + } + -- returns a table to store key value-list pairs (i.e. multiple values), + -- and a second table automatically mapping keys to the first value + -- using the key value-list pairs in the first table: + new_params_list = function() + local params_list = setmetatable({}, params_list_metatable) + local params = setmetatable({}, params_metatable) + params_list_mapping[params] = params_list + return params_list, params + end +end + +-- parses URL encoded form data and stores it in +-- a key value-list pairs structure that has to be +-- previously obtained by calling by new_params_list(): +local function read_urlencoded_form(tbl, data) + for rawkey, rawvalue in string.gmatch(data, "([^?=&]*)=([^?=&]*)") do + local subtbl = tbl[decode_uri(rawkey)] + subtbl[#subtbl+1] = decode_uri(rawvalue) + end end -- extracts first value from each subtable: