summaryrefslogtreecommitdiff
path: root/qs.lua
blob: 70484b7f3d608ec7ce88064fe13e9e352c26cbf5 (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
-- querystring and post body parsing

local function urldecode(s)
	return s:gsub('+',' ')
		:gsub('%%(%x%x)',function(x)
			return string.char(tonumber(x,16))
		end)
end

local function parse_qs(s)
	local ix = 1
	local res = {}
	while true do
		local _,next_ix,ek,ev = s:find("([^=]+)=([^&]*)&?",ix)
		if not ek then break end
		local k,v = urldecode(ek), urldecode(ev)
		res[k] = v
		ix = next_ix + 1
	end
	return res
end

return {
	urldecode = urldecode,
	parse_qs = parse_qs,
}