Mercurial > hg > nginx-ranges
comparison src/http/modules/ngx_http_realip_module.c @ 122:d25a1d6034f1 NGINX_0_3_8
nginx 0.3.8
*) Security: nginx now checks URI got from a backend in
"X-Accel-Redirect" header line or in SSI file for the "/../" paths
and zeroes.
*) Change: nginx now does not treat the empty user name in the
"Authorization" header line as valid one.
*) Feature: the "ssl_session_timeout" directives of the
ngx_http_ssl_module and ngx_imap_ssl_module.
*) Feature: the "auth_http_header" directive of the
ngx_imap_auth_http_module.
*) Feature: the "add_header" directive.
*) Feature: the ngx_http_realip_module.
*) Feature: the new variables to use in the "log_format" directive:
$bytes_sent, $apache_bytes_sent, $status, $time_gmt, $uri,
$request_time, $request_length, $upstream_status,
$upstream_response_time, $gzip_ratio, $uid_got, $uid_set,
$connection, $pipe, and $msec. The parameters in the "%name" form
will be canceled soon.
*) Change: now the false variable values in the "if" directive are the
empty string "" and string starting with "0".
*) Bugfix: while using proxied or FastCGI-server nginx may leave
connections and temporary files with client requests in open state.
*) Bugfix: the worker processes did not flush the buffered logs on
graceful exit.
*) Bugfix: if the request URI was changes by the "rewrite" directive
and the request was proxied in location given by regular expression,
then the incorrect request was transferred to backend; bug appeared
in 0.2.6.
*) Bugfix: the "expires" directive did not remove the previous
"Expires" header.
*) Bugfix: nginx may stop to accept requests if the "rtsig" method and
several worker processes were used.
*) Bugfix: the "\"" and "\'" escape symbols were incorrectly handled in
SSI commands.
*) Bugfix: if the response was ended just after the SSI command and
gzipping was used, then the response did not transferred complete or
did not transferred at all.
author | Igor Sysoev <http://sysoev.ru> |
---|---|
date | Wed, 09 Nov 2005 00:00:00 +0300 |
parents | |
children | 82d695e3d662 |
comparison
equal
deleted
inserted
replaced
121:737953b238a4 | 122:d25a1d6034f1 |
---|---|
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 { | |
15 in_addr_t mask; | |
16 in_addr_t addr; | |
17 } ngx_http_realip_from_t; | |
18 | |
19 | |
20 typedef struct { | |
21 ngx_array_t *from; /* array of ngx_http_realip_from_t */ | |
22 | |
23 ngx_uint_t xfwd; | |
24 } ngx_http_realip_loc_conf_t; | |
25 | |
26 | |
27 static ngx_int_t ngx_http_realip_handler(ngx_http_request_t *r); | |
28 static char *ngx_http_realip_from(ngx_conf_t *cf, ngx_command_t *cmd, | |
29 void *conf); | |
30 static void *ngx_http_realip_create_loc_conf(ngx_conf_t *cf); | |
31 static char *ngx_http_realip_merge_loc_conf(ngx_conf_t *cf, | |
32 void *parent, void *child); | |
33 static ngx_int_t ngx_http_realip_init(ngx_cycle_t *cycle); | |
34 | |
35 | |
36 static ngx_conf_enum_t ngx_http_realip_header[] = { | |
37 { ngx_string("X-Forwarded-For"), 1 }, | |
38 { ngx_string("X-Real-IP"), 0 }, | |
39 { ngx_null_string, 0 } | |
40 }; | |
41 | |
42 | |
43 static ngx_command_t ngx_http_realip_commands[] = { | |
44 | |
45 { ngx_string("set_real_ip_from"), | |
46 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
47 ngx_http_realip_from, | |
48 NGX_HTTP_LOC_CONF_OFFSET, | |
49 0, | |
50 NULL }, | |
51 | |
52 { ngx_string("real_ip_header"), | |
53 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
54 ngx_conf_set_enum_slot, | |
55 NGX_HTTP_LOC_CONF_OFFSET, | |
56 offsetof(ngx_http_realip_loc_conf_t, xfwd), | |
57 &ngx_http_realip_header }, | |
58 | |
59 ngx_null_command | |
60 }; | |
61 | |
62 | |
63 | |
64 ngx_http_module_t ngx_http_realip_module_ctx = { | |
65 NULL, /* preconfiguration */ | |
66 NULL, /* postconfiguration */ | |
67 | |
68 NULL, /* create main configuration */ | |
69 NULL, /* init main configuration */ | |
70 | |
71 NULL, /* create server configuration */ | |
72 NULL, /* merge server configuration */ | |
73 | |
74 ngx_http_realip_create_loc_conf, /* create location configuration */ | |
75 ngx_http_realip_merge_loc_conf /* merge location configuration */ | |
76 }; | |
77 | |
78 | |
79 ngx_module_t ngx_http_realip_module = { | |
80 NGX_MODULE_V1, | |
81 &ngx_http_realip_module_ctx, /* module context */ | |
82 ngx_http_realip_commands, /* module directives */ | |
83 NGX_HTTP_MODULE, /* module type */ | |
84 NULL, /* init master */ | |
85 ngx_http_realip_init, /* init module */ | |
86 NULL, /* init process */ | |
87 NULL, /* init thread */ | |
88 NULL, /* exit thread */ | |
89 NULL, /* exit process */ | |
90 NULL, /* exit master */ | |
91 NGX_MODULE_V1_PADDING | |
92 }; | |
93 | |
94 | |
95 static ngx_int_t | |
96 ngx_http_realip_handler(ngx_http_request_t *r) | |
97 { | |
98 u_char *ip, *p; | |
99 size_t len; | |
100 ngx_uint_t i; | |
101 struct sockaddr_in *sin; | |
102 ngx_http_realip_from_t *from; | |
103 ngx_http_realip_loc_conf_t *rlcf; | |
104 | |
105 if (r->realip_set) { | |
106 return NGX_OK; | |
107 } | |
108 | |
109 rlcf = ngx_http_get_module_loc_conf(r, ngx_http_realip_module); | |
110 | |
111 if (rlcf->from == NULL) { | |
112 return NGX_OK; | |
113 } | |
114 | |
115 if (rlcf->xfwd == 0) { | |
116 if (r->headers_in.x_real_ip == NULL) { | |
117 return NGX_OK; | |
118 } | |
119 | |
120 len = r->headers_in.x_real_ip->value.len; | |
121 ip = r->headers_in.x_real_ip->value.data; | |
122 | |
123 } else { | |
124 if (r->headers_in.x_forwarded_for == NULL) { | |
125 return NGX_OK; | |
126 } | |
127 | |
128 len = r->headers_in.x_forwarded_for->value.len; | |
129 ip = r->headers_in.x_forwarded_for->value.data; | |
130 | |
131 for (p = ip + len; p > ip; p--) { | |
132 if (*p == ' ' || *p == ',') { | |
133 p++; | |
134 len -= p - ip; | |
135 ip = p; | |
136 break; | |
137 } | |
138 } | |
139 } | |
140 | |
141 /* AF_INET only */ | |
142 | |
143 sin = (struct sockaddr_in *) r->connection->sockaddr; | |
144 | |
145 from = rlcf->from->elts; | |
146 for (i = 0; i < rlcf->from->nelts; i++) { | |
147 | |
148 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
149 "realip: %08XD %08XD %08XD", | |
150 sin->sin_addr.s_addr, from[i].mask, from[i].addr); | |
151 | |
152 if ((sin->sin_addr.s_addr & from[i].mask) == from[i].addr) { | |
153 | |
154 r->connection->addr_text.len = len; | |
155 r->connection->addr_text.data = ip; | |
156 | |
157 sin->sin_addr.s_addr = inet_addr((char *) ip); | |
158 | |
159 r->realip_set = 1; | |
160 | |
161 return NGX_OK; | |
162 } | |
163 } | |
164 | |
165 return NGX_OK; | |
166 } | |
167 | |
168 | |
169 static char * | |
170 ngx_http_realip_from(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
171 { | |
172 ngx_http_realip_loc_conf_t *rlcf = conf; | |
173 | |
174 ngx_str_t *value; | |
175 ngx_inet_cidr_t in_cidr; | |
176 ngx_http_realip_from_t *from; | |
177 | |
178 if (rlcf->from == NULL) { | |
179 rlcf->from = ngx_array_create(cf->pool, 2, | |
180 sizeof(ngx_http_realip_from_t)); | |
181 if (rlcf->from == NULL) { | |
182 return NGX_CONF_ERROR; | |
183 } | |
184 } | |
185 | |
186 from = ngx_array_push(rlcf->from); | |
187 if (from == NULL) { | |
188 return NGX_CONF_ERROR; | |
189 } | |
190 | |
191 value = cf->args->elts; | |
192 | |
193 from->addr = inet_addr((char *) value[1].data); | |
194 | |
195 if (from->addr != INADDR_NONE) { | |
196 from->mask = 0xffffffff; | |
197 | |
198 return NGX_CONF_OK; | |
199 } | |
200 | |
201 if (ngx_ptocidr(&value[1], &in_cidr) == NGX_ERROR) { | |
202 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"", | |
203 &value[1]); | |
204 return NGX_CONF_ERROR; | |
205 } | |
206 | |
207 from->mask = in_cidr.mask; | |
208 from->addr = in_cidr.addr; | |
209 | |
210 return NGX_CONF_OK; | |
211 } | |
212 | |
213 | |
214 static void * | |
215 ngx_http_realip_create_loc_conf(ngx_conf_t *cf) | |
216 { | |
217 ngx_http_realip_loc_conf_t *conf; | |
218 | |
219 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_realip_loc_conf_t)); | |
220 if (conf == NULL) { | |
221 return NGX_CONF_ERROR; | |
222 } | |
223 | |
224 /* | |
225 * set by ngx_pcalloc(): | |
226 * | |
227 * conf->from = NULL; | |
228 */ | |
229 | |
230 conf->xfwd = NGX_CONF_UNSET_UINT; | |
231 | |
232 return conf; | |
233 } | |
234 | |
235 | |
236 static char * | |
237 ngx_http_realip_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
238 { | |
239 ngx_http_realip_loc_conf_t *prev = parent; | |
240 ngx_http_realip_loc_conf_t *conf = child; | |
241 | |
242 if (conf->from == NULL) { | |
243 conf->from = prev->from; | |
244 } | |
245 | |
246 ngx_conf_merge_unsigned_value(conf->xfwd, prev->xfwd, 0); | |
247 | |
248 return NGX_CONF_OK; | |
249 } | |
250 | |
251 | |
252 static ngx_int_t | |
253 ngx_http_realip_init(ngx_cycle_t *cycle) | |
254 { | |
255 ngx_http_handler_pt *h; | |
256 ngx_http_core_main_conf_t *cmcf; | |
257 | |
258 cmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_core_module); | |
259 | |
260 h = ngx_array_push(&cmcf->phases[NGX_HTTP_POST_READ_PHASE].handlers); | |
261 if (h == NULL) { | |
262 return NGX_ERROR; | |
263 } | |
264 | |
265 *h = ngx_http_realip_handler; | |
266 | |
267 h = ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers); | |
268 if (h == NULL) { | |
269 return NGX_ERROR; | |
270 } | |
271 | |
272 *h = ngx_http_realip_handler; | |
273 | |
274 return NGX_OK; | |
275 } |