2003-11-26 04:44:56 +08:00
|
|
|
|
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
|
|
|
|
|
|
|
|
static void *ngx_regex_malloc(size_t size);
|
|
|
|
static void ngx_regex_free(void *p);
|
|
|
|
|
|
|
|
|
|
|
|
/* THREADS: this pool should be private for each thread */
|
|
|
|
static ngx_pool_t *ngx_pcre_pool;
|
|
|
|
|
|
|
|
|
|
|
|
void ngx_regex_init()
|
|
|
|
{
|
|
|
|
pcre_malloc = ngx_regex_malloc;
|
|
|
|
pcre_free = ngx_regex_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ngx_regex_t *ngx_regex_compile(ngx_str_t *pattern, ngx_int_t options,
|
|
|
|
ngx_pool_t *pool, ngx_str_t *err)
|
|
|
|
{
|
|
|
|
int erroff;
|
|
|
|
const char *errstr;
|
|
|
|
ngx_regex_t *re;
|
|
|
|
|
|
|
|
ngx_pcre_pool = pool;
|
|
|
|
|
2004-03-16 15:10:12 +08:00
|
|
|
re = pcre_compile((const char *) pattern->data, (int) options,
|
|
|
|
&errstr, &erroff, NULL);
|
2003-11-26 04:44:56 +08:00
|
|
|
|
|
|
|
if (re == NULL) {
|
2003-12-09 04:48:12 +08:00
|
|
|
if ((size_t) erroff == pattern->len) {
|
2004-03-16 15:10:12 +08:00
|
|
|
ngx_snprintf((char *) err->data, err->len - 1,
|
2003-11-26 04:44:56 +08:00
|
|
|
"pcre_compile() failed: %s in \"%s\"",
|
|
|
|
errstr, pattern->data);
|
|
|
|
} else {
|
2004-03-16 15:10:12 +08:00
|
|
|
ngx_snprintf((char *) err->data, err->len - 1,
|
2003-11-26 04:44:56 +08:00
|
|
|
"pcre_compile() failed: %s in \"%s\" at \"%s\"",
|
|
|
|
errstr, pattern->data, pattern->data + erroff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return re;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-26 04:26:58 +08:00
|
|
|
ngx_int_t ngx_regex_exec(ngx_regex_t *re, ngx_str_t *s,
|
|
|
|
int *matches, ngx_int_t size)
|
2003-11-26 04:44:56 +08:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2004-03-16 15:10:12 +08:00
|
|
|
rc = pcre_exec(re, NULL, (const char *) s->data, s->len, 0, 0,
|
|
|
|
matches, size);
|
2003-11-26 04:44:56 +08:00
|
|
|
|
|
|
|
if (rc == -1) {
|
|
|
|
return NGX_DECLINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void *ngx_regex_malloc(size_t size)
|
|
|
|
{
|
|
|
|
return ngx_palloc(ngx_pcre_pool, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void ngx_regex_free(void *p)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|