[nginx] Referer: "server_names" parsing deferred to merge phase.

Sergey Kandaurov pluknet at nginx.com
Thu Aug 29 18:37:57 UTC 2013


details:   http://hg.nginx.org/nginx/rev/a2c772963b04
branches:  
changeset: 5351:a2c772963b04
user:      Sergey Kandaurov <pluknet at nginx.com>
date:      Thu Aug 29 22:35:27 2013 +0400
description:
Referer: "server_names" parsing deferred to merge phase.

This allows to approach "server_name" values specified below the
"valid_referers" directive when used within the "server_names" parameter, e.g.:

    server_name  example.org;
    valid_referers server_names;
    server_name  example.com;

As a bonus, this fixes bogus error with "server_names" specified several times.

diffstat:

 src/http/modules/ngx_http_referer_module.c |  78 +++++++++++++++++------------
 1 files changed, 45 insertions(+), 33 deletions(-)

diffs (139 lines):

diff -r 8220e393c241 -r a2c772963b04 src/http/modules/ngx_http_referer_module.c
--- a/src/http/modules/ngx_http_referer_module.c	Thu Aug 29 22:35:26 2013 +0400
+++ b/src/http/modules/ngx_http_referer_module.c	Thu Aug 29 22:35:27 2013 +0400
@@ -23,6 +23,7 @@ typedef struct {
 
     ngx_flag_t               no_referer;
     ngx_flag_t               blocked_referer;
+    ngx_flag_t               server_names;
 
     ngx_hash_keys_arrays_t  *keys;
 
@@ -272,6 +273,14 @@ ngx_http_referer_create_conf(ngx_conf_t 
         return NULL;
     }
 
+    /*
+     * set by ngx_pcalloc():
+     *
+     *     conf->hash = { NULL };
+     *     conf->server_names = 0;
+     *     conf->keys = NULL;
+     */
+
 #if (NGX_PCRE)
     conf->regex = NGX_CONF_UNSET_PTR;
     conf->server_name_regex = NGX_CONF_UNSET_PTR;
@@ -292,7 +301,10 @@ ngx_http_referer_merge_conf(ngx_conf_t *
     ngx_http_referer_conf_t *prev = parent;
     ngx_http_referer_conf_t *conf = child;
 
-    ngx_hash_init_t  hash;
+    ngx_uint_t                 n;
+    ngx_hash_init_t            hash;
+    ngx_http_server_name_t    *sn;
+    ngx_http_core_srv_conf_t  *cscf;
 
     if (conf->keys == NULL) {
         conf->hash = prev->hash;
@@ -312,6 +324,33 @@ ngx_http_referer_merge_conf(ngx_conf_t *
         return NGX_CONF_OK;
     }
 
+    if (conf->server_names == 1) {
+        cscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_core_module);
+
+        sn = cscf->server_names.elts;
+        for (n = 0; n < cscf->server_names.nelts; n++) {
+
+#if (NGX_PCRE)
+            if (sn[n].regex) {
+
+                if (ngx_http_add_regex_server_name(cf, conf, sn[n].regex)
+                    != NGX_OK)
+                {
+                    return NGX_CONF_ERROR;
+                }
+
+                continue;
+            }
+#endif
+
+            if (ngx_http_add_referer(cf, conf->keys, &sn[n].name, NULL)
+                != NGX_OK)
+            {
+                return NGX_CONF_ERROR;
+            }
+        }
+    }
+
     if ((conf->no_referer == 1 || conf->blocked_referer == 1)
         && conf->keys->keys.nelts == 0
         && conf->keys->dns_wc_head.nelts == 0
@@ -415,10 +454,8 @@ ngx_http_valid_referers(ngx_conf_t *cf, 
 
     u_char                    *p;
     ngx_str_t                 *value, uri, name;
-    ngx_uint_t                 i, n;
+    ngx_uint_t                 i;
     ngx_http_variable_t       *var;
-    ngx_http_server_name_t    *sn;
-    ngx_http_core_srv_conf_t  *cscf;
 
     ngx_str_set(&name, "invalid_referer");
 
@@ -462,35 +499,8 @@ ngx_http_valid_referers(ngx_conf_t *cf, 
             continue;
         }
 
-        ngx_str_null(&uri);
-
         if (ngx_strcmp(value[i].data, "server_names") == 0) {
-
-            cscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_core_module);
-
-            sn = cscf->server_names.elts;
-            for (n = 0; n < cscf->server_names.nelts; n++) {
-
-#if (NGX_PCRE)
-                if (sn[n].regex) {
-
-                    if (ngx_http_add_regex_server_name(cf, rlcf, sn[n].regex)
-                        != NGX_OK)
-                    {
-                        return NGX_CONF_ERROR;
-                    }
-
-                    continue;
-                }
-#endif
-
-                if (ngx_http_add_referer(cf, rlcf->keys, &sn[n].name, &uri)
-                    != NGX_OK)
-                {
-                    return NGX_CONF_ERROR;
-                }
-            }
-
+            rlcf->server_names = 1;
             continue;
         }
 
@@ -502,6 +512,8 @@ ngx_http_valid_referers(ngx_conf_t *cf, 
             continue;
         }
 
+        ngx_str_null(&uri);
+
         p = (u_char *) ngx_strchr(value[i].data, '/');
 
         if (p) {
@@ -526,7 +538,7 @@ ngx_http_add_referer(ngx_conf_t *cf, ngx
     ngx_int_t   rc;
     ngx_str_t  *u;
 
-    if (uri->len == 0) {
+    if (uri == NULL || uri->len == 0) {
         u = NGX_HTTP_REFERER_NO_URI_PART;
 
     } else {



More information about the nginx-devel mailing list