| |
| |
| |
|
|
| local url = {} |
|
|
| local function quote_char(c) |
| return string.format("%%%02X", string.byte(c)) |
| end |
|
|
| |
| |
| |
| function url.quote(s, quote_plus) |
| if not s or not type(s) == "string" then |
| return s |
| end |
|
|
| s = s:gsub("\n", "\r\n") |
| s = s:gsub("([^A-Za-z0-9 %-_%./])", quote_char) |
| if quote_plus then |
| s = s:gsub(" ", "+") |
| s = s:gsub("/", quote_char) |
| else |
| s = s:gsub(" ", "%%20") |
| end |
|
|
| return s |
| end |
|
|
| local function unquote_char(h) |
| return string.char(tonumber(h, 16)) |
| end |
|
|
| |
| |
| function url.unquote(s) |
| if not s or not type(s) == "string" then |
| return s |
| end |
|
|
| s = s:gsub("+", " ") |
| s = s:gsub("%%(%x%x)", unquote_char) |
| s = s:gsub("\r\n", "\n") |
|
|
| return s |
| end |
|
|
| return url |
|
|