comparison src/http/ngx_http_variables.c @ 8072:cca4c8a715de

PROXY protocol v2 TLV variables. The variables have prefix $proxy_protocol_tlv_ and are accessible by name and by type. Examples are: $proxy_protocol_tlv_0x01, $proxy_protocol_tlv_alpn.
author Roman Arutyunyan <arut@nginx.com>
date Wed, 12 Oct 2022 16:58:16 +0400
parents c263f9ffa1fd
children b71e69247483
comparison
equal deleted inserted replaced
8071:017fd847f4f7 8072:cca4c8a715de
58 static ngx_int_t ngx_http_variable_remote_port(ngx_http_request_t *r, 58 static ngx_int_t ngx_http_variable_remote_port(ngx_http_request_t *r,
59 ngx_http_variable_value_t *v, uintptr_t data); 59 ngx_http_variable_value_t *v, uintptr_t data);
60 static ngx_int_t ngx_http_variable_proxy_protocol_addr(ngx_http_request_t *r, 60 static ngx_int_t ngx_http_variable_proxy_protocol_addr(ngx_http_request_t *r,
61 ngx_http_variable_value_t *v, uintptr_t data); 61 ngx_http_variable_value_t *v, uintptr_t data);
62 static ngx_int_t ngx_http_variable_proxy_protocol_port(ngx_http_request_t *r, 62 static ngx_int_t ngx_http_variable_proxy_protocol_port(ngx_http_request_t *r,
63 ngx_http_variable_value_t *v, uintptr_t data);
64 static ngx_int_t ngx_http_variable_proxy_protocol_tlv(ngx_http_request_t *r,
63 ngx_http_variable_value_t *v, uintptr_t data); 65 ngx_http_variable_value_t *v, uintptr_t data);
64 static ngx_int_t ngx_http_variable_server_addr(ngx_http_request_t *r, 66 static ngx_int_t ngx_http_variable_server_addr(ngx_http_request_t *r,
65 ngx_http_variable_value_t *v, uintptr_t data); 67 ngx_http_variable_value_t *v, uintptr_t data);
66 static ngx_int_t ngx_http_variable_server_port(ngx_http_request_t *r, 68 static ngx_int_t ngx_http_variable_server_port(ngx_http_request_t *r,
67 ngx_http_variable_value_t *v, uintptr_t data); 69 ngx_http_variable_value_t *v, uintptr_t data);
212 214
213 { ngx_string("proxy_protocol_server_port"), NULL, 215 { ngx_string("proxy_protocol_server_port"), NULL,
214 ngx_http_variable_proxy_protocol_port, 216 ngx_http_variable_proxy_protocol_port,
215 offsetof(ngx_proxy_protocol_t, dst_port), 0, 0 }, 217 offsetof(ngx_proxy_protocol_t, dst_port), 0, 0 },
216 218
219 { ngx_string("proxy_protocol_tlv_"), NULL,
220 ngx_http_variable_proxy_protocol_tlv,
221 0, NGX_HTTP_VAR_PREFIX, 0 },
222
217 { ngx_string("server_addr"), NULL, ngx_http_variable_server_addr, 0, 0, 0 }, 223 { ngx_string("server_addr"), NULL, ngx_http_variable_server_addr, 0, 0, 0 },
218 224
219 { ngx_string("server_port"), NULL, ngx_http_variable_server_port, 0, 0, 0 }, 225 { ngx_string("server_port"), NULL, ngx_http_variable_server_port, 0, 0, 0 },
220 226
221 { ngx_string("server_protocol"), NULL, ngx_http_variable_request, 227 { ngx_string("server_protocol"), NULL, ngx_http_variable_request,
1379 port = *(in_port_t *) ((char *) pp + data); 1385 port = *(in_port_t *) ((char *) pp + data);
1380 1386
1381 if (port > 0 && port < 65536) { 1387 if (port > 0 && port < 65536) {
1382 v->len = ngx_sprintf(v->data, "%ui", port) - v->data; 1388 v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
1383 } 1389 }
1390
1391 return NGX_OK;
1392 }
1393
1394
1395 static ngx_int_t
1396 ngx_http_variable_proxy_protocol_tlv(ngx_http_request_t *r,
1397 ngx_http_variable_value_t *v, uintptr_t data)
1398 {
1399 ngx_str_t *name = (ngx_str_t *) data;
1400
1401 ngx_int_t rc;
1402 ngx_str_t tlv, value;
1403
1404 tlv.len = name->len - (sizeof("proxy_protocol_tlv_") - 1);
1405 tlv.data = name->data + sizeof("proxy_protocol_tlv_") - 1;
1406
1407 rc = ngx_proxy_protocol_get_tlv(r->connection, &tlv, &value);
1408
1409 if (rc == NGX_ERROR) {
1410 return NGX_ERROR;
1411 }
1412
1413 if (rc == NGX_DECLINED) {
1414 v->not_found = 1;
1415 return NGX_OK;
1416 }
1417
1418 v->len = value.len;
1419 v->valid = 1;
1420 v->no_cacheable = 0;
1421 v->not_found = 0;
1422 v->data = value.data;
1384 1423
1385 return NGX_OK; 1424 return NGX_OK;
1386 } 1425 }
1387 1426
1388 1427