From cb96968b1b705a3cc60658f006a646acc89c9b73 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sat, 12 Nov 2022 14:58:29 +0000 Subject: [PATCH] [Project] Add a simple url encoding function + tests --- lualib/lua_maps.lua | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lualib/lua_maps.lua b/lualib/lua_maps.lua index 14439f778..b2f8dda92 100644 --- a/lualib/lua_maps.lua +++ b/lualib/lua_maps.lua @@ -99,10 +99,15 @@ local rspamd_http = require "rspamd_http" local ucl = require "ucl" local function url_encode_string(str) - -- TODO: implement encoding + str = string.gsub(str, "([^%w _%%%-%.~])", + function(c) return string.format("%%%02X", string.byte(c)) end) + str = string.gsub(str, " ", "+") return str end +assert(url_encode_string('上海+中國') == '%E4%B8%8A%E6%B5%B7%2B%E4%B8%AD%E5%9C%8B') +assert(url_encode_string('? and the Mysterians') == '%3F+and+the+Mysterians') + local function query_external_map(map_config, upstreams, key, callback, task) local http_method = (map_config.method == 'body' or map_config.method == 'form') and 'POST' or 'GET' local upstream = upstreams:get_upstream_round_robin() -- 2.39.5