diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2022-07-03 12:58:45 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rspamd.com> | 2022-07-03 12:58:45 +0100 |
commit | 36ca24fd2f41e0cf0c8f28729ad36c5c7dcef19e (patch) | |
tree | ce6e3c43d00c8f7db82d87bd7e23424d14bf0349 /lualib/lua_util.lua | |
parent | b6f01cfb0c5fda0b92fc7d8e2e0f7c63d2a7b07f (diff) | |
download | rspamd-36ca24fd2f41e0cf0c8f28729ad36c5c7dcef19e.tar.gz rspamd-36ca24fd2f41e0cf0c8f28729ad36c5c7dcef19e.zip |
[Feature] Add function to store upstreams for HTTP urls
Diffstat (limited to 'lualib/lua_util.lua')
-rw-r--r-- | lualib/lua_util.lua | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua index 6ade90e4b..bd7c2dd29 100644 --- a/lualib/lua_util.lua +++ b/lualib/lua_util.lua @@ -1488,4 +1488,37 @@ end --]]] exports.unhex = function(str) return str:gsub('(..)', hex_table) end +local http_upstream_lists = {} +local function http_upstreams_by_url(pool, url) + local rspamd_url = require "rspamd_url" + + local cached = http_upstream_lists[url] + if cached then return cached end + + local real_url = rspamd_url.create(pool, url) + + if not real_url then return nil end + + local host = real_url:get_host() + local proto = real_url:get_protocol() or 'http' + local port = real_url:get_port() or (proto == 'https' and 443 or 80) + local upstream_list = require "rspamd_upstream_list" + local upstreams = upstream_list.create(host, port) + + if upstreams then + http_upstream_lists[url] = upstreams + return upstreams + end + + return nil +end +---[[[ +-- @function lua_util.http_upstreams_by_url(pool, url) +-- Returns a cached or new upstreams list that corresponds to the specific url +-- @param {mempool} pool memory pool to use (typically static pool from rspamd_config) +-- @param {string} url full url +-- @return {upstreams_list} object to get upstream from an url +--]]] +exports.http_upstreams_by_url = http_upstreams_by_url + return exports |