Comparison

net/http.lua @ 4338:5d5d6c6d121a

net.http: Add formdecode to decode an urlencoded form
author Matthew Wild <mwild1@gmail.com>
date Wed, 10 Aug 2011 17:49:10 -0400
parent 3569:f30da46e0add
child 4350:0b9ed126286e
comparison
equal deleted inserted replaced
4337:a2ee8ab82dd9 4338:5d5d6c6d121a
36 else 36 else
37 return "+"; 37 return "+";
38 end 38 end
39 end)); 39 end));
40 end 40 end
41
41 function formencode(form) 42 function formencode(form)
42 local result = {}; 43 local result = {};
43 for _, field in ipairs(form) do 44 for _, field in ipairs(form) do
44 t_insert(result, _formencodepart(field.name).."=".._formencodepart(field.value)); 45 t_insert(result, _formencodepart(field.name).."=".._formencodepart(field.value));
45 end 46 end
46 return t_concat(result, "&"); 47 return t_concat(result, "&");
48 end
49
50 function formdecode(s)
51 if not s:match("=") then return urldecode(s); end
52 local r = {};
53 for k, v in s:gmatch("([^=&]*)=([^&]*)") do
54 k, v = k:gsub("%+", "%%20"), v:gsub("%+", "%%20");
55 k, v = urldecode(k), urldecode(v);
56 t_insert(r, { name = k, value = v });
57 r[k] = v;
58 end
59 return r;
47 end 60 end
48 61
49 local function request_reader(request, data, startpos) 62 local function request_reader(request, data, startpos)
50 if not request.parser then 63 if not request.parser then
51 local function success_cb(r) 64 local function success_cb(r)