Skip to content

feature: add option arg raw for unescape_uri, get_uri_args #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/resty/core/request.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ ffi.cdef[[
size_t ngx_http_lua_ffi_req_get_querystring_len(ngx_http_request_t *r);

int ngx_http_lua_ffi_req_get_uri_args(ngx_http_request_t *r,
unsigned char *buf, ngx_http_lua_ffi_table_elt_t *out, int count);
unsigned char *buf, ngx_http_lua_ffi_table_elt_t *out, int count,
int raw);

double ngx_http_lua_ffi_req_start_time(ngx_http_request_t *r);

Expand Down Expand Up @@ -132,7 +133,7 @@ function ngx.req.get_headers(max_headers, raw)
end


function ngx.req.get_uri_args(max_args)
function ngx.req.get_uri_args(max_args, raw)
local r = getfenv(0).__ngx_req
if not r then
return error("no request found")
Expand All @@ -151,12 +152,18 @@ function ngx.req.get_uri_args(max_args)
return {}
end

if not raw then
raw = 0
else
raw = 1
end

local args_len = C.ngx_http_lua_ffi_req_get_querystring_len(r)

local strbuf = get_string_buf(args_len + n * table_elt_size)
local kvbuf = ffi_cast(table_elt_type, strbuf + args_len)

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

local args = new_tab(0, nargs)
for i = 0, nargs - 1 do
Expand Down
15 changes: 11 additions & 4 deletions lib/resty/core/uri.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ ffi.cdef[[
void ngx_http_lua_ffi_escape_uri(const unsigned char *src, size_t len,
unsigned char *dst);

size_t ngx_http_lua_ffi_unescape_uri(const unsigned char *src,
size_t len, unsigned char *dst);
size_t ngx_http_lua_ffi_unescape_uri(const unsigned char *src, size_t len,
unsigned char *dst, int raw);
]]


Expand All @@ -49,18 +49,25 @@ ngx.escape_uri = function (s)
end


ngx.unescape_uri = function (s)
ngx.unescape_uri = function (s, raw)
if type(s) ~= 'string' then
if not s then
s = ''
else
s = tostring(s)
end
end

if not raw then
raw = 0
else
raw = 1
end

local slen = #s
local dlen = slen
local dst = get_string_buf(dlen)
dlen = C.ngx_http_lua_ffi_unescape_uri(s, slen, dst)
dlen = C.ngx_http_lua_ffi_unescape_uri(s, slen, dst, raw)
return ffi_string(dst, dlen)
end

Expand Down
88 changes: 88 additions & 0 deletions t/request.t
Original file line number Diff line number Diff line change
Expand Up @@ -568,3 +568,91 @@ qr/\[TRACE \d+ content_by_lua:4 loop\]/
bad argument type
stitch



=== TEST 17: ngx.req.get_uri_args (raw is not set)
--- http_config eval: $::HttpConfig
--- config
location = /t {
set $foo hello;
content_by_lua '
local ffi = require "ffi"
local args
for i = 1, 100 do
args = ngx.req.get_uri_args()
end
if type(args) ~= "table" then
ngx.say("bad args type found: ", args)
return
end
local keys = {}
for k, _ in pairs(args) do
keys[#keys + 1] = k
end
table.sort(keys)
for _, k in ipairs(keys) do
local v = args[k]
if type(v) == "table" then
ngx.say(k, ": ", table.concat(v, ", "))
else
ngx.say(k, ": ", v)
end
end
';
}
--- request
GET /t?a=3%200+10&foo%20bar=&a=hello&blah
--- response_body
a: 3 0 10, hello
blah: true
foo bar:
--- error_log eval
qr/\[TRACE \d+ .*? -> \d+\]/
--- no_error_log
[error]
-- NYI:



=== TEST 18: ngx.req.get_uri_args (raw is true)
--- http_config eval: $::HttpConfig
--- config
location = /t {
set $foo hello;
content_by_lua '
local ffi = require "ffi"
local args
for i = 1, 100 do
args = ngx.req.get_uri_args(nil, true)
end
if type(args) ~= "table" then
ngx.say("bad args type found: ", args)
return
end
local keys = {}
for k, _ in pairs(args) do
keys[#keys + 1] = k
end
table.sort(keys)
for _, k in ipairs(keys) do
local v = args[k]
if type(v) == "table" then
ngx.say(k, ": ", table.concat(v, ", "))
else
ngx.say(k, ": ", v)
end
end
';
}
--- request
GET /t?a=3%200+10&foo%20bar=&a=hello&blah
--- response_body
a: 3 0+10, hello
blah: true
foo bar:
--- error_log eval
qr/\[TRACE \d+ .*? -> \d+\]/
--- no_error_log
[error]
-- NYI:

46 changes: 46 additions & 0 deletions t/uri.t
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,49 @@ qr/\[TRACE \d+ content_by_lua:3 loop\]/
--- no_error_log
[error]



=== TEST 11: unescape_uri (string) raw is not set
--- http_config eval: $::HttpConfig
--- config
location = /uri {
content_by_lua '
local s
for i = 1, 100 do
s = ngx.unescape_uri("hello+%20world")
end
ngx.say(s)
';
}
--- request
GET /uri
--- response_body
hello world
--- error_log eval
qr/\[TRACE \d+ content_by_lua:3 loop\]/
--- no_error_log
[error]



=== TEST 12: unescape_uri (string) raw is true
--- http_config eval: $::HttpConfig
--- config
location = /uri {
content_by_lua '
local s
for i = 1, 100 do
s = ngx.unescape_uri("hello+%20world", true)
end
ngx.say(s)
';
}
--- request
GET /uri
--- response_body
hello+ world
--- error_log eval
qr/\[TRACE \d+ content_by_lua:3 loop\]/
--- no_error_log
[error]