Patch is to be merged with next.
This is basically a copy from grpc proxy.

 src/http/modules/ngx_http_proxy_module.c |  67 ++++++++++++++++++++++++++++++++
 1 files changed, 67 insertions(+), 0 deletions(-)


# HG changeset patch
# User Vladimir Khomutov <v...@wbsrv.ru>
# Date 1703082897 -10800
#      Wed Dec 20 17:34:57 2023 +0300
# Node ID fcbbdbc00cbf51dc54f6da114e12ba5ec0f278cc
# Parent  183d5a20c159a380d9a7562f3188d91aea465ab7
Added host/host_set logic to proxy module.

Patch is to be merged with next.
This is basically a copy from grpc proxy.

diff --git a/src/http/modules/ngx_http_proxy_module.c b/src/http/modules/ngx_http_proxy_module.c
--- a/src/http/modules/ngx_http_proxy_module.c
+++ b/src/http/modules/ngx_http_proxy_module.c
@@ -1,5 +1,6 @@
 
 /*
+ * Copyright (C) 2023 Web Server LLC
  * Copyright (C) Igor Sysoev
  * Copyright (C) Nginx, Inc.
  */
@@ -126,6 +127,11 @@ typedef struct {
     ngx_str_t                      ssl_crl;
     ngx_array_t                   *ssl_conf_commands;
 #endif
+
+#if (NGX_HTTP_V3)
+    ngx_str_t                      host;
+    ngx_uint_t                     host_set;
+#endif
 } ngx_http_proxy_loc_conf_t;
 
 
@@ -138,6 +144,10 @@ typedef struct {
     ngx_chain_t                   *free;
     ngx_chain_t                   *busy;
 
+#if (NGX_HTTP_V3)
+    ngx_str_t                      host;
+#endif
+
     unsigned                       head:1;
     unsigned                       internal_chunked:1;
     unsigned                       header_sent:1;
@@ -958,6 +968,9 @@ ngx_http_proxy_handler(ngx_http_request_
     u = r->upstream;
 
     if (plcf->proxy_lengths == NULL) {
+#if (NGX_HTTP_V3)
+        ctx->host = plcf->host;
+#endif
         ctx->vars = plcf->vars;
         u->schema = plcf->vars.schema;
 #if (NGX_HTTP_SSL)
@@ -1128,6 +1141,22 @@ ngx_http_proxy_eval(ngx_http_request_t *
     u->resolved->port = (in_port_t) (url.no_port ? port : url.port);
     u->resolved->no_port = url.no_port;
 
+#if (NGX_HTTP_V3)
+    if (url.family != AF_UNIX) {
+
+        if (url.no_port) {
+            ctx->host = url.host;
+
+        } else {
+            ctx->host.len = url.host.len + 1 + url.port_text.len;
+            ctx->host.data = url.host.data;
+        }
+
+    } else {
+        ngx_str_set(&ctx->host, "localhost");
+    }
+#endif
+
     return NGX_OK;
 }
 
@@ -3351,6 +3380,9 @@ ngx_http_proxy_create_loc_conf(ngx_conf_
      *     conf->ssl_ciphers = { 0, NULL };
      *     conf->ssl_trusted_certificate = { 0, NULL };
      *     conf->ssl_crl = { 0, NULL };
+     *
+     *     conf->host = { 0, NULL };
+     *     conf->host_set = 0;
      */
 
     conf->upstream.store = NGX_CONF_UNSET;
@@ -3859,6 +3891,9 @@ ngx_http_proxy_merge_loc_conf(ngx_conf_t
         conf->upstream.upstream = prev->upstream.upstream;
         conf->location = prev->location;
         conf->vars = prev->vars;
+#if (NGX_HTTP_V3)
+        conf->host = prev->host;
+#endif
 
         conf->proxy_lengths = prev->proxy_lengths;
         conf->proxy_values = prev->proxy_values;
@@ -3905,6 +3940,10 @@ ngx_http_proxy_merge_loc_conf(ngx_conf_t
 #if (NGX_HTTP_CACHE)
         conf->headers_cache = prev->headers_cache;
 #endif
+
+#if (NGX_HTTP_V3)
+        conf->host_set = prev->host_set;
+#endif
     }
 
     rc = ngx_http_proxy_init_headers(cf, conf, &conf->headers,
@@ -3937,6 +3976,10 @@ ngx_http_proxy_merge_loc_conf(ngx_conf_t
 #if (NGX_HTTP_CACHE)
         prev->headers_cache = conf->headers_cache;
 #endif
+
+#if (NGX_HTTP_V3)
+        conf->host_set = prev->host_set;
+#endif
     }
 
     return NGX_CONF_OK;
@@ -3989,6 +4032,14 @@ ngx_http_proxy_init_headers(ngx_conf_t *
         src = conf->headers_source->elts;
         for (i = 0; i < conf->headers_source->nelts; i++) {
 
+#if (NGX_HTTP_V3)
+            if (src[i].key.len == 4
+                && ngx_strncasecmp(src[i].key.data, (u_char *) "Host", 4) == 0)
+            {
+                conf->host_set = 1;
+            }
+#endif
+
             s = ngx_array_push(&headers_merged);
             if (s == NULL) {
                 return NGX_ERROR;
@@ -4203,6 +4254,22 @@ ngx_http_proxy_pass(ngx_conf_t *cf, ngx_
     plcf->vars.schema.data = url->data;
     plcf->vars.key_start = plcf->vars.schema;
 
+#if (NGX_HTTP_V3)
+    if (u.family != AF_UNIX) {
+
+        if (u.no_port) {
+            plcf->host = u.host;
+
+        } else {
+            plcf->host.len = u.host.len + 1 + u.port_text.len;
+            plcf->host.data = u.host.data;
+        }
+
+    } else {
+        ngx_str_set(&plcf->host, "localhost");
+    }
+#endif
+
     ngx_http_proxy_set_vars(&u, &plcf->vars);
 
     plcf->location = clcf->name;
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
https://mailman.nginx.org/mailman/listinfo/nginx-devel

Reply via email to