diff src/http/modules/ngx_http_rewrite_handler.c @ 30:e1ada20fc595 NGINX_0_1_15

nginx 0.1.15 *) Bugfix: the error while the connecting to the FastCGI server caused segmentation fault. *) Bugfix: the correct handling of the regular expression, that has different number of the captures and substitutions. *) Feature: the location, that is passed to the FastCGI server, can be regular expression. *) Bugfix: the FastCGI's parameter REQUEST_URI is now passed with the arguments and in the original state. *) Bugfix: the ngx_http_rewrite_module module was required to be built to use the regular expressions in locations. *) Bugfix: the directive "proxy_preserve_host on" adds port 80 to the "Host" headers, if upstream listen on port 80; bug appeared in 0.1.14. *) Bugfix: the same paths in autoconfiguration parameters --http-client-body-temp-path=PATH and --http-proxy-temp-path=PATH, or --http-client-body-temp-path=PATH and --http-fastcgi-temp-path=PATH caused segmentation fault.
author Igor Sysoev <http://sysoev.ru>
date Wed, 19 Jan 2005 00:00:00 +0300
parents 46833bd150cb
children da8c190bdaba
line wrap: on
line diff
--- a/src/http/modules/ngx_http_rewrite_handler.c
+++ b/src/http/modules/ngx_http_rewrite_handler.c
@@ -23,7 +23,7 @@ typedef struct {
 
 typedef struct {
     ngx_regex_t  *regex;
-    ngx_uint_t    msize;
+    ngx_uint_t    ncaptures;
 
     ngx_array_t   ops;
     ngx_uint_t    size;
@@ -113,7 +113,7 @@ ngx_module_t  ngx_http_rewrite_module = 
 
 static ngx_int_t ngx_http_rewrite_handler(ngx_http_request_t *r)
 {
-    int                          *matches;
+    int                          *captures;
     u_char                       *p;
     size_t                        len;
     uintptr_t                     data;
@@ -132,18 +132,20 @@ static ngx_int_t ngx_http_rewrite_handle
     rule = scf->rules.elts;
     for (i = 0; i < scf->rules.nelts; i++) {
 
-        if (rule[i].msize) {
-            if (!(matches = ngx_palloc(r->pool, rule[i].msize * sizeof(int)))) {
+        if (rule[i].ncaptures) {
+            captures = ngx_palloc(r->pool, rule[i].ncaptures * sizeof(int));
+            if (captures == NULL) {
                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
             }
 
         } else {
-            matches = NULL;
+            captures = NULL;
         }
 
-        rc = ngx_regex_exec(rule[i].regex, &r->uri, matches, rule[i].msize);
+        rc = ngx_regex_exec(rule[i].regex, &r->uri,
+                            captures, rule[i].ncaptures);
 
-        if (rc == NGX_DECLINED) {
+        if (rc == NGX_REGEX_NO_MATCHED) {
             if (scf->log) {
                 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
                               "\"%V\" does not match \"%V\"",
@@ -174,7 +176,7 @@ static ngx_int_t ngx_http_rewrite_handle
         uri.len = rule[i].size;
 
         for (n = 1; n < (ngx_uint_t) rc; n++) {
-           uri.len += matches[2 * n + 1] - matches[2 * n];
+           uri.len += captures[2 * n + 1] - captures[2 * n];
         }
 
         if (!(uri.data = ngx_palloc(r->pool, uri.len))) {
@@ -198,11 +200,13 @@ static ngx_int_t ngx_http_rewrite_handle
 
             } else { /* NGX_HTTP_REWRITE_COPY_MATCH */
                 m = 2 * op[n].data;
-                p = ngx_cpymem(p, &r->uri.data[matches[m]],
-                               matches[m + 1] - matches[m]);
+                p = ngx_cpymem(p, &r->uri.data[captures[m]],
+                               captures[m + 1] - captures[m]);
             }
         }
 
+        uri.len = p - uri.data;
+
         if (scf->log) {
             ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
                           "rewritten uri: \"%V\"", &uri);
@@ -309,7 +313,7 @@ static char *ngx_http_rewrite_rule(ngx_c
     u_char                   *data, *p;
     size_t                    len;
     ngx_str_t                *value, err;
-    ngx_uint_t                i;
+    ngx_uint_t                i, n;
     ngx_http_rewrite_op_t    *op;
     ngx_http_rewrite_rule_t  *rule;
     u_char                    errstr[NGX_MAX_CONF_ERRSTR];
@@ -321,7 +325,7 @@ static char *ngx_http_rewrite_rule(ngx_c
     ngx_init_array(rule->ops, cf->pool, 5, sizeof(ngx_http_rewrite_op_t),
                    NGX_CONF_ERROR);
 
-    rule->msize = 0;
+    rule->ncaptures = 0;
     rule->size = 0;
     rule->status = 0;
     rule->last = 0;
@@ -371,8 +375,8 @@ static char *ngx_http_rewrite_rule(ngx_c
                 op->op = NGX_HTTP_REWRITE_COPY_MATCH; 
                 op->data = value[2].data[++i] - '0';
 
-                if (rule->msize < op->data) {
-                    rule->msize = op->data;
+                if (rule->ncaptures < op->data) {
+                    rule->ncaptures = op->data;
                 }
 
                 i++;
@@ -414,9 +418,22 @@ static char *ngx_http_rewrite_rule(ngx_c
             }
         }
 
-        if (rule->msize) {
-            rule->msize++;
-            rule->msize *= 3;
+        n = ngx_regex_capture_count(rule->regex);
+
+        if (rule->ncaptures > n) {
+            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                               "pattern \"%V\" has less captures "
+                               "than referrenced in substitution \"%V\"",
+                               &value[1], &value[2]);
+            return NGX_CONF_ERROR;
+        }
+
+        if (rule->ncaptures < n) {
+            rule->ncaptures = n;
+        }
+
+        if (rule->ncaptures) {
+            rule->ncaptures = (rule->ncaptures + 1) * 3;
         }
 
         if (cf->args->nelts > 3) {