Skip to content

Commit 7904459

Browse files
add not_component argument for ngx.escape_uri
1 parent 8966382 commit 7904459

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

README.markdown

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5567,11 +5567,13 @@ 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_component?)*
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.
5575+
Since the `v0.10.16rc6` release, this function accepts an optional boolean `not_component` argument. When this argument is `true`, this function act like encodeURI. These characters bellow will not be escape.
5576+
a-zA-Z0-9-_.~!*'();:@&=+$,/?#
55755577

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

src/ngx_http_lua_string.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -433,17 +433,20 @@ 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 not_component)
437438
{
438-
return len + 2 * ngx_http_lua_escape_uri(NULL, (u_char *) src, len,
439-
NGX_ESCAPE_URI_COMPONENT);
439+
int type = not_component ? NGX_ESCAPE_URI : NGX_ESCAPE_URI_COMPONENT;
440+
return len + 2 * ngx_http_lua_escape_uri(NULL, (u_char *) src, len, type);
440441
}
441442

442443

443444
void
444-
ngx_http_lua_ffi_escape_uri(const u_char *src, size_t len, u_char *dst)
445+
ngx_http_lua_ffi_escape_uri(const u_char *src, size_t len, u_char *dst,
446+
int not_component)
445447
{
446-
ngx_http_lua_escape_uri(dst, (u_char *) src, len, NGX_ESCAPE_URI_COMPONENT);
448+
int type = not_component ? NGX_ESCAPE_URI : NGX_ESCAPE_URI_COMPONENT;
449+
ngx_http_lua_escape_uri(dst, (u_char *) src, len, type);
447450
}
448451

449452

src/ngx_http_lua_util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1892,7 +1892,7 @@ 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+
0x50000051, /* 0101 0000 0000 0000 0000 0000 0010 0101 */
18961896

18971897
/* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
18981898
0x78000000, /* 0111 1000 0000 0000 0000 0000 0000 0000 */

t/006-escape.t

Lines changed: 21 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 + 3);
88

99
no_long_string();
1010

@@ -197,3 +197,23 @@ GET /lua
197197
%2C%24%40%7C%60
198198
--- no_error_log
199199
[error]
200+
201+
202+
203+
=== TEST 15: not component argument
204+
--- config
205+
location /lua {
206+
content_by_lua_block {
207+
ngx.say(ngx.escape_uri("https://www.google.com", true))
208+
ngx.say(ngx.escape_uri("https://www.google.com/query?q=test", true))
209+
ngx.say(ngx.escape_uri("https://www.google.com/query?\r\nq=test", true))
210+
}
211+
}
212+
--- request
213+
GET /lua
214+
--- response_body
215+
https://www.google.com
216+
https://www.google.com/query?q=test
217+
https://www.google.com/query?%0D%0Aq=test
218+
--- no_error_log
219+
[error]

0 commit comments

Comments
 (0)