[PATCH]add proxy_protocol_port variable for rfc6302

junpei yoshino junpei.yoshino at gmail.com
Tue Dec 8 00:35:32 UTC 2015


Hello,

I made merged patch.

# HG changeset patch
# User Junpei Yoshino <junpei.yoshino at gmail.com>
# Date 1449499172 -32400
#      Mon Dec 07 23:39:32 2015 +0900
# Node ID f4cd90a03eca5c330f51ac4ba2673e64348c622e
# Parent  29f35e60840b8eed2927dd3495ef2d8e524862f7
Http: add proxy_protocol_port variable for rfc6302

diff -r 29f35e60840b -r f4cd90a03eca src/core/ngx_connection.h
--- a/src/core/ngx_connection.h Mon Dec 07 16:30:48 2015 +0300
+++ b/src/core/ngx_connection.h Mon Dec 07 23:39:32 2015 +0900
@@ -146,6 +146,7 @@
     ngx_str_t           addr_text;

     ngx_str_t           proxy_protocol_addr;
+    ngx_int_t           proxy_protocol_port;

 #if (NGX_SSL)
     ngx_ssl_connection_t  *ssl;
diff -r 29f35e60840b -r f4cd90a03eca src/core/ngx_proxy_protocol.c
--- a/src/core/ngx_proxy_protocol.c Mon Dec 07 16:30:48 2015 +0300
+++ b/src/core/ngx_proxy_protocol.c Mon Dec 07 23:39:32 2015 +0900
@@ -13,7 +13,7 @@
 ngx_proxy_protocol_read(ngx_connection_t *c, u_char *buf, u_char *last)
 {
     size_t  len;
-    u_char  ch, *p, *addr;
+    u_char  ch, *p, *addr, *port;

     p = buf;
     len = last - buf;
@@ -71,8 +71,40 @@
     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;
+        }
+
+        ch = *p++;
+
+        if (ch == ' ') {
+            break;
+        }
+    }
+    port = p;
+    for ( ;; ) {
+        if (p == last) {
+            goto invalid;
+        }
+
+        ch = *p++;
+
+        if (ch == ' ') {
+            break;
+        }
+
+        if (ch < '0' || ch > '9')
+        {
+            goto invalid;
+        }
+    }
+    len = p - port - 1;
+    c->proxy_protocol_port = ngx_atoi(port,len);
+
+    ngx_log_debug2(NGX_LOG_DEBUG_CORE, c->log, 0,
+                   "PROXY protocol address: \"%V\", PROXY protocol
port: \"%d\"",
+                   &c->proxy_protocol_addr, c->proxy_protocol_port);

 skip:

diff -r 29f35e60840b -r f4cd90a03eca src/http/ngx_http_variables.c
--- a/src/http/ngx_http_variables.c Mon Dec 07 16:30:48 2015 +0300
+++ b/src/http/ngx_http_variables.c Mon Dec 07 23:39:32 2015 +0900
@@ -58,6 +58,8 @@
     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,
@@ -192,6 +194,9 @@
     { 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 },
@@ -1250,6 +1255,29 @@


 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_int_t port = r->connection->proxy_protocol_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;
