0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9
|
|
10
|
656
|
11 typedef struct {
|
|
12 ngx_flag_t pcre_jit;
|
|
13 } ngx_regex_conf_t;
|
|
14
|
|
15
|
52
|
16 static void * ngx_libc_cdecl ngx_regex_malloc(size_t size);
|
|
17 static void ngx_libc_cdecl ngx_regex_free(void *p);
|
0
|
18
|
656
|
19 static ngx_int_t ngx_regex_module_init(ngx_cycle_t *cycle);
|
|
20
|
|
21 static void *ngx_regex_create_conf(ngx_cycle_t *cycle);
|
|
22 static char *ngx_regex_init_conf(ngx_cycle_t *cycle, void *conf);
|
|
23
|
|
24 static char *ngx_regex_pcre_jit(ngx_conf_t *cf, void *post, void *data);
|
|
25 static ngx_conf_post_t ngx_regex_pcre_jit_post = { ngx_regex_pcre_jit };
|
|
26
|
|
27
|
|
28 static ngx_command_t ngx_regex_commands[] = {
|
|
29
|
|
30 { ngx_string("pcre_jit"),
|
|
31 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
|
|
32 ngx_conf_set_flag_slot,
|
|
33 0,
|
|
34 offsetof(ngx_regex_conf_t, pcre_jit),
|
|
35 &ngx_regex_pcre_jit_post },
|
|
36
|
|
37 ngx_null_command
|
|
38 };
|
|
39
|
|
40
|
|
41 static ngx_core_module_t ngx_regex_module_ctx = {
|
|
42 ngx_string("regex"),
|
|
43 ngx_regex_create_conf,
|
|
44 ngx_regex_init_conf
|
|
45 };
|
|
46
|
|
47
|
|
48 ngx_module_t ngx_regex_module = {
|
|
49 NGX_MODULE_V1,
|
|
50 &ngx_regex_module_ctx, /* module context */
|
|
51 ngx_regex_commands, /* module directives */
|
|
52 NGX_CORE_MODULE, /* module type */
|
|
53 NULL, /* init master */
|
|
54 ngx_regex_module_init, /* init module */
|
|
55 NULL, /* init process */
|
|
56 NULL, /* init thread */
|
|
57 NULL, /* exit thread */
|
|
58 NULL, /* exit process */
|
|
59 NULL, /* exit master */
|
|
60 NGX_MODULE_V1_PADDING
|
|
61 };
|
|
62
|
0
|
63
|
|
64 static ngx_pool_t *ngx_pcre_pool;
|
656
|
65 static ngx_list_t *ngx_pcre_studies;
|
0
|
66
|
|
67
|
52
|
68 void
|
|
69 ngx_regex_init(void)
|
0
|
70 {
|
|
71 pcre_malloc = ngx_regex_malloc;
|
|
72 pcre_free = ngx_regex_free;
|
|
73 }
|
|
74
|
|
75
|
546
|
76 static ngx_inline void
|
|
77 ngx_regex_malloc_init(ngx_pool_t *pool)
|
0
|
78 {
|
|
79 #if (NGX_THREADS)
|
|
80 ngx_core_tls_t *tls;
|
|
81
|
|
82 if (ngx_threaded) {
|
|
83 tls = ngx_thread_get_tls(ngx_core_tls_key);
|
|
84 tls->pool = pool;
|
546
|
85 return;
|
0
|
86 }
|
|
87
|
550
|
88 #endif
|
0
|
89
|
|
90 ngx_pcre_pool = pool;
|
546
|
91 }
|
0
|
92
|
546
|
93
|
|
94 static ngx_inline void
|
|
95 ngx_regex_malloc_done(void)
|
|
96 {
|
|
97 #if (NGX_THREADS)
|
|
98 ngx_core_tls_t *tls;
|
|
99
|
|
100 if (ngx_threaded) {
|
|
101 tls = ngx_thread_get_tls(ngx_core_tls_key);
|
|
102 tls->pool = NULL;
|
|
103 return;
|
|
104 }
|
|
105
|
550
|
106 #endif
|
546
|
107
|
|
108 ngx_pcre_pool = NULL;
|
|
109 }
|
|
110
|
|
111
|
|
112 ngx_int_t
|
|
113 ngx_regex_compile(ngx_regex_compile_t *rc)
|
|
114 {
|
656
|
115 int n, erroff;
|
|
116 char *p;
|
|
117 pcre *re;
|
|
118 const char *errstr;
|
|
119 ngx_regex_elt_t *elt;
|
546
|
120
|
|
121 ngx_regex_malloc_init(rc->pool);
|
|
122
|
|
123 re = pcre_compile((const char *) rc->pattern.data, (int) rc->options,
|
0
|
124 &errstr, &erroff, NULL);
|
|
125
|
546
|
126 /* ensure that there is no current pool */
|
|
127 ngx_regex_malloc_done();
|
|
128
|
0
|
129 if (re == NULL) {
|
546
|
130 if ((size_t) erroff == rc->pattern.len) {
|
|
131 rc->err.len = ngx_snprintf(rc->err.data, rc->err.len,
|
|
132 "pcre_compile() failed: %s in \"%V\"",
|
|
133 errstr, &rc->pattern)
|
|
134 - rc->err.data;
|
|
135
|
0
|
136 } else {
|
546
|
137 rc->err.len = ngx_snprintf(rc->err.data, rc->err.len,
|
|
138 "pcre_compile() failed: %s in \"%V\" at \"%s\"",
|
|
139 errstr, &rc->pattern, rc->pattern.data + erroff)
|
|
140 - rc->err.data;
|
0
|
141 }
|
546
|
142
|
|
143 return NGX_ERROR;
|
|
144 }
|
|
145
|
656
|
146 rc->regex = ngx_pcalloc(rc->pool, sizeof(ngx_regex_t));
|
|
147 if (rc->regex == NULL) {
|
|
148 return NGX_ERROR;
|
|
149 }
|
|
150
|
|
151 rc->regex->pcre = re;
|
|
152
|
|
153 /* do not study at runtime */
|
|
154
|
|
155 if (ngx_pcre_studies != NULL) {
|
|
156 elt = ngx_list_push(ngx_pcre_studies);
|
|
157 if (elt == NULL) {
|
|
158 return NGX_ERROR;
|
|
159 }
|
|
160
|
|
161 elt->regex = rc->regex;
|
|
162 elt->name = rc->pattern.data;
|
|
163 }
|
546
|
164
|
|
165 n = pcre_fullinfo(re, NULL, PCRE_INFO_CAPTURECOUNT, &rc->captures);
|
|
166 if (n < 0) {
|
|
167 p = "pcre_fullinfo(\"%V\", PCRE_INFO_CAPTURECOUNT) failed: %d";
|
|
168 goto failed;
|
|
169 }
|
|
170
|
|
171 if (rc->captures == 0) {
|
|
172 return NGX_OK;
|
0
|
173 }
|
|
174
|
546
|
175 n = pcre_fullinfo(re, NULL, PCRE_INFO_NAMECOUNT, &rc->named_captures);
|
|
176 if (n < 0) {
|
|
177 p = "pcre_fullinfo(\"%V\", PCRE_INFO_NAMECOUNT) failed: %d";
|
|
178 goto failed;
|
|
179 }
|
|
180
|
|
181 if (rc->named_captures == 0) {
|
|
182 return NGX_OK;
|
|
183 }
|
0
|
184
|
546
|
185 n = pcre_fullinfo(re, NULL, PCRE_INFO_NAMEENTRYSIZE, &rc->name_size);
|
|
186 if (n < 0) {
|
|
187 p = "pcre_fullinfo(\"%V\", PCRE_INFO_NAMEENTRYSIZE) failed: %d";
|
|
188 goto failed;
|
0
|
189 }
|
546
|
190
|
|
191 n = pcre_fullinfo(re, NULL, PCRE_INFO_NAMETABLE, &rc->names);
|
|
192 if (n < 0) {
|
|
193 p = "pcre_fullinfo(\"%V\", PCRE_INFO_NAMETABLE) failed: %d";
|
|
194 goto failed;
|
|
195 }
|
0
|
196
|
546
|
197 return NGX_OK;
|
|
198
|
|
199 failed:
|
|
200
|
|
201 rc->err.len = ngx_snprintf(rc->err.data, rc->err.len, p, &rc->pattern, n)
|
|
202 - rc->err.data;
|
|
203 return NGX_OK;
|
0
|
204 }
|
|
205
|
|
206
|
52
|
207 ngx_int_t
|
358
|
208 ngx_regex_exec_array(ngx_array_t *a, ngx_str_t *s, ngx_log_t *log)
|
|
209 {
|
|
210 ngx_int_t n;
|
|
211 ngx_uint_t i;
|
|
212 ngx_regex_elt_t *re;
|
|
213
|
|
214 re = a->elts;
|
|
215
|
|
216 for (i = 0; i < a->nelts; i++) {
|
|
217
|
|
218 n = ngx_regex_exec(re[i].regex, s, NULL, 0);
|
|
219
|
|
220 if (n == NGX_REGEX_NO_MATCHED) {
|
|
221 continue;
|
|
222 }
|
|
223
|
|
224 if (n < 0) {
|
|
225 ngx_log_error(NGX_LOG_ALERT, log, 0,
|
546
|
226 ngx_regex_exec_n " failed: %i on \"%V\" using \"%s\"",
|
358
|
227 n, s, re[i].name);
|
|
228 return NGX_ERROR;
|
|
229 }
|
|
230
|
|
231 /* match */
|
|
232
|
|
233 return NGX_OK;
|
|
234 }
|
|
235
|
|
236 return NGX_DECLINED;
|
|
237 }
|
|
238
|
|
239
|
52
|
240 static void * ngx_libc_cdecl
|
|
241 ngx_regex_malloc(size_t size)
|
0
|
242 {
|
|
243 ngx_pool_t *pool;
|
|
244 #if (NGX_THREADS)
|
|
245 ngx_core_tls_t *tls;
|
|
246
|
|
247 if (ngx_threaded) {
|
|
248 tls = ngx_thread_get_tls(ngx_core_tls_key);
|
|
249 pool = tls->pool;
|
546
|
250
|
0
|
251 } else {
|
|
252 pool = ngx_pcre_pool;
|
|
253 }
|
546
|
254
|
0
|
255 #else
|
546
|
256
|
0
|
257 pool = ngx_pcre_pool;
|
546
|
258
|
0
|
259 #endif
|
|
260
|
|
261 if (pool) {
|
382
|
262 return ngx_palloc(pool, size);
|
0
|
263 }
|
|
264
|
|
265 return NULL;
|
|
266 }
|
|
267
|
|
268
|
52
|
269 static void ngx_libc_cdecl
|
|
270 ngx_regex_free(void *p)
|
0
|
271 {
|
|
272 return;
|
|
273 }
|
656
|
274
|
|
275
|
|
276 static ngx_int_t
|
|
277 ngx_regex_module_init(ngx_cycle_t *cycle)
|
|
278 {
|
|
279 int opt;
|
|
280 const char *errstr;
|
|
281 ngx_uint_t i;
|
|
282 ngx_list_part_t *part;
|
|
283 ngx_regex_elt_t *elts;
|
|
284
|
|
285 opt = 0;
|
|
286
|
|
287 #if (NGX_HAVE_PCRE_JIT)
|
|
288 {
|
|
289 ngx_regex_conf_t *rcf;
|
|
290
|
|
291 rcf = (ngx_regex_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_regex_module);
|
|
292
|
|
293 if (rcf->pcre_jit) {
|
|
294 opt = PCRE_STUDY_JIT_COMPILE;
|
|
295 }
|
|
296 }
|
|
297 #endif
|
|
298
|
|
299 ngx_regex_malloc_init(cycle->pool);
|
|
300
|
|
301 part = &ngx_pcre_studies->part;
|
|
302 elts = part->elts;
|
|
303
|
|
304 for (i = 0 ; /* void */ ; i++) {
|
|
305
|
|
306 if (i >= part->nelts) {
|
|
307 if (part->next == NULL) {
|
|
308 break;
|
|
309 }
|
|
310
|
|
311 part = part->next;
|
|
312 elts = part->elts;
|
|
313 i = 0;
|
|
314 }
|
|
315
|
|
316 elts[i].regex->extra = pcre_study(elts[i].regex->pcre, opt, &errstr);
|
|
317
|
|
318 if (errstr != NULL) {
|
|
319 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
|
|
320 "pcre_study() failed: %s in \"%s\"",
|
|
321 errstr, elts[i].name);
|
|
322 }
|
|
323
|
|
324 #if (NGX_HAVE_PCRE_JIT)
|
|
325 if (opt & PCRE_STUDY_JIT_COMPILE) {
|
|
326 int jit, n;
|
|
327
|
|
328 jit = 0;
|
|
329 n = pcre_fullinfo(elts[i].regex->pcre, elts[i].regex->extra,
|
|
330 PCRE_INFO_JIT, &jit);
|
|
331
|
|
332 if (n != 0 || jit != 1) {
|
|
333 ngx_log_error(NGX_LOG_INFO, cycle->log, 0,
|
|
334 "JIT compiler does not support pattern: \"%s\"",
|
|
335 elts[i].name);
|
|
336 }
|
|
337 }
|
|
338 #endif
|
|
339 }
|
|
340
|
|
341 ngx_regex_malloc_done();
|
|
342
|
|
343 ngx_pcre_studies = NULL;
|
|
344
|
|
345 return NGX_OK;
|
|
346 }
|
|
347
|
|
348
|
|
349 static void *
|
|
350 ngx_regex_create_conf(ngx_cycle_t *cycle)
|
|
351 {
|
|
352 ngx_regex_conf_t *rcf;
|
|
353
|
|
354 rcf = ngx_pcalloc(cycle->pool, sizeof(ngx_regex_conf_t));
|
|
355 if (rcf == NULL) {
|
|
356 return NULL;
|
|
357 }
|
|
358
|
|
359 rcf->pcre_jit = NGX_CONF_UNSET;
|
|
360
|
|
361 ngx_pcre_studies = ngx_list_create(cycle->pool, 8, sizeof(ngx_regex_elt_t));
|
|
362 if (ngx_pcre_studies == NULL) {
|
|
363 return NULL;
|
|
364 }
|
|
365
|
|
366 return rcf;
|
|
367 }
|
|
368
|
|
369
|
|
370 static char *
|
|
371 ngx_regex_init_conf(ngx_cycle_t *cycle, void *conf)
|
|
372 {
|
|
373 ngx_regex_conf_t *rcf = conf;
|
|
374
|
|
375 ngx_conf_init_value(rcf->pcre_jit, 0);
|
|
376
|
|
377 return NGX_CONF_OK;
|
|
378 }
|
|
379
|
|
380
|
|
381 static char *
|
|
382 ngx_regex_pcre_jit(ngx_conf_t *cf, void *post, void *data)
|
|
383 {
|
|
384 ngx_flag_t *fp = data;
|
|
385
|
|
386 if (*fp == 0) {
|
|
387 return NGX_CONF_OK;
|
|
388 }
|
|
389
|
|
390 #if (NGX_HAVE_PCRE_JIT)
|
|
391 {
|
|
392 int jit, r;
|
|
393
|
|
394 jit = 0;
|
|
395 r = pcre_config(PCRE_CONFIG_JIT, &jit);
|
|
396
|
|
397 if (r != 0 || jit != 1) {
|
|
398 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
399 "PCRE library does not support JIT");
|
|
400 *fp = 0;
|
|
401 }
|
|
402 }
|
|
403 #else
|
|
404 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
405 "nginx was build without PCRE JIT support");
|
|
406 *fp = 0;
|
|
407 #endif
|
|
408
|
|
409 return NGX_CONF_OK;
|
|
410 }
|