Skip to content

Commit

Permalink
Provide uri_arg_ when radixtree_uri_with_parameter
Browse files Browse the repository at this point in the history
Add the prefix `uri_arg_` to the APISIX `ctx.var` allowing a APISIX configured to use the `radixtree_uri_with_parameter` router to access `opts.matched`.
This allows the usage of the matches inside for example the `proxy_rewrite` plugin.

Also see https://github.com/api7/lua-resty-radixtree/#parameters-in-path
  • Loading branch information
boekkooi-lengoo committed Dec 13, 2023
1 parent 4db435e commit cd13465
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
5 changes: 5 additions & 0 deletions apisix/core/ctx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,11 @@ do
end
end

elseif core_str.has_prefix(key, "uri_arg_") then
if t._ctx.curr_req_matched then
val = t._ctx.curr_req_matched[(sub_str(key, 9))]
end

elseif core_str.has_prefix(key, "http_") then
key = key:lower()
key = re_gsub(key, "-", "_", "jo")
Expand Down
106 changes: 106 additions & 0 deletions t/core/ctx_with_params.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
use t::APISIX 'no_plan';

repeat_each(1);
no_long_string();
no_root_location();
no_shuffle();

our $yaml_config = <<_EOC_;
apisix:
router:
http: 'radixtree_uri_with_parameter'
_EOC_

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

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

run_tests;

__DATA__
=== TEST 1: sanity
--- config
location /t {
content_by_lua_block {
local core = require("apisix.core")
local ctx = {}
core.ctx.set_vars_meta(ctx)
ngx.say("remote_addr: ", ctx.var["remote_addr"])
ngx.say("server_port: ", ctx.var["server_port"])
}
}
--- request
GET /t
--- response_body
remote_addr: 127.0.0.1
server_port: 1984
=== TEST 1: add route and get `uri_arg_`
--- 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,
[[{
"methods": ["GET"],
"plugins": {
"serverless-pre-function": {
"phase": "access",
"functions" : ["return function() ngx.log(ngx.INFO, \"uri_arg_path: \", ngx.ctx.api_ctx.var.uri_arg_path) end"]
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:1980": 1
}
},
"uri": "/:path"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
=== TEST 8: `uri_arg_path` exist
--- request
GET /hello
--- response_body
hello world
--- error_log
uri_arg_path: hello

0 comments on commit cd13465

Please sign in to comment.