[nginx] Mirror module.

Roman Arutyunyan arut at nginx.com
Thu Jul 20 17:03:39 UTC 2017


details:   http://hg.nginx.org/nginx/rev/0bb747b2d7cb
branches:  
changeset: 7072:0bb747b2d7cb
user:      Roman Arutyunyan <arut at nginx.com>
date:      Thu Jul 20 08:50:49 2017 +0300
description:
Mirror module.

diffstat:

 auto/modules                              |   11 +
 auto/options                              |    3 +
 src/http/modules/ngx_http_mirror_module.c |  223 ++++++++++++++++++++++++++++++
 3 files changed, 237 insertions(+), 0 deletions(-)

diffs (275 lines):

diff -r cce6936ed2f4 -r 0bb747b2d7cb auto/modules
--- a/auto/modules	Thu Jul 20 15:51:11 2017 +0300
+++ b/auto/modules	Thu Jul 20 08:50:49 2017 +0300
@@ -506,6 +506,17 @@ if [ $HTTP = YES ]; then
         . auto/module
     fi
 
+    if [ $HTTP_MIRROR = YES ]; then
+        ngx_module_name=ngx_http_mirror_module
+        ngx_module_incs=
+        ngx_module_deps=
+        ngx_module_srcs=src/http/modules/ngx_http_mirror_module.c
+        ngx_module_libs=
+        ngx_module_link=$HTTP_MIRROR
+
+        . auto/module
+    fi
+
     if :; then
         ngx_module_name=ngx_http_try_files_module
         ngx_module_incs=
diff -r cce6936ed2f4 -r 0bb747b2d7cb auto/options
--- a/auto/options	Thu Jul 20 15:51:11 2017 +0300
+++ b/auto/options	Thu Jul 20 08:50:49 2017 +0300
@@ -70,6 +70,7 @@ HTTP_DAV=NO
 HTTP_ACCESS=YES
 HTTP_AUTH_BASIC=YES
 HTTP_AUTH_REQUEST=NO
+HTTP_MIRROR=YES
 HTTP_USERID=YES
 HTTP_SLICE=NO
 HTTP_AUTOINDEX=YES
@@ -249,6 +250,7 @@ do
         --without-http_userid_module)    HTTP_USERID=NO             ;;
         --without-http_access_module)    HTTP_ACCESS=NO             ;;
         --without-http_auth_basic_module) HTTP_AUTH_BASIC=NO        ;;
+        --without-http_mirror_module)    HTTP_MIRROR=NO             ;;
         --without-http_autoindex_module) HTTP_AUTOINDEX=NO          ;;
         --without-http_status_module)    HTTP_STATUS=NO             ;;
         --without-http_geo_module)       HTTP_GEO=NO                ;;
@@ -458,6 +460,7 @@ cat << END
   --without-http_userid_module       disable ngx_http_userid_module
   --without-http_access_module       disable ngx_http_access_module
   --without-http_auth_basic_module   disable ngx_http_auth_basic_module
+  --without-http_mirror_module       disable ngx_http_mirror_module
   --without-http_autoindex_module    disable ngx_http_autoindex_module
   --without-http_geo_module          disable ngx_http_geo_module
   --without-http_map_module          disable ngx_http_map_module
