comparison src/http/modules/ngx_http_realip_module.c @ 396:349057ecf4d5 NGINX_0_7_10

nginx 0.7.10 *) Bugfix: in the "addition_types", "charset_types", "gzip_types", "ssi_types", "sub_filter_types", and "xslt_types" directives; the bugs had appeared in 0.7.9. *) Bugfix: of recursive error_page for 500 status code. *) Bugfix: now the ngx_http_realip_module set address not for whole keepalive connection, but for each request passed via the connection.
author Igor Sysoev <http://sysoev.ru>
date Wed, 13 Aug 2008 00:00:00 +0400
parents 984bb0b1399b
children 6ebbca3d5ed7
comparison
equal deleted inserted replaced
395:ebf3256f0c2b 396:349057ecf4d5
10 10
11 11
12 /* AF_INET only */ 12 /* AF_INET only */
13 13
14 typedef struct { 14 typedef struct {
15 in_addr_t mask; 15 in_addr_t mask;
16 in_addr_t addr; 16 in_addr_t addr;
17 } ngx_http_realip_from_t; 17 } ngx_http_realip_from_t;
18 18
19 19
20 typedef struct { 20 typedef struct {
21 ngx_array_t *from; /* array of ngx_http_realip_from_t */ 21 ngx_array_t *from; /* array of ngx_http_realip_from_t */
22 22 ngx_uint_t xfwd;
23 ngx_uint_t xfwd;
24 } ngx_http_realip_loc_conf_t; 23 } ngx_http_realip_loc_conf_t;
25 24
26 25
26 typedef struct {
27 ngx_connection_t *connection;
28 in_addr_t addr;
29 ngx_str_t addr_text;
30 } ngx_http_realip_ctx_t;
31
32
27 static ngx_int_t ngx_http_realip_handler(ngx_http_request_t *r); 33 static ngx_int_t ngx_http_realip_handler(ngx_http_request_t *r);
34 static void ngx_http_realip_cleanup(void *data);
28 static char *ngx_http_realip_from(ngx_conf_t *cf, ngx_command_t *cmd, 35 static char *ngx_http_realip_from(ngx_conf_t *cf, ngx_command_t *cmd,
29 void *conf); 36 void *conf);
30 static void *ngx_http_realip_create_loc_conf(ngx_conf_t *cf); 37 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, 38 static char *ngx_http_realip_merge_loc_conf(ngx_conf_t *cf,
32 void *parent, void *child); 39 void *parent, void *child);
98 u_char *ip, *p; 105 u_char *ip, *p;
99 size_t len; 106 size_t len;
100 in_addr_t addr; 107 in_addr_t addr;
101 ngx_uint_t i; 108 ngx_uint_t i;
102 struct sockaddr_in *sin; 109 struct sockaddr_in *sin;
110 ngx_connection_t *c;
111 ngx_pool_cleanup_t *cln;
112 ngx_http_realip_ctx_t *ctx;
103 ngx_http_realip_from_t *from; 113 ngx_http_realip_from_t *from;
104 ngx_http_realip_loc_conf_t *rlcf; 114 ngx_http_realip_loc_conf_t *rlcf;
105 115
106 if (r->realip_set) { 116 ctx = ngx_http_get_module_ctx(r, ngx_http_realip_module);
117
118 if (ctx) {
107 return NGX_DECLINED; 119 return NGX_DECLINED;
120 }
121
122 cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_realip_ctx_t));
123 if (cln == NULL) {
124 return NGX_HTTP_INTERNAL_SERVER_ERROR;
108 } 125 }
109 126
110 rlcf = ngx_http_get_module_loc_conf(r, ngx_http_realip_module); 127 rlcf = ngx_http_get_module_loc_conf(r, ngx_http_realip_module);
111 128
112 if (rlcf->from == NULL) { 129 if (rlcf->from == NULL) {
137 break; 154 break;
138 } 155 }
139 } 156 }
140 } 157 }
141 158
142 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 159 c = r->connection;
143 "realip: \"%s\"", ip); 160
161 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "realip: \"%s\"", ip);
144 162
145 /* AF_INET only */ 163 /* AF_INET only */
146 164
147 sin = (struct sockaddr_in *) r->connection->sockaddr; 165 sin = (struct sockaddr_in *) c->sockaddr;
148 166
149 from = rlcf->from->elts; 167 from = rlcf->from->elts;
150 for (i = 0; i < rlcf->from->nelts; i++) { 168 for (i = 0; i < rlcf->from->nelts; i++) {
151 169
152 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 170 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
153 "realip: %08XD %08XD %08XD", 171 "realip: %08XD %08XD %08XD",
154 sin->sin_addr.s_addr, from[i].mask, from[i].addr); 172 sin->sin_addr.s_addr, from[i].mask, from[i].addr);
155 173
156 if ((sin->sin_addr.s_addr & from[i].mask) == from[i].addr) { 174 if ((sin->sin_addr.s_addr & from[i].mask) == from[i].addr) {
157 175
158 r->realip_set = 1; 176 ctx = cln->data;
177
178 ngx_http_set_ctx(r, ctx, ngx_http_realip_module);
159 179
160 addr = inet_addr((char *) ip); 180 addr = inet_addr((char *) ip);
161 181
162 if (addr == INADDR_NONE) { 182 if (addr == INADDR_NONE) {
163 return NGX_DECLINED; 183 return NGX_DECLINED;
164 } 184 }
165 185
166 p = ngx_pnalloc(r->connection->pool, len); 186 p = ngx_pnalloc(c->pool, len);
167 if (p == NULL) { 187 if (p == NULL) {
168 return NGX_HTTP_INTERNAL_SERVER_ERROR; 188 return NGX_HTTP_INTERNAL_SERVER_ERROR;
169 } 189 }
170 190
171 ngx_memcpy(p, ip, len); 191 ngx_memcpy(p, ip, len);
172 192
193 cln->handler = ngx_http_realip_cleanup;
194
195 ctx->connection = c;
196 ctx->addr = sin->sin_addr.s_addr;
197 ctx->addr_text = c->addr_text;
198
173 sin->sin_addr.s_addr = addr; 199 sin->sin_addr.s_addr = addr;
174 200
175 r->connection->addr_text.len = len; 201 c->addr_text.len = len;
176 r->connection->addr_text.data = p; 202 c->addr_text.data = p;
177 203
178 return NGX_DECLINED; 204 return NGX_DECLINED;
179 } 205 }
180 } 206 }
181 207
182 return NGX_DECLINED; 208 return NGX_DECLINED;
209 }
210
211
212 static void
213 ngx_http_realip_cleanup(void *data)
214 {
215 ngx_http_realip_ctx_t *ctx = data;
216
217 ngx_connection_t *c;
218 struct sockaddr_in *sin;
219
220 c = ctx->connection;
221
222 sin = (struct sockaddr_in *) c->sockaddr;
223 sin->sin_addr.s_addr = ctx->addr;
224
225 c->addr_text = ctx->addr_text;
183 } 226 }
184 227
185 228
186 static char * 229 static char *
187 ngx_http_realip_from(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) 230 ngx_http_realip_from(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)