Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_limit_zone_module.c @ 1011:19118c44303f
test length of variable and number of connections
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 11 Jan 2007 16:50:06 +0000 |
parents | cdde2d4ea5d8 |
children | 11ffb8e4753f |
rev | line source |
---|---|
980 | 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 typedef struct { | |
981 | 13 u_char color; |
14 u_char len; | |
980 | 15 u_short conn; |
16 u_char data[1]; | |
17 } ngx_http_limit_zone_node_t; | |
18 | |
19 | |
20 typedef struct { | |
21 ngx_shm_zone_t *shm_zone; | |
22 ngx_rbtree_node_t *node; | |
23 } ngx_http_limit_zone_cleanup_t; | |
24 | |
25 | |
26 typedef struct { | |
987 | 27 ngx_rbtree_t *rbtree; |
28 ngx_int_t index; | |
29 ngx_str_t var; | |
30 } ngx_http_limit_zone_ctx_t; | |
31 | |
32 | |
33 typedef struct { | |
980 | 34 ngx_shm_zone_t *shm_zone; |
35 ngx_uint_t conn; | |
36 } ngx_http_limit_zone_conf_t; | |
37 | |
38 | |
39 static void ngx_http_limit_zone_cleanup(void *data); | |
40 | |
41 static void *ngx_http_limit_zone_create_conf(ngx_conf_t *cf); | |
42 static char *ngx_http_limit_zone_merge_conf(ngx_conf_t *cf, void *parent, | |
43 void *child); | |
44 static char *ngx_http_limit_zone(ngx_conf_t *cf, ngx_command_t *cmd, | |
45 void *conf); | |
46 static char *ngx_http_limit_conn(ngx_conf_t *cf, ngx_command_t *cmd, | |
47 void *conf); | |
48 static ngx_int_t ngx_http_limit_zone_init(ngx_conf_t *cf); | |
49 | |
50 | |
51 static ngx_command_t ngx_http_limit_zone_commands[] = { | |
52 | |
53 { ngx_string("limit_zone"), | |
987 | 54 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE3, |
980 | 55 ngx_http_limit_zone, |
56 0, | |
57 0, | |
58 NULL }, | |
59 | |
60 { ngx_string("limit_conn"), | |
987 | 61 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2, |
980 | 62 ngx_http_limit_conn, |
63 NGX_HTTP_LOC_CONF_OFFSET, | |
64 0, | |
65 NULL }, | |
66 | |
67 ngx_null_command | |
68 }; | |
69 | |
70 | |
71 static ngx_http_module_t ngx_http_limit_zone_module_ctx = { | |
72 NULL, /* preconfiguration */ | |
73 ngx_http_limit_zone_init, /* postconfiguration */ | |
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_limit_zone_create_conf, /* create location configration */ | |
82 ngx_http_limit_zone_merge_conf /* merge location configration */ | |
83 }; | |
84 | |
85 | |
86 ngx_module_t ngx_http_limit_zone_module = { | |
87 NGX_MODULE_V1, | |
88 &ngx_http_limit_zone_module_ctx, /* module context */ | |
89 ngx_http_limit_zone_commands, /* module directives */ | |
90 NGX_HTTP_MODULE, /* module type */ | |
91 NULL, /* init master */ | |
92 NULL, /* init module */ | |
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_limit_zone_handler(ngx_http_request_t *r) | |
104 { | |
105 size_t len, n; | |
106 uint32_t hash; | |
107 ngx_slab_pool_t *shpool; | |
108 ngx_rbtree_node_t *node, *sentinel; | |
109 ngx_pool_cleanup_t *cln; | |
110 ngx_http_variable_value_t *vv; | |
987 | 111 ngx_http_limit_zone_ctx_t *ctx; |
980 | 112 ngx_http_limit_zone_node_t *lz; |
113 ngx_http_limit_zone_conf_t *lzcf; | |
114 ngx_http_limit_zone_cleanup_t *lzcln; | |
115 | |
997
5900ec20604a
do not count connection for subrequest
Igor Sysoev <igor@sysoev.ru>
parents:
993
diff
changeset
|
116 if (r->main->limit_zone_set) { |
984
dd128232e6ba
count connection once per request
Igor Sysoev <igor@sysoev.ru>
parents:
981
diff
changeset
|
117 return NGX_DECLINED; |
dd128232e6ba
count connection once per request
Igor Sysoev <igor@sysoev.ru>
parents:
981
diff
changeset
|
118 } |
dd128232e6ba
count connection once per request
Igor Sysoev <igor@sysoev.ru>
parents:
981
diff
changeset
|
119 |
980 | 120 lzcf = ngx_http_get_module_loc_conf(r, ngx_http_limit_zone_module); |
121 | |
122 if (lzcf->shm_zone == NULL) { | |
123 return NGX_DECLINED; | |
124 } | |
125 | |
987 | 126 ctx = lzcf->shm_zone->data; |
127 | |
128 vv = ngx_http_get_indexed_variable(r, ctx->index); | |
980 | 129 |
130 if (vv == NULL || vv->not_found) { | |
131 return NGX_DECLINED; | |
132 } | |
133 | |
1011
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
134 len = vv->len; |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
135 |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
136 if (len == 0) { |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
137 return NGX_DECLINED; |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
138 } |
984
dd128232e6ba
count connection once per request
Igor Sysoev <igor@sysoev.ru>
parents:
981
diff
changeset
|
139 |
1011
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
140 if (len > 255) { |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
141 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
142 "the value of the \"%V\" variable " |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
143 "is more than 255 bytes: \"%V\"", |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
144 &ctx->var, vv); |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
145 return NGX_DECLINED; |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
146 } |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
147 |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
148 r->main->limit_zone_set = 1; |
980 | 149 |
150 hash = ngx_crc32_short(vv->data, len); | |
151 | |
152 cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_limit_zone_cleanup_t)); | |
153 if (cln == NULL) { | |
154 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
155 } | |
156 | |
157 shpool = (ngx_slab_pool_t *) lzcf->shm_zone->shm.addr; | |
158 | |
159 ngx_shmtx_lock(&shpool->mutex); | |
160 | |
987 | 161 node = ctx->rbtree->root; |
162 sentinel = ctx->rbtree->sentinel; | |
980 | 163 |
164 while (node != sentinel) { | |
165 | |
166 if (hash < node->key) { | |
167 node = node->left; | |
168 continue; | |
169 } | |
170 | |
171 if (hash > node->key) { | |
172 node = node->right; | |
173 continue; | |
174 } | |
175 | |
176 if (hash == node->key ){ | |
981 | 177 lz = (ngx_http_limit_zone_node_t *) &node->color; |
980 | 178 |
179 if (len == (size_t) lz->len | |
180 && ngx_strncmp(lz->data, vv->data, len) == 0) | |
181 { | |
981 | 182 if ((ngx_uint_t) lz->conn < lzcf->conn) { |
980 | 183 lz->conn++; |
184 goto done; | |
185 } | |
186 | |
187 ngx_shmtx_unlock(&shpool->mutex); | |
188 | |
189 return NGX_HTTP_SERVICE_UNAVAILABLE; | |
190 } | |
191 } | |
192 } | |
193 | |
981 | 194 n = offsetof(ngx_rbtree_node_t, color) |
980 | 195 + offsetof(ngx_http_limit_zone_node_t, data) |
196 + len; | |
197 | |
198 node = ngx_slab_alloc_locked(shpool, n); | |
199 if (node == NULL) { | |
200 ngx_shmtx_unlock(&shpool->mutex); | |
201 return NGX_HTTP_SERVICE_UNAVAILABLE; | |
202 } | |
203 | |
981 | 204 lz = (ngx_http_limit_zone_node_t *) &node->color; |
980 | 205 |
206 node->key = hash; | |
981 | 207 lz->len = (u_char) len; |
980 | 208 lz->conn = 1; |
209 ngx_memcpy(lz->data, vv->data, len); | |
210 | |
987 | 211 ngx_rbtree_insert(ctx->rbtree, node); |
980 | 212 |
213 done: | |
214 | |
215 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
216 "limit zone: %08XD %d", node->key, lz->conn); | |
217 | |
218 ngx_shmtx_unlock(&shpool->mutex); | |
219 | |
220 cln->handler = ngx_http_limit_zone_cleanup; | |
221 lzcln = cln->data; | |
222 | |
223 lzcln->shm_zone = lzcf->shm_zone; | |
224 lzcln->node = node; | |
225 | |
226 return NGX_DECLINED; | |
227 } | |
228 | |
229 | |
230 static void | |
231 ngx_http_limit_zone_cleanup(void *data) | |
232 { | |
233 ngx_http_limit_zone_cleanup_t *lzcln = data; | |
234 | |
235 ngx_slab_pool_t *shpool; | |
236 ngx_rbtree_node_t *node; | |
987 | 237 ngx_http_limit_zone_ctx_t *ctx; |
980 | 238 ngx_http_limit_zone_node_t *lz; |
239 | |
987 | 240 ctx = lzcln->shm_zone->data; |
980 | 241 shpool = (ngx_slab_pool_t *) lzcln->shm_zone->shm.addr; |
242 node = lzcln->node; | |
981 | 243 lz = (ngx_http_limit_zone_node_t *) &node->color; |
980 | 244 |
245 ngx_shmtx_lock(&shpool->mutex); | |
246 | |
247 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, lzcln->shm_zone->shm.log, 0, | |
248 "limit zone cleanup: %08XD %d", node->key, lz->conn); | |
249 | |
250 lz->conn--; | |
251 | |
252 if (lz->conn == 0) { | |
987 | 253 ngx_rbtree_delete(ctx->rbtree, node); |
980 | 254 ngx_slab_free_locked(shpool, node); |
255 } | |
256 | |
257 ngx_shmtx_unlock(&shpool->mutex); | |
258 } | |
259 | |
260 | |
261 static ngx_int_t | |
993
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
262 ngx_http_limit_zone_init_zone(ngx_shm_zone_t *shm_zone, void *data) |
980 | 263 { |
993
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
264 ngx_http_limit_zone_ctx_t *octx = data; |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
265 |
987 | 266 ngx_slab_pool_t *shpool; |
267 ngx_rbtree_node_t *sentinel; | |
268 ngx_http_limit_zone_ctx_t *ctx; | |
980 | 269 |
993
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
270 ctx = shm_zone->data; |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
271 |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
272 if (octx) { |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
273 if (ngx_strcmp(ctx->var.data, octx->var.data) != 0) { |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
274 ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0, |
1001 | 275 "limit_zone \"%V\" uses the \"%V\" variable " |
993
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
276 "while previously it used the \"%V\" variable", |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
277 &shm_zone->name, &ctx->var, &octx->var); |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
278 return NGX_ERROR; |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
279 } |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
280 |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
281 ctx->rbtree = octx->rbtree; |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
282 |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
283 return NGX_OK; |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
284 } |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
285 |
980 | 286 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; |
287 | |
987 | 288 ctx->rbtree = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_t)); |
289 if (ctx->rbtree == NULL) { | |
980 | 290 return NGX_ERROR; |
291 } | |
292 | |
293 sentinel = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_node_t)); | |
294 if (sentinel == NULL) { | |
295 return NGX_ERROR; | |
296 } | |
297 | |
298 ngx_rbtree_sentinel_init(sentinel); | |
299 | |
987 | 300 ctx->rbtree->root = sentinel; |
301 ctx->rbtree->sentinel = sentinel; | |
302 ctx->rbtree->insert = ngx_rbtree_insert_value; | |
980 | 303 |
304 return NGX_OK; | |
305 } | |
306 | |
307 | |
308 static void * | |
309 ngx_http_limit_zone_create_conf(ngx_conf_t *cf) | |
310 { | |
311 ngx_http_limit_zone_conf_t *conf; | |
312 | |
313 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_zone_conf_t)); | |
314 if (conf == NULL) { | |
315 return NGX_CONF_ERROR; | |
316 } | |
317 | |
318 /* | |
319 * set by ngx_pcalloc(): | |
320 * | |
321 * conf->shm_zone = NULL; | |
322 * conf->conn = 0; | |
323 */ | |
324 | |
325 return conf; | |
326 } | |
327 | |
328 | |
329 static char * | |
330 ngx_http_limit_zone_merge_conf(ngx_conf_t *cf, void *parent, void *child) | |
331 { | |
332 ngx_http_limit_zone_conf_t *prev = parent; | |
333 ngx_http_limit_zone_conf_t *conf = child; | |
334 | |
335 if (conf->shm_zone == NULL) { | |
336 *conf = *prev; | |
337 } | |
338 | |
339 return NGX_CONF_OK; | |
340 } | |
341 | |
342 | |
343 static char * | |
344 ngx_http_limit_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
345 { | |
987 | 346 ssize_t n; |
347 ngx_str_t *value; | |
348 ngx_shm_zone_t *shm_zone; | |
349 ngx_http_limit_zone_ctx_t *ctx; | |
980 | 350 |
351 value = cf->args->elts; | |
352 | |
987 | 353 if (value[2].data[0] != '$') { |
354 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
355 "invalid variable name \"%V\"", &value[2]); | |
356 return NGX_CONF_ERROR; | |
357 } | |
358 | |
359 value[2].len--; | |
360 value[2].data++; | |
361 | |
362 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_zone_ctx_t)); | |
363 if (ctx == NULL) { | |
364 return NGX_CONF_ERROR; | |
365 } | |
366 | |
367 ctx->index = ngx_http_get_variable_index(cf, &value[2]); | |
368 if (ctx->index == NGX_ERROR) { | |
369 return NGX_CONF_ERROR; | |
370 } | |
371 | |
372 ctx->var = value[2]; | |
373 | |
374 n = ngx_parse_size(&value[3]); | |
980 | 375 |
376 if (n == NGX_ERROR) { | |
377 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
987 | 378 "invalid size of limit_zone \"%V\"", &value[3]); |
980 | 379 return NGX_CONF_ERROR; |
380 } | |
381 | |
382 if (n < (ngx_int_t) (8 * ngx_pagesize)) { | |
383 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
384 "limit_zone \"%V\" is too small", &value[1]); | |
385 return NGX_CONF_ERROR; | |
386 } | |
387 | |
388 | |
389 shm_zone = ngx_shared_memory_add(cf, &value[1], n, | |
390 &ngx_http_limit_zone_module); | |
391 if (shm_zone == NULL) { | |
392 return NGX_CONF_ERROR; | |
393 } | |
394 | |
987 | 395 if (shm_zone->data) { |
396 ctx = shm_zone->data; | |
397 | |
398 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
399 "limit_zone \"%V\" is already bound to variable \"%V\"", | |
400 &value[1], &ctx->var); | |
401 return NGX_CONF_ERROR; | |
402 } | |
403 | |
980 | 404 shm_zone->init = ngx_http_limit_zone_init_zone; |
987 | 405 shm_zone->data = ctx; |
980 | 406 |
407 return NGX_CONF_OK; | |
408 } | |
409 | |
410 | |
411 static char * | |
412 ngx_http_limit_conn(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
413 { | |
414 ngx_http_limit_zone_conf_t *lzcf = conf; | |
415 | |
416 ngx_int_t n; | |
417 ngx_str_t *value; | |
418 | |
419 value = cf->args->elts; | |
420 | |
987 | 421 lzcf->shm_zone = ngx_shared_memory_add(cf, &value[1], 0, |
422 &ngx_http_limit_zone_module); | |
423 if (lzcf->shm_zone == NULL) { | |
980 | 424 return NGX_CONF_ERROR; |
425 } | |
426 | |
987 | 427 n = ngx_atoi(value[2].data, value[2].len); |
428 if (n <= 0) { | |
980 | 429 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
987 | 430 "invalid number of connections \"%V\"", &value[2]); |
980 | 431 return NGX_CONF_ERROR; |
432 } | |
433 | |
1011
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
434 if (n > 65535) { |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
435 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
436 "connection limit must be less 65536"); |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
437 return NGX_CONF_ERROR; |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
438 } |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
439 |
980 | 440 lzcf->conn = n; |
441 | |
442 return NGX_CONF_OK; | |
443 } | |
444 | |
445 | |
446 static ngx_int_t | |
447 ngx_http_limit_zone_init(ngx_conf_t *cf) | |
448 { | |
449 ngx_http_handler_pt *h; | |
450 ngx_http_core_main_conf_t *cmcf; | |
451 | |
452 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); | |
453 | |
454 h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers); | |
455 if (h == NULL) { | |
456 return NGX_ERROR; | |
457 } | |
458 | |
459 *h = ngx_http_limit_zone_handler; | |
460 | |
461 return NGX_OK; | |
462 } |