-
Notifications
You must be signed in to change notification settings - Fork 4
/
urlencode.lua
46 lines (33 loc) · 976 Bytes
/
urlencode.lua
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
local string = require "string"
local table = require "table"
--Module table
local urlencode={}
--URL encode a string.
local function encode(str)
--Ensure all newlines are in CRLF form
str = string.gsub (str, "\r?\n", "\r\n")
--Percent-encode all non-unreserved characters
--as per RFC 3986, Section 2.3
--(except for space, which gets plus-encoded)
str = string.gsub (str, "([^%w%-%.%_%~ ])",
function (c) return string.format ("%%%02X", string.byte(c)) end)
--Convert spaces to plus signs
str = string.gsub (str, " ", "+")
return str
end
--Make this function available as part of the module
urlencode.string = encode
--URL encode a table as a series of parameters.
function urlencode.table(t)
--table of argument strings
local argts = {}
--insertion iterator
local i = 1
--URL-encode every pair
for k, v in pairs(t) do
argts[i]=encode(k).."="..encode(v)
i=i+1
end
return table.concat(argts,'&')
end
return urlencode