Skip to content

Commit 679541b

Browse files
add not_uri_comp argument for ngx.escape_uri
1 parent 6c7c481 commit 679541b

File tree

7 files changed

+47
-23
lines changed

7 files changed

+47
-23
lines changed

README.markdown

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5567,19 +5567,17 @@ This method was introduced in the `0.5.0rc30` release.
55675567
ngx.escape_uri
55685568
--------------
55695569

5570-
**syntax:** *newstr = ngx.escape_uri(str)*
5570+
**syntax:** *newstr = ngx.escape_uri(str, not_uri_comp?)*
55715571

55725572
**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**
55735573

55745574
Escape `str` as a URI component.
55755575

5576-
Since `v0.10.16rc6`, this function accepts an optional boolean `not_component` argument. When this argument is `true`, these characters bellow will not be escaped.
5576+
Since `v0.10.16rc6`, this function accepts an optional boolean `not_uri_comp` argument. When this argument is `true`, these characters bellow will be escaped.
55775577

55785578

5579-
Alphabets: a-zA-Z
5580-
Digits: 0-9
5581-
Reserve characters: -_.~!*'();:@&=+$,/?#
5582-
5579+
ASCII code: 0~32, 127~255
5580+
Characters: ?%#!
55835581

55845582

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

doc/HttpLuaModule.wiki

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4672,21 +4672,19 @@ This method was introduced in the <code>0.5.0rc30</code> release.
46724672
46734673
== ngx.escape_uri ==
46744674
4675-
'''syntax:''' ''newstr = ngx.escape_uri(str)''
4675+
'''syntax:''' ''newstr = ngx.escape_uri(str, not_uri_comp?)''
46764676
46774677
'''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*''
46784678
46794679
Escape <code>str</code> as a URI component.
46804680
4681-
Since `v0.10.16rc6`, this function accepts an optional boolean <code>not_component</code> argument. When this argument is <code>true</code>, these characters bellow will not be escaped.
4681+
Since `v0.10.16rc6`, this function accepts an optional boolean <code>not_uri_comp</code> argument. When this argument is <code>true</code>, these characters bellow will be escaped.
46824682
46834683
<geshi lang="text">
4684-
Alphabets: a-zA-Z
4685-
Digits: 0-9
4686-
Reserve characters: -_.~!*'();:@&=+$,/?#
4684+
ASCII code: 0~32, 127~255
4685+
Characters: ?%#!
46874686
</geshi>
46884687
4689-
46904688
== ngx.unescape_uri ==
46914689
46924690
'''syntax:''' ''newstr = ngx.unescape_uri(str)''

src/ngx_http_lua_consts.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,34 @@
1313

1414
#include "ngx_http_lua_consts.h"
1515

16+
void
17+
ngx_http_lua_inject_escape_consts(lua_State *L)
18+
{
19+
/* {{{ escape constants */
20+
lua_pushinteger(L, NGX_ESCAPE_URI);
21+
lua_setfield(L, -2, "NGX_ESCAPE_URI");
22+
23+
lua_pushinteger(L, NGX_ESCAPE_ARGS);
24+
lua_setfield(L, -2, "NGX_ESCAPE_ARGS");
25+
26+
lua_pushinteger(L, NGX_ESCAPE_URI_COMPONENT);
27+
lua_setfield(L, -2, "NGX_ESCAPE_URI_COMPONENT");
28+
29+
lua_pushinteger(L, NGX_ESCAPE_HTML);
30+
lua_setfield(L, -2, "NGX_ESCAPE_HTML");
31+
32+
lua_pushinteger(L, NGX_ESCAPE_REFRESH);
33+
lua_setfield(L, -2, "NGX_ESCAPE_REFRESH");
34+
35+
lua_pushinteger(L, NGX_ESCAPE_MEMCACHED);
36+
lua_setfield(L, -2, "NGX_ESCAPE_MEMCACHED");
37+
38+
lua_pushinteger(L, NGX_ESCAPE_MAIL_AUTH);
39+
lua_setfield(L, -2, "ERNGX_ESCAPE_MAIL_AUTHROR");
40+
41+
/* }}} */
42+
}
43+
1644

1745
void
1846
ngx_http_lua_inject_core_consts(lua_State *L)

src/ngx_http_lua_consts.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
void ngx_http_lua_inject_http_consts(lua_State *L);
1515
void ngx_http_lua_inject_core_consts(lua_State *L);
16+
void ngx_http_lua_inject_escape_consts(lua_State *L);
1617

1718

1819
#endif /* _NGX_HTTP_LUA_CONSTS_H_INCLUDED_ */

src/ngx_http_lua_string.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,18 +434,16 @@ ngx_http_lua_ffi_unescape_uri(const u_char *src, size_t len, u_char *dst)
434434

435435
size_t
436436
ngx_http_lua_ffi_uri_escaped_length(const u_char *src, size_t len,
437-
int not_component)
437+
int type)
438438
{
439-
int type = not_component ? NGX_ESCAPE_URI : NGX_ESCAPE_URI_COMPONENT;
440439
return len + 2 * ngx_http_lua_escape_uri(NULL, (u_char *) src, len, type);
441440
}
442441

443442

444443
void
445444
ngx_http_lua_ffi_escape_uri(const u_char *src, size_t len, u_char *dst,
446-
int not_component)
445+
int type)
447446
{
448-
int type = not_component ? NGX_ESCAPE_URI : NGX_ESCAPE_URI_COMPONENT;
449447
ngx_http_lua_escape_uri(dst, (u_char *) src, len, type);
450448
}
451449

src/ngx_http_lua_util.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,7 @@ ngx_http_lua_inject_ngx_api(lua_State *L, ngx_http_lua_main_conf_t *lmcf,
726726

727727
ngx_http_lua_inject_arg_api(L);
728728

729+
ngx_http_lua_inject_escape_consts(L);
729730
ngx_http_lua_inject_http_consts(L);
730731
ngx_http_lua_inject_core_consts(L);
731732

@@ -1892,13 +1893,13 @@ ngx_http_lua_escape_uri(u_char *dst, u_char *src, size_t size, ngx_uint_t type)
18921893
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
18931894

18941895
/* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
1895-
0x50000025, /* 0101 0000 0000 0000 0000 0000 0010 0101 */
1896+
0x80000029, /* 1000 0000 0000 0000 0000 0000 0010 1001 */
18961897

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

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

19031904
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
19041905
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */

t/006-escape.t

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,9 @@ GET /lua
215215
GET /lua
216216
--- response_body
217217
https://www.google.com
218-
https://www.google.com/query?q=test
219-
https://www.google.com/query?%0D%0Aq=test
220-
-_.~!*'();:@&=+$,/?#
221-
%3C%3E%5B%5D%7B%7D%5C%22%20
218+
https://www.google.com/query%3Fq=test
219+
https://www.google.com/query%3F%0D%0Aq=test
220+
-_.~!*'();:@&=+$,/%3F%23
221+
<>[]{}\"%20
222222
--- no_error_log
223223
[error]

0 commit comments

Comments
 (0)