diff options
author | Kim Alvefur <zash@zash.se> | 2019-01-06 10:39:33 +0100 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2019-01-06 10:39:33 +0100 |
commit | 60213520c2a27bf0472a0ef5a2cf16640134a1a8 (patch) | |
tree | 2b2a2608c757517d0a4a736cb2b5eea207edae15 /util | |
parent | d9e14b0e994443bb64eaae05b080a44929b8d03d (diff) | |
download | prosody-60213520c2a27bf0472a0ef5a2cf16640134a1a8.tar.gz prosody-60213520c2a27bf0472a0ef5a2cf16640134a1a8.zip |
util.http: Pre-generate urlencoding mappings (optimization)
Function calls are more expensive than table lookups
Diffstat (limited to 'util')
-rw-r--r-- | util/http.lua | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/util/http.lua b/util/http.lua index cfb89193..1730d4d4 100644 --- a/util/http.lua +++ b/util/http.lua @@ -6,24 +6,25 @@ -- local format, char = string.format, string.char; -local pairs, ipairs, tonumber = pairs, ipairs, tonumber; +local pairs, ipairs = pairs, ipairs; local t_insert, t_concat = table.insert, table.concat; +local url_codes = {}; +for i = 0, 255 do + local c = char(i); + local u = format("%%%02x", i); + url_codes[c] = u; + url_codes[u] = c; +end local function urlencode(s) - return s and (s:gsub("[^a-zA-Z0-9.~_-]", function (c) return format("%%%02x", c:byte()); end)); + return s and (s:gsub("[^a-zA-Z0-9.~_-]", url_codes)); end local function urldecode(s) - return s and (s:gsub("%%(%x%x)", function (c) return char(tonumber(c,16)); end)); + return s and (s:gsub("%%%x%x", url_codes)); end local function _formencodepart(s) - return s and (s:gsub("%W", function (c) - if c ~= " " then - return format("%%%02x", c:byte()); - else - return "+"; - end - end)); + return s and (urlencode(s):gsub("%%20", "+")); end local function formencode(form) |