diff --git a/apisix/core/string.lua b/apisix/core/string.lua index 952152a8..51d1577a 100644 --- a/apisix/core/string.lua +++ b/apisix/core/string.lua @@ -36,6 +36,7 @@ setmetatable(_M, {__index = string}) -- find a needle from a haystack in the plain text way +-- note: Make sure that the haystack is 'string' type, otherwise an exception will be thrown. function _M.find(haystack, needle, from) return str_find(haystack, needle, from or 1, true) end diff --git a/apisix/discovery/eureka.lua b/apisix/discovery/eureka.lua index 179d763f..82ee89a1 100644 --- a/apisix/discovery/eureka.lua +++ b/apisix/discovery/eureka.lua @@ -28,7 +28,7 @@ local ngx = ngx local ngx_timer_at = ngx.timer.at local ngx_timer_every = ngx.timer.every local string_sub = string.sub -local string_find = string.find +local str_find = core.string.find local log = core.log local default_weight @@ -76,9 +76,9 @@ local function service_info() local basic_auth -- TODO Add health check to get healthy nodes. local url = host[math_random(#host)] - local auth_idx = string_find(url, "@", 1, true) + local auth_idx = str_find(url, "@") if auth_idx then - local protocol_idx = string_find(url, "://", 1, true) + local protocol_idx = str_find(url, "://") local protocol = string_sub(url, 1, protocol_idx + 2) local user_and_password = string_sub(url, protocol_idx + 3, auth_idx - 1) local other = string_sub(url, auth_idx + 1) diff --git a/apisix/plugins/batch-requests.lua b/apisix/plugins/batch-requests.lua index dfe0dfde..fc4dc64f 100644 --- a/apisix/plugins/batch-requests.lua +++ b/apisix/plugins/batch-requests.lua @@ -20,7 +20,7 @@ local plugin = require("apisix.plugin") local ngx = ngx local ipairs = ipairs local pairs = pairs -local str_find = string.find +local str_find = core.string.find local str_lower = string.lower @@ -163,7 +163,7 @@ local function set_common_header(data) if outer_headers then for k, v in pairs(outer_headers) do - local is_content_header = str_find(k, "content-", 1, true) == 1 + local is_content_header = str_find(k, "content-") == 1 -- skip header start with "content-" if not req.headers[k] and not is_content_header then req.headers[k] = v diff --git a/apisix/plugins/cors.lua b/apisix/plugins/cors.lua index 3251729f..e4cdbd37 100644 --- a/apisix/plugins/cors.lua +++ b/apisix/plugins/cors.lua @@ -17,7 +17,7 @@ local core = require("apisix.core") local ngx = ngx local plugin_name = "cors" -local str_find = string.find +local str_find = core.string.find local re_gmatch = ngx.re.gmatch @@ -86,7 +86,7 @@ local _M = { local function create_mutiple_origin_cache(conf) - if not str_find(conf.allow_origins, ",", 1, true) then + if not str_find(conf.allow_origins, ",") then return nil end local origin_cache = {} diff --git a/apisix/plugins/ip-restriction.lua b/apisix/plugins/ip-restriction.lua index 2dead12c..b1860c91 100644 --- a/apisix/plugins/ip-restriction.lua +++ b/apisix/plugins/ip-restriction.lua @@ -18,7 +18,7 @@ local ipairs = ipairs local core = require("apisix.core") local ipmatcher = require("resty.ipmatcher") local str_sub = string.sub -local str_find = string.find +local str_find = core.string.find local tonumber = tonumber local lrucache = core.lrucache.new({ ttl = 300, count = 512 @@ -69,7 +69,7 @@ local _M = { local function valid_ip(ip) local mask = 0 - local sep_pos = str_find(ip, "/", 1, true) + local sep_pos = str_find(ip, "/") if sep_pos then mask = str_sub(ip, sep_pos + 1) mask = tonumber(mask) diff --git a/apisix/plugins/log-rotate.lua b/apisix/plugins/log-rotate.lua index de2266cc..217c2f41 100644 --- a/apisix/plugins/log-rotate.lua +++ b/apisix/plugins/log-rotate.lua @@ -25,6 +25,7 @@ local io = io local os = os local table = table local string = string +local str_find = core.string.find local local_conf @@ -57,7 +58,7 @@ end local function get_last_index(str, key) local rev = string.reverse(str) - local _, idx = string.find(rev, key, 1, true) + local _, idx = str_find(rev, key) local n if idx then n = string.len(rev) - idx + 1 diff --git a/apisix/plugins/proxy-rewrite.lua b/apisix/plugins/proxy-rewrite.lua index d24d0b42..1a5e7017 100644 --- a/apisix/plugins/proxy-rewrite.lua +++ b/apisix/plugins/proxy-rewrite.lua @@ -22,7 +22,7 @@ local ngx = ngx local type = type local re_sub = ngx.re.sub local sub_str = string.sub -local find_str = string.find +local str_find = core.string.find local schema = { type = "object", @@ -158,7 +158,7 @@ function _M.rewrite(conf, ctx) end end - local index = find_str(upstream_uri, "?", 1, true) + local index = str_find(upstream_uri, "?") if index then upstream_uri = core.utils.uri_safe_encode(sub_str(upstream_uri, 1, index-1)) .. sub_str(upstream_uri, index) diff --git a/apisix/ssl/router/radixtree_sni.lua b/apisix/ssl/router/radixtree_sni.lua index dadbd3dc..ed2c8d65 100644 --- a/apisix/ssl/router/radixtree_sni.lua +++ b/apisix/ssl/router/radixtree_sni.lua @@ -22,7 +22,7 @@ local config_util = require("apisix.core.config_util") local ipairs = ipairs local type = type local error = error -local str_find = string.find +local str_find = core.string.find local aes = require "resty.aes" local assert = assert local str_gsub = string.gsub @@ -218,7 +218,7 @@ function _M.match_and_set(api_ctx) if type(api_ctx.matched_sni) == "table" then local matched = false for _, msni in ipairs(api_ctx.matched_sni) do - if sni_rev == msni or not str_find(sni_rev, ".", #msni, true) then + if sni_rev == msni or not str_find(sni_rev, ".", #msni) then matched = true end end @@ -233,7 +233,7 @@ function _M.match_and_set(api_ctx) return false end else - if str_find(sni_rev, ".", #api_ctx.matched_sni, true) then + if str_find(sni_rev, ".", #api_ctx.matched_sni) then core.log.warn("failed to find any SSL certificate by SNI: ", sni, " matched SNI: ", api_ctx.matched_sni:reverse()) return false diff --git a/t/lib/test_admin.lua b/t/lib/test_admin.lua index 88003616..5663b79d 100644 --- a/t/lib/test_admin.lua +++ b/t/lib/test_admin.lua @@ -19,7 +19,7 @@ local json = require("cjson.safe") local core = require("apisix.core") local aes = require "resty.aes" local ngx_encode_base64 = ngx.encode_base64 -local str_find = string.find +local str_find = core.string.find local dir_names = {}