comparison src/http/modules/ngx_http_limit_zone_module.c @ 272:29a6403156b0 NGINX_0_5_6

nginx 0.5.6 *) Change: now the ngx_http_index_module ignores all methods except the GET, HEAD, and POST methods. *) Feature: the ngx_http_limit_zone_module. *) Feature: the $binary_remote_addr variable. *) Feature: the "ssl_session_cache" directives of the ngx_http_ssl_module and ngx_imap_ssl_module. *) Feature: the DELETE method supports recursive removal. *) Bugfix: the byte-ranges were transferred incorrectly if the $r->sendfile() was used.
author Igor Sysoev <http://sysoev.ru>
date Tue, 09 Jan 2007 00:00:00 +0300
parents
children 052a7b1d40e5
comparison
equal deleted inserted replaced
271:fcbee7dacf2b 272:29a6403156b0
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 {
13 u_char color;
14 u_char len;
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 {
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 {
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"),
54 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE3,
55 ngx_http_limit_zone,
56 0,
57 0,
58 NULL },
59
60 { ngx_string("limit_conn"),
61 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
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;
111 ngx_http_limit_zone_ctx_t *ctx;
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
116 if (r->main->limit_zone_set) {
117 return NGX_DECLINED;
118 }
119
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
126 ctx = lzcf->shm_zone->data;
127
128 vv = ngx_http_get_indexed_variable(r, ctx->index);
129
130 if (vv == NULL || vv->not_found) {
131 return NGX_DECLINED;
132 }
133
134 r->limit_zone_set = 1;
135
136 len = vv->len;
137
138 hash = ngx_crc32_short(vv->data, len);
139
140 cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_limit_zone_cleanup_t));
141 if (cln == NULL) {
142 return NGX_HTTP_INTERNAL_SERVER_ERROR;
143 }
144
145 shpool = (ngx_slab_pool_t *) lzcf->shm_zone->shm.addr;
146
147 ngx_shmtx_lock(&shpool->mutex);
148
149 node = ctx->rbtree->root;
150 sentinel = ctx->rbtree->sentinel;
151
152 while (node != sentinel) {
153
154 if (hash < node->key) {
155 node = node->left;
156 continue;
157 }
158
159 if (hash > node->key) {
160 node = node->right;
161 continue;
162 }
163
164 if (hash == node->key ){
165 lz = (ngx_http_limit_zone_node_t *) &node->color;
166
167 if (len == (size_t) lz->len
168 && ngx_strncmp(lz->data, vv->data, len) == 0)
169 {
170 if ((ngx_uint_t) lz->conn < lzcf->conn) {
171 lz->conn++;
172 goto done;
173 }
174
175 ngx_shmtx_unlock(&shpool->mutex);
176
177 return NGX_HTTP_SERVICE_UNAVAILABLE;
178 }
179 }
180 }
181
182 n = offsetof(ngx_rbtree_node_t, color)
183 + offsetof(ngx_http_limit_zone_node_t, data)
184 + len;
185
186 node = ngx_slab_alloc_locked(shpool, n);
187 if (node == NULL) {
188 ngx_shmtx_unlock(&shpool->mutex);
189 return NGX_HTTP_SERVICE_UNAVAILABLE;
190 }
191
192 lz = (ngx_http_limit_zone_node_t *) &node->color;
193
194 node->key = hash;
195 lz->len = (u_char) len;
196 lz->conn = 1;
197 ngx_memcpy(lz->data, vv->data, len);
198
199 ngx_rbtree_insert(ctx->rbtree, node);
200
201 done:
202
203 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
204 "limit zone: %08XD %d", node->key, lz->conn);
205
206 ngx_shmtx_unlock(&shpool->mutex);
207
208 cln->handler = ngx_http_limit_zone_cleanup;
209 lzcln = cln->data;
210
211 lzcln->shm_zone = lzcf->shm_zone;
212 lzcln->node = node;
213
214 return NGX_DECLINED;
215 }
216
217
218 static void
219 ngx_http_limit_zone_cleanup(void *data)
220 {
221 ngx_http_limit_zone_cleanup_t *lzcln = data;
222
223 ngx_slab_pool_t *shpool;
224 ngx_rbtree_node_t *node;
225 ngx_http_limit_zone_ctx_t *ctx;
226 ngx_http_limit_zone_node_t *lz;
227
228 ctx = lzcln->shm_zone->data;
229 shpool = (ngx_slab_pool_t *) lzcln->shm_zone->shm.addr;
230 node = lzcln->node;
231 lz = (ngx_http_limit_zone_node_t *) &node->color;
232
233 ngx_shmtx_lock(&shpool->mutex);
234
235 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, lzcln->shm_zone->shm.log, 0,
236 "limit zone cleanup: %08XD %d", node->key, lz->conn);
237
238 lz->conn--;
239
240 if (lz->conn == 0) {
241 ngx_rbtree_delete(ctx->rbtree, node);
242 ngx_slab_free_locked(shpool, node);
243 }
244
245 ngx_shmtx_unlock(&shpool->mutex);
246 }
247
248
249 static ngx_int_t
250 ngx_http_limit_zone_init_zone(ngx_shm_zone_t *shm_zone, void *data)
251 {
252 ngx_http_limit_zone_ctx_t *octx = data;
253
254 ngx_slab_pool_t *shpool;
255 ngx_rbtree_node_t *sentinel;
256 ngx_http_limit_zone_ctx_t *ctx;
257
258 ctx = shm_zone->data;
259
260 if (octx) {
261 if (ngx_strcmp(ctx->var.data, octx->var.data) != 0) {
262 ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0,
263 "limit_zone \"%V\" use the \"%V\" variable "
264 "while previously it used the \"%V\" variable",
265 &shm_zone->name, &ctx->var, &octx->var);
266 return NGX_ERROR;
267 }
268
269 ctx->rbtree = octx->rbtree;
270
271 return NGX_OK;
272 }
273
274 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
275
276 ctx->rbtree = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_t));
277 if (ctx->rbtree == NULL) {
278 return NGX_ERROR;
279 }
280
281 sentinel = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_node_t));
282 if (sentinel == NULL) {
283 return NGX_ERROR;
284 }
285
286 ngx_rbtree_sentinel_init(sentinel);
287
288 ctx->rbtree->root = sentinel;
289 ctx->rbtree->sentinel = sentinel;
290 ctx->rbtree->insert = ngx_rbtree_insert_value;
291
292 return NGX_OK;
293 }
294
295
296 static void *
297 ngx_http_limit_zone_create_conf(ngx_conf_t *cf)
298 {
299 ngx_http_limit_zone_conf_t *conf;
300
301 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_zone_conf_t));
302 if (conf == NULL) {
303 return NGX_CONF_ERROR;
304 }
305
306 /*
307 * set by ngx_pcalloc():
308 *
309 * conf->shm_zone = NULL;
310 * conf->conn = 0;
311 */
312
313 return conf;
314 }
315
316
317 static char *
318 ngx_http_limit_zone_merge_conf(ngx_conf_t *cf, void *parent, void *child)
319 {
320 ngx_http_limit_zone_conf_t *prev = parent;
321 ngx_http_limit_zone_conf_t *conf = child;
322
323 if (conf->shm_zone == NULL) {
324 *conf = *prev;
325 }
326
327 return NGX_CONF_OK;
328 }
329
330
331 static char *
332 ngx_http_limit_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
333 {
334 ssize_t n;
335 ngx_str_t *value;
336 ngx_shm_zone_t *shm_zone;
337 ngx_http_limit_zone_ctx_t *ctx;
338
339 value = cf->args->elts;
340
341 if (value[2].data[0] != '$') {
342 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
343 "invalid variable name \"%V\"", &value[2]);
344 return NGX_CONF_ERROR;
345 }
346
347 value[2].len--;
348 value[2].data++;
349
350 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_zone_ctx_t));
351 if (ctx == NULL) {
352 return NGX_CONF_ERROR;
353 }
354
355 ctx->index = ngx_http_get_variable_index(cf, &value[2]);
356 if (ctx->index == NGX_ERROR) {
357 return NGX_CONF_ERROR;
358 }
359
360 ctx->var = value[2];
361
362 n = ngx_parse_size(&value[3]);
363
364 if (n == NGX_ERROR) {
365 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
366 "invalid size of limit_zone \"%V\"", &value[3]);
367 return NGX_CONF_ERROR;
368 }
369
370 if (n < (ngx_int_t) (8 * ngx_pagesize)) {
371 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
372 "limit_zone \"%V\" is too small", &value[1]);
373 return NGX_CONF_ERROR;
374 }
375
376
377 shm_zone = ngx_shared_memory_add(cf, &value[1], n,
378 &ngx_http_limit_zone_module);
379 if (shm_zone == NULL) {
380 return NGX_CONF_ERROR;
381 }
382
383 if (shm_zone->data) {
384 ctx = shm_zone->data;
385
386 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
387 "limit_zone \"%V\" is already bound to variable \"%V\"",
388 &value[1], &ctx->var);
389 return NGX_CONF_ERROR;
390 }
391
392 shm_zone->init = ngx_http_limit_zone_init_zone;
393 shm_zone->data = ctx;
394
395 return NGX_CONF_OK;
396 }
397
398
399 static char *
400 ngx_http_limit_conn(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
401 {
402 ngx_http_limit_zone_conf_t *lzcf = conf;
403
404 ngx_int_t n;
405 ngx_str_t *value;
406
407 value = cf->args->elts;
408
409 lzcf->shm_zone = ngx_shared_memory_add(cf, &value[1], 0,
410 &ngx_http_limit_zone_module);
411 if (lzcf->shm_zone == NULL) {
412 return NGX_CONF_ERROR;
413 }
414
415 n = ngx_atoi(value[2].data, value[2].len);
416 if (n <= 0) {
417 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
418 "invalid number of connections \"%V\"", &value[2]);
419 return NGX_CONF_ERROR;
420 }
421
422 lzcf->conn = n;
423
424 return NGX_CONF_OK;
425 }
426
427
428 static ngx_int_t
429 ngx_http_limit_zone_init(ngx_conf_t *cf)
430 {
431 ngx_http_handler_pt *h;
432 ngx_http_core_main_conf_t *cmcf;
433
434 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
435
436 h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers);
437 if (h == NULL) {
438 return NGX_ERROR;
439 }
440
441 *h = ngx_http_limit_zone_handler;
442
443 return NGX_OK;
444 }