apisix icon indicating copy to clipboard operation
apisix copied to clipboard

bug: the routes with vars parametes , whos max match is not supported.(route的vars匹配规则不支持最大匹配)

Open HaoTien opened this issue 3 years ago • 9 comments

Current Behavior

the routes with vars parametes , whos max match is not supported.

Expected Behavior

i respected match the route of test_route_2.

Error Logs

init.lua:514: http_access_phase(): matched route: {"key":"\/apisix\/routes\/1","createdIndex":152570,"modifiedIndex":218593,"value":{"vars":[["http_parameter1","~~","value[2,3]"]],upstream":{"type":"roundrobin","scheme":"http","hash_on":"vars","parent":{"key":"\/apisix\/routes\/1","createdIndex":152570,"modifiedIndex":218593,"value":"table: 0x7f009f38a618","clean_handlers":"table: 0x7f009f1f3f70","has_domain":true,"update_count":0,"orig_modifiedIndex":218593},"nodes":[{"host":"www.test1.com","weight":1}],"pass_host":"node"},"priority":0,"uri":"\/*","create_time":1673344005,"name":"test_route_2","plugin_config_id":"plugin-config","status":1,"id":"1"},"clean_handlers":{},"has_domain":true,"update_count":0,"orig_modifiedIndex":218593},

Steps to Reproduce

If i add two routes at the same time test_route_1:

curl http: //127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: 88888' -X PUT -i -d '
{
  "name": "test_route_1",
  "vars": [
    [
      "http_parameter1",
      "~~",
      "value[2,3]"
    ]
  ],
  "plugin_config_id": "plugin-config",
  "upstream": {
    "pass_host": "node",
    "nodes": {
      "www.test1.com": 1
    },
    "scheme": "http",
    "type": "roundrobin",
    "hash_on": "vars"
  },
  "status": 1,
  "priority": 0,
  "uri": "\/*"
}'

test_route_2:

curl http://127.0.0.1:9180/apisix/admin/routes/2 -H 'X-API-KEY: 88888' -X PUT -i -d '
{
  "name": "test_route_2",
  "vars": [
    [
      "http_parameter1",
      "~~",
      "value[2,3]"
    ],
    [
      "http_parameter2",
      "==",
      "2"
    ]
  ],
  "plugin_config_id": "plugin-config",
  "priority": 0,
  "upstream": {
    "pass_host": "node",
    "nodes": {
      "www.test2.com": 1
    },
    "type": "roundrobin",
    "scheme": "http"
  },
  "uri": "/*",
  "status": 1
}'

then i take a request,like:

curl 127.0.0.1:9080 -H "parameter1: value2" -H "parameter2: 2"

it will always match the route of test_route_1,but i respected match the route of test_route_2.

Environment

  • APISIX version (run apisix version):3.0.0
  • Operating system (run uname -a):CentOS Linux release 7.9.2009 (Core)
  • OpenResty / Nginx version (run openresty -V or nginx -V):openresty/1.21.4.1
  • etcd version, if relevant (run curl http://127.0.0.1:9090/v1/server_info):3.0.0
  • APISIX Dashboard version, if relevant:
  • Plugin runner version, for issues related to plugin runners:
  • LuaRocks version, for installation issues (run luarocks --version):

HaoTien avatar May 08 '23 10:05 HaoTien

Can you format your input? Use ``` to wrap code snippets.

And I read your description I don't quite understand what you want

Sn0rt avatar Jul 24 '23 10:07 Sn0rt

i have test in apisix.3.6.0 , and it works well, and can not reproduce. curl 127.0.0.1:9080 -H "parameter1: value2" -H "parameter2: 2" then route of test_route_2.

hanqingwu avatar Dec 21 '23 08:12 hanqingwu

i have test in apisix.3.6.0 , and it works well, and can not reproduce. curl 127.0.0.1:9080 -H "parameter1: value2" -H "parameter2: 2" then route of test_route_2.

I upgraded to version 3.6 and the problem still exists

Environment APISIX version (run apisix version):3.6.0 Operating system (run uname -a):CentOS Linux release 7.9.2009 (Core) OpenResty / Nginx version (run openresty -V or nginx -V):openresty/1.21.4.2 etcd version, if relevant (run curl http://127.0.0.1:9090/v1/server_info):3.4.0 APISIX Dashboard version, if relevant: Plugin runner version, for issues related to plugin runners: LuaRocks version, for installation issues (run luarocks --version):

HaoTien avatar Dec 26 '23 07:12 HaoTien

sorry , i have check again. and so i found change test_route_2 to route/1 test_route_1 to route/2 , then it works so it should match by route id first .

hanqingwu avatar Dec 26 '23 09:12 hanqingwu

i have try this method vars use AND , but it not work for this issue. https://github.com/api7/lua-resty-expr

hanqingwu avatar Dec 26 '23 10:12 hanqingwu

i have trace code to apisix/http/router/radixtree_host_uri.lua image and api_ctx.matched_route return from this dispatch .

hanqingwu avatar Dec 26 '23 11:12 hanqingwu

only_uri_route:dipatch(...) then it call in resty.radixtree

local function match_route(self, path, opts, args)
    if opts.host then
        opts.host = str_lower(opts.host)
    end

    if opts.matched then
        clear_tab(opts.matched)
    end

    local routes = self.hash_path[path]
    if routes then
        local opts_matched_exists = (opts.matched ~= nil)
        for _, route in ipairs(routes) do
            if match_route_opts(route, opts, args) then
                if opts_matched_exists then
                    opts.matched._path = path
                end
                return route
            end
        end
    end

so i guess it not support max match depth.

hanqingwu avatar Dec 26 '23 11:12 hanqingwu

only_uri_route:dipatch(...) then it call in resty.radixtree

local function match_route(self, path, opts, args)
    if opts.host then
        opts.host = str_lower(opts.host)
    end

    if opts.matched then
        clear_tab(opts.matched)
    end

    local routes = self.hash_path[path]
    if routes then
        local opts_matched_exists = (opts.matched ~= nil)
        for _, route in ipairs(routes) do
            if match_route_opts(route, opts, args) then
                if opts_matched_exists then
                    opts.matched._path = path
                end
                return route
            end
        end
    end

so i guess it not support max match depth.

I can temporarily solve this problem by increasing the route's priority.

HaoTien avatar Dec 27 '23 03:12 HaoTien

Please assign this issue to me, I would like to work on it.

janiussyafiq avatar Jan 26 '26 06:01 janiussyafiq