[nginx] Added the $proxy_protocol_port variable.

Dmitry Volyntsev xeioex at nginx.com
Mon May 23 15:45:31 UTC 2016


details:   http://hg.nginx.org/nginx/rev/28c76d9d75b7
branches:  
changeset: 6561:28c76d9d75b7
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon May 23 18:44:21 2016 +0300
description:
Added the $proxy_protocol_port variable.

diffstat:

 src/core/ngx_connection.h     |   1 +
 src/core/ngx_proxy_protocol.c |  41 +++++++++++++++++++++++++++++++++++++----
 src/http/ngx_http_variables.c |  31 +++++++++++++++++++++++++++++++
 3 files changed, 69 insertions(+), 4 deletions(-)

diffs (124 lines):

diff -r c90cf79d0e1d -r 28c76d9d75b7 src/core/ngx_connection.h
--- a/src/core/ngx_connection.h	Mon May 23 16:37:28 2016 +0300
+++ b/src/core/ngx_connection.h	Mon May 23 18:44:21 2016 +0300
@@ -149,6 +149,7 @@ struct ngx_connection_s {
     ngx_str_t           addr_text;
 
     ngx_str_t           proxy_protocol_addr;
+    in_port_t           proxy_protocol_port;
 
 #if (NGX_SSL)
     ngx_ssl_connection_t  *ssl;
diff -r c90cf79d0e1d -r 28c76d9d75b7 src/core/ngx_proxy_protocol.c
--- a/src/core/ngx_proxy_protocol.c	Mon May 23 16:37:28 2016 +0300
+++ b/src/core/ngx_proxy_protocol.c	Mon May 23 18:44:21 2016 +0300
@@ -12,8 +12,9 @@
 u_char *
 ngx_proxy_protocol_read(ngx_connection_t *c, u_char *buf, u_char *last)
 {
-    size_t  len;
-    u_char  ch, *p, *addr;
+    size_t     len;
+    u_char     ch, *p, *addr, *port;
+    ngx_int_t  n;
 
     p = buf;
     len = last - buf;
@@ -71,8 +72,40 @@ ngx_proxy_protocol_read(ngx_connection_t
     ngx_memcpy(c->proxy_protocol_addr.data, addr, len);
     c->proxy_protocol_addr.len = len;
 
-    ngx_log_debug1(NGX_LOG_DEBUG_CORE, c->log, 0,
-                   "PROXY protocol address: \"%V\"", &c->proxy_protocol_addr);
+    for ( ;; ) {
+        if (p == last) {
+            goto invalid;
+        }
+
+        if (*p++ == ' ') {
+            break;
+        }
+    }
+
+    port = p;
+
+    for ( ;; ) {
+        if (p == last) {
+            goto invalid;
+        }
+
+        if (*p++ == ' ') {
+            break;
+        }
+    }
+
+    len = p - port - 1;
+
+    n = ngx_atoi(port, len);
+
+    if (n < 0 || n > 65535) {
+        goto invalid;
+    }
+
+    c->proxy_protocol_port = (in_port_t) n;
+
+    ngx_log_debug2(NGX_LOG_DEBUG_CORE, c->log, 0,
+                   "PROXY protocol address: %V %i", &c->proxy_protocol_addr, n);
 
 skip:
 
diff -r c90cf79d0e1d -r 28c76d9d75b7 src/http/ngx_http_variables.c
--- a/src/http/ngx_http_variables.c	Mon May 23 16:37:28 2016 +0300
+++ b/src/http/ngx_http_variables.c	Mon May 23 18:44:21 2016 +0300
@@ -58,6 +58,8 @@ static ngx_int_t ngx_http_variable_remot
     ngx_http_variable_value_t *v, uintptr_t data);
 static ngx_int_t ngx_http_variable_proxy_protocol_addr(ngx_http_request_t *r,
     ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_variable_proxy_protocol_port(ngx_http_request_t *r,
+    ngx_http_variable_value_t *v, uintptr_t data);
 static ngx_int_t ngx_http_variable_server_addr(ngx_http_request_t *r,
     ngx_http_variable_value_t *v, uintptr_t data);
 static ngx_int_t ngx_http_variable_server_port(ngx_http_request_t *r,
@@ -194,6 +196,9 @@ static ngx_http_variable_t  ngx_http_cor
     { ngx_string("proxy_protocol_addr"), NULL,
       ngx_http_variable_proxy_protocol_addr, 0, 0, 0 },
 
+    { ngx_string("proxy_protocol_port"), NULL,
+      ngx_http_variable_proxy_protocol_port, 0, 0, 0 },
+
     { ngx_string("server_addr"), NULL, ngx_http_variable_server_addr, 0, 0, 0 },
 
     { ngx_string("server_port"), NULL, ngx_http_variable_server_port, 0, 0, 0 },
@@ -1256,6 +1261,32 @@ ngx_http_variable_proxy_protocol_addr(ng
 
 
 static ngx_int_t
+ngx_http_variable_proxy_protocol_port(ngx_http_request_t *r,
+    ngx_http_variable_value_t *v, uintptr_t data)
+{
+    ngx_uint_t  port;
+
+    v->len = 0;
+    v->valid = 1;
+    v->no_cacheable = 0;
+    v->not_found = 0;
+
+    v->data = ngx_pnalloc(r->pool, sizeof("65535") - 1);
+    if (v->data == NULL) {
+        return NGX_ERROR;
+    }
+
+    port = r->connection->proxy_protocol_port;
+
+    if (port > 0 && port < 65536) {
+        v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
+    }
+
+    return NGX_OK;
+}
+
+
+static ngx_int_t
 ngx_http_variable_server_addr(ngx_http_request_t *r,
     ngx_http_variable_value_t *v, uintptr_t data)
 {



More information about the nginx-devel mailing list