diff src/http/modules/ngx_http_rewrite_handler.c @ 36:a39d1b793287 NGINX_0_1_18

nginx 0.1.18 *) Workaround: the default values of the devpoll_events and the devpoll_changes directives changed from 512 to 32 to be compatible with Solaris 10. *) Bugfix: the proxy_set_x_var and fastcgi_set_var directives were not inherited. *) Bugfix: in the redirect rewrite directive the arguments were concatenated with URI by the "&" rather than the "?". *) Bugfix: the lines without trailing ";" in the file being included by the ngx_http_geo_module were silently ignored. *) Feature: the ngx_http_stub_status_module. *) Bugfix: the unknown log format in the access_log directive caused the segmentation fault. *) Feature: the new "document_root" parameter of the fastcgi_params directive. *) Feature: the fastcgi_redirect_errors directive. *) Feature: the new "break" modifier of the "rewrite" directive allows to stop the rewrite/location cycle and sets the current configuration to the request.
author Igor Sysoev <http://sysoev.ru>
date Wed, 09 Feb 2005 00:00:00 +0300
parents aab2ea7c0458
children 6cfc63e68377
line wrap: on
line diff
--- a/src/http/modules/ngx_http_rewrite_handler.c
+++ b/src/http/modules/ngx_http_rewrite_handler.c
@@ -47,6 +47,7 @@ typedef struct {
     uintptr_t                     args:1;
 
     uintptr_t                     redirect:1;
+    uintptr_t                     break_cycle:1;
 
     ngx_str_t                     name;
 } ngx_http_rewrite_regex_code_t;
