Skip to content

Commit 8174409

Browse files
zhuizhuhaomengagentzh
authored andcommitted
feature: resty.core.uri: ngx.escape_uri: add optional argument 'type'.
Signed-off-by: Yichun Zhang (agentzh) <[email protected]>
1 parent 266d19c commit 8174409

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

lib/resty/core/uri.lua

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@ local ngx_lua_ffi_escape_uri
1818
local ngx_lua_ffi_unescape_uri
1919
local ngx_lua_ffi_uri_escaped_length
2020

21+
local NGX_ESCAPE_URI = 0
22+
local NGX_ESCAPE_URI_COMPONENT = 2
23+
local NGX_ESCAPE_MAIL_AUTH = 6
24+
2125

2226
if subsystem == "http" then
2327
ffi.cdef[[
2428
size_t ngx_http_lua_ffi_uri_escaped_length(const unsigned char *src,
25-
size_t len);
29+
size_t len, int type);
2630

2731
void ngx_http_lua_ffi_escape_uri(const unsigned char *src, size_t len,
28-
unsigned char *dst);
32+
unsigned char *dst, int type);
2933

3034
size_t ngx_http_lua_ffi_unescape_uri(const unsigned char *src,
3135
size_t len, unsigned char *dst);
@@ -38,10 +42,10 @@ if subsystem == "http" then
3842
elseif subsystem == "stream" then
3943
ffi.cdef[[
4044
size_t ngx_stream_lua_ffi_uri_escaped_length(const unsigned char *src,
41-
size_t len);
45+
size_t len, int type);
4246

4347
void ngx_stream_lua_ffi_escape_uri(const unsigned char *src, size_t len,
44-
unsigned char *dst);
48+
unsigned char *dst, int type);
4549

4650
size_t ngx_stream_lua_ffi_unescape_uri(const unsigned char *src,
4751
size_t len, unsigned char *dst);
@@ -53,22 +57,38 @@ elseif subsystem == "stream" then
5357
end
5458

5559

56-
ngx.escape_uri = function (s)
60+
ngx.escape_uri = function (s, esc_type)
5761
if type(s) ~= 'string' then
5862
if not s then
5963
s = ''
64+
6065
else
6166
s = tostring(s)
6267
end
6368
end
69+
70+
if esc_type == nil then
71+
esc_type = NGX_ESCAPE_URI_COMPONENT
72+
73+
else
74+
if type(esc_type) ~= 'number' then
75+
error("\"type\" is not number", 3)
76+
end
77+
78+
if esc_type < NGX_ESCAPE_URI or esc_type > NGX_ESCAPE_MAIL_AUTH then
79+
error("\"type\" " .. esc_type .. " out of range", 3)
80+
end
81+
end
82+
6483
local slen = #s
65-
local dlen = ngx_lua_ffi_uri_escaped_length(s, slen)
84+
local dlen = ngx_lua_ffi_uri_escaped_length(s, slen, esc_type)
85+
6686
-- print("dlen: ", tonumber(dlen))
6787
if dlen == slen then
6888
return s
6989
end
7090
local dst = get_string_buf(dlen)
71-
ngx_lua_ffi_escape_uri(s, slen, dst)
91+
ngx_lua_ffi_escape_uri(s, slen, dst, esc_type)
7292
return ffi_string(dst, dlen)
7393
end
7494

0 commit comments

Comments
 (0)