+    }
+    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)
 {


On Tue, Dec 8, 2015 at 12:56 AM, junpei yoshino
<junpei.yoshino at gmail.com> wrote:
> Hello.
>
> I wrote additional patch.
>
> # HG changeset patch
> # User Junpei Yoshino <junpei.yoshino at gmail.com>
> # Date 1449499172 -32400
> #      Mon Dec 07 23:39:32 2015 +0900
> # Node ID e2984af905ff8cf523b22860620a9f3ff22d555a
> # Parent  59cadccedf402ec325b078cb72a284465639e0fe
> Change definition of  proxy_protocol_port
>
> diff -r 59cadccedf40 -r e2984af905ff src/core/ngx_connection.h
> --- a/src/core/ngx_connection.h Thu Nov 05 20:36:47 2015 +0900
> +++ b/src/core/ngx_connection.h Mon Dec 07 23:39:32 2015 +0900
> @@ -146,7 +146,7 @@
>      ngx_str_t           addr_text;
>
>      ngx_str_t           proxy_protocol_addr;
> -    ngx_str_t           proxy_protocol_port;
> +    ngx_int_t           proxy_protocol_port;
>
>  #if (NGX_SSL)
>      ngx_ssl_connection_t  *ssl;
> diff -r 59cadccedf40 -r e2984af905ff src/core/ngx_proxy_protocol.c
> --- a/src/core/ngx_proxy_protocol.c     Thu Nov 05 20:36:47 2015 +0900
> +++ b/src/core/ngx_proxy_protocol.c     Mon Dec 07 23:39:32 2015 +0900
> @@ -81,14 +81,6 @@
>          if (ch == ' ') {
>              break;
>          }
> -
> -        if (ch != ':' && ch != '.'
> -            && (ch < 'a' || ch > 'f')
> -            && (ch < 'A' || ch > 'F')
> -            && (ch < '0' || ch > '9'))
> -        {
> -            goto invalid;
> -        }
>      }
>      port = p;
>      for ( ;; ) {
> @@ -108,19 +100,11 @@
>          }
>      }
>      len = p - port - 1;
> -    c->proxy_protocol_port.data = ngx_pnalloc(c->pool, len);
> +    c->proxy_protocol_port = ngx_atoi(port,len);
>
> -    if (c->proxy_protocol_port.data == NULL) {
> -        return NULL;
> -    }
> -
> -    ngx_memcpy(c->proxy_protocol_port.data, port, len);
> -    c->proxy_protocol_port.len = len;
> -
> -    ngx_log_debug1(NGX_LOG_DEBUG_CORE, c->log, 0,
> -                   "PROXY protocol address: \"%V\"", &c->proxy_protocol_addr);
> -    ngx_log_debug1(NGX_LOG_DEBUG_CORE, c->log, 0,
> -                   "PROXY protocol port: \"%V\"", &c->proxy_protocol_port);
> +    ngx_log_debug2(NGX_LOG_DEBUG_CORE, c->log, 0,
> +                   "PROXY protocol address: \"%V\", PROXY protocol
> port: \"%d\"",
> +                   &c->proxy_protocol_addr, c->proxy_protocol_port);
>
>  skip:
>
> diff -r 59cadccedf40 -r e2984af905ff src/http/ngx_http_variables.c
> --- a/src/http/ngx_http_variables.c     Thu Nov 05 20:36:47 2015 +0900
> +++ b/src/http/ngx_http_variables.c     Mon Dec 07 23:39:32 2015 +0900
> @@ -1258,11 +1258,20 @@
>  ngx_http_variable_proxy_protocol_port(ngx_http_request_t *r,
>      ngx_http_variable_value_t *v, uintptr_t data)
>  {
> -    v->len = r->connection->proxy_protocol_port.len;
> +    ngx_int_t port = r->connection->proxy_protocol_port;
> +
> +    v->len = 0;
>      v->valid = 1;
>      v->no_cacheable = 0;
>      v->not_found = 0;
> -    v->data = r->connection->proxy_protocol_port.data;
> +    v->data = ngx_pnalloc(r->pool, sizeof("65535") - 1);
> +
> +    if (v->data == NULL) {
> +        return NGX_ERROR;
> +    }
> +    if (port > 0 && port < 65536) {
> +        v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
> +    }
>
>      return NGX_OK;
>  }
>
>
> On Mon, Dec 7, 2015 at 11:51 AM, Maxim Dounin <mdounin at mdounin.ru> wrote:
>> Hello!
>>
>> On Mon, Dec 07, 2015 at 12:14:38AM +0900, junpei yoshino wrote:
>>
>>> > but we need someone to do the rest of the work.
>>>
>>> Could I contribute it?
>>> At first, I will revise this patch along your review.
>>
>> It may be a bit too many for someone with small nginx coding
>> experience, but you may try to.
>>
>> --
>> Maxim Dounin
>> http://nginx.org/
>>
>> _______________________________________________
>> nginx-devel mailing list
>> nginx-devel at nginx.org
>> http://mailman.nginx.org/mailman/listinfo/nginx-devel
>
>
>
> --
> junpei.yoshino at gmail.com



-- 
junpei.yoshino at gmail.com



More information about the nginx-devel mailing list