Skip to content

Commit bb045f2

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 5d15172 commit bb045f2

File tree

5 files changed

+127
-13
lines changed

5 files changed

+127
-13
lines changed

README.markdown

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5593,11 +5593,19 @@ This method was introduced in the `0.5.0rc30` release.
55935593
ngx.escape_uri
55945594
--------------
55955595

5596-
**syntax:** *newstr = ngx.escape_uri(str)*
5596+
**syntax:** *newstr = ngx.escape_uri(str, type?)*
55975597

55985598
**context:** *init_by_lua&#42;, init_worker_by_lua&#42;, set_by_lua&#42;, rewrite_by_lua&#42;, access_by_lua&#42;, content_by_lua&#42;, header_filter_by_lua&#42;, body_filter_by_lua&#42;, log_by_lua&#42;, ngx.timer.&#42;, balancer_by_lua&#42;, ssl_certificate_by_lua&#42;, ssl_session_fetch_by_lua&#42;, ssl_session_store_by_lua&#42;*
55995599

5600-
Escape `str` as a URI component.
5600+
Since `v0.10.16`, this function accepts an optional `type` argument.
5601+
It accepts the following values (defaults to `2`):
5602+
5603+
* `0`: escapes `str` as a full URI. And the characters
5604+
` ` (space), `#`, `%`,
5605+
`?`, 0x00 ~ 0x1F, 0x7F ~ 0xFF will be escaped.
5606+
* `2`: escape `str` as a URI component. All characters except
5607+
alphabetic characters, digits, `-`, `.`, <code>_<code>,
5608+
`~` will be encoded as `%XX`.
56015609

