Skip to content

Commit 08841a2

Browse files
swanananzhuizhuhaomeng
authored andcommitted
feature: support pcre2
1 parent 7a3acd6 commit 08841a2

21 files changed

+856
-181
lines changed

.travis.yml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ env:
2929
- LUAJIT_INC=$LUAJIT_PREFIX/include/luajit-2.1
3030
- LUA_INCLUDE_DIR=$LUAJIT_INC
3131
- PCRE_VER=8.45
32+
- PCRE2_VER=10.37
3233
- PCRE_PREFIX=/opt/pcre
34+
- PCRE2_PREFIX=/opt/pcre2
3335
- PCRE_LIB=$PCRE_PREFIX/lib
36+
- PCRE2_LIB=$PCRE2_PREFIX/lib
3437
- PCRE_INC=$PCRE_PREFIX/include
38+
- PCRE2_INC=$PCRE2_PREFIX/include
3539
- OPENSSL_PREFIX=/opt/ssl
3640
- OPENSSL_LIB=$OPENSSL_PREFIX/lib
3741
- OPENSSL_INC=$OPENSSL_PREFIX/include
@@ -42,7 +46,7 @@ env:
4246
- NGINX_VERSION=1.21.4 OPENSSL_VER=1.1.0l
4347
- NGINX_VERSION=1.21.4 OPENSSL_VER=1.1.1s
4448
- NGINX_VERSION=1.25.1 OPENSSL_VER=1.1.0l
45-
- NGINX_VERSION=1.25.1 OPENSSL_VER=1.1.1s
49+
- NGINX_VERSION=1.25.1 OPENSSL_VER=1.1.1s USE_PCRE2=Y
4650

4751
services:
4852
- memcache
@@ -51,7 +55,8 @@ services:
5155
install:
5256
- sudo apt update
5357
- sudo apt install --only-upgrade ca-certificates
54-
- if [ ! -f download-cache/pcre-$PCRE_VER.tar.gz ]; then wget -P download-cache/ https://downloads.sourceforge.net/project/pcre/pcre/${PCRE_VER}/pcre-${PCRE_VER}.tar.gz; fi
58+
- if [ "$USE_PCRE2" != "Y" ] && [ ! -f download-cache/pcre-$PCRE_VER.tar.gz ]; then wget -P download-cache/ https://downloads.sourceforge.net/project/pcre/pcre/${PCRE_VER}/pcre-${PCRE_VER}.tar.gz; fi
59+
- if [ "$USE_PCRE2" = "Y" ] && [ ! -f download-cache/pcre2-$PCRE2_VER.tar.gz ]; then wget -P download-cache https://downloads.sourceforge.net/project/pcre/pcre2/${PCRE2_VER}/pcre2-${PCRE2_VER}.tar.gz; fi
5560
- if [ ! -f download-cache/openssl-$OPENSSL_VER.tar.gz ]; then wget -P download-cache https://www.openssl.org/source/openssl-$OPENSSL_VER.tar.gz || wget -P download-cache https://www.openssl.org/source/old/${OPENSSL_VER//[a-z]/}/openssl-$OPENSSL_VER.tar.gz; fi
5661
- git clone https://github.com/openresty/openresty-devel-utils.git
5762
- git clone https://github.com/openresty/lua-cjson.git
@@ -75,12 +80,8 @@ script:
7580
- sudo ip addr add 10.254.254.1/24 dev lo
7681
- sudo ip addr add 10.254.254.2/24 dev lo
7782
- sudo ip route add prohibit 0.0.0.1/32
78-
- tar zxf download-cache/pcre-$PCRE_VER.tar.gz
79-
- cd pcre-$PCRE_VER/
80-
- ./configure --prefix=$PCRE_PREFIX --enable-jit --enable-utf --enable-unicode-properties > build.log 2>&1 || (cat build.log && exit 1)
81-
- make -j$JOBS > build.log 2>&1 || (cat build.log && exit 1)
82-
- sudo PATH=$PATH make install > build.log 2>&1 || (cat build.log && exit 1)
83-
- cd ..
83+
- if [ "$USE_PCRE2" != "Y" ]; then tar zxf download-cache/pcre-$PCRE_VER.tar.gz; cd pcre-$PCRE_VER/; ./configure --prefix=$PCRE_PREFIX --enable-jit --enable-utf --enable-unicode-properties > build.log 2>&1 || (cat build.log && exit 1); make -j$JOBS > build.log 2>&1 || (cat build.log && exit 1); sudo PATH=$PATH make install > build.log 2>&1 || (cat build.log && exit 1); cd ..; fi
84+
- if [ "$USE_PCRE2" = "Y" ]; then tar zxf download-cache/pcre2-$PCRE2_VER.tar.gz; cd pcre2-$PCRE2_VER/; ./configure --prefix=$PCRE2_PREFIX --enable-jit --enable-utf > build.log 2>&1 || (cat build.log && exit 1); make -j$JOBS > build.log 2>&1 || (cat build.log && exit 1); sudo PATH=$PATH make install > build.log 2>&1 || (cat build.log && exit 1); cd ..; fi
8485
- cd luajit2
8586
- make -j$JOBS CCDEBUG=-g Q= PREFIX=$LUAJIT_PREFIX CC=$CC XCFLAGS='-DLUA_USE_APICHECK -DLUA_USE_ASSERT' > build.log 2>&1 || (cat build.log && exit 1)
8687
- sudo make install PREFIX=$LUAJIT_PREFIX > build.log 2>&1 || (cat build.log && exit 1)
@@ -103,4 +104,4 @@ script:
103104
- export TEST_NGINX_RESOLVER=8.8.4.4
104105
- dig +short @$TEST_NGINX_RESOLVER openresty.org || exit 0
105106
- dig +short @$TEST_NGINX_RESOLVER agentzh.org || exit 0
106-
- prove -I. -r t
107+
- prove -I. -Itest-nginx/lib -r t

config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ fi
405405

406406
# ----------------------------------------
407407

408-
if [ $USE_PCRE = YES -o $PCRE != NONE ] && [ $PCRE != NO -a $PCRE != YES ]; then
408+
if [ $USE_PCRE = YES -o $PCRE != NONE ] && [ $PCRE != NO -a $PCRE != YES ] && [ $PCRE2 != YES ]; then
409409
# force pcre_version symbol to be required when PCRE is statically linked
410410
case "$NGX_PLATFORM" in
411411
Darwin:*)

src/ngx_stream_lua_common.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747

4848
#endif
4949

50+
#if (NGX_PCRE2)
51+
# define LUA_HAVE_PCRE_JIT 1
52+
#endif
53+
5054

5155
#if !defined(nginx_version) || nginx_version < 1013006
5256
#error at least nginx 1.13.6 is required but found an older version
@@ -189,15 +193,16 @@ struct ngx_stream_lua_main_conf_s {
189193

190194
ngx_connection_t *watcher; /* for watching the process exit event */
191195

192-
#if (NGX_PCRE)
196+
#if (NGX_PCRE || NGX_PCRE2)
193197
ngx_int_t regex_cache_entries;
194198
ngx_int_t regex_cache_max_entries;
195199
ngx_int_t regex_match_limit;
196200

197-
#if (LUA_HAVE_PCRE_JIT)
201+
#if (NGX_PCRE2)
202+
pcre2_jit_stack *jit_stack;
203+
#elif (LUA_HAVE_PCRE_JIT)
198204
pcre_jit_stack *jit_stack;
199-
#endif
200-
205+
# endif
201206
#endif
202207

203208
ngx_array_t *shm_zones; /* of ngx_shm_zone_t* */

src/ngx_stream_lua_module.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ static char *ngx_stream_lua_ssl_conf_command_check(ngx_conf_t *cf, void *post,
5656
#endif
5757
static char *ngx_stream_lua_malloc_trim(ngx_conf_t *cf, ngx_command_t *cmd,
5858
void *conf);
59+
#if (NGX_PCRE2)
60+
extern void ngx_stream_lua_regex_cleanup(void *data);
61+
#endif
5962

6063

6164
static ngx_conf_post_t ngx_stream_lua_lowat_post =
@@ -577,7 +580,16 @@ ngx_stream_lua_init(ngx_conf_t *cf)
577580
cln->data = lmcf;
578581
cln->handler = ngx_stream_lua_sema_mm_cleanup;
579582

583+
#if (NGX_PCRE2)
584+
/* add the cleanup of pcre2 regex */
585+
cln = ngx_pool_cleanup_add(cf->pool, 0);
586+
if (cln == NULL) {
587+
return NGX_ERROR;
588+
}
580589

590+
cln->data = lmcf;
591+
cln->handler = ngx_stream_lua_regex_cleanup;
592+
#endif
581593

582594
if (lmcf->lua == NULL) {
583595
dd("initializing lua vm");

src/ngx_stream_lua_pcrefix.c

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,63 @@
2222
#include "ngx_stream_lua_pcrefix.h"
2323
#include "stdio.h"
2424

25-
#if (NGX_PCRE)
25+
#if (NGX_PCRE || NGX_PCRE2)
2626

2727
static ngx_pool_t *ngx_stream_lua_pcre_pool = NULL;
2828

29+
#if (NGX_PCRE2)
30+
static ngx_uint_t ngx_regex_direct_alloc;
31+
#else
2932
static void *(*old_pcre_malloc)(size_t);
3033
static void (*old_pcre_free)(void *ptr);
34+
#endif
3135

3236

3337
/* XXX: work-around to nginx regex subsystem, must init a memory pool
3438
* to use PCRE functions. As PCRE still has memory-leaking problems,
3539
* and nginx overwrote pcre_malloc/free hooks with its own static
3640
* functions, so nobody else can reuse nginx regex subsystem... */
41+
#if (NGX_PCRE2)
42+
43+
void *
44+
ngx_stream_lua_pcre_malloc(size_t size, void *data)
45+
{
46+
dd("lua pcre pool is %p", ngx_stream_lua_pcre_pool);
47+
48+
if (ngx_stream_lua_pcre_pool) {
49+
return ngx_palloc(ngx_stream_lua_pcre_pool, size);
50+
}
51+
52+
if (ngx_regex_direct_alloc) {
53+
return ngx_alloc(size, ngx_cycle->log);
54+
}
55+
56+
fprintf(stderr, "error: lua pcre malloc failed due to empty pcre pool");
57+
58+
return NULL;
59+
}
60+
61+
62+
void
63+
ngx_stream_lua_pcre_free(void *ptr, void *data)
64+
{
65+
dd("lua pcre pool is %p", ngx_stream_lua_pcre_pool);
66+
67+
if (ngx_stream_lua_pcre_pool) {
68+
ngx_pfree(ngx_stream_lua_pcre_pool, ptr);
69+
return;
70+
}
71+
72+
if (ngx_regex_direct_alloc) {
73+
ngx_free(ptr);
74+
return;
75+
}
76+
77+
fprintf(stderr, "error: lua pcre free failed due to empty pcre pool");
78+
}
79+
80+
#else
81+
3782
static void *
3883
ngx_stream_lua_pcre_malloc(size_t size)
3984
{
@@ -62,6 +107,41 @@ ngx_stream_lua_pcre_free(void *ptr)
62107
fprintf(stderr, "error: lua pcre free failed due to empty pcre pool");
63108
}
64109

110+
#endif
111+
112+
113+
#if (NGX_PCRE2)
114+
115+
ngx_pool_t *
116+
ngx_stream_lua_pcre_malloc_init(ngx_pool_t *pool)
117+
{
118+
ngx_pool_t *old_pool;
119+
120+
dd("lua pcre pool was %p", ngx_stream_lua_pcre_pool);
121+
122+
ngx_regex_direct_alloc = (pool == NULL) ? 1 : 0;
123+
124+
old_pool = ngx_stream_lua_pcre_pool;
125+
ngx_stream_lua_pcre_pool = pool;
126+
127+
dd("lua pcre pool is %p", ngx_stream_lua_pcre_pool);
128+
129+
return old_pool;
130+
}
131+
132+
133+
void
134+
ngx_stream_lua_pcre_malloc_done(ngx_pool_t *old_pool)
135+
{
136+
dd("lua pcre pool was %p", ngx_stream_lua_pcre_pool);
137+
138+
ngx_stream_lua_pcre_pool = old_pool;
139+
ngx_regex_direct_alloc = 0;
140+
141+
dd("lua pcre pool is %p", ngx_stream_lua_pcre_pool);
142+
}
143+
144+
#else
65145

66146
ngx_pool_t *
67147
ngx_stream_lua_pcre_malloc_init(ngx_pool_t *pool)
@@ -109,6 +189,7 @@ ngx_stream_lua_pcre_malloc_done(ngx_pool_t *old_pool)
109189
}
110190
}
111191

112-
#endif /* NGX_PCRE */
192+
#endif
193+
#endif /* NGX_PCRE || NGX_PCRE2 */
113194

114195
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */

src/ngx_stream_lua_pcrefix.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@
2020
#include "ngx_stream_lua_common.h"
2121

2222

23-
#if (NGX_PCRE)
23+
#if (NGX_PCRE || NGX_PCRE2)
2424
ngx_pool_t *ngx_stream_lua_pcre_malloc_init(ngx_pool_t *pool);
2525
void ngx_stream_lua_pcre_malloc_done(ngx_pool_t *old_pool);
26+
27+
#if NGX_PCRE2
28+
void *ngx_stream_lua_pcre_malloc(size_t size, void *data);
29+
void ngx_stream_lua_pcre_free(void *ptr, void *data);
30+
#endif
2631
#endif
2732

2833

0 commit comments

Comments
 (0)