Skip to content

Commit c7951b2

Browse files
authored
feature: provide a way to reuse table in ngx.req.get_uri_args (#288)
1 parent 3efd448 commit c7951b2

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

lib/resty/core/request.lua

+8-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ local subsystem = ngx.config.subsystem
1010
local FFI_BAD_CONTEXT = base.FFI_BAD_CONTEXT
1111
local FFI_DECLINED = base.FFI_DECLINED
1212
local FFI_OK = base.FFI_OK
13+
local clear_tab = base.clear_tab
1314
local new_tab = base.new_tab
1415
local C = ffi.C
1516
local ffi_cast = ffi.cast
@@ -192,7 +193,7 @@ function ngx.req.get_headers(max_headers, raw)
192193
end
193194

194195

195-
function ngx.req.get_uri_args(max_args)
196+
function ngx.req.get_uri_args(max_args, tab)
196197
local r = get_request()
197198
if not r then
198199
error("no request found")
@@ -202,13 +203,17 @@ function ngx.req.get_uri_args(max_args)
202203
max_args = -1
203204
end
204205

206+
if tab then
207+
clear_tab(tab)
208+
end
209+
205210
local n = C.ngx_http_lua_ffi_req_get_uri_args_count(r, max_args, truncated)
206211
if n == FFI_BAD_CONTEXT then
207212
error("API disabled in the current context", 2)
208213
end
209214

210215
if n == 0 then
211-
return {}
216+
return tab or {}
212217
end
213218

214219
local args_len = C.ngx_http_lua_ffi_req_get_querystring_len(r)
@@ -218,7 +223,7 @@ function ngx.req.get_uri_args(max_args)
218223

219224
local nargs = C.ngx_http_lua_ffi_req_get_uri_args(r, strbuf, kvbuf, n)
220225

221-
local args = new_tab(0, nargs)
226+
local args = tab or new_tab(0, nargs)
222227
for i = 0, nargs - 1 do
223228
local arg = kvbuf[i]
224229

t/request.t

+68-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use t::TestCore;
88

99
repeat_each(2);
1010

11-
plan tests => repeat_each() * (blocks() * 5 + 2);
11+
plan tests => repeat_each() * (blocks() * 4 + 19);
1212

1313
#no_diff();
1414
#no_long_string();
@@ -612,3 +612,70 @@ hello
612612
[error]
613613
--- skip_nginx
614614
2: < 1.21.1
615+
616+
617+
618+
=== TEST 20: get_uri_args allows to reuse table
619+
--- config
620+
location = /t {
621+
set $foo hello;
622+
content_by_lua_block {
623+
local base = require "resty.core.base"
624+
local args = base.new_tab(0, 3)
625+
local id = tostring(args)
626+
for i = 1, 5 do
627+
base.clear_tab(args)
628+
args = ngx.req.get_uri_args(-1, args)
629+
assert(tostring(args) == id)
630+
end
631+
local keys = {}
632+
for k, _ in pairs(args) do
633+
keys[#keys + 1] = k
634+
end
635+
table.sort(keys)
636+
for _, k in ipairs(keys) do
637+
local v = args[k]
638+
if type(v) == "table" then
639+
ngx.say(k, ": ", table.concat(v, ", "))
640+
else
641+
ngx.say(k, ": ", v)
642+
end
643+
end
644+
}
645+
}
646+
--- request
647+
GET /t?a=3%200&foo%20bar=&a=hello&blah
648+
--- response_body
649+
a: 3 0, hello
650+
blah: true
651+
foo bar:
652+
--- no_error_log
653+
[error]
654+
655+
656+
657+
=== TEST 21: get_uri_args allows to reuse table (empty)
658+
--- config
659+
location = /t {
660+
set $foo hello;
661+
content_by_lua_block {
662+
local base = require "resty.core.base"
663+
local args = base.new_tab(0, 3)
664+
local id = tostring(args)
665+
for i = 1, 5 do
666+
args = ngx.req.get_uri_args(-1, args)
667+
assert(tostring(args) == id)
668+
end
669+
local n_key = 0
670+
for k, _ in pairs(args) do
671+
n_key = n_key + 1
672+
end
673+
ngx.say(n_key)
674+
}
675+
}
676+
--- request
677+
GET /t
678+
--- response_body
679+
0
680+
--- no_error_log
681+
[error]

0 commit comments

Comments
 (0)