Mercurial > hg > nginx
comparison src/http/modules/ngx_http_realip_module.c @ 6294:cebe43bace93
Realip: the $realip_remote_addr variable.
author | Ruslan Ermilov <ru@nginx.com> |
---|---|
date | Mon, 16 Nov 2015 16:02:02 +0300 |
parents | 3a72b1805c52 |
children | 2cd019520210 |
comparison
equal
deleted
inserted
replaced
6293:ec6b07be88a5 | 6294:cebe43bace93 |
---|---|
41 void *conf); | 41 void *conf); |
42 static char *ngx_http_realip(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); | 42 static char *ngx_http_realip(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); |
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_init(ngx_conf_t *cf); | 47 static ngx_int_t ngx_http_realip_init(ngx_conf_t *cf); |
48 | |
49 | |
50 static ngx_int_t ngx_http_realip_remote_addr_variable(ngx_http_request_t *r, | |
51 ngx_http_variable_value_t *v, uintptr_t data); | |
47 | 52 |
48 | 53 |
49 static ngx_command_t ngx_http_realip_commands[] = { | 54 static ngx_command_t ngx_http_realip_commands[] = { |
50 | 55 |
51 { ngx_string("set_real_ip_from"), | 56 { ngx_string("set_real_ip_from"), |
73 }; | 78 }; |
74 | 79 |
75 | 80 |
76 | 81 |
77 static ngx_http_module_t ngx_http_realip_module_ctx = { | 82 static ngx_http_module_t ngx_http_realip_module_ctx = { |
78 NULL, /* preconfiguration */ | 83 ngx_http_realip_add_variables, /* preconfiguration */ |
79 ngx_http_realip_init, /* postconfiguration */ | 84 ngx_http_realip_init, /* postconfiguration */ |
80 | 85 |
81 NULL, /* create main configuration */ | 86 NULL, /* create main configuration */ |
82 NULL, /* init main configuration */ | 87 NULL, /* init main configuration */ |
83 | 88 |
103 NULL, /* exit master */ | 108 NULL, /* exit master */ |
104 NGX_MODULE_V1_PADDING | 109 NGX_MODULE_V1_PADDING |
105 }; | 110 }; |
106 | 111 |
107 | 112 |
113 static ngx_http_variable_t ngx_http_realip_vars[] = { | |
114 | |
115 { ngx_string("realip_remote_addr"), NULL, | |
116 ngx_http_realip_remote_addr_variable, 0, 0, 0 }, | |
117 | |
118 { ngx_null_string, NULL, NULL, 0, 0, 0 } | |
119 }; | |
120 | |
121 | |
108 static ngx_int_t | 122 static ngx_int_t |
109 ngx_http_realip_handler(ngx_http_request_t *r) | 123 ngx_http_realip_handler(ngx_http_request_t *r) |
110 { | 124 { |
111 u_char *p; | 125 u_char *p; |
112 size_t len; | 126 size_t len; |
415 return NGX_CONF_OK; | 429 return NGX_CONF_OK; |
416 } | 430 } |
417 | 431 |
418 | 432 |
419 static ngx_int_t | 433 static ngx_int_t |
434 ngx_http_realip_add_variables(ngx_conf_t *cf) | |
435 { | |
436 ngx_http_variable_t *var, *v; | |
437 | |
438 for (v = ngx_http_realip_vars; v->name.len; v++) { | |
439 var = ngx_http_add_variable(cf, &v->name, v->flags); | |
440 if (var == NULL) { | |
441 return NGX_ERROR; | |
442 } | |
443 | |
444 var->get_handler = v->get_handler; | |
445 var->data = v->data; | |
446 } | |
447 | |
448 return NGX_OK; | |
449 } | |
450 | |
451 | |
452 static ngx_int_t | |
420 ngx_http_realip_init(ngx_conf_t *cf) | 453 ngx_http_realip_init(ngx_conf_t *cf) |
421 { | 454 { |
422 ngx_http_handler_pt *h; | 455 ngx_http_handler_pt *h; |
423 ngx_http_core_main_conf_t *cmcf; | 456 ngx_http_core_main_conf_t *cmcf; |
424 | 457 |
438 | 471 |
439 *h = ngx_http_realip_handler; | 472 *h = ngx_http_realip_handler; |
440 | 473 |
441 return NGX_OK; | 474 return NGX_OK; |
442 } | 475 } |
476 | |
477 | |
478 static ngx_int_t | |
479 ngx_http_realip_remote_addr_variable(ngx_http_request_t *r, | |
480 ngx_http_variable_value_t *v, uintptr_t data) | |
481 { | |
482 ngx_str_t *addr_text; | |
483 ngx_pool_cleanup_t *cln; | |
484 ngx_http_realip_ctx_t *ctx; | |
485 | |
486 ctx = ngx_http_get_module_ctx(r, ngx_http_realip_module); | |
487 | |
488 if (ctx == NULL && (r->internal || r->filter_finalize)) { | |
489 | |
490 /* | |
491 * if module context was reset, the original address | |
492 * can still be found in the cleanup handler | |
493 */ | |
494 | |
495 for (cln = r->pool->cleanup; cln; cln = cln->next) { | |
496 if (cln->handler == ngx_http_realip_cleanup) { | |
497 ctx = cln->data; | |
498 break; | |
499 } | |
500 } | |
501 } | |
502 | |
503 addr_text = ctx ? &ctx->addr_text : &r->connection->addr_text; | |
504 | |
505 v->len = addr_text->len; | |
506 v->valid = 1; | |
507 v->no_cacheable = 0; | |
508 v->not_found = 0; | |
509 v->data = addr_text->data; | |
510 | |
511 return NGX_OK; | |
512 } |