comparison util/http.lua @ 11200:bf8f2da84007

Merge 0.11->trunk
author Kim Alvefur <zash@zash.se>
date Thu, 05 Nov 2020 22:31:25 +0100
parents ff88b03c343f
children f15e23840780
comparison
equal deleted inserted replaced
11199:6c7c50a4de32 11200:bf8f2da84007
4 -- This project is MIT/X11 licensed. Please see the 4 -- This project is MIT/X11 licensed. Please see the
5 -- COPYING file in the source package for more information. 5 -- COPYING file in the source package for more information.
6 -- 6 --
7 7
8 local format, char = string.format, string.char; 8 local format, char = string.format, string.char;
9 local pairs, ipairs, tonumber = pairs, ipairs, tonumber; 9 local pairs, ipairs = pairs, ipairs;
10 local t_insert, t_concat = table.insert, table.concat; 10 local t_insert, t_concat = table.insert, table.concat;
11 11
12 local url_codes = {};
13 for i = 0, 255 do
14 local c = char(i);
15 local u = format("%%%02x", i);
16 url_codes[c] = u;
17 url_codes[u] = c;
18 url_codes[u:upper()] = c;
19 end
12 local function urlencode(s) 20 local function urlencode(s)
13 return s and (s:gsub("[^a-zA-Z0-9.~_-]", function (c) return format("%%%02x", c:byte()); end)); 21 return s and (s:gsub("[^a-zA-Z0-9.~_-]", url_codes));
14 end 22 end
15 local function urldecode(s) 23 local function urldecode(s)
16 return s and (s:gsub("%%(%x%x)", function (c) return char(tonumber(c,16)); end)); 24 return s and (s:gsub("%%%x%x", url_codes));
17 end 25 end
18 26
19 local function _formencodepart(s) 27 local function _formencodepart(s)
20 return s and (s:gsub("%W", function (c) 28 return s and (urlencode(s):gsub("%%20", "+"));
21 if c ~= " " then
22 return format("%%%02x", c:byte());
23 else
24 return "+";
25 end
26 end));
27 end 29 end
28 30
29 local function formencode(form) 31 local function formencode(form)
30 local result = {}; 32 local result = {};
31 if form[1] then -- Array of ordered { name, value } 33 if form[1] then -- Array of ordered { name, value }