56025610
[Back to TOC](#nginx-api-for-lua)
56035611

doc/HttpLuaModule.wiki

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4698,11 +4698,19 @@ This method was introduced in the <code>0.5.0rc30</code> release.
46984698
46994699
== ngx.escape_uri ==
47004700
4701-
'''syntax:''' ''newstr = ngx.escape_uri(str)''
4701+
'''syntax:''' ''newstr = ngx.escape_uri(str, type?)''
47024702
47034703
'''context:''' ''init_by_lua*, init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*''
47044704
4705-
Escape <code>str</code> as a URI component.
4705+
Since `v0.10.16`, this function accepts an optional <code>type</code> argument.
4706+
It accepts the following values (defaults to `2`):
4707+
4708+
* `0`: escapes <code>str</code> as a full URI. And the characters
4709+
<code> </code> (space), <code>#</code>, <code>%</code>,
4710+
`?`, 0x00 ~ 0x1F, 0x7F ~ 0xFF will be escaped.
4711+
* `2`: escape <code>str</code> as a URI component. All characters except
4712+
alphabetic characters, digits, <code>-</code>, <code>.</code>, <code>_<code>,
4713+
<code>~</code> will be encoded as `%XX`.
47064714
47074715
== ngx.unescape_uri ==
47084716

src/ngx_http_lua_string.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -433,17 +433,18 @@ ngx_http_lua_ffi_unescape_uri(const u_char *src, size_t len, u_char *dst)
433433

434434

435435
size_t
436-
ngx_http_lua_ffi_uri_escaped_length(const u_char *src, size_t len)
436+
ngx_http_lua_ffi_uri_escaped_length(const u_char *src, size_t len,
437+
int type)
437438
{
438-
return len + 2 * ngx_http_lua_escape_uri(NULL, (u_char *) src, len,
439-
NGX_ESCAPE_URI_COMPONENT);
439+
return len + 2 * ngx_http_lua_escape_uri(NULL, (u_char *) src, len, type);
440440
}
441441

442442

443443
void
444-
ngx_http_lua_ffi_escape_uri(const u_char *src, size_t len, u_char *dst)
444+
ngx_http_lua_ffi_escape_uri(const u_char *src, size_t len, u_char *dst,
445+
int type)
445446
{
446-
ngx_http_lua_escape_uri(dst, (u_char *) src, len, NGX_ESCAPE_URI_COMPONENT);
447+
ngx_http_lua_escape_uri(dst, (u_char *) src, len, type);
447448
}
448449

449450

src/ngx_http_lua_util.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1892,13 +1892,13 @@ ngx_http_lua_escape_uri(u_char *dst, u_char *src, size_t size, ngx_uint_t type)
18921892
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
18931893

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

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

19001900
/* ~}| {zyx wvut srqp onml kjih gfed cba` */
1901-
0xa8000000, /* 1010 1000 0000 0000 0000 0000 0000 0000 */
1901+
0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */
19021902

19031903
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
19041904
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */

t/006-escape.t

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

55
repeat_each(2);
66

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

99
no_long_string();
1010

@@ -197,3 +197,100 @@ GET /lua
197197
%2C%24%40%7C%60
198198
--- no_error_log
199199
[error]
200+
201+
202+
203+
=== TEST 15: escape type argument
204+
--- config
205+
location /lua {
206+
content_by_lua_block {
207+
ngx.say(ngx.escape_uri("https://www.google.com", 0))
208+
ngx.say(ngx.escape_uri("https://www.google.com/query?q=test", 0))
209+
ngx.say(ngx.escape_uri("https://www.google.com/query?\r\nq=test", 0))
210+
ngx.say(ngx.escape_uri("-_.~!*'();:@&=+$,/?#", 0))
211+
ngx.say(ngx.escape_uri("<>[]{}\\\" ", 0))
212+
}
213+
}
214+
--- request
215+
GET /lua
216+
--- response_body
217+
https://www.google.com
218+
https://www.google.com/query%3Fq=test
219+
https://www.google.com/query%3F%0D%0Aq=test
220+
-_.~!*'();:@&=+$,/%3F%23
221+
<>[]{}\"%20
222+
--- no_error_log
223+
[error]
224+
225+
226+
227+
=== TEST 16: escape type argument
228+
--- config
229+
location /lua {
230+
content_by_lua_block {
231+
ngx.say(ngx.escape_uri("https://www.google.com/?t=abc@ :", 0))
232+
ngx.say(ngx.escape_uri("https://www.google.com/?t=abc@ :", 1))
233+
ngx.say(ngx.escape_uri("https://www.google.com/?t=abc@ :", 2))
234+
ngx.say(ngx.escape_uri("https://www.google.com/?t=abc@ :", 3))
235+
ngx.say(ngx.escape_uri("https://www.google.com/?t=abc@ :", 4))
236+
ngx.say(ngx.escape_uri("https://www.google.com/?t=abc@ :", 5))
237+
ngx.say(ngx.escape_uri("https://www.google.com/?t=abc@ :", 6))
238+
}
239+
}
240+
--- request
241+
GET /lua
242+
--- response_body
243+
https://www.google.com/%3Ft=abc@%20:
244+
https://www.google.com/%3Ft=abc@%20:
245+
https%3A%2F%2Fwww.google.com%2F%3Ft%3Dabc%40%20%3A
246+
https://www.google.com/?t=abc@%20:
247+
https://www.google.com/?t=abc@%20:
248+
https://www.google.com/?t=abc@%20:
249+
https://www.google.com/?t=abc@%20:
250+
--- no_error_log
251+
[error]
252+
253+
254+
255+
=== TEST 17: escape type out of range
256+
--- config
257+
location /lua {
258+
content_by_lua_block {
259+
ngx.say(ngx.escape_uri("https://www.google.com", -1))
260+
}
261+
}
262+
--- request
263+
GET /lua
264+
--- error_code: 500
265+
--- error_log eval
266+
qr/\[error\] \d+#\d+: \*\d+ lua entry thread aborted: runtime error: "type" \-1 out of range/
267+
268+
269+
270+
=== TEST 18: escape type out of range
271+
--- config
272+
location /lua {
273+
content_by_lua_block {
274+
ngx.say(ngx.escape_uri("https://www.google.com", 10))
275+
}
276+
}
277+
--- request
278+
GET /lua
279+
--- error_code: 500
280+
--- error_log eval
281+
qr/\[error\] \d+#\d+: \*\d+ lua entry thread aborted: runtime error: "type" 10 out of range/
282+
283+
284+
285+
=== TEST 19: escape type not integer
286+
--- config
287+
location /lua {
288+
content_by_lua_block {
289+
ngx.say(ngx.escape_uri("https://www.google.com", true))
290+
}
291+
}
292+
--- request
293+
GET /lua
294+
--- error_code: 500
295+
--- error_log eval
296+
qr/\[error\] \d+#\d+: \*\d+ lua entry thread aborted: runtime error: "type" is not number/

0 commit comments

Comments
 (0)