[PATCH] Mirror directive supports variable

一江春水,向东流 agile6v at agile6v.com
Fri Mar 13 15:29:51 UTC 2020


# HG changeset patch
# User agile6v <liuwei19 at xiaomi.com>
# Date 1584110606 -28800
#      Fri Mar 13 22:43:26 2020 +0800
# Node ID e4a0277cab79865fde6fefeed9374154449e6948
# Parent  4eb606b4a5b521603c23223cf8863f3999df743c
mirror directive supports variable.

diff -r 4eb606b4a5b5 -r e4a0277cab79 src/http/modules/ngx_http_mirror_module.c
--- a/src/http/modules/ngx_http_mirror_module.c    Tue Mar 03 18:04:21 2020 +0300
+++ b/src/http/modules/ngx_http_mirror_module.c    Fri Mar 13 22:43:26 2020 +0800
@@ -149,17 +149,28 @@
 static ngx_int_t
 ngx_http_mirror_handler_internal(ngx_http_request_t *r)
 {
-    ngx_str_t                   *name;
+    ngx_str_t                    val;
     ngx_uint_t                   i;
     ngx_http_request_t          *sr;
     ngx_http_mirror_loc_conf_t  *mlcf;
+    ngx_http_complex_value_t    *pcv;

     mlcf = ngx_http_get_module_loc_conf(r, ngx_http_mirror_module);

-    name = mlcf->mirror->elts;
+    pcv = mlcf->mirror->elts;

     for (i = 0; i < mlcf->mirror->nelts; i++) {
-        if (ngx_http_subrequest(r, &name[i], &r->args, &sr, NULL,
+        if (ngx_http_complex_value(r, &pcv[i], &val) != NGX_OK) {
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
+        }
+
+        if (val.len == 0
+            || (val.len == 3 && ngx_strncmp(val.data, "off", 3) == 0))
+        {
+            continue;
+        }
+
+        if (ngx_http_subrequest(r, &val, &r->args, &sr, NULL,
                                 NGX_HTTP_SUBREQUEST_BACKGROUND)
             != NGX_OK)
         {
@@ -208,9 +219,10 @@
 static char *
 ngx_http_mirror(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 {
-    ngx_http_mirror_loc_conf_t *mlcf = conf;
-
-    ngx_str_t  *value, *s;
+    ngx_http_mirror_loc_conf_t        *mlcf = conf;
+    ngx_http_complex_value_t          *pcv, cv;
+    ngx_http_compile_complex_value_t   ccv;
+    ngx_str_t                         *value;

     value = cf->args->elts;

@@ -227,19 +239,30 @@
         return "is duplicate";
     }

+    ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
+
+    ccv.cf = cf;
+    ccv.value = &value[1];
+    ccv.complex_value = &cv;
+
+    if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
+        return NGX_CONF_ERROR;
+    }
+
     if (mlcf->mirror == NGX_CONF_UNSET_PTR) {
-        mlcf->mirror = ngx_array_create(cf->pool, 4, sizeof(ngx_str_t));
+        mlcf->mirror = ngx_array_create(cf->pool, 4,
+                                sizeof(ngx_http_complex_value_t));
         if (mlcf->mirror == NULL) {
             return NGX_CONF_ERROR;
         }
     }

-    s = ngx_array_push(mlcf->mirror);
-    if (s == NULL) {
+    pcv = ngx_array_push(mlcf->mirror);
+    if (pcv == NULL) {
         return NGX_CONF_ERROR;
     }

-    *s = value[1];
+    *pcv = cv;

     return NGX_CONF_OK;
 }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20200313/d1253783/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: nginx.patch
Type: application/octet-stream
Size: 2800 bytes
Desc: not available
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20200313/d1253783/attachment.obj>


More information about the nginx-devel mailing list