Skip to content

Commit

Permalink
change: don't treat route segment with ':' as parameter by default
Browse files Browse the repository at this point in the history
Fix #3134.

You can still get the original behavior by set
enable_parameter_in_route to true.

Signed-off-by: spacewander <spacewanderlzx@gmail.com>
  • Loading branch information
spacewander committed Dec 29, 2020
1 parent 45e909a commit a140eae
Show file tree
Hide file tree
Showing 10 changed files with 268 additions and 4 deletions.
2 changes: 1 addition & 1 deletion apisix/control/router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
-- limitations under the License.
--
local require = require
local router = require("resty.radixtree")
local router = require("apisix.router")
local builtin_v1_routes = require("apisix.control.v1")
local plugin_mod = require("apisix.plugin")
local core = require("apisix.core")
Expand Down
2 changes: 1 addition & 1 deletion apisix/http/router/radixtree_host_uri.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
-- limitations under the License.
--
local require = require
local router = require("resty.radixtree")
local router = require("apisix.router")
local core = require("apisix.core")
local http_route = require("apisix.http.route")
local ipairs = ipairs
Expand Down
2 changes: 1 addition & 1 deletion apisix/http/router/radixtree_uri.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
-- limitations under the License.
--
local require = require
local router = require("resty.radixtree")
local router = require("apisix.router")
local core = require("apisix.core")
local http_route = require("apisix.http.route")
local ipairs = ipairs
Expand Down
16 changes: 16 additions & 0 deletions apisix/router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
-- limitations under the License.
--
local require = require
local resty_router = require("resty.radixtree")
local core = require("apisix.core")
local plugin_checker = require("apisix.plugin").plugin_checker
local error = error
Expand Down Expand Up @@ -128,4 +129,19 @@ end
_M.filter_test = filter


do
local router_opts = {
no_param_match = true
}

function _M.new(routes)
local local_conf = core.config.local_conf()
router_opts.no_param_match = not core.table.try_read_attr(local_conf, "apisix", "feature",
"enable_parameter_in_route")
return resty_router.new(routes, router_opts)
end

end


return _M
2 changes: 2 additions & 0 deletions conf/config-default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ apisix:
# control:
# ip: "127.0.0.1"
# port: 9090
feature:
enable_parameter_in_route: false # allow using ':param' in the router. See https://github.com/api7/lua-resty-radixtree/#parameters-in-path

nginx_config: # config for render the template to generate nginx.conf
error_log: "logs/error.log"
Expand Down
20 changes: 20 additions & 0 deletions doc/router-radixtree.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,26 @@ Here are the rules:
|/blog/foo/gloo | `/blog/foo/*` |
|/blog/bar | not match |

#### 4. Parameter match

When `enable_parameter_in_route` is configured, we can match routes with parameters.

For example, with configuration:
```yaml
apisix:
feature:
enable_parameter_in_route: true # default false
```
route like
```
/blog/:name
```

will match both `/blog/dog` and `/blog/cat`.

For more details, see https://github.com/api7/lua-resty-radixtree/#parameters-in-path.

### How to filter route by Nginx builtin variable

Please take a look at [radixtree-new](https://github.com/iresty/lua-resty-radixtree#new),
Expand Down
2 changes: 1 addition & 1 deletion rockspec/apisix-master-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ dependencies = {
"lua-resty-cookie = 0.1.0",
"lua-resty-session = 2.24",
"opentracing-openresty = 0.1",
"lua-resty-radixtree = 2.5",
"lua-resty-radixtree = 2.6",
"lua-protobuf = 0.3.1",
"lua-resty-openidc = 1.7.2-1",
"luafilesystem = 1.7.0-2",
Expand Down
16 changes: 16 additions & 0 deletions t/node/route-parameter-uri.t
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ worker_connections(256);
no_root_location();
no_shuffle();

our $yaml_config = <<_EOC_;
apisix:
node_listen: 1984
admin_key: null
feature:
enable_parameter_in_route: true
_EOC_

add_block_preprocessor(sub {
my ($block) = @_;

if (!$block->yaml_config) {
$block->set_value("yaml_config", $yaml_config);
}
});

run_tests();

__DATA__
Expand Down
157 changes: 157 additions & 0 deletions t/router/radixtree-host-uri2.t
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,160 @@ qr/1981/
use config_center: yaml
--- no_error_log
[error]
=== TEST 6: set route with ':'
--- yaml_config
apisix:
node_listen: 1984
router:
http: 'radixtree_host_uri'
admin_key: null
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"uri": "/file:listReputationHistories",
"plugins":{"proxy-rewrite":{"uri":"/hello"}},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
}
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]
=== TEST 7: hit routes
--- yaml_config
apisix:
router:
http: 'radixtree_host_uri'
--- request
GET /file:listReputationHistories
--- response_body
hello world
--- no_error_log
[error]
=== TEST 8: not hit
--- yaml_config
apisix:
router:
http: 'radixtree_host_uri'
--- request
GET /file:xx
--- error_code: 404
--- no_error_log
[error]
=== TEST 9: set route with ':' & host
--- yaml_config
apisix:
node_listen: 1984
router:
http: 'radixtree_host_uri'
admin_key: null
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"uri": "/do:listReputationHistories",
"hosts": ["t.com"],
"plugins":{"proxy-rewrite":{"uri":"/hello"}},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
}
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]
=== TEST 10: hit routes
--- yaml_config
apisix:
router:
http: 'radixtree_host_uri'
--- request
GET /do:listReputationHistories
--- more_headers
Host: t.com
--- response_body
hello world
--- no_error_log
[error]
=== TEST 11: not hit
--- yaml_config
apisix:
router:
http: 'radixtree_host_uri'
--- request
GET /do:xx
--- more_headers
Host: t.com
--- error_code: 404
--- no_error_log
[error]
=== TEST 12: hit as enable_parameter_in_route enabled
--- yaml_config
apisix:
router:
http: 'radixtree_host_uri'
feature:
enable_parameter_in_route: true
--- request
GET /do:xx
--- more_headers
Host: t.com
--- response_body
hello world
--- no_error_log
[error]
53 changes: 53 additions & 0 deletions t/router/radixtree-uri-host.t
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,56 @@ GET /hello?name=json
hello world
--- no_error_log
[error]
=== TEST 15: set route with ':'
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"uri": "/file:listReputationHistories",
"plugins":{"proxy-rewrite":{"uri":"/hello"}},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
}
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]
=== TEST 16: hit routes
--- request
GET /file:listReputationHistories
--- response_body
hello world
--- no_error_log
[error]
=== TEST 17: not hit
--- request
GET /file:xx
--- error_code: 404
--- no_error_log
[error]

0 comments on commit a140eae

Please sign in to comment.