@@ -121,18 +122,18 @@ struct ngx_http_rewrite_engine_s {
 static ngx_int_t ngx_http_rewrite_init(ngx_cycle_t *cycle);
 static void *ngx_http_rewrite_create_loc_conf(ngx_conf_t *cf);
 static char *ngx_http_rewrite_merge_loc_conf(ngx_conf_t *cf,
-                                             void *parent, void *child);
+    void *parent, void *child);
 static char *ngx_http_rewrite(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
 static char *ngx_http_rewrite_return(ngx_conf_t *cf, ngx_command_t *cmd,
-                                     void *conf);
+    void *conf);
 static char *ngx_http_rewrite_if(ngx_conf_t *cf, ngx_command_t *cmd,
-                                 void *conf);
-static char *ngx_http_rewrite_valid_referers(ngx_conf_t *cf, ngx_command_t *cmd,
-                                             void *conf);
+    void *conf);
+static char *ngx_http_rewrite_valid_referers(ngx_conf_t *cf,
+    ngx_command_t *cmd, void *conf);
 static void *ngx_http_rewrite_start_code(ngx_pool_t *pool,
-                                         ngx_array_t **codes, size_t size);
+    ngx_array_t **codes, size_t size);
 static void *ngx_http_rewrite_add_code(ngx_array_t *codes, size_t size,
-                                       u_char **main);
+    void *code);
 
 
 static ngx_command_t  ngx_http_rewrite_commands[] = {
@@ -208,7 +209,8 @@ ngx_module_t  ngx_http_rewrite_module = 
 uintptr_t ngx_http_rewrite_exit_code = (uintptr_t) NULL;
 
 
-static ngx_int_t ngx_http_rewrite_handler(ngx_http_request_t *r)
+static ngx_int_t
+ngx_http_rewrite_handler(ngx_http_request_t *r)
 {
     ngx_http_rewrite_code_pt      code;
     ngx_http_rewrite_engine_t    *e;
@@ -259,7 +261,8 @@ static ngx_int_t ngx_http_rewrite_handle
 }
 
 
-static void ngx_http_rewrite_regex_start_code(ngx_http_rewrite_engine_t *e)
+static void
+ngx_http_rewrite_regex_start_code(ngx_http_rewrite_engine_t *e)
 {
     ngx_int_t                       rc;
     ngx_uint_t                      n;
@@ -318,7 +321,9 @@ static void ngx_http_rewrite_regex_start
     e->buf.len = code->size;
 
     if (code->uri) {
-        r->uri_changed = 1;
+        if (!code->break_cycle) {
+            r->uri_changed = 1;
+        }
 
         if (rc && (r->quoted_uri || r->plus_in_uri)) {
             e->buf.len += 2 * ngx_escape_uri(NULL, r->uri.data, r->uri.len,
@@ -348,7 +353,8 @@ static void ngx_http_rewrite_regex_start
 }
 
 
-static void ngx_http_rewrite_regex_end_code(ngx_http_rewrite_engine_t *e)
+static void
+ngx_http_rewrite_regex_end_code(ngx_http_rewrite_engine_t *e)
 {
     ngx_http_request_t                 *r;
     ngx_http_rewrite_regex_end_code_t  *code;
@@ -377,7 +383,7 @@ static void ngx_http_rewrite_regex_end_c
 
     } else {
         if (code->args && r->args.len) {
-            *e->pos++ = '&';
+            *e->pos++ = '?';
             e->pos = ngx_cpymem(e->pos, r->args.data, r->args.len);
         }
 
@@ -425,7 +431,8 @@ static void ngx_http_rewrite_regex_end_c
 }
 
 
-static void ngx_http_rewrite_copy_capture_code(ngx_http_rewrite_engine_t *e)
+static void
+ngx_http_rewrite_copy_capture_code(ngx_http_rewrite_engine_t *e)
 {
     ngx_http_rewrite_copy_capture_code_t  *code;
 
@@ -450,7 +457,8 @@ static void ngx_http_rewrite_copy_captur
 }
 
 
-static void ngx_http_rewrite_copy_code(ngx_http_rewrite_engine_t *e)
+static void
+ngx_http_rewrite_copy_code(ngx_http_rewrite_engine_t *e)
 {
     ngx_http_rewrite_copy_code_t  *code;
 
@@ -467,7 +475,8 @@ static void ngx_http_rewrite_copy_code(n
 }
 
 
-static void ngx_http_rewrite_start_args_code(ngx_http_rewrite_engine_t *e)
+static void
+ngx_http_rewrite_start_args_code(ngx_http_rewrite_engine_t *e)
 {
     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
                    "http rewrite args");
@@ -477,7 +486,8 @@ static void ngx_http_rewrite_start_args_
 }
 
 
-static void ngx_http_rewrite_return_code(ngx_http_rewrite_engine_t *e)
+static void
+ngx_http_rewrite_return_code(ngx_http_rewrite_engine_t *e)
 {
     ngx_http_rewrite_return_code_t  *code;
 
@@ -489,7 +499,8 @@ static void ngx_http_rewrite_return_code
 }
 
 
-static void ngx_http_rewrite_if_code(ngx_http_rewrite_engine_t *e)
+static void
+ngx_http_rewrite_if_code(ngx_http_rewrite_engine_t *e)
 {
     ngx_http_rewrite_if_code_t  *code;
 
@@ -514,7 +525,8 @@ static void ngx_http_rewrite_if_code(ngx
 }
 
 
-static void ngx_http_rewrite_var_code(ngx_http_rewrite_engine_t *e)
+static void
+ngx_http_rewrite_var_code(ngx_http_rewrite_engine_t *e)
 {
     ngx_http_variable_value_t    *value;
     ngx_http_rewrite_var_code_t  *code;
@@ -540,7 +552,8 @@ static void ngx_http_rewrite_var_code(ng
 }
 
 
-static void ngx_http_rewrite_invalid_referer_code(ngx_http_rewrite_engine_t *e)
+static void
+ngx_http_rewrite_invalid_referer_code(ngx_http_rewrite_engine_t *e)
 {
     u_char                       *ref;
     size_t                        len;
@@ -625,13 +638,15 @@ static void ngx_http_rewrite_invalid_ref
 }
 
 
-static void ngx_http_rewrite_nop_code(ngx_http_rewrite_engine_t *e)
+static void
+ngx_http_rewrite_nop_code(ngx_http_rewrite_engine_t *e)
 {
     e->ip += sizeof(uintptr_t);
 }
 
 
-static ngx_int_t ngx_http_rewrite_init(ngx_cycle_t *cycle)
+static ngx_int_t
+ngx_http_rewrite_init(ngx_cycle_t *cycle)
 {   
     ngx_http_handler_pt        *h;
     ngx_http_core_main_conf_t  *cmcf;
@@ -649,7 +664,8 @@ static ngx_int_t ngx_http_rewrite_init(n
 }   
 
 
-static void *ngx_http_rewrite_create_loc_conf(ngx_conf_t *cf)
+static void *
+ngx_http_rewrite_create_loc_conf(ngx_conf_t *cf)
 {
     ngx_http_rewrite_loc_conf_t  *conf;
 
@@ -665,8 +681,8 @@ static void *ngx_http_rewrite_create_loc
 }
 
 
-static char *ngx_http_rewrite_merge_loc_conf(ngx_conf_t *cf,
-                                             void *parent, void *child)
+static char *
+ngx_http_rewrite_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
 {
     ngx_http_rewrite_loc_conf_t *prev = parent;
     ngx_http_rewrite_loc_conf_t *conf = child;
@@ -737,7 +753,8 @@ static char *ngx_http_rewrite_merge_loc_
 }
 
 
-static char *ngx_http_rewrite(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+static char *
+ngx_http_rewrite(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
     ngx_http_rewrite_loc_conf_t *lcf = conf;
     
@@ -793,6 +810,10 @@ static char *ngx_http_rewrite(ngx_conf_t
         if (ngx_strcmp(value[3].data, "last") == 0) {
             last = 1;
 
+        } else if (ngx_strcmp(value[3].data, "break") == 0) {
+            regex->break_cycle = 1;
+            last = 1;
+
         } else if (ngx_strcmp(value[3].data, "redirect") == 0) {
             regex->status = NGX_HTTP_MOVED_TEMPORARILY;
             regex->redirect = 1;
@@ -824,7 +845,7 @@ static char *ngx_http_rewrite(ngx_conf_t
 
             copy_capture = ngx_http_rewrite_add_code(lcf->codes,
                                   sizeof(ngx_http_rewrite_copy_capture_code_t),
-                                  (u_char **) &regex);
+                                  &regex);
             if (copy_capture == NULL) {
                 return NGX_CONF_ERROR;
             }
@@ -857,7 +878,7 @@ static char *ngx_http_rewrite(ngx_conf_t
 
             if (!regex->redirect) {
                 code = ngx_http_rewrite_add_code(lcf->codes, sizeof(uintptr_t),
-                                                 (u_char **) &regex);
+                                                 &regex);
                 if (code == NULL) {
                     return NGX_CONF_ERROR;
                 }
@@ -907,7 +928,7 @@ static char *ngx_http_rewrite(ngx_conf_t
 
         copy = ngx_http_rewrite_add_code(lcf->codes,
                                    sizeof(ngx_http_rewrite_copy_code_t) + size,
-                                   (u_char **) &regex);
+                                   &regex);
         if (copy == NULL) {
             return NGX_CONF_ERROR;
         }
@@ -939,7 +960,7 @@ static char *ngx_http_rewrite(ngx_conf_t
 
     regex_end = ngx_http_rewrite_add_code(lcf->codes,
                                      sizeof(ngx_http_rewrite_regex_end_code_t),
-                                     (u_char **) &regex);
+                                     &regex);
     if (regex_end == NULL) {
         return NGX_CONF_ERROR;
     }
@@ -951,7 +972,7 @@ static char *ngx_http_rewrite(ngx_conf_t
 
     if (last) {
         code = ngx_http_rewrite_add_code(lcf->codes, sizeof(uintptr_t),
-                                         (u_char **) &regex);
+                                         &regex);
         if (code == NULL) {
             return NGX_CONF_ERROR;
         }
@@ -966,9 +987,8 @@ static char *ngx_http_rewrite(ngx_conf_t
 }
 
 
-
-static char *ngx_http_rewrite_return(ngx_conf_t *cf, ngx_command_t *cmd,
-                                     void *conf)
+static char *
+ngx_http_rewrite_return(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
     ngx_http_rewrite_loc_conf_t *lcf = conf;
 
@@ -996,7 +1016,8 @@ static char *ngx_http_rewrite_return(ngx
 }
 
 
-static char *ngx_http_rewrite_if(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+static char *
+ngx_http_rewrite_if(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
     ngx_http_rewrite_loc_conf_t *lcf = conf;
 
@@ -1179,8 +1200,8 @@ static char *ngx_http_rewrite_if(ngx_con
 }
 
 
-static char *ngx_http_rewrite_valid_referers(ngx_conf_t *cf, ngx_command_t *cmd,
-                                             void *conf)
+static char *
+ngx_http_rewrite_valid_referers(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
     ngx_http_rewrite_loc_conf_t *lcf = conf;
 
@@ -1266,8 +1287,8 @@ static char *ngx_http_rewrite_valid_refe
 }
 
 
-static void *ngx_http_rewrite_start_code(ngx_pool_t *pool,
-                                         ngx_array_t **codes, size_t size)
+static void *
+ngx_http_rewrite_start_code(ngx_pool_t *pool, ngx_array_t **codes, size_t size)
 {
     if (*codes == NULL) {
         if (!(*codes = ngx_array_create(pool, 256, 1))) {
@@ -1279,10 +1300,10 @@ static void *ngx_http_rewrite_start_code
 }
 
 
-static void *ngx_http_rewrite_add_code(ngx_array_t *codes, size_t size,
-                                       u_char **main)
+static void *
+ngx_http_rewrite_add_code(ngx_array_t *codes, size_t size, void *code)
 {
-    u_char  *elts;
+    u_char  *elts, **p;
     void    *new;
 
     elts = codes->elts;
@@ -1292,7 +1313,8 @@ static void *ngx_http_rewrite_add_code(n
     }
 
     if (elts != codes->elts) {
-        *main += (u_char *) codes->elts - elts;
+        p = code;
+        *p += (u_char *) codes->elts - elts;
     }
 
     return new;