diff --git a/kong-3.5.1-0.rockspec b/kong-3.5.1-0.rockspec index 90612fe82a14..60d5f6310296 100644 --- a/kong-3.5.1-0.rockspec +++ b/kong-3.5.1-0.rockspec @@ -164,6 +164,7 @@ build = { ["kong.tools.protobuf"] = "kong/tools/protobuf.lua", ["kong.tools.mime_type"] = "kong/tools/mime_type.lua", ["kong.tools.request_aware_table"] = "kong/tools/request_aware_table.lua", + ["kong.tools.string"] = "kong/tools/string.lua", ["kong.runloop.handler"] = "kong/runloop/handler.lua", ["kong.runloop.events"] = "kong/runloop/events.lua", diff --git a/kong/pdk/request.lua b/kong/pdk/request.lua index a5fc2f04d7d3..06fb846a2ae6 100644 --- a/kong/pdk/request.lua +++ b/kong/pdk/request.lua @@ -41,9 +41,6 @@ local get_body_file = req.get_body_file local decode_args = ngx.decode_args -local is_http_subsystem = ngx and ngx.config.subsystem == "http" - - local PHASES = phase_checker.phases @@ -85,19 +82,7 @@ local function new(self) end end - local replace_dashes do - -- 1.000.000 iterations with input of "my-header": - -- string.gsub: 81ms - -- ngx.re.gsub: 74ms - -- loop/string.buffer: 28ms - -- str_replace_char: 14ms - if is_http_subsystem then - local str_replace_char = require("resty.core.utils").str_replace_char - replace_dashes = function(str) - return str_replace_char(str, "-", "_") - end - end - end + local replace_dashes = require("kong.tools.string").replace_dashes --- diff --git a/kong/router/atc.lua b/kong/router/atc.lua index 653f09af2b58..7c59cba03b4d 100644 --- a/kong/router/atc.lua +++ b/kong/router/atc.lua @@ -562,6 +562,7 @@ local get_queries_key do local tb_sort = table.sort local tb_concat = table.concat + local replace_dashes_lower = require("kong.tools.string").replace_dashes_lower local str_buf = buffer.new(64) @@ -570,7 +571,7 @@ do -- NOTE: DO NOT yield until str_buf:get() for name, value in pairs(headers) do - local name = name:gsub("-", "_"):lower() + local name = replace_dashes_lower(name) if type(value) == "table" then for i, v in ipairs(value) do diff --git a/kong/router/compat.lua b/kong/router/compat.lua index dc0b5cdd08e9..6da3522f4698 100644 --- a/kong/router/compat.lua +++ b/kong/router/compat.lua @@ -10,7 +10,8 @@ local tb_nkeys = require("table.nkeys") local uuid = require("resty.jit-uuid") -local shallow_copy = require("kong.tools.utils").shallow_copy +local shallow_copy = require("kong.tools.utils").shallow_copy +local replace_dashes_lower = require("kong.tools.string").replace_dashes_lower local is_regex_magic = utils.is_regex_magic @@ -251,7 +252,7 @@ local function get_expression(route) single_header_buf:reset():put("(") for i, value in ipairs(v) do - local name = "any(http.headers." .. h:gsub("-", "_"):lower() .. ")" + local name = "any(http.headers." .. replace_dashes_lower(h) .. ")" local op = OP_EQUAL -- value starts with "~*" diff --git a/kong/tools/string.lua b/kong/tools/string.lua new file mode 100644 index 000000000000..3ed03a5d293a --- /dev/null +++ b/kong/tools/string.lua @@ -0,0 +1,45 @@ +local find = string.find +local gsub = string.gsub + + +local _M = {} + + +local replace_dashes +local replace_dashes_lower +do + local str_replace_char + + if ngx and ngx.config.subsystem == "http" then + + -- 1,000,000 iterations with input of "my-header": + -- string.gsub: 81ms + -- ngx.re.gsub: 74ms + -- loop/string.buffer: 28ms + -- str_replace_char: 14ms + str_replace_char = require("resty.core.utils").str_replace_char + + else -- stream subsystem + str_replace_char = function(str, ch, replace) + if not find(str, ch, nil, true) then + return str + end + + return gsub(str, ch, replace) + end + end + + replace_dashes = function(str) + return str_replace_char(str, "-", "_") + end + + replace_dashes_lower = function(str) + return str_replace_char(str:lower(), "-", "_") + end +end +_M.replace_dashes = replace_dashes +_M.replace_dashes_lower = replace_dashes_lower + + +return _M +