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