moonbridge

changeset 167:625ab06babe9

Use old system for GET/POST param tables in new HTTP module implementation
author jbe
date Mon Jun 15 00:07:08 2015 +0200 (2015-06-15)
parents 41a44ae5c293
children d4f5d6a7d401
files moonbridge_http.lua
line diff
     1.1 --- a/moonbridge_http.lua	Sat Jun 13 20:34:50 2015 +0200
     1.2 +++ b/moonbridge_http.lua	Mon Jun 15 00:07:08 2015 +0200
     1.3 @@ -83,20 +83,69 @@
     1.4    ["304"] = true
     1.5  }
     1.6  
     1.7 --- parses URL encoded form data:
     1.8 -local function read_urlencoded_form(data)
     1.9 -  local tbl = {}
    1.10 -  for rawkey, rawvalue in string.gmatch(data, "([^?=&]*)=([^?=&]*)") do
    1.11 -    local key = decode_uri(rawkey)
    1.12 -    local value = decode_uri(rawvalue)
    1.13 -    local subtbl = tbl[key]
    1.14 -    if subtbl then
    1.15 -      subtbl[#subtbl+1] = value
    1.16 -    else
    1.17 -      tbl[key] = {value}
    1.18 +-- handling of GET/POST param tables:
    1.19 +local new_params_list  -- defined later
    1.20 +do
    1.21 +  local params_list_mapping = setmetatable({}, {__mode="k"})
    1.22 +  local function nextnonempty(tbl, key)
    1.23 +    while true do
    1.24 +      key = next(tbl, key)
    1.25 +      if key == nil then
    1.26 +        return nil
    1.27 +      end
    1.28 +      local value = tbl[key]
    1.29 +      if #value > 0 then
    1.30 +        return key, value
    1.31 +      end
    1.32      end
    1.33    end
    1.34 -  return tbl
    1.35 +  local function nextvalue(tbl, key)
    1.36 +    key = next(tbl, key)
    1.37 +    if key == nil then
    1.38 +      return nil
    1.39 +    end
    1.40 +    return key, tbl[key][1]
    1.41 +  end
    1.42 +  local params_list_metatable = {
    1.43 +    __index = function(self, key)
    1.44 +      local tbl = {}
    1.45 +      self[key] = tbl
    1.46 +      return tbl
    1.47 +    end,
    1.48 +    __pairs = function(self)
    1.49 +      return nextnonempty, self, nil
    1.50 +    end
    1.51 +  }
    1.52 +  local params_metatable = {
    1.53 +    __index = function(self, key)
    1.54 +      return params_list_mapping[self][key][1]
    1.55 +    end,
    1.56 +    __newindex = function(self, key, value)
    1.57 +      params_list_mapping[self][key] = {value}
    1.58 +    end,
    1.59 +    __pairs = function(self)
    1.60 +      return nextvalue, params_list_mapping[self], nil
    1.61 +    end
    1.62 +  }
    1.63 +  -- returns a table to store key value-list pairs (i.e. multiple values),
    1.64 +  -- and a second table automatically mapping keys to the first value
    1.65 +  -- using the key value-list pairs in the first table:
    1.66 +  new_params_list = function()
    1.67 +    local params_list = setmetatable({}, params_list_metatable)
    1.68 +    local params = setmetatable({}, params_metatable)
    1.69 +    params_list_mapping[params] = params_list
    1.70 +    return params_list, params
    1.71 +  end
    1.72 +end
    1.73 +
    1.74 +-- parses URL encoded form data and stores it in
    1.75 +-- a key value-list pairs structure that has to be
    1.76 +-- previously obtained by calling by new_params_list():
    1.77 +local function read_urlencoded_form(tbl, data)
    1.78 +  for rawkey, rawvalue in string.gmatch(data, "([^?=&]*)=([^?=&]*)") do
    1.79 +    local subtbl = tbl[decode_uri(rawkey)]
    1.80 +    subtbl[#subtbl+1] = decode_uri(rawvalue)
    1.81 +  end
    1.82  end
    1.83  
    1.84  -- extracts first value from each subtable:

Impressum / About Us