From 19ae10200db3a2ea39b01ce4833a9d3529e2db5d Mon Sep 17 00:00:00 2001 From: Kay0u Date: Fri, 17 Jan 2020 14:01:24 +0700 Subject: [PATCH] fix string.match --- access.lua | 32 +++++++++----------------------- helpers.lua | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/access.lua b/access.lua index 95e0ee9..c24ae09 100644 --- a/access.lua +++ b/access.lua @@ -22,9 +22,6 @@ local conf = config.get_config() -- Import helpers local hlp = require "helpers" --- Import Perl regular expressions library -local rex = require "rex_pcre" - -- Load logging module local logger = require("log") @@ -208,17 +205,6 @@ end -- If the URL matches one of the `redirected_urls` in the configuration file, -- 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) if hlp.string.starts(redirect_url, "http://") or hlp.string.starts(redirect_url, "https://") then @@ -243,9 +229,9 @@ end if conf["redirected_regex"] then for regex, redirect_url in pairs(conf["redirected_regex"]) do - if 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 match(ngx.var.uri..hlp.uri_args_string(), regex) then + if hlp.match(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 hlp.match(ngx.var.uri..hlp.uri_args_string(), regex) then logger.debug("Requested URI is in redirected_regex") detect_redirection(redirect_url) end @@ -274,8 +260,8 @@ end if conf["skipped_regex"] then for _, regex in ipairs(conf["skipped_regex"]) do - if (match(ngx.var.host..ngx.var.uri..hlp.uri_args_string(), regex) - or match(ngx.var.uri..hlp.uri_args_string(), regex)) + if (hlp.match(ngx.var.host..ngx.var.uri..hlp.uri_args_string(), regex) + or hlp.match(ngx.var.uri..hlp.uri_args_string(), regex)) then logger.debug("Skipping "..ngx.var.uri) return hlp.pass() @@ -308,8 +294,8 @@ function is_protected() end end for _, regex in ipairs(conf["protected_regex"]) do - if match(ngx.var.host..ngx.var.uri..hlp.uri_args_string(), regex) - or match(ngx.var.uri..hlp.uri_args_string(), regex) then + if hlp.match(ngx.var.host..ngx.var.uri..hlp.uri_args_string(), regex) + or hlp.match(ngx.var.uri..hlp.uri_args_string(), regex) then logger.debug(ngx.var.uri.." is in protected_regex") return true end @@ -399,8 +385,8 @@ end if conf["unprotected_regex"] then for _, regex in ipairs(conf["unprotected_regex"]) do - if (match(ngx.var.host..ngx.var.uri..hlp.uri_args_string(), regex) - or match(ngx.var.uri..hlp.uri_args_string(), regex)) + if (hlp.match(ngx.var.host..ngx.var.uri..hlp.uri_args_string(), regex) + or hlp.match(ngx.var.uri..hlp.uri_args_string(), regex)) and not is_protected() then if hlp.is_logged_in() then hlp.set_headers() diff --git a/helpers.lua b/helpers.lua index eed2311..fd45715 100644 --- a/helpers.lua +++ b/helpers.lua @@ -14,6 +14,20 @@ local logger = require("log") -- url parser, c.f. https://rosettacode.org/wiki/URL_parser#Lua 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 function read_file(file)