Mercurial > hg > nginx
comparison src/http/modules/ngx_http_realip_module.c @ 6562:b13d3a6f0512
Added the $realip_remote_port variable.
author | Dmitry Volyntsev <xeioex@nginx.com> |
---|---|
date | Mon, 23 May 2016 18:44:22 +0300 |
parents | 2cd019520210 |
children | 26feae43987f |
comparison
equal
deleted
inserted
replaced
6561:28c76d9d75b7 | 6562:b13d3a6f0512 |
---|---|
43 static void *ngx_http_realip_create_loc_conf(ngx_conf_t *cf); | 43 static void *ngx_http_realip_create_loc_conf(ngx_conf_t *cf); |
44 static char *ngx_http_realip_merge_loc_conf(ngx_conf_t *cf, | 44 static char *ngx_http_realip_merge_loc_conf(ngx_conf_t *cf, |
45 void *parent, void *child); | 45 void *parent, void *child); |
46 static ngx_int_t ngx_http_realip_add_variables(ngx_conf_t *cf); | 46 static ngx_int_t ngx_http_realip_add_variables(ngx_conf_t *cf); |
47 static ngx_int_t ngx_http_realip_init(ngx_conf_t *cf); | 47 static ngx_int_t ngx_http_realip_init(ngx_conf_t *cf); |
48 static ngx_http_realip_ctx_t *ngx_http_realip_get_module_ctx( | |
49 ngx_http_request_t *r); | |
48 | 50 |
49 | 51 |
50 static ngx_int_t ngx_http_realip_remote_addr_variable(ngx_http_request_t *r, | 52 static ngx_int_t ngx_http_realip_remote_addr_variable(ngx_http_request_t *r, |
53 ngx_http_variable_value_t *v, uintptr_t data); | |
54 static ngx_int_t ngx_http_realip_remote_port_variable(ngx_http_request_t *r, | |
51 ngx_http_variable_value_t *v, uintptr_t data); | 55 ngx_http_variable_value_t *v, uintptr_t data); |
52 | 56 |
53 | 57 |
54 static ngx_command_t ngx_http_realip_commands[] = { | 58 static ngx_command_t ngx_http_realip_commands[] = { |
55 | 59 |
113 static ngx_http_variable_t ngx_http_realip_vars[] = { | 117 static ngx_http_variable_t ngx_http_realip_vars[] = { |
114 | 118 |
115 { ngx_string("realip_remote_addr"), NULL, | 119 { ngx_string("realip_remote_addr"), NULL, |
116 ngx_http_realip_remote_addr_variable, 0, 0, 0 }, | 120 ngx_http_realip_remote_addr_variable, 0, 0, 0 }, |
117 | 121 |
122 { ngx_string("realip_remote_port"), NULL, | |
123 ngx_http_realip_remote_port_variable, 0, 0, 0 }, | |
124 | |
118 { ngx_null_string, NULL, NULL, 0, 0, 0 } | 125 { ngx_null_string, NULL, NULL, 0, 0, 0 } |
119 }; | 126 }; |
120 | 127 |
121 | 128 |
122 static ngx_int_t | 129 static ngx_int_t |
473 | 480 |
474 return NGX_OK; | 481 return NGX_OK; |
475 } | 482 } |
476 | 483 |
477 | 484 |
478 static ngx_int_t | 485 static ngx_http_realip_ctx_t * |
479 ngx_http_realip_remote_addr_variable(ngx_http_request_t *r, | 486 ngx_http_realip_get_module_ctx(ngx_http_request_t *r) |
480 ngx_http_variable_value_t *v, uintptr_t data) | 487 { |
481 { | |
482 ngx_str_t *addr_text; | |
483 ngx_pool_cleanup_t *cln; | 488 ngx_pool_cleanup_t *cln; |
484 ngx_http_realip_ctx_t *ctx; | 489 ngx_http_realip_ctx_t *ctx; |
485 | 490 |
486 ctx = ngx_http_get_module_ctx(r, ngx_http_realip_module); | 491 ctx = ngx_http_get_module_ctx(r, ngx_http_realip_module); |
487 | 492 |
498 break; | 503 break; |
499 } | 504 } |
500 } | 505 } |
501 } | 506 } |
502 | 507 |
508 return ctx; | |
509 } | |
510 | |
511 | |
512 static ngx_int_t | |
513 ngx_http_realip_remote_addr_variable(ngx_http_request_t *r, | |
514 ngx_http_variable_value_t *v, uintptr_t data) | |
515 { | |
516 ngx_str_t *addr_text; | |
517 ngx_http_realip_ctx_t *ctx; | |
518 | |
519 ctx = ngx_http_realip_get_module_ctx(r); | |
520 | |
503 addr_text = ctx ? &ctx->addr_text : &r->connection->addr_text; | 521 addr_text = ctx ? &ctx->addr_text : &r->connection->addr_text; |
504 | 522 |
505 v->len = addr_text->len; | 523 v->len = addr_text->len; |
506 v->valid = 1; | 524 v->valid = 1; |
507 v->no_cacheable = 0; | 525 v->no_cacheable = 0; |
508 v->not_found = 0; | 526 v->not_found = 0; |
509 v->data = addr_text->data; | 527 v->data = addr_text->data; |
510 | 528 |
511 return NGX_OK; | 529 return NGX_OK; |
512 } | 530 } |
531 | |
532 | |
533 static ngx_int_t | |
534 ngx_http_realip_remote_port_variable(ngx_http_request_t *r, | |
535 ngx_http_variable_value_t *v, uintptr_t data) | |
536 { | |
537 ngx_uint_t port; | |
538 struct sockaddr *sa; | |
539 ngx_http_realip_ctx_t *ctx; | |
540 | |
541 ctx = ngx_http_realip_get_module_ctx(r); | |
542 | |
543 sa = ctx ? ctx->sockaddr : r->connection->sockaddr; | |
544 | |
545 v->len = 0; | |
546 v->valid = 1; | |
547 v->no_cacheable = 0; | |
548 v->not_found = 0; | |
549 | |
550 v->data = ngx_pnalloc(r->pool, sizeof("65535") - 1); | |
551 if (v->data == NULL) { | |
552 return NGX_ERROR; | |
553 } | |
554 | |
555 switch (sa->sa_family) { | |
556 | |
557 #if (NGX_HAVE_INET6) | |
558 case AF_INET6: | |
559 port = ntohs(((struct sockaddr_in6 *) sa)->sin6_port); | |
560 break; | |
561 #endif | |
562 | |
563 #if (NGX_HAVE_UNIX_DOMAIN) | |
564 case AF_UNIX: | |
565 port = 0; | |
566 break; | |
567 #endif | |
568 | |
569 default: /* AF_INET */ | |
570 port = ntohs(((struct sockaddr_in *) sa)->sin_port); | |
571 break; | |
572 } | |
573 | |
574 if (port > 0 && port < 65536) { | |
575 v->len = ngx_sprintf(v->data, "%ui", port) - v->data; | |
576 } | |
577 | |
578 return NGX_OK; | |
579 } |