Skip to content

Commit e454b0b

Browse files
committed
Merge branch 'master' into ffi
2 parents 0bd56bb + a442980 commit e454b0b

File tree

12 files changed

+793
-143
lines changed

12 files changed

+793
-143
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ src/phase.[ch]
150150
src/probe.h
151151
src/uthread.[ch]
152152
src/timer.[ch]
153+
src/config.[ch]
153154
*.plist
154155
lua
155156
ttimer

README

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,13 @@ Description
207207

208208
* lua-resty-upload (<https://github.com/agentzh/lua-resty-upload>)
209209

210+
* lua-resty-websocket
211+
(<https://github.com/agentzh/lua-resty-websocket>)
212+
213+
* lua-resty-lock (<https://github.com/agentzh/lua-resty-lock>)
214+
215+
* lua-resty-string (<https://github.com/agentzh/lua-resty-string>)
216+
210217
* ngx_memc
211218

212219
* ngx_postgres (<https://github.com/FRiCKLE/ngx_postgres>)
@@ -3822,21 +3829,21 @@ Nginx API for Lua
38223829

38233830
The optional fourth argument, "ctx", can be a Lua table holding an
38243831
optional "pos" field. When the "pos" field in the "ctx" table argument
3825-
is specified, "ngx.re.match" will start matching from that offset.
3826-
Regardless of the presence of the "pos" field in the "ctx" table,
3827-
"ngx.re.match" will always set this "pos" field to the position *after*
3828-
the substring matched by the whole pattern in case of a successful
3829-
match. When match fails, the "ctx" table will be left intact.
3832+
is specified, "ngx.re.match" will start matching from that offset
3833+
(starting from 1). Regardless of the presence of the "pos" field in the
3834+
"ctx" table, "ngx.re.match" will always set this "pos" field to the
3835+
position *after* the substring matched by the whole pattern in case of a
3836+
successful match. When match fails, the "ctx" table will be left intact.
38303837

38313838
local ctx = {}
38323839
local m, err = ngx.re.match("1234, hello", "[0-9]+", "", ctx)
38333840
-- m[0] = "1234"
3834-
-- ctx.pos == 4
3841+
-- ctx.pos == 5
38353842

38363843
local ctx = { pos = 2 }
38373844
local m, err = ngx.re.match("1234, hello", "[0-9]+", "", ctx)
38383845
-- m[0] = "34"
3839-
-- ctx.pos == 4
3846+
-- ctx.pos == 5
38403847

38413848
The "ctx" table argument combined with the "a" regex modifier can be
38423849
used to construct a lexer atop "ngx.re.match".

README.markdown

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,9 @@ At least the following Lua libraries and Nginx modules can be used with this ngx
400400
* [lua-resty-redis](https://github.com/agentzh/lua-resty-redis)
401401
* [lua-resty-dns](https://github.com/agentzh/lua-resty-dns)
402402
* [lua-resty-upload](https://github.com/agentzh/lua-resty-upload)
403+
* [lua-resty-websocket](https://github.com/agentzh/lua-resty-websocket)
404+
* [lua-resty-lock](https://github.com/agentzh/lua-resty-lock)
405+
* [lua-resty-string](https://github.com/agentzh/lua-resty-string)
403406
* [ngx_memc](http://github.com/agentzh/memc-nginx-module)
404407
* [ngx_postgres](https://github.com/FRiCKLE/ngx_postgres)
405408
* [ngx_redis2](http://github.com/agentzh/redis2-nginx-module)
@@ -4079,22 +4082,22 @@ These options can be combined:
40794082

40804083
The `o` option is useful for performance tuning, because the regex pattern in question will only be compiled once, cached in the worker-process level, and shared among all requests in the current Nginx worker process. The upper limit of the regex cache can be tuned via the [lua_regex_cache_max_entries](#lua_regex_cache_max_entries) directive.
40814084

4082-
The optional fourth argument, `ctx`, can be a Lua table holding an optional `pos` field. When the `pos` field in the `ctx` table argument is specified, `ngx.re.match` will start matching from that offset. Regardless of the presence of the `pos` field in the `ctx` table, `ngx.re.match` will always set this `pos` field to the position *after* the substring matched by the whole pattern in case of a successful match. When match fails, the `ctx` table will be left intact.
4085+
The optional fourth argument, `ctx`, can be a Lua table holding an optional `pos` field. When the `pos` field in the `ctx` table argument is specified, `ngx.re.match` will start matching from that offset (starting from 1). Regardless of the presence of the `pos` field in the `ctx` table, `ngx.re.match` will always set this `pos` field to the position *after* the substring matched by the whole pattern in case of a successful match. When match fails, the `ctx` table will be left intact.
40834086

40844087
```lua
40854088

40864089
local ctx = {}
40874090
local m, err = ngx.re.match("1234, hello", "[0-9]+", "", ctx)
40884091
-- m[0] = "1234"
4089-
-- ctx.pos == 4
4092+
-- ctx.pos == 5
40904093
```
40914094

40924095
```lua
40934096

40944097
local ctx = { pos = 2 }
40954098
local m, err = ngx.re.match("1234, hello", "[0-9]+", "", ctx)
40964099
-- m[0] = "34"
4097-
-- ctx.pos == 4
4100+
-- ctx.pos == 5
40984101
```
40994102

41004103
The `ctx` table argument combined with the `a` regex modifier can be used to construct a lexer atop `ngx.re.match`.

doc/HttpLuaModule.wiki

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ At least the following Lua libraries and Nginx modules can be used with this ngx
195195
* [https://github.com/agentzh/lua-resty-redis lua-resty-redis]
196196
* [https://github.com/agentzh/lua-resty-dns lua-resty-dns]
197197
* [https://github.com/agentzh/lua-resty-upload lua-resty-upload]
198+
* [https://github.com/agentzh/lua-resty-websocket lua-resty-websocket]
199+
* [https://github.com/agentzh/lua-resty-lock lua-resty-lock]
200+
* [https://github.com/agentzh/lua-resty-string lua-resty-string]
198201
* [[HttpMemcModule|ngx_memc]]
199202
* [https://github.com/FRiCKLE/ngx_postgres ngx_postgres]
200203
* [[HttpRedis2Module|ngx_redis2]]
@@ -3394,20 +3397,20 @@ These options can be combined:
33943397
33953398
The <code>o</code> option is useful for performance tuning, because the regex pattern in question will only be compiled once, cached in the worker-process level, and shared among all requests in the current Nginx worker process. The upper limit of the regex cache can be tuned via the [[#lua_regex_cache_max_entries|lua_regex_cache_max_entries]] directive.
33963399
3397-
The optional fourth argument, <code>ctx</code>, can be a Lua table holding an optional <code>pos</code> field. When the <code>pos</code> field in the <code>ctx</code> table argument is specified, <code>ngx.re.match</code> will start matching from that offset. Regardless of the presence of the <code>pos</code> field in the <code>ctx</code> table, <code>ngx.re.match</code> will always set this <code>pos</code> field to the position ''after'' the substring matched by the whole pattern in case of a successful match. When match fails, the <code>ctx</code> table will be left intact.
3400+
The optional fourth argument, <code>ctx</code>, can be a Lua table holding an optional <code>pos</code> field. When the <code>pos</code> field in the <code>ctx</code> table argument is specified, <code>ngx.re.match</code> will start matching from that offset (starting from 1). Regardless of the presence of the <code>pos</code> field in the <code>ctx</code> table, <code>ngx.re.match</code> will always set this <code>pos</code> field to the position ''after'' the substring matched by the whole pattern in case of a successful match. When match fails, the <code>ctx</code> table will be left intact.
33983401
33993402
<geshi lang="lua">
34003403
local ctx = {}
34013404
local m, err = ngx.re.match("1234, hello", "[0-9]+", "", ctx)
34023405
-- m[0] = "1234"
3403-
-- ctx.pos == 4
3406+
-- ctx.pos == 5
34043407
</geshi>
34053408
34063409
<geshi lang="lua">
34073410
local ctx = { pos = 2 }
34083411
local m, err = ngx.re.match("1234, hello", "[0-9]+", "", ctx)
34093412
-- m[0] = "34"
3410-
-- ctx.pos == 4
3413+
-- ctx.pos == 5
34113414
</geshi>
34123415
34133416
The <code>ctx</code> table argument combined with the <code>a</code> regex modifier can be used to construct a lexer atop <code>ngx.re.match</code>.

src/ngx_http_lua_module.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -738,9 +738,9 @@ ngx_http_lua_cleanup_vm(void *data)
738738
lua_State *L = data;
739739

740740
if (L != NULL) {
741+
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0, "lua close the "
742+
"global Lua VM");
741743
lua_close(L);
742-
743-
dd("Lua VM closed!");
744744
}
745745
}
746746

@@ -753,8 +753,11 @@ ngx_http_lua_init_vm(ngx_conf_t *cf, ngx_http_lua_main_conf_t *lmcf)
753753
lua_State *L;
754754
ngx_uint_t i;
755755

756+
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "lua initialize the "
757+
"global Lua VM");
758+
756759
ngx_http_lua_content_length_hash =
757-
ngx_http_lua_hash_literal("content-length");
760+
ngx_http_lua_hash_literal("content-length");
758761

759762
ngx_http_lua_location_hash = ngx_http_lua_hash_literal("location");
760763

src/ngx_http_lua_regex.c

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ static int ngx_http_lua_ngx_re_gmatch_iterator(lua_State *L);
8888
static ngx_uint_t ngx_http_lua_ngx_re_parse_opts(lua_State *L,
8989
ngx_http_lua_regex_compile_t *re, ngx_str_t *opts, int narg);
9090
static int ngx_http_lua_ngx_re_sub_helper(lua_State *L, unsigned global);
91+
static int ngx_http_lua_ngx_re_match_helper(lua_State *L, int wantcaps);
92+
static int ngx_http_lua_ngx_re_find(lua_State *L);
9193
static int ngx_http_lua_ngx_re_match(lua_State *L);
9294
static int ngx_http_lua_ngx_re_gmatch(lua_State *L);
9395
static int ngx_http_lua_ngx_re_sub(lua_State *L);
@@ -115,6 +117,20 @@ static void ngx_http_lua_re_collect_named_captures(lua_State *L,
115117

116118
static int
117119
ngx_http_lua_ngx_re_match(lua_State *L)
120+
{
121+
return ngx_http_lua_ngx_re_match_helper(L, 1 /* want captures */);
122+
}
123+
124+
125+
static int
126+
ngx_http_lua_ngx_re_find(lua_State *L)
127+
{
128+
return ngx_http_lua_ngx_re_match_helper(L, 0 /* want captures */);
129+
}
130+
131+
132+
static int
133+
ngx_http_lua_ngx_re_match_helper(lua_State *L, int wantcaps)
118134
{
119135
/* u_char *p; */
120136
ngx_http_request_t *r;
@@ -166,8 +182,11 @@ ngx_http_lua_ngx_re_match(lua_State *L)
166182
lua_getfield(L, 4, "pos");
167183
if (lua_isnumber(L, -1)) {
168184
pos = (ngx_int_t) lua_tointeger(L, -1);
169-
if (pos < 0) {
185+
if (pos <= 0) {
170186
pos = 0;
187+
188+
} else {
189+
pos--; /* 1-based on the Lua land */
171190
}
172191

173192
} else if (lua_isnil(L, -1)) {
@@ -293,8 +312,11 @@ ngx_http_lua_ngx_re_match(lua_State *L)
293312
dd("compile failed");
294313

295314
lua_pushnil(L);
315+
if (!wantcaps) {
316+
lua_pushnil(L);
317+
}
296318
lua_pushlstring(L, (char *) re_comp.err.data, re_comp.err.len);
297-
return 2;
319+
return wantcaps ? 2 : 3;
298320
}
299321

300322
#if (LUA_HAVE_PCRE_JIT)
@@ -480,7 +502,10 @@ ngx_http_lua_ngx_re_match(lua_State *L)
480502
}
481503

482504
lua_pushnil(L);
483-
return 1;
505+
if (!wantcaps) {
506+
lua_pushnil(L);
507+
}
508+
return wantcaps ? 1 : 2;
484509
}
485510

486511
if (rc < 0) {
@@ -500,6 +525,18 @@ ngx_http_lua_ngx_re_match(lua_State *L)
500525

501526
dd("rc = %d", (int) rc);
502527

528+
if (nargs == 4) { /* having ctx table */
529+
pos = cap[1];
530+
lua_pushinteger(L, (lua_Integer) (pos + 1));
531+
lua_setfield(L, 4, "pos");
532+
}
533+
534+
if (!wantcaps) {
535+
lua_pushinteger(L, cap[0] + 1);
536+
lua_pushinteger(L, cap[1]);
537+
return 2;
538+
}
539+
503540
lua_createtable(L, rc /* narr */, 0 /* nrec */);
504541

505542
for (i = 0, n = 0; i < rc; i++, n += 2) {
@@ -522,12 +559,6 @@ ngx_http_lua_ngx_re_match(lua_State *L)
522559
name_entry_size, flags, &subj);
523560
}
524561

525-
if (nargs == 4) { /* having ctx table */
526-
pos = cap[1];
527-
lua_pushinteger(L, (lua_Integer) pos);
528-
lua_setfield(L, 4, "pos");
529-
}
530-
531562
if (!(flags & NGX_LUA_RE_COMPILE_ONCE)) {
532563

533564
if (sd) {
@@ -556,8 +587,11 @@ ngx_http_lua_ngx_re_match(lua_State *L)
556587
}
557588

558589
lua_pushnil(L);
590+
if (!wantcaps) {
591+
lua_pushnil(L);
592+
}
559593
lua_pushstring(L, msg);
560-
return 2;
594+
return wantcaps ? 2 : 3;
561595
}
562596

563597

@@ -1815,7 +1849,10 @@ ngx_http_lua_inject_regex_api(lua_State *L)
18151849
{
18161850
/* ngx.re */
18171851

1818-
lua_createtable(L, 0, 4 /* nrec */); /* .re */
1852+
lua_createtable(L, 0, 5 /* nrec */); /* .re */
1853+
1854+
lua_pushcfunction(L, ngx_http_lua_ngx_re_find);
1855+
lua_setfield(L, -2, "find");
18191856

18201857
lua_pushcfunction(L, ngx_http_lua_ngx_re_match);
18211858
lua_setfield(L, -2, "match");

src/ngx_http_lua_util.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ extern char ngx_http_lua_req_get_headers_metatable_key;
4848
#endif
4949

5050

51+
#if defined(nginx_version) && nginx_version < 1000000
52+
#define ngx_memmove(dst, src, n) (void) memmove(dst, src, n)
53+
#endif
54+
55+
5156
#define ngx_http_lua_context_name(c) \
5257
((c) == NGX_HTTP_LUA_CONTEXT_SET ? "set_by_lua*" \
5358
: (c) == NGX_HTTP_LUA_CONTEXT_REWRITE ? "rewrite_by_lua*" \

t/034-match.t

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ not matched: nil
202202

203203

204204

205-
=== TEST 10: case sensitive by default
205+
=== TEST 10: case insensitive
206206
--- config
207207
location /re {
208208
content_by_lua '
@@ -495,15 +495,15 @@ not matched!
495495
GET /re
496496
--- response_body
497497
1234
498-
4
498+
5
499499

500500

501501

502502
=== TEST 24: match with ctx and a pos
503503
--- config
504504
location /re {
505505
content_by_lua '
506-
local ctx = { pos = 2 }
506+
local ctx = { pos = 3 }
507507
m = ngx.re.match("1234, hello", "([0-9]+)", "", ctx)
508508
if m then
509509
ngx.say(m[0])
@@ -518,7 +518,7 @@ not matched!
518518
GET /re
519519
--- response_body
520520
34
521-
4
521+
5
522522

523523

524524

@@ -732,7 +732,7 @@ done
732732
--- request
733733
GET /re
734734
--- response_body
735-
pos: 0
735+
pos: 1
736736
m:
737737
--- no_error_log
738738
[error]

t/038-match-o.t

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -471,15 +471,15 @@ not matched!
471471
GET /re
472472
--- response_body
473473
1234
474-
4
474+
5
475475

476476

477477

478478
=== TEST 23: match with ctx and a pos
479479
--- config
480480
location /re {
481481
content_by_lua '
482-
local ctx = { pos = 2 }
482+
local ctx = { pos = 3 }
483483
m = ngx.re.match("1234, hello", "([0-9]+)", "o", ctx)
484484
if m then
485485
ngx.say(m[0])
@@ -494,7 +494,7 @@ not matched!
494494
GET /re
495495
--- response_body
496496
34
497-
4
497+
5
498498

499499

500500

@@ -574,7 +574,7 @@ nil
574574
ngx.say(m and m[0])
575575
ngx.say(ctx.pos)
576576
577-
ctx.pos = 0
577+
ctx.pos = 1
578578
m = ngx.re.match("hi, 1234", "([A-Z]+)", "o", ctx)
579579
ngx.say(m and m[0])
580580
ngx.say(ctx.pos)
@@ -584,11 +584,11 @@ nil
584584
GET /re
585585
--- response_body
586586
hello
587-
5
587+
6
588588
okay
589-
10
589+
11
590590
nil
591-
0
591+
1
592592

593593

594594

t/062-count.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ n = 2
359359
--- request
360360
GET /test
361361
--- response_body
362-
n = 4
362+
n = 5
363363
--- no_error_log
364364
[error]
365365

0 commit comments

Comments
 (0)