Mercurial > hg > nginx
annotate src/core/ngx_regex.c @ 4104:84f34973684c
Version bump.
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 14 Sep 2011 15:20:01 +0000 |
parents | 169d84ff4064 |
children | 7423800ef437 |
rev | line source |
---|---|
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
381
diff
changeset
|
1 |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
381
diff
changeset
|
2 /* |
444
42d11f017717
nginx-0.1.0-2004-09-29-20:00:49 import; remove years from copyright
Igor Sysoev <igor@sysoev.ru>
parents:
441
diff
changeset
|
3 * Copyright (C) Igor Sysoev |
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
381
diff
changeset
|
4 */ |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
381
diff
changeset
|
5 |
195
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
6 |
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
7 #include <ngx_config.h> |
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
8 #include <ngx_core.h> |
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
9 |
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
10 |
503 | 11 static void * ngx_libc_cdecl ngx_regex_malloc(size_t size); |
12 static void ngx_libc_cdecl ngx_regex_free(void *p); | |
195
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
13 |
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
14 |
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
15 static ngx_pool_t *ngx_pcre_pool; |
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
16 |
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
17 |
503 | 18 void |
19 ngx_regex_init(void) | |
195
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
20 { |
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
21 pcre_malloc = ngx_regex_malloc; |
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
22 pcre_free = ngx_regex_free; |
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
23 } |
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
24 |
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
25 |
3325 | 26 static ngx_inline void |
27 ngx_regex_malloc_init(ngx_pool_t *pool) | |
195
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
28 { |
381
02a511569afb
nginx-0.0.7-2004-07-07-19:01:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
294
diff
changeset
|
29 #if (NGX_THREADS) |
02a511569afb
nginx-0.0.7-2004-07-07-19:01:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
294
diff
changeset
|
30 ngx_core_tls_t *tls; |
02a511569afb
nginx-0.0.7-2004-07-07-19:01:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
294
diff
changeset
|
31 |
02a511569afb
nginx-0.0.7-2004-07-07-19:01:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
294
diff
changeset
|
32 if (ngx_threaded) { |
02a511569afb
nginx-0.0.7-2004-07-07-19:01:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
294
diff
changeset
|
33 tls = ngx_thread_get_tls(ngx_core_tls_key); |
02a511569afb
nginx-0.0.7-2004-07-07-19:01:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
294
diff
changeset
|
34 tls->pool = pool; |
3325 | 35 return; |
381
02a511569afb
nginx-0.0.7-2004-07-07-19:01:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
294
diff
changeset
|
36 } |
02a511569afb
nginx-0.0.7-2004-07-07-19:01:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
294
diff
changeset
|
37 |
3349
169d84ff4064
fix pcre allocation on Win32, the bug had been introduced in r3326
Igor Sysoev <igor@sysoev.ru>
parents:
3325
diff
changeset
|
38 #endif |
195
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
39 |
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
40 ngx_pcre_pool = pool; |
3325 | 41 } |
381
02a511569afb
nginx-0.0.7-2004-07-07-19:01:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
294
diff
changeset
|
42 |
3325 | 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 | |
3349
169d84ff4064
fix pcre allocation on Win32, the bug had been introduced in r3326
Igor Sysoev <igor@sysoev.ru>
parents:
3325
diff
changeset
|
56 #endif |
3325 | 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, | |
290
87e73f067470
nginx-0.0.2-2004-03-16-10:10:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
216
diff
changeset
|
73 &errstr, &erroff, NULL); |
195
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
74 |
3325 | 75 /* ensure that there is no current pool */ |
76 ngx_regex_malloc_done(); | |
77 | |
195
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
78 if (re == NULL) { |
3325 | 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 | |
195
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
85 } else { |
3325 | 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; | |
195
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
90 } |
3325 | 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; | |
195
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
105 } |
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
106 |
3325 | 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 } | |
294
5cfd65b8b0a7
nginx-0.0.3-2004-03-23-09:01:52 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
116 |
3325 | 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; | |
381
02a511569afb
nginx-0.0.7-2004-07-07-19:01:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
294
diff
changeset
|
121 } |
3325 | 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 } | |
294
5cfd65b8b0a7
nginx-0.0.3-2004-03-23-09:01:52 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
128 |
3325 | 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; | |
195
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
136 } |
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
137 |
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
138 |
503 | 139 ngx_int_t |
140 ngx_regex_capture_count(ngx_regex_t *re) | |
481 | 141 { |
142 int rc, n; | |
143 | |
144 n = 0; | |
145 | |
146 rc = pcre_fullinfo(re, NULL, PCRE_INFO_CAPTURECOUNT, &n); | |
147 | |
501 | 148 if (rc < 0) { |
149 return (ngx_int_t) rc; | |
150 } | |
151 | |
152 return (ngx_int_t) n; | |
481 | 153 } |
154 | |
155 | |
503 | 156 ngx_int_t |
1784 | 157 ngx_regex_exec_array(ngx_array_t *a, ngx_str_t *s, ngx_log_t *log) |
158 { | |
159 ngx_int_t n; | |
160 ngx_uint_t i; | |
161 ngx_regex_elt_t *re; | |
162 | |
163 re = a->elts; | |
164 | |
165 for (i = 0; i < a->nelts; i++) { | |
166 | |
167 n = ngx_regex_exec(re[i].regex, s, NULL, 0); | |
168 | |
169 if (n == NGX_REGEX_NO_MATCHED) { | |
170 continue; | |
171 } | |
172 | |
173 if (n < 0) { | |
174 ngx_log_error(NGX_LOG_ALERT, log, 0, | |
3325 | 175 ngx_regex_exec_n " failed: %i on \"%V\" using \"%s\"", |
1784 | 176 n, s, re[i].name); |
177 return NGX_ERROR; | |
178 } | |
179 | |
180 /* match */ | |
181 | |
182 return NGX_OK; | |
183 } | |
184 | |
185 return NGX_DECLINED; | |
186 } | |
187 | |
188 | |
503 | 189 static void * ngx_libc_cdecl |
190 ngx_regex_malloc(size_t size) | |
195
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
191 { |
381
02a511569afb
nginx-0.0.7-2004-07-07-19:01:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
294
diff
changeset
|
192 ngx_pool_t *pool; |
02a511569afb
nginx-0.0.7-2004-07-07-19:01:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
294
diff
changeset
|
193 #if (NGX_THREADS) |
02a511569afb
nginx-0.0.7-2004-07-07-19:01:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
294
diff
changeset
|
194 ngx_core_tls_t *tls; |
02a511569afb
nginx-0.0.7-2004-07-07-19:01:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
294
diff
changeset
|
195 |
02a511569afb
nginx-0.0.7-2004-07-07-19:01:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
294
diff
changeset
|
196 if (ngx_threaded) { |
02a511569afb
nginx-0.0.7-2004-07-07-19:01:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
294
diff
changeset
|
197 tls = ngx_thread_get_tls(ngx_core_tls_key); |
02a511569afb
nginx-0.0.7-2004-07-07-19:01:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
294
diff
changeset
|
198 pool = tls->pool; |
3325 | 199 |
381
02a511569afb
nginx-0.0.7-2004-07-07-19:01:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
294
diff
changeset
|
200 } else { |
02a511569afb
nginx-0.0.7-2004-07-07-19:01:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
294
diff
changeset
|
201 pool = ngx_pcre_pool; |
02a511569afb
nginx-0.0.7-2004-07-07-19:01:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
294
diff
changeset
|
202 } |
3325 | 203 |
381
02a511569afb
nginx-0.0.7-2004-07-07-19:01:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
294
diff
changeset
|
204 #else |
3325 | 205 |
381
02a511569afb
nginx-0.0.7-2004-07-07-19:01:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
294
diff
changeset
|
206 pool = ngx_pcre_pool; |
3325 | 207 |
381
02a511569afb
nginx-0.0.7-2004-07-07-19:01:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
294
diff
changeset
|
208 #endif |
02a511569afb
nginx-0.0.7-2004-07-07-19:01:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
294
diff
changeset
|
209 |
02a511569afb
nginx-0.0.7-2004-07-07-19:01:00 import
Igor Sysoev <igor@sysoev.ru>
parents:
294
diff
changeset
|
210 if (pool) { |
2049 | 211 return ngx_palloc(pool, size); |
294
5cfd65b8b0a7
nginx-0.0.3-2004-03-23-09:01:52 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
212 } |
5cfd65b8b0a7
nginx-0.0.3-2004-03-23-09:01:52 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
213 |
5cfd65b8b0a7
nginx-0.0.3-2004-03-23-09:01:52 import
Igor Sysoev <igor@sysoev.ru>
parents:
290
diff
changeset
|
214 return NULL; |
195
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
215 } |
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
216 |
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
217 |
503 | 218 static void ngx_libc_cdecl |
219 ngx_regex_free(void *p) | |
195
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
220 { |
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
221 return; |
8dee38ea9117
nginx-0.0.1-2003-11-25-23:44:56 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
222 } |