changeset 7079:7564a919d333

Referer: fixed $invalid_referer. The variable was considered non-existent in the absence of any valid_referers directives. Given the following config snippet, location / { return 200 $invalid_referer; } location /referer { valid_referers server_names; } "location /" should work identically and independently on other "location /referer". The fix is to always add the $invalid_referer variable as long as the module is compiled in, as is done by other modules.
author Ruslan Ermilov <ru@nginx.com>
date Fri, 04 Aug 2017 08:01:55 +0300
parents 1eb753aa8e5e
children ac120e797d28
files src/http/modules/ngx_http_referer_module.c
diffstat 1 files changed, 25 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/http/modules/ngx_http_referer_module.c
+++ b/src/http/modules/ngx_http_referer_module.c
@@ -32,6 +32,7 @@ typedef struct {
 } ngx_http_referer_conf_t;
 
 
+static ngx_int_t ngx_http_referer_add_variables(ngx_conf_t *cf);
 static void * ngx_http_referer_create_conf(ngx_conf_t *cf);
 static char * ngx_http_referer_merge_conf(ngx_conf_t *cf, void *parent,
     void *child);
@@ -77,7 +78,7 @@ static ngx_command_t  ngx_http_referer_c
 
 
 static ngx_http_module_t  ngx_http_referer_module_ctx = {
-    NULL,                                  /* preconfiguration */
+    ngx_http_referer_add_variables,        /* preconfiguration */
     NULL,                                  /* postconfiguration */
 
     NULL,                                  /* create main configuration */
@@ -107,6 +108,9 @@ ngx_module_t  ngx_http_referer_module = 
 };
 
 
+static ngx_str_t  ngx_http_invalid_referer_name = ngx_string("invalid_referer");
+
+
 static ngx_int_t
 ngx_http_referer_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
     uintptr_t data)
@@ -263,6 +267,23 @@ valid:
 }
 
 
+static ngx_int_t
+ngx_http_referer_add_variables(ngx_conf_t *cf)
+{
+    ngx_http_variable_t  *var;
+
+    var = ngx_http_add_variable(cf, &ngx_http_invalid_referer_name,
+                                NGX_HTTP_VAR_CHANGEABLE);
+    if (var == NULL) {
+        return NGX_ERROR;
+    }
+
+    var->get_handler = ngx_http_referer_variable;
+
+    return NGX_OK;
+}
+
+
 static void *
 ngx_http_referer_create_conf(ngx_conf_t *cf)
 {
@@ -452,19 +473,9 @@ ngx_http_valid_referers(ngx_conf_t *cf, 
 {
     ngx_http_referer_conf_t  *rlcf = conf;
 
-    u_char                    *p;
-    ngx_str_t                 *value, uri, name;
-    ngx_uint_t                 i;
-    ngx_http_variable_t       *var;
-
-    ngx_str_set(&name, "invalid_referer");
-
-    var = ngx_http_add_variable(cf, &name, NGX_HTTP_VAR_CHANGEABLE);
-    if (var == NULL) {
-        return NGX_CONF_ERROR;
-    }
-
-    var->get_handler = ngx_http_referer_variable;
+    u_char      *p;
+    ngx_str_t   *value, uri;
+    ngx_uint_t   i;
 
     if (rlcf->keys == NULL) {
         rlcf->keys = ngx_pcalloc(cf->temp_pool, sizeof(ngx_hash_keys_arrays_t));