url.lua 466 B

1234567891011121314151617181920212223242526272829
  1. local url = {}
  2. local function decode_func(c)
  3. return string.char(tonumber(c, 16))
  4. end
  5. local function decode(str)
  6. local str = str:gsub('+', ' ')
  7. return str:gsub("%%(..)", decode_func)
  8. end
  9. function url.parse(u)
  10. local path,query = u:match "([^?]*)%??(.*)"
  11. if path then
  12. path = decode(path)
  13. end
  14. return path, query
  15. end
  16. function url.parse_query(q)
  17. local r = {}
  18. for k,v in q:gmatch "(.-)=([^&]*)&?" do
  19. r[decode(k)] = decode(v)
  20. end
  21. return r
  22. end
  23. return url