diff options
author | Igor Sysoev <igor@sysoev.ru> | 2009-11-16 12:19:02 +0000 |
---|---|---|
committer | Igor Sysoev <igor@sysoev.ru> | 2009-11-16 12:19:02 +0000 |
commit | c05f20ec2eace8239c53f680294c9a2154b39e7d (patch) | |
tree | fba2dd2e0c4f7a36f4b396b759645ced61c3c709 /src/core | |
parent | c0ae4716ece5e48c4e82863959d3ae9ff6693e65 (diff) | |
download | nginx-c05f20ec2eace8239c53f680294c9a2154b39e7d.tar.gz nginx-c05f20ec2eace8239c53f680294c9a2154b39e7d.zip |
regex named captures
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/ngx_core.h | 3 | ||||
-rw-r--r-- | src/core/ngx_regex.c | 125 | ||||
-rw-r--r-- | src/core/ngx_regex.h | 28 |
3 files changed, 115 insertions, 41 deletions
diff --git a/src/core/ngx_core.h b/src/core/ngx_core.h index 58d0b8030..0994aa5cd 100644 --- a/src/core/ngx_core.h +++ b/src/core/ngx_core.h @@ -83,7 +83,8 @@ typedef void (*ngx_connection_handler_pt)(ngx_connection_t *c); #define CRLF "\x0d\x0a" -#define ngx_abs(value) (((value) >= 0) ? (value) : - (value)) +#define ngx_abs(value) (((value) >= 0) ? (value) : - (value)) +#define ngx_max(val1, val2) ((val1 < val2) ? (val2) : (val1)) void ngx_cpuinfo(void); diff --git a/src/core/ngx_regex.c b/src/core/ngx_regex.c index c5a495209..18e3c26c5 100644 --- a/src/core/ngx_regex.c +++ b/src/core/ngx_regex.c @@ -23,25 +23,16 @@ ngx_regex_init(void) } -ngx_regex_t * -ngx_regex_compile(ngx_str_t *pattern, ngx_int_t options, ngx_pool_t *pool, - ngx_str_t *err) +static ngx_inline void +ngx_regex_malloc_init(ngx_pool_t *pool) { - int erroff; - const char *errstr; - ngx_regex_t *re; #if (NGX_THREADS) ngx_core_tls_t *tls; -#if (NGX_SUPPRESS_WARN) - tls = NULL; -#endif - if (ngx_threaded) { tls = ngx_thread_get_tls(ngx_core_tls_key); tls->pool = pool; - } else { - ngx_pcre_pool = pool; + return; } #else @@ -49,35 +40,103 @@ ngx_regex_compile(ngx_str_t *pattern, ngx_int_t options, ngx_pool_t *pool, ngx_pcre_pool = pool; #endif +} + + +static ngx_inline void +ngx_regex_malloc_done(void) +{ +#if (NGX_THREADS) + ngx_core_tls_t *tls; + + if (ngx_threaded) { + tls = ngx_thread_get_tls(ngx_core_tls_key); + tls->pool = NULL; + return; + } + +#else + + ngx_pcre_pool = NULL; + +#endif +} - re = pcre_compile((const char *) pattern->data, (int) options, + +ngx_int_t +ngx_regex_compile(ngx_regex_compile_t *rc) +{ + int n, erroff; + char *p; + const char *errstr; + ngx_regex_t *re; + + ngx_regex_malloc_init(rc->pool); + + re = pcre_compile((const char *) rc->pattern.data, (int) rc->options, &errstr, &erroff, NULL); + /* ensure that there is no current pool */ + ngx_regex_malloc_done(); + if (re == NULL) { - if ((size_t) erroff == pattern->len) { - ngx_snprintf(err->data, err->len - 1, - "pcre_compile() failed: %s in \"%s\"%Z", - errstr, pattern->data); + if ((size_t) erroff == rc->pattern.len) { + rc->err.len = ngx_snprintf(rc->err.data, rc->err.len, + "pcre_compile() failed: %s in \"%V\"", + errstr, &rc->pattern) + - rc->err.data; + } else { - ngx_snprintf(err->data, err->len - 1, - "pcre_compile() failed: %s in \"%s\" at \"%s\"%Z", - errstr, pattern->data, pattern->data + erroff); + rc->err.len = ngx_snprintf(rc->err.data, rc->err.len, + "pcre_compile() failed: %s in \"%V\" at \"%s\"", + errstr, &rc->pattern, rc->pattern.data + erroff) + - rc->err.data; } + + return NGX_ERROR; } - /* ensure that there is no current pool */ + rc->regex = re; -#if (NGX_THREADS) - if (ngx_threaded) { - tls->pool = NULL; - } else { - ngx_pcre_pool = NULL; + n = pcre_fullinfo(re, NULL, PCRE_INFO_CAPTURECOUNT, &rc->captures); + if (n < 0) { + p = "pcre_fullinfo(\"%V\", PCRE_INFO_CAPTURECOUNT) failed: %d"; + goto failed; } -#else - ngx_pcre_pool = NULL; -#endif - return re; + if (rc->captures == 0) { + return NGX_OK; + } + + n = pcre_fullinfo(re, NULL, PCRE_INFO_NAMECOUNT, &rc->named_captures); + if (n < 0) { + p = "pcre_fullinfo(\"%V\", PCRE_INFO_NAMECOUNT) failed: %d"; + goto failed; + } + + if (rc->named_captures == 0) { + return NGX_OK; + } + + n = pcre_fullinfo(re, NULL, PCRE_INFO_NAMEENTRYSIZE, &rc->name_size); + if (n < 0) { + p = "pcre_fullinfo(\"%V\", PCRE_INFO_NAMEENTRYSIZE) failed: %d"; + goto failed; + } + + n = pcre_fullinfo(re, NULL, PCRE_INFO_NAMETABLE, &rc->names); + if (n < 0) { + p = "pcre_fullinfo(\"%V\", PCRE_INFO_NAMETABLE) failed: %d"; + goto failed; + } + + return NGX_OK; + +failed: + + rc->err.len = ngx_snprintf(rc->err.data, rc->err.len, p, &rc->pattern, n) + - rc->err.data; + return NGX_OK; } @@ -117,7 +176,7 @@ ngx_regex_exec_array(ngx_array_t *a, ngx_str_t *s, ngx_log_t *log) if (n < 0) { ngx_log_error(NGX_LOG_ALERT, log, 0, - ngx_regex_exec_n " failed: %d on \"%V\" using \"%s\"", + ngx_regex_exec_n " failed: %i on \"%V\" using \"%s\"", n, s, re[i].name); return NGX_ERROR; } @@ -141,11 +200,15 @@ ngx_regex_malloc(size_t size) if (ngx_threaded) { tls = ngx_thread_get_tls(ngx_core_tls_key); pool = tls->pool; + } else { pool = ngx_pcre_pool; } + #else + pool = ngx_pcre_pool; + #endif if (pool) { diff --git a/src/core/ngx_regex.h b/src/core/ngx_regex.h index 4a4857222..fc35059b3 100644 --- a/src/core/ngx_regex.h +++ b/src/core/ngx_regex.h @@ -20,26 +20,36 @@ typedef pcre ngx_regex_t; + typedef struct { - ngx_regex_t *regex; - u_char *name; + ngx_str_t pattern; + ngx_pool_t *pool; + ngx_int_t options; + + ngx_regex_t *regex; + int captures; + int named_captures; + int name_size; + u_char *names; + ngx_str_t err; +} ngx_regex_compile_t; + + +typedef struct { + ngx_regex_t *regex; + u_char *name; } ngx_regex_elt_t; void ngx_regex_init(void); -ngx_regex_t *ngx_regex_compile(ngx_str_t *pattern, ngx_int_t options, - ngx_pool_t *pool, ngx_str_t *err); -ngx_int_t ngx_regex_capture_count(ngx_regex_t *re); +ngx_int_t ngx_regex_compile(ngx_regex_compile_t *rc); #define ngx_regex_exec(re, s, captures, size) \ pcre_exec(re, NULL, (const char *) (s)->data, (s)->len, 0, 0, \ captures, size) +#define ngx_regex_exec_n "pcre_exec()" ngx_int_t ngx_regex_exec_array(ngx_array_t *a, ngx_str_t *s, ngx_log_t *log); -#define ngx_regex_exec_n "pcre_exec()" -#define ngx_regex_capture_count_n "pcre_fullinfo()" - - #endif /* _NGX_REGEX_H_INCLUDED_ */ |