[PATCH]add proxy_protocol_port variable for rfc6302
吉野純平
junpei.yoshino at gmail.com
Thu Nov 5 15:45:49 UTC 2015
# HG changeset patch
# User Junpei Yoshino <junpei.yoshino at gmail.com>
# Date 1446723407 -32400
# Thu Nov 05 20:36:47 2015 +0900
# Node ID 59cadccedf402ec325b078cb72a284465639e0fe
# Parent 4ccb37b04454dec6afb9476d085c06aea00adaa0
Http: add proxy_protocol_port variable for rfc6302
Logging source port is recommended in rfc6302.
use case
logging
sending information by http request headers
diff -r 4ccb37b04454 -r 59cadccedf40 src/core/ngx_connection.h
--- a/src/core/ngx_connection.h Fri Oct 30 21:43:30 2015 +0300
+++ b/src/core/ngx_connection.h Thu Nov 05 20:36:47 2015 +0900
@@ -146,6 +146,7 @@
ngx_str_t addr_text;
ngx_str_t proxy_protocol_addr;
+ ngx_str_t proxy_protocol_port;
#if (NGX_SSL)
ngx_ssl_connection_t *ssl;
diff -r 4ccb37b04454 -r 59cadccedf40 src/core/ngx_proxy_protocol.c
--- a/src/core/ngx_proxy_protocol.c Fri Oct 30 21:43:30 2015 +0300
+++ b/src/core/ngx_proxy_protocol.c Thu Nov 05 20:36:47 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,56 @@
ngx_memcpy(c->proxy_protocol_addr.data, addr, len);
c->proxy_protocol_addr.len = len;
+ for ( ;; ) {
+ if (p == last) {
+ goto invalid;
+ }
+
+ ch = *p++;
+
+ if (ch == ' ') {
+ break;
+ }
+
+ if (ch != ':' && ch != '.'
+ && (ch < 'a' || ch > 'f')
+ && (ch < 'A' || ch > 'F')
+ && (ch < '0' || ch > '9'))
+ {
+ goto invalid;
+ }
+ }
+ 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.data = ngx_pnalloc(c->pool, 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);
skip:
diff -r 4ccb37b04454 -r 59cadccedf40 src/http/ngx_http_variables.c
--- a/src/http/ngx_http_variables.c Fri Oct 30 21:43:30 2015 +0300
+++ b/src/http/ngx_http_variables.c Thu Nov 05 20:36:47 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,20 @@
static ngx_int_t
+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;
+ v->valid = 1;
+ v->no_cacheable = 0;
+ v->not_found = 0;
+ v->data = r->connection->proxy_protocol_port.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