fix string.match

This commit is contained in:
Kay0u 2020-01-17 14:01:24 +07:00
parent 972d858959
commit 19ae10200d
No known key found for this signature in database
GPG key ID: 7FF262C033518333
2 changed files with 23 additions and 23 deletions

View file

@ -22,9 +22,6 @@ local conf = config.get_config()
-- Import helpers -- Import helpers
local hlp = require "helpers" local hlp = require "helpers"
-- Import Perl regular expressions library
local rex = require "rex_pcre"
-- Load logging module -- Load logging module
local logger = require("log") local logger = require("log")
@ -208,17 +205,6 @@ end
-- If the URL matches one of the `redirected_urls` in the configuration file, -- If the URL matches one of the `redirected_urls` in the configuration file,
-- just redirect to the target URL/URI -- just redirect to the target URL/URI
-- --
-- The 'match' function uses PCRE regex as default
-- If '%.' is found in the regex, we assume it's a LUA regex (legacy code)
-- 'match' returns the matched text.
function match(s, regex)
if not string.find(regex, '%%%.') then
return rex.match(s, regex)
else
return string.match(s,regex)
end
end
function detect_redirection(redirect_url) function detect_redirection(redirect_url)
if hlp.string.starts(redirect_url, "http://") if hlp.string.starts(redirect_url, "http://")
or hlp.string.starts(redirect_url, "https://") then or hlp.string.starts(redirect_url, "https://") then
@ -243,9 +229,9 @@ end
if conf["redirected_regex"] then if conf["redirected_regex"] then
for regex, redirect_url in pairs(conf["redirected_regex"]) do for regex, redirect_url in pairs(conf["redirected_regex"]) do
if match(ngx.var.host..ngx.var.uri..hlp.uri_args_string(), regex) if hlp.match(ngx.var.host..ngx.var.uri..hlp.uri_args_string(), regex)
or match(ngx.var.scheme.."://"..ngx.var.host..ngx.var.uri..hlp.uri_args_string(), regex) or hlp.match(ngx.var.scheme.."://"..ngx.var.host..ngx.var.uri..hlp.uri_args_string(), regex)
or match(ngx.var.uri..hlp.uri_args_string(), regex) then or hlp.match(ngx.var.uri..hlp.uri_args_string(), regex) then
logger.debug("Requested URI is in redirected_regex") logger.debug("Requested URI is in redirected_regex")
detect_redirection(redirect_url) detect_redirection(redirect_url)
end end
@ -274,8 +260,8 @@ end
if conf["skipped_regex"] then if conf["skipped_regex"] then
for _, regex in ipairs(conf["skipped_regex"]) do for _, regex in ipairs(conf["skipped_regex"]) do
if (match(ngx.var.host..ngx.var.uri..hlp.uri_args_string(), regex) if (hlp.match(ngx.var.host..ngx.var.uri..hlp.uri_args_string(), regex)
or match(ngx.var.uri..hlp.uri_args_string(), regex)) or hlp.match(ngx.var.uri..hlp.uri_args_string(), regex))
then then
logger.debug("Skipping "..ngx.var.uri) logger.debug("Skipping "..ngx.var.uri)
return hlp.pass() return hlp.pass()
@ -308,8 +294,8 @@ function is_protected()
end end
end end
for _, regex in ipairs(conf["protected_regex"]) do for _, regex in ipairs(conf["protected_regex"]) do
if match(ngx.var.host..ngx.var.uri..hlp.uri_args_string(), regex) if hlp.match(ngx.var.host..ngx.var.uri..hlp.uri_args_string(), regex)
or match(ngx.var.uri..hlp.uri_args_string(), regex) then or hlp.match(ngx.var.uri..hlp.uri_args_string(), regex) then
logger.debug(ngx.var.uri.." is in protected_regex") logger.debug(ngx.var.uri.." is in protected_regex")
return true return true
end end
@ -399,8 +385,8 @@ end
if conf["unprotected_regex"] then if conf["unprotected_regex"] then
for _, regex in ipairs(conf["unprotected_regex"]) do for _, regex in ipairs(conf["unprotected_regex"]) do
if (match(ngx.var.host..ngx.var.uri..hlp.uri_args_string(), regex) if (hlp.match(ngx.var.host..ngx.var.uri..hlp.uri_args_string(), regex)
or match(ngx.var.uri..hlp.uri_args_string(), regex)) or hlp.match(ngx.var.uri..hlp.uri_args_string(), regex))
and not is_protected() then and not is_protected() then
if hlp.is_logged_in() then if hlp.is_logged_in() then
hlp.set_headers() hlp.set_headers()

View file

@ -14,6 +14,20 @@ local logger = require("log")
-- url parser, c.f. https://rosettacode.org/wiki/URL_parser#Lua -- url parser, c.f. https://rosettacode.org/wiki/URL_parser#Lua
local url_parser = require "socket.url" local url_parser = require "socket.url"
-- Import Perl regular expressions library
local rex = require "rex_pcre"
-- The 'match' function uses PCRE regex as default
-- If '%.' is found in the regex, we assume it's a LUA regex (legacy code)
-- 'match' returns the matched text.
function match(s, regex)
if not string.find(regex, '%%%.') then
return rex.match(s, regex)
else
return string.match(s,regex)
end
end
-- Read a FS stored file -- Read a FS stored file
function read_file(file) function read_file(file)