simple url parser

This commit is contained in:
Cloud Wu
2014-07-22 23:04:37 +08:00
parent d2ad63da81
commit 551d5048c4
2 changed files with 43 additions and 1 deletions

28
lualib/http/url.lua Normal file
View File

@@ -0,0 +1,28 @@
local url = {}
local function decode_func(c)
return string.char(tonumber(c, 16))
end
local function decode(str)
local str = str:gsub('+', ' ')
return str:gsub("%%(..)", decode_func)
end
function url.parse(u)
local path,query = u:match "([^?]*)%??(.*)"
if path then
path = decode(path)
end
return path, query
end
function url.parse_query(q)
local r = {}
for k,v in q:gmatch "(.-)=([^&]*)&?" do
r[decode(k)] = decode(v)
end
return r
end
return url