[nginx] Added the $realip_remote_port variable.
Dmitry Volyntsev
xeioex at nginx.com
Mon May 23 15:45:33 UTC 2016
details: http://hg.nginx.org/nginx/rev/b13d3a6f0512
branches:
changeset: 6562:b13d3a6f0512
user: Dmitry Volyntsev <xeioex at nginx.com>
date: Mon May 23 18:44:22 2016 +0300
description:
Added the $realip_remote_port variable.
diffstat:
src/http/modules/ngx_http_realip_module.c | 75 +++++++++++++++++++++++++++++-
1 files changed, 71 insertions(+), 4 deletions(-)
diffs (115 lines):
diff -r 28c76d9d75b7 -r b13d3a6f0512 src/http/modules/ngx_http_realip_module.c
--- a/src/http/modules/ngx_http_realip_module.c Mon May 23 18:44:21 2016 +0300
+++ b/src/http/modules/ngx_http_realip_module.c Mon May 23 18:44:22 2016 +0300
@@ -45,10 +45,14 @@ static char *ngx_http_realip_merge_loc_c
void *parent, void *child);
static ngx_int_t ngx_http_realip_add_variables(ngx_conf_t *cf);
static ngx_int_t ngx_http_realip_init(ngx_conf_t *cf);
+static ngx_http_realip_ctx_t *ngx_http_realip_get_module_ctx(
+ ngx_http_request_t *r);
static ngx_int_t ngx_http_realip_remote_addr_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_realip_remote_port_variable(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data);
static ngx_command_t ngx_http_realip_commands[] = {
@@ -115,6 +119,9 @@ static ngx_http_variable_t ngx_http_rea
{ ngx_string("realip_remote_addr"), NULL,
ngx_http_realip_remote_addr_variable, 0, 0, 0 },
+ { ngx_string("realip_remote_port"), NULL,
+ ngx_http_realip_remote_port_variable, 0, 0, 0 },
+
{ ngx_null_string, NULL, NULL, 0, 0, 0 }
};
@@ -475,11 +482,9 @@ ngx_http_realip_init(ngx_conf_t *cf)
}
-static ngx_int_t
-ngx_http_realip_remote_addr_variable(ngx_http_request_t *r,
- ngx_http_variable_value_t *v, uintptr_t data)
+static ngx_http_realip_ctx_t *
+ngx_http_realip_get_module_ctx(ngx_http_request_t *r)
{
- ngx_str_t *addr_text;
ngx_pool_cleanup_t *cln;
ngx_http_realip_ctx_t *ctx;
@@ -500,6 +505,19 @@ ngx_http_realip_remote_addr_variable(ngx
}
}
+ return ctx;
+}
+
+
+static ngx_int_t
+ngx_http_realip_remote_addr_variable(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data)
+{
+ ngx_str_t *addr_text;
+ ngx_http_realip_ctx_t *ctx;
+
+ ctx = ngx_http_realip_get_module_ctx(r);
+
addr_text = ctx ? &ctx->addr_text : &r->connection->addr_text;
v->len = addr_text->len;
@@ -510,3 +528,52 @@ ngx_http_realip_remote_addr_variable(ngx
return NGX_OK;
}
+
+
+static ngx_int_t
+ngx_http_realip_remote_port_variable(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data)
+{
+ ngx_uint_t port;
+ struct sockaddr *sa;
+ ngx_http_realip_ctx_t *ctx;
+
+ ctx = ngx_http_realip_get_module_ctx(r);
+
+ sa = ctx ? ctx->sockaddr : r->connection->sockaddr;
+
+ 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;
+ }
+
+ switch (sa->sa_family) {
+
+#if (NGX_HAVE_INET6)
+ case AF_INET6:
+ port = ntohs(((struct sockaddr_in6 *) sa)->sin6_port);
+ break;
+#endif
+
+#if (NGX_HAVE_UNIX_DOMAIN)
+ case AF_UNIX:
+ port = 0;
+ break;
+#endif
+
+ default: /* AF_INET */
+ port = ntohs(((struct sockaddr_in *) sa)->sin_port);
+ break;
+ }
+
+ if (port > 0 && port < 65536) {
+ v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
+ }
+
+ return NGX_OK;
+}
More information about the nginx-devel
mailing list