Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_realip_module.c @ 2251:91d7a9eb8ade release-0.7.17
nginx-0.7.17-RELEASE
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 15 Sep 2008 16:59:30 +0000 |
parents | 2300ab9b069e |
children | 74d270c8821e |
rev | line source |
---|---|
573 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
9 #include <ngx_http.h> | |
10 | |
11 | |
12 /* AF_INET only */ | |
13 | |
14 typedef struct { | |
2176
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
15 in_addr_t mask; |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
16 in_addr_t addr; |
573 | 17 } ngx_http_realip_from_t; |
18 | |
19 | |
20 typedef struct { | |
2176
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
21 ngx_array_t *from; /* array of ngx_http_realip_from_t */ |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
22 ngx_uint_t xfwd; |
573 | 23 } ngx_http_realip_loc_conf_t; |
24 | |
25 | |
2176
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
26 typedef struct { |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
27 ngx_connection_t *connection; |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
28 in_addr_t addr; |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
29 ngx_str_t addr_text; |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
30 } ngx_http_realip_ctx_t; |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
31 |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
32 |
573 | 33 static ngx_int_t ngx_http_realip_handler(ngx_http_request_t *r); |
2176
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
34 static void ngx_http_realip_cleanup(void *data); |
573 | 35 static char *ngx_http_realip_from(ngx_conf_t *cf, ngx_command_t *cmd, |
36 void *conf); | |
37 static void *ngx_http_realip_create_loc_conf(ngx_conf_t *cf); | |
38 static char *ngx_http_realip_merge_loc_conf(ngx_conf_t *cf, | |
39 void *parent, void *child); | |
681 | 40 static ngx_int_t ngx_http_realip_init(ngx_conf_t *cf); |
573 | 41 |
42 | |
43 static ngx_conf_enum_t ngx_http_realip_header[] = { | |
44 { ngx_string("X-Forwarded-For"), 1 }, | |
45 { ngx_string("X-Real-IP"), 0 }, | |
46 { ngx_null_string, 0 } | |
47 }; | |
48 | |
49 | |
50 static ngx_command_t ngx_http_realip_commands[] = { | |
51 | |
52 { ngx_string("set_real_ip_from"), | |
53 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
54 ngx_http_realip_from, | |
55 NGX_HTTP_LOC_CONF_OFFSET, | |
56 0, | |
57 NULL }, | |
58 | |
59 { ngx_string("real_ip_header"), | |
60 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
61 ngx_conf_set_enum_slot, | |
62 NGX_HTTP_LOC_CONF_OFFSET, | |
63 offsetof(ngx_http_realip_loc_conf_t, xfwd), | |
64 &ngx_http_realip_header }, | |
65 | |
66 ngx_null_command | |
67 }; | |
68 | |
69 | |
70 | |
667 | 71 static ngx_http_module_t ngx_http_realip_module_ctx = { |
573 | 72 NULL, /* preconfiguration */ |
681 | 73 ngx_http_realip_init, /* postconfiguration */ |
573 | 74 |
75 NULL, /* create main configuration */ | |
76 NULL, /* init main configuration */ | |
77 | |
78 NULL, /* create server configuration */ | |
79 NULL, /* merge server configuration */ | |
80 | |
81 ngx_http_realip_create_loc_conf, /* create location configuration */ | |
82 ngx_http_realip_merge_loc_conf /* merge location configuration */ | |
83 }; | |
84 | |
85 | |
86 ngx_module_t ngx_http_realip_module = { | |
87 NGX_MODULE_V1, | |
88 &ngx_http_realip_module_ctx, /* module context */ | |
89 ngx_http_realip_commands, /* module directives */ | |
90 NGX_HTTP_MODULE, /* module type */ | |
91 NULL, /* init master */ | |
681 | 92 NULL, /* init module */ |
573 | 93 NULL, /* init process */ |
94 NULL, /* init thread */ | |
95 NULL, /* exit thread */ | |
96 NULL, /* exit process */ | |
97 NULL, /* exit master */ | |
98 NGX_MODULE_V1_PADDING | |
99 }; | |
100 | |
101 | |
102 static ngx_int_t | |
103 ngx_http_realip_handler(ngx_http_request_t *r) | |
104 { | |
105 u_char *ip, *p; | |
106 size_t len; | |
1114
3f354952e91d
fix broken values, debug logging, and style fix
Igor Sysoev <igor@sysoev.ru>
parents:
986
diff
changeset
|
107 in_addr_t addr; |
573 | 108 ngx_uint_t i; |
109 struct sockaddr_in *sin; | |
2176
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
110 ngx_connection_t *c; |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
111 ngx_pool_cleanup_t *cln; |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
112 ngx_http_realip_ctx_t *ctx; |
573 | 113 ngx_http_realip_from_t *from; |
114 ngx_http_realip_loc_conf_t *rlcf; | |
115 | |
2176
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
116 ctx = ngx_http_get_module_ctx(r, ngx_http_realip_module); |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
117 |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
118 if (ctx) { |
986
68c85f283043
ngx_http_realip_module must return NGX_DECLINED
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
119 return NGX_DECLINED; |
573 | 120 } |
121 | |
2176
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
122 cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_realip_ctx_t)); |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
123 if (cln == NULL) { |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
124 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
125 } |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
126 |
573 | 127 rlcf = ngx_http_get_module_loc_conf(r, ngx_http_realip_module); |
128 | |
129 if (rlcf->from == NULL) { | |
986
68c85f283043
ngx_http_realip_module must return NGX_DECLINED
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
130 return NGX_DECLINED; |
573 | 131 } |
132 | |
133 if (rlcf->xfwd == 0) { | |
134 if (r->headers_in.x_real_ip == NULL) { | |
986
68c85f283043
ngx_http_realip_module must return NGX_DECLINED
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
135 return NGX_DECLINED; |
573 | 136 } |
137 | |
138 len = r->headers_in.x_real_ip->value.len; | |
139 ip = r->headers_in.x_real_ip->value.data; | |
140 | |
141 } else { | |
142 if (r->headers_in.x_forwarded_for == NULL) { | |
986
68c85f283043
ngx_http_realip_module must return NGX_DECLINED
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
143 return NGX_DECLINED; |
573 | 144 } |
145 | |
146 len = r->headers_in.x_forwarded_for->value.len; | |
147 ip = r->headers_in.x_forwarded_for->value.data; | |
148 | |
1114
3f354952e91d
fix broken values, debug logging, and style fix
Igor Sysoev <igor@sysoev.ru>
parents:
986
diff
changeset
|
149 for (p = ip + len - 1; p > ip; p--) { |
573 | 150 if (*p == ' ' || *p == ',') { |
1114
3f354952e91d
fix broken values, debug logging, and style fix
Igor Sysoev <igor@sysoev.ru>
parents:
986
diff
changeset
|
151 p++; |
3f354952e91d
fix broken values, debug logging, and style fix
Igor Sysoev <igor@sysoev.ru>
parents:
986
diff
changeset
|
152 len -= p - ip; |
3f354952e91d
fix broken values, debug logging, and style fix
Igor Sysoev <igor@sysoev.ru>
parents:
986
diff
changeset
|
153 ip = p; |
3f354952e91d
fix broken values, debug logging, and style fix
Igor Sysoev <igor@sysoev.ru>
parents:
986
diff
changeset
|
154 break; |
573 | 155 } |
156 } | |
157 } | |
158 | |
2176
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
159 c = r->connection; |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
160 |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
161 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "realip: \"%s\"", ip); |
1114
3f354952e91d
fix broken values, debug logging, and style fix
Igor Sysoev <igor@sysoev.ru>
parents:
986
diff
changeset
|
162 |
573 | 163 /* AF_INET only */ |
164 | |
2176
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
165 sin = (struct sockaddr_in *) c->sockaddr; |
573 | 166 |
167 from = rlcf->from->elts; | |
168 for (i = 0; i < rlcf->from->nelts; i++) { | |
169 | |
2176
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
170 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0, |
573 | 171 "realip: %08XD %08XD %08XD", |
172 sin->sin_addr.s_addr, from[i].mask, from[i].addr); | |
173 | |
174 if ((sin->sin_addr.s_addr & from[i].mask) == from[i].addr) { | |
175 | |
2176
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
176 ctx = cln->data; |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
177 |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
178 ngx_http_set_ctx(r, ctx, ngx_http_realip_module); |
1114
3f354952e91d
fix broken values, debug logging, and style fix
Igor Sysoev <igor@sysoev.ru>
parents:
986
diff
changeset
|
179 |
3f354952e91d
fix broken values, debug logging, and style fix
Igor Sysoev <igor@sysoev.ru>
parents:
986
diff
changeset
|
180 addr = inet_addr((char *) ip); |
3f354952e91d
fix broken values, debug logging, and style fix
Igor Sysoev <igor@sysoev.ru>
parents:
986
diff
changeset
|
181 |
3f354952e91d
fix broken values, debug logging, and style fix
Igor Sysoev <igor@sysoev.ru>
parents:
986
diff
changeset
|
182 if (addr == INADDR_NONE) { |
3f354952e91d
fix broken values, debug logging, and style fix
Igor Sysoev <igor@sysoev.ru>
parents:
986
diff
changeset
|
183 return NGX_DECLINED; |
3f354952e91d
fix broken values, debug logging, and style fix
Igor Sysoev <igor@sysoev.ru>
parents:
986
diff
changeset
|
184 } |
3f354952e91d
fix broken values, debug logging, and style fix
Igor Sysoev <igor@sysoev.ru>
parents:
986
diff
changeset
|
185 |
2176
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
186 p = ngx_pnalloc(c->pool, len); |
1118
cec2866f29bd
a client address must be allocated from a connection pool
Igor Sysoev <igor@sysoev.ru>
parents:
1114
diff
changeset
|
187 if (p == NULL) { |
cec2866f29bd
a client address must be allocated from a connection pool
Igor Sysoev <igor@sysoev.ru>
parents:
1114
diff
changeset
|
188 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
cec2866f29bd
a client address must be allocated from a connection pool
Igor Sysoev <igor@sysoev.ru>
parents:
1114
diff
changeset
|
189 } |
cec2866f29bd
a client address must be allocated from a connection pool
Igor Sysoev <igor@sysoev.ru>
parents:
1114
diff
changeset
|
190 |
cec2866f29bd
a client address must be allocated from a connection pool
Igor Sysoev <igor@sysoev.ru>
parents:
1114
diff
changeset
|
191 ngx_memcpy(p, ip, len); |
cec2866f29bd
a client address must be allocated from a connection pool
Igor Sysoev <igor@sysoev.ru>
parents:
1114
diff
changeset
|
192 |
2176
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
193 cln->handler = ngx_http_realip_cleanup; |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
194 |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
195 ctx->connection = c; |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
196 ctx->addr = sin->sin_addr.s_addr; |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
197 ctx->addr_text = c->addr_text; |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
198 |
1114
3f354952e91d
fix broken values, debug logging, and style fix
Igor Sysoev <igor@sysoev.ru>
parents:
986
diff
changeset
|
199 sin->sin_addr.s_addr = addr; |
3f354952e91d
fix broken values, debug logging, and style fix
Igor Sysoev <igor@sysoev.ru>
parents:
986
diff
changeset
|
200 |
2176
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
201 c->addr_text.len = len; |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
202 c->addr_text.data = p; |
573 | 203 |
986
68c85f283043
ngx_http_realip_module must return NGX_DECLINED
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
204 return NGX_DECLINED; |
573 | 205 } |
206 } | |
207 | |
986
68c85f283043
ngx_http_realip_module must return NGX_DECLINED
Igor Sysoev <igor@sysoev.ru>
parents:
681
diff
changeset
|
208 return NGX_DECLINED; |
573 | 209 } |
210 | |
211 | |
2176
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
212 static void |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
213 ngx_http_realip_cleanup(void *data) |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
214 { |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
215 ngx_http_realip_ctx_t *ctx = data; |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
216 |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
217 ngx_connection_t *c; |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
218 struct sockaddr_in *sin; |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
219 |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
220 c = ctx->connection; |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
221 |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
222 sin = (struct sockaddr_in *) c->sockaddr; |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
223 sin->sin_addr.s_addr = ctx->addr; |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
224 |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
225 c->addr_text = ctx->addr_text; |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
226 } |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
227 |
29d26406e1bd
restore connection address on request closure,
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
228 |
573 | 229 static char * |
230 ngx_http_realip_from(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
231 { | |
232 ngx_http_realip_loc_conf_t *rlcf = conf; | |
233 | |
1380
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1118
diff
changeset
|
234 ngx_int_t rc; |
573 | 235 ngx_str_t *value; |
236 ngx_inet_cidr_t in_cidr; | |
237 ngx_http_realip_from_t *from; | |
238 | |
239 if (rlcf->from == NULL) { | |
240 rlcf->from = ngx_array_create(cf->pool, 2, | |
241 sizeof(ngx_http_realip_from_t)); | |
242 if (rlcf->from == NULL) { | |
243 return NGX_CONF_ERROR; | |
244 } | |
245 } | |
246 | |
247 from = ngx_array_push(rlcf->from); | |
248 if (from == NULL) { | |
249 return NGX_CONF_ERROR; | |
250 } | |
251 | |
252 value = cf->args->elts; | |
253 | |
1380
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1118
diff
changeset
|
254 rc = ngx_ptocidr(&value[1], &in_cidr); |
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1118
diff
changeset
|
255 |
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1118
diff
changeset
|
256 if (rc == NGX_ERROR) { |
573 | 257 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"", |
258 &value[1]); | |
259 return NGX_CONF_ERROR; | |
260 } | |
261 | |
1380
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1118
diff
changeset
|
262 if (rc == NGX_DONE) { |
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1118
diff
changeset
|
263 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1118
diff
changeset
|
264 "low address bits of %V are meaningless", &value[1]); |
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1118
diff
changeset
|
265 } |
b590a528fd41
ignore meaningless bits in CIDR and warn about them
Igor Sysoev <igor@sysoev.ru>
parents:
1118
diff
changeset
|
266 |
573 | 267 from->mask = in_cidr.mask; |
268 from->addr = in_cidr.addr; | |
269 | |
270 return NGX_CONF_OK; | |
271 } | |
272 | |
273 | |
274 static void * | |
275 ngx_http_realip_create_loc_conf(ngx_conf_t *cf) | |
276 { | |
277 ngx_http_realip_loc_conf_t *conf; | |
278 | |
279 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_realip_loc_conf_t)); | |
280 if (conf == NULL) { | |
281 return NGX_CONF_ERROR; | |
282 } | |
283 | |
284 /* | |
285 * set by ngx_pcalloc(): | |
286 * | |
287 * conf->from = NULL; | |
288 */ | |
289 | |
290 conf->xfwd = NGX_CONF_UNSET_UINT; | |
291 | |
292 return conf; | |
293 } | |
294 | |
295 | |
296 static char * | |
297 ngx_http_realip_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
298 { | |
299 ngx_http_realip_loc_conf_t *prev = parent; | |
300 ngx_http_realip_loc_conf_t *conf = child; | |
301 | |
302 if (conf->from == NULL) { | |
303 conf->from = prev->from; | |
304 } | |
305 | |
663 | 306 ngx_conf_merge_uint_value(conf->xfwd, prev->xfwd, 0); |
573 | 307 |
308 return NGX_CONF_OK; | |
309 } | |
310 | |
311 | |
312 static ngx_int_t | |
681 | 313 ngx_http_realip_init(ngx_conf_t *cf) |
573 | 314 { |
315 ngx_http_handler_pt *h; | |
316 ngx_http_core_main_conf_t *cmcf; | |
317 | |
681 | 318 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); |
573 | 319 |
320 h = ngx_array_push(&cmcf->phases[NGX_HTTP_POST_READ_PHASE].handlers); | |
321 if (h == NULL) { | |
322 return NGX_ERROR; | |
323 } | |
324 | |
325 *h = ngx_http_realip_handler; | |
326 | |
581 | 327 h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers); |
573 | 328 if (h == NULL) { |
329 return NGX_ERROR; | |
330 } | |
331 | |
332 *h = ngx_http_realip_handler; | |
333 | |
334 return NGX_OK; | |
335 } |