-
-
Save Sledmine/799b376b3658365bc50260d4abe9ec13 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local char_to_hex = function(c) | |
return string.format("%%%02X", string.byte(c)) | |
end | |
local function urlencode(url) | |
if url == nil then | |
return | |
end | |
url = url:gsub("\n", "\r\n") | |
url = url:gsub("([^%w ])", char_to_hex) | |
url = url:gsub(" ", "+") | |
return url | |
end | |
local hex_to_char = function(x) | |
return string.char(tonumber(x, 16)) | |
end | |
local urldecode = function(url) | |
if url == nil then | |
return | |
end | |
url = url:gsub("+", " ") | |
url = url:gsub("%%(%x%x)", hex_to_char) | |
return url | |
end | |
-- ref: https://gist.github.com/ignisdesign/4323051 | |
-- ref: http://stackoverflow.com/questions/20282054/how-to-urldecode-a-request-uri-string-in-lua | |
-- to encode table as parameters, see https://github.com/stuartpb/tvtropes-lua/blob/master/urlencode.lua |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment