forked from openresty/lua-nginx-module
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathngx_http_lua_regex.c
More file actions
598 lines (436 loc) · 14.3 KB
/
Copy pathngx_http_lua_regex.c
File metadata and controls
598 lines (436 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/*
* Copyright (C) Yichun Zhang (agentzh)
*/
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#if (NGX_PCRE)
#include "ngx_http_lua_pcrefix.h"
#include "ngx_http_lua_script.h"
#include "ngx_http_lua_util.h"
#if (PCRE_MAJOR >= 6)
# define LUA_HAVE_PCRE_DFA 1
#else
# define LUA_HAVE_PCRE_DFA 0
#endif
#define NGX_LUA_RE_MODE_DFA (1<<1)
#define NGX_LUA_RE_MODE_JIT (1<<2)
#define NGX_LUA_RE_NO_UTF8_CHECK (1<<4)
#define NGX_LUA_RE_DFA_MODE_WORKSPACE_COUNT (100)
#define NGX_LUA_RE_MIN_JIT_STACK_SIZE 32 * 1024
typedef struct {
ngx_pool_t *pool;
u_char *name_table;
int name_count;
int name_entry_size;
int ncaptures;
int *captures;
pcre *regex;
pcre_extra *regex_sd;
ngx_http_lua_complex_value_t *replace;
/* only for (stap) debugging, and may be an invalid pointer */
const u_char *pattern;
} ngx_http_lua_regex_t;
typedef struct {
ngx_str_t pattern;
ngx_pool_t *pool;
ngx_int_t options;
pcre *regex;
int captures;
ngx_str_t err;
} ngx_http_lua_regex_compile_t;
typedef struct {
ngx_http_request_t *request;
pcre *regex;
pcre_extra *regex_sd;
int ncaptures;
int *captures;
int captures_len;
uint8_t flags;
} ngx_http_lua_regex_ctx_t;
static void ngx_http_lua_regex_free_study_data(ngx_pool_t *pool,
pcre_extra *sd);
static ngx_int_t ngx_http_lua_regex_compile(ngx_http_lua_regex_compile_t *rc);
#define ngx_http_lua_regex_exec(re, e, s, start, captures, size, opts) \
pcre_exec(re, e, (const char *) (s)->data, (s)->len, start, opts, \
captures, size)
#define ngx_http_lua_regex_dfa_exec(re, e, s, start, captures, size, ws, \
wscount, opts) \
pcre_dfa_exec(re, e, (const char *) (s)->data, (s)->len, start, opts, \
captures, size, ws, wscount)
static void
ngx_http_lua_regex_free_study_data(ngx_pool_t *pool, pcre_extra *sd)
{
ngx_pool_t *old_pool;
old_pool = ngx_http_lua_pcre_malloc_init(pool);
#if LUA_HAVE_PCRE_JIT
pcre_free_study(sd);
#else
pcre_free(sd);
#endif
ngx_http_lua_pcre_malloc_done(old_pool);
}
static ngx_int_t
ngx_http_lua_regex_compile(ngx_http_lua_regex_compile_t *rc)
{
int n, erroff;
char *p;
const char *errstr;
pcre *re;
ngx_pool_t *old_pool;
old_pool = ngx_http_lua_pcre_malloc_init(rc->pool);
re = pcre_compile((const char *) rc->pattern.data, (int) rc->options,
&errstr, &erroff, NULL);
ngx_http_lua_pcre_malloc_done(old_pool);
if (re == NULL) {
if ((size_t) erroff == rc->pattern.len) {
rc->err.len = ngx_snprintf(rc->err.data, rc->err.len,
"pcre_compile() failed: %s in \"%V\"",
errstr, &rc->pattern)
- rc->err.data;
} else {
rc->err.len = ngx_snprintf(rc->err.data, rc->err.len,
"pcre_compile() failed: %s in \"%V\" "
"at \"%s\"", errstr, &rc->pattern,
rc->pattern.data + erroff)
- rc->err.data;
}
return NGX_ERROR;
}
rc->regex = re;
#if 1
n = pcre_fullinfo(re, NULL, PCRE_INFO_CAPTURECOUNT, &rc->captures);
if (n < 0) {
p = "pcre_fullinfo(\"%V\", PCRE_INFO_CAPTURECOUNT) failed: %d";
goto failed;
}
#endif
return NGX_OK;
failed:
rc->err.len = ngx_snprintf(rc->err.data, rc->err.len, p, &rc->pattern, n)
- rc->err.data;
return NGX_OK;
}
ngx_int_t
ngx_http_lua_ffi_set_jit_stack_size(int size, u_char *errstr,
size_t *errstr_size)
{
#if LUA_HAVE_PCRE_JIT
ngx_http_lua_main_conf_t *lmcf;
ngx_pool_t *pool, *old_pool;
lmcf = ngx_http_cycle_get_module_main_conf(ngx_cycle,
ngx_http_lua_module);
if (size < NGX_LUA_RE_MIN_JIT_STACK_SIZE) {
size = NGX_LUA_RE_MIN_JIT_STACK_SIZE;
}
pool = lmcf->pool;
dd("server pool %p", lmcf->pool);
if (lmcf->jit_stack) {
old_pool = ngx_http_lua_pcre_malloc_init(pool);
pcre_jit_stack_free(lmcf->jit_stack);
ngx_http_lua_pcre_malloc_done(old_pool);
}
old_pool = ngx_http_lua_pcre_malloc_init(pool);
lmcf->jit_stack = pcre_jit_stack_alloc(NGX_LUA_RE_MIN_JIT_STACK_SIZE,
size);
ngx_http_lua_pcre_malloc_done(old_pool);
if (lmcf->jit_stack == NULL) {
*errstr_size = ngx_snprintf(errstr, *errstr_size,
"pcre jit stack allocation failed")
- errstr;
return NGX_ERROR;
}
return NGX_OK;
#else /* LUA_HAVE_PCRE_JIT */
*errstr_size = ngx_snprintf(errstr, *errstr_size,
"no pcre jit support found")
- errstr;
return NGX_ERROR;
#endif /* LUA_HAVE_PCRE_JIT */
}
ngx_http_lua_regex_t *
ngx_http_lua_ffi_compile_regex(const unsigned char *pat, size_t pat_len,
int flags, int pcre_opts, u_char *errstr,
size_t errstr_size)
{
int *cap = NULL, ovecsize;
u_char *p;
ngx_int_t rc;
const char *msg;
ngx_pool_t *pool, *old_pool;
pcre_extra *sd = NULL;
ngx_http_lua_regex_t *re;
ngx_http_lua_main_conf_t *lmcf;
ngx_http_lua_regex_compile_t re_comp;
pool = ngx_create_pool(512, ngx_cycle->log);
if (pool == NULL) {
msg = "no memory";
goto error;
}
pool->log = (ngx_log_t *) &ngx_cycle->new_log;
re = ngx_palloc(pool, sizeof(ngx_http_lua_regex_t));
if (re == NULL) {
ngx_destroy_pool(pool);
pool = NULL;
msg = "no memory";
goto error;
}
re->pool = pool;
re_comp.options = pcre_opts;
re_comp.pattern.data = (u_char *) pat;
re_comp.pattern.len = pat_len;
re_comp.err.len = errstr_size - 1;
re_comp.err.data = errstr;
re_comp.pool = pool;
old_pool = ngx_http_lua_pcre_malloc_init(pool);
rc = ngx_http_lua_regex_compile(&re_comp);
ngx_http_lua_pcre_malloc_done(old_pool);
if (rc != NGX_OK) {
re_comp.err.data[re_comp.err.len] = '\0';
msg = (char *) re_comp.err.data;
goto error;
}
lmcf = ngx_http_cycle_get_module_main_conf(ngx_cycle,
ngx_http_lua_module);
#if (LUA_HAVE_PCRE_JIT)
if (flags & NGX_LUA_RE_MODE_JIT) {
old_pool = ngx_http_lua_pcre_malloc_init(pool);
sd = pcre_study(re_comp.regex, PCRE_STUDY_JIT_COMPILE, &msg);
ngx_http_lua_pcre_malloc_done(old_pool);
# if (NGX_DEBUG)
if (msg != NULL) {
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
"pcre study failed with PCRE_STUDY_JIT_COMPILE: "
"%s (%p)", msg, sd);
}
if (sd != NULL) {
int jitted;
old_pool = ngx_http_lua_pcre_malloc_init(pool);
pcre_fullinfo(re_comp.regex, sd, PCRE_INFO_JIT, &jitted);
ngx_http_lua_pcre_malloc_done(old_pool);
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
"pcre JIT compiling result: %d", jitted);
}
# endif /* !(NGX_DEBUG) */
} else {
old_pool = ngx_http_lua_pcre_malloc_init(pool);
sd = pcre_study(re_comp.regex, 0, &msg);
ngx_http_lua_pcre_malloc_done(old_pool);
}
if (sd && lmcf->jit_stack) {
pcre_assign_jit_stack(sd, NULL, lmcf->jit_stack);
}
#endif /* LUA_HAVE_PCRE_JIT */
if (sd
&& lmcf && lmcf->regex_match_limit > 0
&& !(flags & NGX_LUA_RE_MODE_DFA))
{
sd->flags |= PCRE_EXTRA_MATCH_LIMIT;
sd->match_limit = lmcf->regex_match_limit;
}
if (flags & NGX_LUA_RE_MODE_DFA) {
ovecsize = 2;
re_comp.captures = 0;
} else {
ovecsize = (re_comp.captures + 1) * 3;
}
dd("allocating cap with size: %d", (int) ovecsize);
cap = ngx_palloc(pool, ovecsize * sizeof(int));
if (cap == NULL) {
msg = "no memory";
goto error;
}
if (pcre_fullinfo(re_comp.regex, NULL, PCRE_INFO_NAMECOUNT,
&re->name_count) != 0)
{
msg = "cannot acquire named subpattern count";
goto error;
}
if (re->name_count > 0) {
if (pcre_fullinfo(re_comp.regex, NULL, PCRE_INFO_NAMEENTRYSIZE,
&re->name_entry_size) != 0)
{
msg = "cannot acquire named subpattern entry size";
goto error;
}
if (pcre_fullinfo(re_comp.regex, NULL, PCRE_INFO_NAMETABLE,
&re->name_table) != 0)
{
msg = "cannot acquire named subpattern table";
goto error;
}
}
re->regex = re_comp.regex;
re->regex_sd = sd;
re->ncaptures = re_comp.captures;
re->captures = cap;
re->replace = NULL;
/* only for (stap) debugging, the pointer might be invalid when the
* string is collected later on.... */
re->pattern = pat;
return re;
error:
p = ngx_snprintf(errstr, errstr_size - 1, "%s", msg);
*p = '\0';
if (sd) {
ngx_http_lua_regex_free_study_data(pool, sd);
}
if (pool) {
ngx_destroy_pool(pool);
}
return NULL;
}
int
ngx_http_lua_ffi_exec_regex(ngx_http_lua_regex_t *re, int flags,
const u_char *s, size_t len, int pos)
{
int rc, ovecsize, exec_opts, *cap;
ngx_str_t subj;
pcre_extra *sd;
cap = re->captures;
sd = re->regex_sd;
if (flags & NGX_LUA_RE_MODE_DFA) {
ovecsize = 2;
re->ncaptures = 0;
} else {
ovecsize = (re->ncaptures + 1) * 3;
}
if (flags & NGX_LUA_RE_NO_UTF8_CHECK) {
exec_opts = PCRE_NO_UTF8_CHECK;
} else {
exec_opts = 0;
}
subj.data = (u_char *) s;
subj.len = len;
if (flags & NGX_LUA_RE_MODE_DFA) {
#if LUA_HAVE_PCRE_DFA
int ws[NGX_LUA_RE_DFA_MODE_WORKSPACE_COUNT];
rc = ngx_http_lua_regex_dfa_exec(re->regex, sd, &subj,
(int) pos, cap, ovecsize, ws,
sizeof(ws)/sizeof(ws[0]), exec_opts);
#else
return PCRE_ERROR_BADOPTION;
#endif /* LUA_HAVE_PCRE_DFA */
} else {
rc = ngx_http_lua_regex_exec(re->regex, sd, &subj, (int) pos, cap,
ovecsize, exec_opts);
}
return rc;
}
void
ngx_http_lua_ffi_destroy_regex(ngx_http_lua_regex_t *re)
{
ngx_pool_t *old_pool;
dd("destroy regex called");
if (re == NULL || re->pool == NULL) {
return;
}
if (re->regex_sd) {
old_pool = ngx_http_lua_pcre_malloc_init(re->pool);
#if LUA_HAVE_PCRE_JIT
pcre_free_study(re->regex_sd);
#else
pcre_free(re->regex_sd);
#endif
ngx_http_lua_pcre_malloc_done(old_pool);
re->regex_sd = NULL;
}
ngx_destroy_pool(re->pool);
}
int
ngx_http_lua_ffi_compile_replace_template(ngx_http_lua_regex_t *re,
const u_char *replace_data, size_t replace_len)
{
ngx_int_t rc;
ngx_str_t tpl;
ngx_http_lua_complex_value_t *ctpl;
ngx_http_lua_compile_complex_value_t ccv;
ctpl = ngx_palloc(re->pool, sizeof(ngx_http_lua_complex_value_t));
if (ctpl == NULL) {
return NGX_ERROR;
}
if (replace_len != 0) {
/* copy the string buffer pointed to by tpl.data from Lua VM */
tpl.data = ngx_palloc(re->pool, replace_len + 1);
if (tpl.data == NULL) {
return NGX_ERROR;
}
ngx_memcpy(tpl.data, replace_data, replace_len);
tpl.data[replace_len] = '\0';
} else {
tpl.data = (u_char *) replace_data;
}
tpl.len = replace_len;
ngx_memzero(&ccv, sizeof(ngx_http_lua_compile_complex_value_t));
ccv.pool = re->pool;
ccv.log = ngx_cycle->log;
ccv.value = &tpl;
ccv.complex_value = ctpl;
rc = ngx_http_lua_compile_complex_value(&ccv);
re->replace = ctpl;
return rc;
}
ngx_http_lua_script_engine_t *
ngx_http_lua_ffi_create_script_engine(void)
{
return ngx_calloc(sizeof(ngx_http_lua_script_engine_t), ngx_cycle->log);
}
void
ngx_http_lua_ffi_init_script_engine(ngx_http_lua_script_engine_t *e,
const unsigned char *subj, ngx_http_lua_regex_t *compiled, int count)
{
e->log = ngx_cycle->log;
e->ncaptures = count * 2;
e->captures = compiled->captures;
e->captures_data = (u_char *) subj;
}
void
ngx_http_lua_ffi_destroy_script_engine(ngx_http_lua_script_engine_t *e)
{
ngx_free(e);
}
size_t
ngx_http_lua_ffi_script_eval_len(ngx_http_lua_script_engine_t *e,
ngx_http_lua_complex_value_t *val)
{
size_t len;
ngx_http_lua_script_len_code_pt lcode;
e->ip = val->lengths;
len = 0;
while (*(uintptr_t *) e->ip) {
lcode = *(ngx_http_lua_script_len_code_pt *) e->ip;
len += lcode(e);
}
return len;
}
void
ngx_http_lua_ffi_script_eval_data(ngx_http_lua_script_engine_t *e,
ngx_http_lua_complex_value_t *val, u_char *dst)
{
ngx_http_lua_script_code_pt code;
e->ip = val->values;
e->pos = dst;
while (*(uintptr_t *) e->ip) {
code = *(ngx_http_lua_script_code_pt *) e->ip;
code(e);
}
}
uint32_t
ngx_http_lua_ffi_max_regex_cache_size(void)
{
ngx_http_lua_main_conf_t *lmcf;
lmcf = ngx_http_cycle_get_module_main_conf(ngx_cycle,
ngx_http_lua_module);
if (lmcf == NULL) {
return 0;
}
return (uint32_t) lmcf->regex_cache_max_entries;
}
const char *
ngx_http_lua_ffi_pcre_version(void)
{
return pcre_version();
}
#endif /* NGX_PCRE */
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */