Skip to content

Commit e7939b3

Browse files
zhuizhuhaomengagentzh
authored andcommitted
feature: ngx.escape_uri: added new optional 'type' argument not_component argument for pure URI escaping (still defauls to URI component escaping).
Signed-off-by: Yichun Zhang (agentzh) <[email protected]>
1 parent 61cfab8 commit e7939b3

File tree

3 files changed

+71
-8
lines changed

3 files changed

+71
-8
lines changed

src/ngx_stream_lua_string.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,18 +441,20 @@ ngx_stream_lua_ffi_unescape_uri(const u_char *src, size_t len, u_char *dst)
441441

442442

443443
size_t
444-
ngx_stream_lua_ffi_uri_escaped_length(const u_char *src, size_t len)
444+
ngx_stream_lua_ffi_uri_escaped_length(const u_char *src, size_t len,
445+
int type)
445446
{
446447
return len + 2 * ngx_stream_lua_escape_uri(NULL, (u_char *) src, len,
447-
NGX_ESCAPE_URI_COMPONENT);
448+
type);
448449
}
449450

450451

451452
void
452-
ngx_stream_lua_ffi_escape_uri(const u_char *src, size_t len, u_char *dst)
453+
ngx_stream_lua_ffi_escape_uri(const u_char *src, size_t len, u_char *dst,
454+
int type)
453455
{
454456
ngx_stream_lua_escape_uri(dst, (u_char *) src, len,
455-
NGX_ESCAPE_URI_COMPONENT);
457+
type);
456458
}
457459

458460

src/ngx_stream_lua_util.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,13 +1641,13 @@ ngx_stream_lua_escape_uri(u_char *dst, u_char *src, size_t size,
16411641
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
16421642

16431643
/* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
1644-
0xfc00886d, /* 1111 1100 0000 0000 1000 1000 0110 1101 */
1644+
0x80000029, /* 1000 0000 0000 0000 0000 0000 0010 1001 */
16451645

16461646
/* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
1647-
0x78000000, /* 0111 1000 0000 0000 0000 0000 0000 0000 */
1647+
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
16481648

16491649
/* ~}| {zyx wvut srqp onml kjih gfed cba` */
1650-
0xa8000000, /* 1010 1000 0000 0000 0000 0000 0000 0000 */
1650+
0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */
16511651

16521652
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
16531653
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */

t/006-escape.t

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use Test::Nginx::Socket::Lua::Stream;
44

55
repeat_each(2);
66

7-
plan tests => repeat_each() * (blocks() * 3);
7+
plan tests => repeat_each() * (blocks() * 3 + 3);
88

99
no_long_string();
1010

@@ -108,3 +108,64 @@ abc
108108
32
109109
--- no_error_log
110110
[error]
111+
112+
113+
114+
=== TEST 11: escape type
115+
--- stream_server_config
116+
content_by_lua_block {
117+
ngx.say(ngx.escape_uri("https://www.google.com/?t=abc@ :", 0))
118+
ngx.say(ngx.escape_uri("https://www.google.com/?t=abc@ :", 1))
119+
ngx.say(ngx.escape_uri("https://www.google.com/?t=abc@ :", 2))
120+
ngx.say(ngx.escape_uri("https://www.google.com/?t=abc@ :", 3))
121+
ngx.say(ngx.escape_uri("https://www.google.com/?t=abc@ :", 4))
122+
ngx.say(ngx.escape_uri("https://www.google.com/?t=abc@ :", 5))
123+
ngx.say(ngx.escape_uri("https://www.google.com/?t=abc@ :", 6))
124+
}
125+
--- stream_response
126+
https://www.google.com/%3Ft=abc@%20:
127+
https://www.google.com/%3Ft=abc@%20:
128+
https%3A%2F%2Fwww.google.com%2F%3Ft%3Dabc%40%20%3A
129+
https://www.google.com/?t=abc@%20:
130+
https://www.google.com/?t=abc@%20:
131+
https://www.google.com/?t=abc@%20:
132+
https://www.google.com/?t=abc@%20:
133+
--- no_error_log
134+
[error]
135+
136+
137+
138+
=== TEST 12: escape type error
139+
--- stream_server_config
140+
content_by_lua_block {
141+
ngx.say(ngx.escape_uri("https://www.google.com/?t=abc@ :", true))
142+
}
143+
--- stream_response
144+
--- error_log eval
145+
qr/\[error\] \d+#\d+: \*\d+ lua entry thread aborted: runtime error: "type" is not a number/
146+
--- no_error_log
147+
[alert]
148+
149+
150+
=== TEST 13: escape type out of range
151+
--- stream_server_config
152+
content_by_lua_block {
153+
ngx.say(ngx.escape_uri("https://www.google.com/?t=abc@ :", -1))
154+
}
155+
--- stream_response
156+
--- error_log eval
157+
qr/\[error\] \d+#\d+: \*\d+ lua entry thread aborted: runtime error: "type" -1 out of range/
158+
--- no_error_log
159+
[alert]
160+
161+
162+
=== TEST 14: escape type error
163+
--- stream_server_config
164+
content_by_lua_block {
165+
ngx.say(ngx.escape_uri("https://www.google.com/?t=abc@ :", 100))
166+
}
167+
--- stream_response
168+
--- error_log eval
169+
qr/\[error\] \d+#\d+: \*\d+ lua entry thread aborted: runtime error: "type" 100 out of range/
170+
--- no_error_log
171+
[alert]

0 commit comments

Comments
 (0)