Skip to content

Commit

Permalink
support http.path.segments.* field
Browse files Browse the repository at this point in the history
  • Loading branch information
chronolaw committed Jan 3, 2024
1 parent e22ac21 commit a12d472
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions kong/router/atc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ do
["String"] = {"net.protocol", "tls.sni",
"http.method", "http.host",
"http.path",
"http.path.segments.*",
"http.headers.*",
"http.queries.*",
},
Expand Down
20 changes: 20 additions & 0 deletions kong/router/fields.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ local HTTP_HEADERS_PREFIX = "http.headers."
local HTTP_QUERIES_PREFIX = "http.queries."


local HTTP_SEGMENTS_PREFIX = "http.path.segments."
local HTTP_SEGMENTS_PREFIX_LEN = #HTTP_SEGMENTS_PREFIX


local FIELDS_FUNCS = {
-- http.*

Expand Down Expand Up @@ -167,6 +171,7 @@ end -- is_http
if is_http then

local fmt = string.format
local ngx_re_split = require("ngx.re").split

-- func => get_headers or get_uri_args
-- name => "headers" or "queries"
Expand Down Expand Up @@ -209,6 +214,21 @@ if is_http then

return params.queries[field:sub(PREFIX_LEN + 1)]
end

elseif field:sub(1, HTTP_SEGMENTS_PREFIX_LEN) == HTTP_SEGMENTS_PREFIX then
return function(params)
if not params.segments then
params.segments = ngx_re_split(params.uri, "/", "jo")
end

-- "/a/b/c"
-- 1="", 2="a", 3="b"
-- http.path.segments.0 => params.segments[2]

local pos = tonumber(field:sub(HTTP_SEGMENTS_PREFIX_LEN + 1)) + 2
return params.segments[pos]
end

end

-- others return nil
Expand Down

0 comments on commit a12d472

Please sign in to comment.