comparison src/core/ngx_regex.c @ 7982:fbbb5ce52995

PCRE2 and PCRE binary compatibility. With this change, dynamic modules using nginx regex interface can be used regardless of the variant of the PCRE library nginx was compiled with. If a module is compiled with different PCRE library variant, in case of ngx_regex_exec() errors it will report wrong function name in error messages. This is believed to be tolerable, given that fixing this will require interface changes.
author Maxim Dounin <mdounin@mdounin.ru>
date Sat, 25 Dec 2021 01:07:16 +0300
parents 0b5f12d5c531
children d07456044b61
comparison
equal deleted inserted replaced
7981:0b5f12d5c531 7982:fbbb5ce52995
116 { 116 {
117 int n, errcode; 117 int n, errcode;
118 char *p; 118 char *p;
119 u_char errstr[128]; 119 u_char errstr[128];
120 size_t erroff; 120 size_t erroff;
121 uint32_t options;
121 pcre2_code *re; 122 pcre2_code *re;
122 ngx_regex_elt_t *elt; 123 ngx_regex_elt_t *elt;
123 pcre2_general_context *gctx; 124 pcre2_general_context *gctx;
124 pcre2_compile_context *cctx; 125 pcre2_compile_context *cctx;
125 126
150 151
151 pcre2_general_context_free(gctx); 152 pcre2_general_context_free(gctx);
152 ngx_regex_malloc_done(); 153 ngx_regex_malloc_done();
153 } 154 }
154 155
156 options = 0;
157
158 if (rc->options & NGX_REGEX_CASELESS) {
159 options |= PCRE2_CASELESS;
160 }
161
162 if (rc->options & ~NGX_REGEX_CASELESS) {
163 rc->err.len = ngx_snprintf(rc->err.data, rc->err.len,
164 "regex \"%V\" compilation failed: invalid options",
165 &rc->pattern)
166 - rc->err.data;
167 return NGX_ERROR;
168 }
169
155 ngx_regex_malloc_init(rc->pool); 170 ngx_regex_malloc_init(rc->pool);
156 171
157 re = pcre2_compile(rc->pattern.data, rc->pattern.len, 172 re = pcre2_compile(rc->pattern.data, rc->pattern.len, options,
158 (uint32_t) rc->options, &errcode, &erroff, 173 &errcode, &erroff, ngx_regex_compile_context);
159 ngx_regex_compile_context);
160 174
161 /* ensure that there is no current pool */ 175 /* ensure that there is no current pool */
162 ngx_regex_malloc_done(); 176 ngx_regex_malloc_done();
163 177
164 if (re == NULL) { 178 if (re == NULL) {
250 { 264 {
251 int n, erroff; 265 int n, erroff;
252 char *p; 266 char *p;
253 pcre *re; 267 pcre *re;
254 const char *errstr; 268 const char *errstr;
269 ngx_uint_t options;
255 ngx_regex_elt_t *elt; 270 ngx_regex_elt_t *elt;
256 271
272 options = 0;
273
274 if (rc->options & NGX_REGEX_CASELESS) {
275 options |= PCRE_CASELESS;
276 }
277
278 if (rc->options & ~NGX_REGEX_CASELESS) {
279 rc->err.len = ngx_snprintf(rc->err.data, rc->err.len,
280 "regex \"%V\" compilation failed: invalid options",
281 &rc->pattern)
282 - rc->err.data;
283 return NGX_ERROR;
284 }
285
257 ngx_regex_malloc_init(rc->pool); 286 ngx_regex_malloc_init(rc->pool);
258 287
259 re = pcre_compile((const char *) rc->pattern.data, (int) rc->options, 288 re = pcre_compile((const char *) rc->pattern.data, (int) options,
260 &errstr, &erroff, NULL); 289 &errstr, &erroff, NULL);
261 290
262 /* ensure that there is no current pool */ 291 /* ensure that there is no current pool */
263 ngx_regex_malloc_done(); 292 ngx_regex_malloc_done();
264 293
411 ngx_regex_malloc_done(); 440 ngx_regex_malloc_done();
412 441
413 return rc; 442 return rc;
414 } 443 }
415 444
445 #else
446
447 ngx_int_t
448 ngx_regex_exec(ngx_regex_t *re, ngx_str_t *s, int *captures, ngx_uint_t size)
449 {
450 return pcre_exec(re->code, re->extra, (const char *) s->data, s->len,
451 0, 0, captures, size);
452 }
453
416 #endif 454 #endif
417 455
418 456
419 ngx_int_t 457 ngx_int_t
420 ngx_regex_exec_array(ngx_array_t *a, ngx_str_t *s, ngx_log_t *log) 458 ngx_regex_exec_array(ngx_array_t *a, ngx_str_t *s, ngx_log_t *log)