0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9
|
|
10
|
52
|
11 static void * ngx_libc_cdecl ngx_regex_malloc(size_t size);
|
|
12 static void ngx_libc_cdecl ngx_regex_free(void *p);
|
0
|
13
|
|
14
|
|
15 static ngx_pool_t *ngx_pcre_pool;
|
|
16
|
|
17
|
52
|
18 void
|
|
19 ngx_regex_init(void)
|
0
|
20 {
|
|
21 pcre_malloc = ngx_regex_malloc;
|
|
22 pcre_free = ngx_regex_free;
|
|
23 }
|
|
24
|
|
25
|
546
|
26 static ngx_inline void
|
|
27 ngx_regex_malloc_init(ngx_pool_t *pool)
|
0
|
28 {
|
|
29 #if (NGX_THREADS)
|
|
30 ngx_core_tls_t *tls;
|
|
31
|
|
32 if (ngx_threaded) {
|
|
33 tls = ngx_thread_get_tls(ngx_core_tls_key);
|
|
34 tls->pool = pool;
|
546
|
35 return;
|
0
|
36 }
|
|
37
|
550
|
38 #endif
|
0
|
39
|
|
40 ngx_pcre_pool = pool;
|
546
|
41 }
|
0
|
42
|
546
|
43
|
|
44 static ngx_inline void
|
|
45 ngx_regex_malloc_done(void)
|
|
46 {
|
|
47 #if (NGX_THREADS)
|
|
48 ngx_core_tls_t *tls;
|
|
49
|
|
50 if (ngx_threaded) {
|
|
51 tls = ngx_thread_get_tls(ngx_core_tls_key);
|
|
52 tls->pool = NULL;
|
|
53 return;
|
|
54 }
|
|
55
|
550
|
56 #endif
|
546
|
57
|
|
58 ngx_pcre_pool = NULL;
|
|
59 }
|
|
60
|
|
61
|
|
62 ngx_int_t
|
|
63 ngx_regex_compile(ngx_regex_compile_t *rc)
|
|
64 {
|
|
65 int n, erroff;
|
|
66 char *p;
|
|
67 const char *errstr;
|
|
68 ngx_regex_t *re;
|
|
69
|
|
70 ngx_regex_malloc_init(rc->pool);
|
|
71
|
|
72 re = pcre_compile((const char *) rc->pattern.data, (int) rc->options,
|
0
|
73 &errstr, &erroff, NULL);
|
|
74
|
546
|
75 /* ensure that there is no current pool */
|
|
76 ngx_regex_malloc_done();
|
|
77
|
0
|
78 if (re == NULL) {
|
546
|
79 if ((size_t) erroff == rc->pattern.len) {
|
|
80 rc->err.len = ngx_snprintf(rc->err.data, rc->err.len,
|
|
81 "pcre_compile() failed: %s in \"%V\"",
|
|
82 errstr, &rc->pattern)
|
|
83 - rc->err.data;
|
|
84
|
0
|
85 } else {
|
546
|
86 rc->err.len = ngx_snprintf(rc->err.data, rc->err.len,
|
|
87 "pcre_compile() failed: %s in \"%V\" at \"%s\"",
|
|
88 errstr, &rc->pattern, rc->pattern.data + erroff)
|
|
89 - rc->err.data;
|
0
|
90 }
|
546
|
91
|
|
92 return NGX_ERROR;
|
|
93 }
|
|
94
|
|
95 rc->regex = re;
|
|
96
|
|
97 n = pcre_fullinfo(re, NULL, PCRE_INFO_CAPTURECOUNT, &rc->captures);
|
|
98 if (n < 0) {
|
|
99 p = "pcre_fullinfo(\"%V\", PCRE_INFO_CAPTURECOUNT) failed: %d";
|
|
100 goto failed;
|
|
101 }
|
|
102
|
|
103 if (rc->captures == 0) {
|
|
104 return NGX_OK;
|
0
|
105 }
|
|
106
|
546
|
107 n = pcre_fullinfo(re, NULL, PCRE_INFO_NAMECOUNT, &rc->named_captures);
|
|
108 if (n < 0) {
|
|
109 p = "pcre_fullinfo(\"%V\", PCRE_INFO_NAMECOUNT) failed: %d";
|
|
110 goto failed;
|
|
111 }
|
|
112
|
|
113 if (rc->named_captures == 0) {
|
|
114 return NGX_OK;
|
|
115 }
|
0
|
116
|
546
|
117 n = pcre_fullinfo(re, NULL, PCRE_INFO_NAMEENTRYSIZE, &rc->name_size);
|
|
118 if (n < 0) {
|
|
119 p = "pcre_fullinfo(\"%V\", PCRE_INFO_NAMEENTRYSIZE) failed: %d";
|
|
120 goto failed;
|
0
|
121 }
|
546
|
122
|
|
123 n = pcre_fullinfo(re, NULL, PCRE_INFO_NAMETABLE, &rc->names);
|
|
124 if (n < 0) {
|
|
125 p = "pcre_fullinfo(\"%V\", PCRE_INFO_NAMETABLE) failed: %d";
|
|
126 goto failed;
|
|
127 }
|
0
|
128
|
546
|
129 return NGX_OK;
|
|
130
|
|
131 failed:
|
|
132
|
|
133 rc->err.len = ngx_snprintf(rc->err.data, rc->err.len, p, &rc->pattern, n)
|
|
134 - rc->err.data;
|
|
135 return NGX_OK;
|
0
|
136 }
|
|
137
|
|
138
|
52
|
139 ngx_int_t
|
358
|
140 ngx_regex_exec_array(ngx_array_t *a, ngx_str_t *s, ngx_log_t *log)
|
|
141 {
|
|
142 ngx_int_t n;
|
|
143 ngx_uint_t i;
|
|
144 ngx_regex_elt_t *re;
|
|
145
|
|
146 re = a->elts;
|
|
147
|
|
148 for (i = 0; i < a->nelts; i++) {
|
|
149
|
|
150 n = ngx_regex_exec(re[i].regex, s, NULL, 0);
|
|
151
|
|
152 if (n == NGX_REGEX_NO_MATCHED) {
|
|
153 continue;
|
|
154 }
|
|
155
|
|
156 if (n < 0) {
|
|
157 ngx_log_error(NGX_LOG_ALERT, log, 0,
|
546
|
158 ngx_regex_exec_n " failed: %i on \"%V\" using \"%s\"",
|
358
|
159 n, s, re[i].name);
|
|
160 return NGX_ERROR;
|
|
161 }
|
|
162
|
|
163 /* match */
|
|
164
|
|
165 return NGX_OK;
|
|
166 }
|
|
167
|
|
168 return NGX_DECLINED;
|
|
169 }
|
|
170
|
|
171
|
52
|
172 static void * ngx_libc_cdecl
|
|
173 ngx_regex_malloc(size_t size)
|
0
|
174 {
|
|
175 ngx_pool_t *pool;
|
|
176 #if (NGX_THREADS)
|
|
177 ngx_core_tls_t *tls;
|
|
178
|
|
179 if (ngx_threaded) {
|
|
180 tls = ngx_thread_get_tls(ngx_core_tls_key);
|
|
181 pool = tls->pool;
|
546
|
182
|
0
|
183 } else {
|
|
184 pool = ngx_pcre_pool;
|
|
185 }
|
546
|
186
|
0
|
187 #else
|
546
|
188
|
0
|
189 pool = ngx_pcre_pool;
|
546
|
190
|
0
|
191 #endif
|
|
192
|
|
193 if (pool) {
|
382
|
194 return ngx_palloc(pool, size);
|
0
|
195 }
|
|
196
|
|
197 return NULL;
|
|
198 }
|
|
199
|
|
200
|
52
|
201 static void ngx_libc_cdecl
|
|
202 ngx_regex_free(void *p)
|
0
|
203 {
|
|
204 return;
|
|
205 }
|