# HG changeset patch
# User Aleksei Bavshin <a.bavshin@f5.com>
# Date 1667328585 25200
#      Tue Nov 01 11:49:45 2022 -0700
# Node ID 4e88d4dba58f80806ea86c4ea38e356381aeb887
# Parent  17d6a537fb1bb587e4de22961bf5be5f0c648fa8
HTTP: add internal_redirect directive.

The directive restarts the request processing with another URI or in a named
location by making an internal redirect.  The behavior is quite similar to the
last parameter of try_files directive, with the following important
differences:

 * no filesystem operations will be performed
 * the parameter is not affected by the root or alias directives
 * no action is taken when the parameter evaluates to an empty value.

The merge handler is intentionally omitted, as the directive is not meant to be
inheritable.

diff --git a/auto/modules b/auto/modules
--- a/auto/modules
+++ b/auto/modules
@@ -518,6 +518,17 @@ if [ $HTTP = YES ]; then
         . auto/module
     fi
 
+    if [ $HTTP_INTERNAL_REDIRECT = YES ]; then
+        ngx_module_name=ngx_http_internal_redirect_module
+        ngx_module_incs=
+        ngx_module_deps=
+        ngx_module_srcs=src/http/modules/ngx_http_internal_redirect_module.c
+        ngx_module_libs=
+        ngx_module_link=$HTTP_INTERNAL_REDIRECT
+
+        . auto/module
+    fi
+
     if [ $HTTP_AUTH_REQUEST = YES ]; then
         ngx_module_name=ngx_http_auth_request_module
         ngx_module_incs=
diff --git a/auto/options b/auto/options
--- a/auto/options
+++ b/auto/options
@@ -74,6 +74,7 @@ HTTP_USERID=YES
 HTTP_SLICE=NO
 HTTP_AUTOINDEX=YES
 HTTP_RANDOM_INDEX=NO
+HTTP_INTERNAL_REDIRECT=YES
 HTTP_STATUS=NO
 HTTP_GEO=YES
 HTTP_GEOIP=NO
@@ -258,6 +259,8 @@ do
         --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_internal_redirect_module)
+                                         HTTP_INTERNAL_REDIRECT=NO  ;;
         --without-http_status_module)    HTTP_STATUS=NO             ;;
         --without-http_geo_module)       HTTP_GEO=NO                ;;
         --without-http_map_module)       HTTP_MAP=NO                ;;
diff --git a/src/http/modules/ngx_http_internal_redirect_module.c b/src/http/modules/ngx_http_internal_redirect_module.c
new file mode 100644
--- /dev/null
+++ b/src/http/modules/ngx_http_internal_redirect_module.c
@@ -0,0 +1,142 @@
+
+/*
+ * Copyright (C) Aleksei Bavshin
+ * Copyright (C) Nginx, Inc.
+ */
+
+#include <ngx_config.h>
+#include <ngx_core.h>
+#include <ngx_http.h>
+
+
+typedef struct {
+    ngx_http_complex_value_t  *uri;
+} ngx_http_internal_redirect_loc_conf_t;
+
+
+static ngx_int_t ngx_http_internal_redirect_handler(ngx_http_request_t *r);
+static void *ngx_http_internal_redirect_create_conf(ngx_conf_t *cf);
+static ngx_int_t ngx_http_internal_redirect_init(ngx_conf_t *cf);
+
+
+static ngx_command_t  ngx_http_internal_redirect_commands[] = {
+
+    { ngx_string("internal_redirect"),
+      NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+      ngx_http_set_complex_value_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_internal_redirect_loc_conf_t, uri),
+      NULL },
+
+      ngx_null_command
+};
+
+
+static ngx_http_module_t  ngx_http_internal_redirect_module_ctx = {
+    NULL,                                   /* preconfiguration */
+    ngx_http_internal_redirect_init,        /* postconfiguration */
+
+    NULL,                                   /* create main configuration */
+    NULL,                                   /* init main configuration */
+    NULL,                                   /* create server configuration */
+    NULL,                                   /* merge server configuration */
+
+    ngx_http_internal_redirect_create_conf, /* create location configuration */
+    NULL                                    /* merge location configuration */
+};
+
+
+ngx_module_t  ngx_http_internal_redirect_module = {
+    NGX_MODULE_V1,
+    &ngx_http_internal_redirect_module_ctx, /* module context */
+    ngx_http_internal_redirect_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_internal_redirect_handler(ngx_http_request_t *r)
+{
+    ngx_str_t                               uri, args;
+    ngx_http_internal_redirect_loc_conf_t  *ilcf;
+
+    ilcf = ngx_http_get_module_loc_conf(r, ngx_http_internal_redirect_module);
+
+    if (ilcf->uri == NULL) {
+        return NGX_DECLINED;
+    }
+
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                   "internal redirect handler");
+
+    if (ngx_http_complex_value(r, ilcf->uri, &uri) != NGX_OK) {
+        return NGX_ERROR;
+    }
+
+    if (uri.len == 0) {
+        return NGX_DECLINED;
+
+    } else if (uri.data[0] == '@') {
+        (void) ngx_http_named_location(r, &uri);
+
+    } else if (uri.data[0] == '/') {
+        ngx_http_split_args(r, &uri, &args);
+        (void) ngx_http_internal_redirect(r, &uri, &args);
+
+    } else {
+        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+                      "invalid internal redirect URI: \"%V\"", &uri);
+        ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
+        return NGX_DONE;
+    }
+
+    ngx_http_finalize_request(r, NGX_DONE);
+    return NGX_DONE;
+}
+
+
+static void *
+ngx_http_internal_redirect_create_conf(ngx_conf_t *cf)
+{
+    ngx_http_internal_redirect_loc_conf_t  *ilcf;
+
+    ilcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_internal_redirect_loc_conf_t));
+    if (ilcf == NULL) {
+        return NULL;
+    }
+
+    /*
+     * set by ngx_pcalloc():
+     *
+     *     ilcf->uri = NULL;
+     */
+
+    return ilcf;
+}
+
+
+static ngx_int_t
+ngx_http_internal_redirect_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_internal_redirect_handler;
+
+    return NGX_OK;
+}
