diff src/http/ngx_http_core_module.c @ 324:f7cd062ee035 NGINX_0_6_6

nginx 0.6.6 *) Feature: the --sysconfdir=PATH option in configure. *) Feature: named locations. *) Feature: the $args variable can be set with the "set" directive. *) Feature: the $is_args variable. *) Bugfix: fair big weight upstream balancer. *) Bugfix: if a client has closed connection to mail proxy then nginx might not close connection to backend. *) Bugfix: if the same host without specified port was used as backend for HTTP and HTTPS, then nginx used only one port - 80 or 443. *) Bugfix: fix building on Solaris/amd64 by Sun Studio 11 and early versions; bug appeared in 0.6.4.
author Igor Sysoev <http://sysoev.ru>
date Mon, 30 Jul 2007 00:00:00 +0400
parents 429900ca25ee
children 9fc4ab6673f9
line wrap: on
line diff
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -945,13 +945,12 @@ ngx_http_core_find_location(ngx_http_req
     clcfp = locations->elts;
     for (i = 0; i < locations->nelts; i++) {
 
+        if (clcfp[i]->noname
 #if (NGX_PCRE)
-        if (clcfp[i]->regex) {
-            break;
-        }
+            || clcfp[i]->regex
 #endif
-
-        if (clcfp[i]->noname) {
+            || clcfp[i]->named)
+        {
             break;
         }
 
@@ -1028,7 +1027,7 @@ ngx_http_core_find_location(ngx_http_req
 
     for (i = regex_start; i < locations->nelts; i++) {
 
-        if (clcfp[i]->noname) {
+        if (!clcfp[i]->regex) {
             break;
         }
 
@@ -1223,7 +1222,8 @@ ngx_http_map_uri_to_path(ngx_http_reques
             return NULL;
         }
 
-        if (ngx_conf_full_name((ngx_cycle_t *) ngx_cycle, path) == NGX_ERROR) {
+        if (ngx_conf_full_name((ngx_cycle_t *) ngx_cycle, path, 0)== NGX_ERROR)
+        {
             return NULL;
         }
 
@@ -1513,6 +1513,51 @@ ngx_http_internal_redirect(ngx_http_requ
 }
 
 
+ngx_int_t
+ngx_http_named_location(ngx_http_request_t *r, ngx_str_t *name)
+{
+    ngx_uint_t                   i;
+    ngx_http_core_srv_conf_t    *cscf;
+    ngx_http_core_loc_conf_t   **clcfp;
+    ngx_http_core_main_conf_t   *cmcf;
+
+    cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
+
+    clcfp = cscf->locations.elts;
+
+    for (i = cscf->named_start; i < cscf->locations.nelts; i++) {
+
+        if (name->len != clcfp[i]->name.len
+            || ngx_strncmp(name->data, clcfp[i]->name.data, name->len) != 0)
+        {
+            continue;
+        }
+
+        ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                       "named location: %V \"%V?%V\"", name, &r->uri, &r->args);
+
+        r->internal = 1;
+
+        r->loc_conf = clcfp[i]->loc_conf;
+
+        ngx_http_update_location_config(r);
+
+        cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
+
+        r->phase_handler = cmcf->phase_engine.location_rewrite_index;
+        ngx_http_core_run_phases(r);
+
+        return NGX_DONE;
+    }
+
+    ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                  "could not find name location \"%V\"", name);
+
+    ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
+    return NGX_DONE;
+}
+
+
 ngx_http_cleanup_t *
 ngx_http_cleanup_add(ngx_http_request_t *r, size_t size)
 {
@@ -1557,10 +1602,8 @@ ngx_http_core_server(ngx_conf_t *cf, ngx
     ngx_http_module_t           *module;
     ngx_http_conf_ctx_t         *ctx, *http_ctx;
     ngx_http_core_srv_conf_t    *cscf, **cscfp;
+    ngx_http_core_loc_conf_t   **clcfp;
     ngx_http_core_main_conf_t   *cmcf;
-#if (NGX_PCRE)
-    ngx_http_core_loc_conf_t   **clcfp;
-#endif
 
     ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
     if (ctx == NULL) {
@@ -1644,10 +1687,11 @@ ngx_http_core_server(ngx_conf_t *cf, ngx
     ngx_sort(cscf->locations.elts, (size_t) cscf->locations.nelts,
              sizeof(ngx_http_core_loc_conf_t *), ngx_http_core_cmp_locations);
 
+    clcfp = cscf->locations.elts;
+
 #if (NGX_PCRE)
 
     cscf->regex_start = cscf->locations.nelts;
-    clcfp = cscf->locations.elts;
 
     for (i = 0; i < cscf->locations.nelts; i++) {
         if (clcfp[i]->regex) {
@@ -1658,6 +1702,15 @@ ngx_http_core_server(ngx_conf_t *cf, ngx
 
 #endif
 
+    cscf->named_start = cscf->locations.nelts;
+
+    for (i = 0; i < cscf->locations.nelts; i++) {
+        if (clcfp[i]->named) {
+            cscf->named_start = i;
+            break;
+        }
+    }
+
     return rv;
 }
 
@@ -1758,7 +1811,12 @@ ngx_http_core_location(ngx_conf_t *cf, n
         }
 
     } else {
+
         clcf->name = value[1];
+
+        if (value[1].data[0] == '@') {
+            clcf->named = 1;
+        }
     }
 
     pclcf = pctx->loc_conf[ngx_http_core_module.ctx_index];
@@ -1784,6 +1842,14 @@ ngx_http_core_location(ngx_conf_t *cf, n
             return NGX_CONF_ERROR;
         }
 
+        if (pclcf->named) {
+            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                               "location \"%V\" could not be inside "
+                               "the named location \"%V\"",
+                               &clcf->name, &pclcf->name);
+            return NGX_CONF_ERROR;
+        }
+
 #if (NGX_PCRE)
         if (clcf->regex == NULL
             && ngx_strncmp(clcf->name.data, pclcf->name.data, pclcf->name.len)
@@ -1861,6 +1927,20 @@ ngx_http_core_cmp_locations(const void *
     first = *(ngx_http_core_loc_conf_t **) one;
     second = *(ngx_http_core_loc_conf_t **) two;
 
+    if (first->named && !second->named) {
+        /* shift named locations to the end */
+        return 1;
+    }
+
+    if (!first->named && second->named) {
+        /* shift named locations to the end */
+        return -1;
+    }
+
+    if (first->named && second->named) {
+        return ngx_strcmp(first->name.data, second->name.data);
+    }
+
     if (first->noname && !second->noname) {
         /* shift no named locations to the end */
         return 1;
@@ -1947,7 +2027,7 @@ ngx_http_core_type(ngx_conf_t *cf, ngx_c
     if (ngx_strcmp(value[0].data, "include") == 0) {
         file = value[1];
 
-        if (ngx_conf_full_name(cf->cycle, &file) == NGX_ERROR){
+        if (ngx_conf_full_name(cf->cycle, &file, 1) == NGX_ERROR){
             return NGX_CONF_ERROR;
         }
 
@@ -2290,7 +2370,7 @@ ngx_http_core_merge_loc_conf(ngx_conf_t 
             conf->root.len = sizeof("html") - 1;
             conf->root.data = (u_char *) "html";
 
-            if (ngx_conf_full_name(cf->cycle, &conf->root) == NGX_ERROR) {
+            if (ngx_conf_full_name(cf->cycle, &conf->root, 0) == NGX_ERROR) {
                 return NGX_CONF_ERROR;
             }
         }
@@ -2706,6 +2786,14 @@ ngx_http_core_root(ngx_conf_t *cf, ngx_c
         return NGX_CONF_ERROR;
     }
 
+    if (lcf->named && alias) {
+        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                           "the \"alias\" directive may not be used "
+                           "inside named location");
+
+        return NGX_CONF_ERROR;
+    }
+
 #if (NGX_PCRE)
 
     if (lcf->regex && alias) {
@@ -2739,7 +2827,7 @@ ngx_http_core_root(ngx_conf_t *cf, ngx_c
     }
 
     if (lcf->root.data[0] != '$') {
-        if (ngx_conf_full_name(cf->cycle, &lcf->root) == NGX_ERROR) {
+        if (ngx_conf_full_name(cf->cycle, &lcf->root, 0) == NGX_ERROR) {
             return NGX_CONF_ERROR;
         }
     }