diff -r cce6936ed2f4 -r 0bb747b2d7cb src/http/modules/ngx_http_mirror_module.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/http/modules/ngx_http_mirror_module.c	Thu Jul 20 08:50:49 2017 +0300
@@ -0,0 +1,223 @@
+
+/*
+ * Copyright (C) Roman Arutyunyan
+ * Copyright (C) Nginx, Inc.
+ */
+
+
+#include <ngx_config.h>
+#include <ngx_core.h>
+#include <ngx_http.h>
+
+
+typedef struct {
+    ngx_array_t  *mirror;
+    ngx_flag_t    request_body;
+} ngx_http_mirror_loc_conf_t;
+
+
+typedef struct {
+    ngx_int_t     status;
+} ngx_http_mirror_ctx_t;
+
+
+static ngx_int_t ngx_http_mirror_handler(ngx_http_request_t *r);
+static void ngx_http_mirror_body_handler(ngx_http_request_t *r);
+static ngx_int_t ngx_http_mirror_handler_internal(ngx_http_request_t *r);
+static void *ngx_http_mirror_create_loc_conf(ngx_conf_t *cf);
+static char *ngx_http_mirror_merge_loc_conf(ngx_conf_t *cf, void *parent,
+    void *child);
+static ngx_int_t ngx_http_mirror_init(ngx_conf_t *cf);
+
+
+static ngx_command_t  ngx_http_mirror_commands[] = {
+
+    { ngx_string("mirror"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+      ngx_conf_set_str_array_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_mirror_loc_conf_t, mirror),
+      NULL },
+
+    { ngx_string("mirror_request_body"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
+      ngx_conf_set_flag_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_mirror_loc_conf_t, request_body),
+      NULL },
+
+      ngx_null_command
+};
+
+
+static ngx_http_module_t  ngx_http_mirror_module_ctx = {
+    NULL,                                  /* preconfiguration */
+    ngx_http_mirror_init,                  /* postconfiguration */
+
+    NULL,                                  /* create main configuration */
+    NULL,                                  /* init main configuration */
+
+    NULL,                                  /* create server configuration */
+    NULL,                                  /* merge server configuration */
+
+    ngx_http_mirror_create_loc_conf,       /* create location configuration */
+    ngx_http_mirror_merge_loc_conf         /* merge location configuration */
+};
+
+
+ngx_module_t  ngx_http_mirror_module = {
+    NGX_MODULE_V1,
+    &ngx_http_mirror_module_ctx,           /* module context */
+    ngx_http_mirror_commands,              /* module directives */
+    NGX_HTTP_MODULE,                       /* module type */
+    NULL,                                  /* init master */
+    NULL,                                  /* init module */
+    NULL,                                  /* init process */
+    NULL,                                  /* init thread */
+    NULL,                                  /* exit thread */
+    NULL,                                  /* exit process */
+    NULL,                                  /* exit master */
+    NGX_MODULE_V1_PADDING
+};
+
+
+static ngx_int_t
+ngx_http_mirror_handler(ngx_http_request_t *r)
+{
+    ngx_int_t                    rc;
+    ngx_http_mirror_ctx_t       *ctx;
+    ngx_http_mirror_loc_conf_t  *mlcf;
+
+    if (r != r->main) {
+        return NGX_DECLINED;
+    }
+
+    mlcf = ngx_http_get_module_loc_conf(r, ngx_http_mirror_module);
+
+    if (mlcf->mirror == NULL) {
+        return NGX_DECLINED;
+    }
+
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "mirror handler");
+
+    if (mlcf->request_body) {
+        ctx = ngx_http_get_module_ctx(r, ngx_http_mirror_module);
+
+        if (ctx) {
+            return ctx->status;
+        }
+
+        ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_mirror_ctx_t));
+        if (ctx == NULL) {
+            return NGX_ERROR;
+        }
+
+        ctx->status = NGX_DONE;
+
+        ngx_http_set_ctx(r, ctx, ngx_http_mirror_module);
+
+        rc = ngx_http_read_client_request_body(r, ngx_http_mirror_body_handler);
+        if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
+            return rc;
+        }
+
+        ngx_http_finalize_request(r, NGX_DONE);
+        return NGX_DONE;
+    }
+
+    return ngx_http_mirror_handler_internal(r);
+}
+
+
+static void
+ngx_http_mirror_body_handler(ngx_http_request_t *r)
+{
+    ngx_http_mirror_ctx_t  *ctx;
+
+    ctx = ngx_http_get_module_ctx(r, ngx_http_mirror_module);
+
+    ctx->status = ngx_http_mirror_handler_internal(r);
+
+    r->preserve_body = 1;
+
+    r->write_event_handler = ngx_http_core_run_phases;
+    ngx_http_core_run_phases(r);
+}
+
+
+static ngx_int_t
+ngx_http_mirror_handler_internal(ngx_http_request_t *r)
+{
+    ngx_str_t                   *name;
+    ngx_uint_t                   i;
+    ngx_http_request_t          *sr;
+    ngx_http_mirror_loc_conf_t  *mlcf;
+
+    mlcf = ngx_http_get_module_loc_conf(r, ngx_http_mirror_module);
+
+    name = mlcf->mirror->elts;
+
+    for (i = 0; i < mlcf->mirror->nelts; i++) {
+        if (ngx_http_subrequest(r, &name[i], &r->args, &sr, NULL,
+                                NGX_HTTP_SUBREQUEST_BACKGROUND)
+            != NGX_OK)
+        {
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
+        }
+
+        sr->header_only = 1;
+        sr->method = r->method;
+        sr->method_name = r->method_name;
+    }
+
+    return NGX_DECLINED;
+}
+
+
+static void *
+ngx_http_mirror_create_loc_conf(ngx_conf_t *cf)
+{
+    ngx_http_mirror_loc_conf_t  *mlcf;
+
+    mlcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_mirror_loc_conf_t));
+    if (mlcf == NULL) {
+        return NULL;
+    }
+
+    mlcf->mirror = NGX_CONF_UNSET_PTR;
+    mlcf->request_body = NGX_CONF_UNSET;
+
+    return mlcf;
+}
+
+
+static char *
+ngx_http_mirror_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
+{
+    ngx_http_mirror_loc_conf_t *prev = parent;
+    ngx_http_mirror_loc_conf_t *conf = child;
+
+    ngx_conf_merge_ptr_value(conf->mirror, prev->mirror, NULL);
+    ngx_conf_merge_value(conf->request_body, prev->request_body, 1);
+
+    return NGX_CONF_OK;
+}
+
+
+static ngx_int_t
+ngx_http_mirror_init(ngx_conf_t *cf)
+{
+    ngx_http_handler_pt        *h;
+    ngx_http_core_main_conf_t  *cmcf;
+
+    cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
+
+    h = ngx_array_push(&cmcf->phases[NGX_HTTP_PRECONTENT_PHASE].handlers);
+    if (h == NULL) {
+        return NGX_ERROR;
+    }
+
+    *h = ngx_http_mirror_handler;
+
+    return NGX_OK;
+}


More information about the nginx-devel mailing list