Mercurial > hg > nginx
comparison src/http/modules/ngx_http_userid_filter_module.c @ 3943:bfab2536197b
$uid_reset
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 19 Jul 2011 11:21:50 +0000 |
parents | 8649a0c0f09d |
children | d620f497c50f |
comparison
equal
deleted
inserted
replaced
3942:2e8dbd773d63 | 3943:bfab2536197b |
---|---|
36 | 36 |
37 typedef struct { | 37 typedef struct { |
38 uint32_t uid_got[4]; | 38 uint32_t uid_got[4]; |
39 uint32_t uid_set[4]; | 39 uint32_t uid_set[4]; |
40 ngx_str_t cookie; | 40 ngx_str_t cookie; |
41 ngx_uint_t reset; | |
41 } ngx_http_userid_ctx_t; | 42 } ngx_http_userid_ctx_t; |
42 | 43 |
43 | 44 |
44 static ngx_http_userid_ctx_t *ngx_http_userid_get_uid(ngx_http_request_t *r, | 45 static ngx_http_userid_ctx_t *ngx_http_userid_get_uid(ngx_http_request_t *r, |
45 ngx_http_userid_conf_t *conf); | 46 ngx_http_userid_conf_t *conf); |
183 NULL, /* exit master */ | 184 NULL, /* exit master */ |
184 NGX_MODULE_V1_PADDING | 185 NGX_MODULE_V1_PADDING |
185 }; | 186 }; |
186 | 187 |
187 | 188 |
188 static ngx_str_t ngx_http_userid_got = ngx_string("uid_got"); | 189 static ngx_str_t ngx_http_userid_got = ngx_string("uid_got"); |
189 static ngx_str_t ngx_http_userid_set = ngx_string("uid_set"); | 190 static ngx_str_t ngx_http_userid_set = ngx_string("uid_set"); |
191 static ngx_str_t ngx_http_userid_reset = ngx_string("uid_reset"); | |
192 static ngx_uint_t ngx_http_userid_reset_index; | |
190 | 193 |
191 | 194 |
192 static ngx_int_t | 195 static ngx_int_t |
193 ngx_http_userid_filter(ngx_http_request_t *r) | 196 ngx_http_userid_filter(ngx_http_request_t *r) |
194 { | 197 { |
385 } | 388 } |
386 | 389 |
387 p = ngx_copy(cookie, conf->name.data, conf->name.len); | 390 p = ngx_copy(cookie, conf->name.data, conf->name.len); |
388 *p++ = '='; | 391 *p++ = '='; |
389 | 392 |
390 if (ctx->uid_got[3] == 0) { | 393 if (ctx->uid_got[3] == 0 || ctx->reset) { |
391 src.len = 16; | 394 src.len = 16; |
392 src.data = (u_char *) ctx->uid_set; | 395 src.data = (u_char *) ctx->uid_set; |
393 dst.data = p; | 396 dst.data = p; |
394 | 397 |
395 ngx_encode_base64(&dst, &src); | 398 ngx_encode_base64(&dst, &src); |
450 | 453 |
451 static ngx_int_t | 454 static ngx_int_t |
452 ngx_http_userid_create_uid(ngx_http_request_t *r, ngx_http_userid_ctx_t *ctx, | 455 ngx_http_userid_create_uid(ngx_http_request_t *r, ngx_http_userid_ctx_t *ctx, |
453 ngx_http_userid_conf_t *conf) | 456 ngx_http_userid_conf_t *conf) |
454 { | 457 { |
455 ngx_connection_t *c; | 458 ngx_connection_t *c; |
456 struct sockaddr_in *sin; | 459 struct sockaddr_in *sin; |
460 ngx_http_variable_value_t *vv; | |
457 #if (NGX_HAVE_INET6) | 461 #if (NGX_HAVE_INET6) |
458 u_char *p; | 462 u_char *p; |
459 struct sockaddr_in6 *sin6; | 463 struct sockaddr_in6 *sin6; |
460 #endif | 464 #endif |
461 | 465 |
462 if (ctx->uid_set[3] != 0) { | 466 if (ctx->uid_set[3] != 0) { |
463 return NGX_OK; | 467 return NGX_OK; |
464 } | 468 } |
465 | 469 |
466 if (ctx->uid_got[3] != 0) { | 470 if (ctx->uid_got[3] != 0) { |
467 | 471 |
468 if (conf->mark == '\0' | 472 vv = ngx_http_get_indexed_variable(r, ngx_http_userid_reset_index); |
469 || (ctx->cookie.len > 23 | 473 |
470 && ctx->cookie.data[22] == conf->mark | 474 if (vv->len == 0 || (vv->len == 1 && vv->data[0] == '0')) { |
471 && ctx->cookie.data[23] == '=')) | 475 |
472 { | 476 if (conf->mark == '\0' |
477 || (ctx->cookie.len > 23 | |
478 && ctx->cookie.data[22] == conf->mark | |
479 && ctx->cookie.data[23] == '=')) | |
480 { | |
481 return NGX_OK; | |
482 } | |
483 | |
484 ctx->uid_set[0] = ctx->uid_got[0]; | |
485 ctx->uid_set[1] = ctx->uid_got[1]; | |
486 ctx->uid_set[2] = ctx->uid_got[2]; | |
487 ctx->uid_set[3] = ctx->uid_got[3]; | |
488 | |
473 return NGX_OK; | 489 return NGX_OK; |
490 | |
491 } else { | |
492 ctx->reset = 1; | |
493 | |
494 if (vv->len == 3 && ngx_strncmp(vv->data, "log", 3) == 0) { | |
495 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, | |
496 "userid cookie \"%V=%08XD%08XD%08XD%08XD\" was reset", | |
497 &conf->name, ctx->uid_got[0], ctx->uid_got[1], | |
498 ctx->uid_got[2], ctx->uid_got[3]); | |
499 } | |
474 } | 500 } |
475 | |
476 ctx->uid_set[0] = ctx->uid_got[0]; | |
477 ctx->uid_set[1] = ctx->uid_got[1]; | |
478 ctx->uid_set[2] = ctx->uid_got[2]; | |
479 ctx->uid_set[3] = ctx->uid_got[3]; | |
480 | |
481 return NGX_OK; | |
482 } | 501 } |
483 | 502 |
484 /* | 503 /* |
485 * TODO: in the threaded mode the sequencers should be in TLS and their | 504 * TODO: in the threaded mode the sequencers should be in TLS and their |
486 * ranges should be divided between threads | 505 * ranges should be divided between threads |
564 return NGX_OK; | 583 return NGX_OK; |
565 } | 584 } |
566 | 585 |
567 | 586 |
568 static ngx_int_t | 587 static ngx_int_t |
588 ngx_http_userid_reset_variable(ngx_http_request_t *r, | |
589 ngx_http_variable_value_t *v, uintptr_t data) | |
590 { | |
591 *v = ngx_http_variable_null_value; | |
592 | |
593 return NGX_OK; | |
594 } | |
595 | |
596 | |
597 static ngx_int_t | |
569 ngx_http_userid_add_variables(ngx_conf_t *cf) | 598 ngx_http_userid_add_variables(ngx_conf_t *cf) |
570 { | 599 { |
600 ngx_int_t n; | |
571 ngx_http_variable_t *var; | 601 ngx_http_variable_t *var; |
572 | 602 |
573 var = ngx_http_add_variable(cf, &ngx_http_userid_got, 0); | 603 var = ngx_http_add_variable(cf, &ngx_http_userid_got, 0); |
574 if (var == NULL) { | 604 if (var == NULL) { |
575 return NGX_ERROR; | 605 return NGX_ERROR; |
581 if (var == NULL) { | 611 if (var == NULL) { |
582 return NGX_ERROR; | 612 return NGX_ERROR; |
583 } | 613 } |
584 | 614 |
585 var->get_handler = ngx_http_userid_set_variable; | 615 var->get_handler = ngx_http_userid_set_variable; |
616 | |
617 var = ngx_http_add_variable(cf, &ngx_http_userid_reset, | |
618 NGX_HTTP_VAR_CHANGEABLE); | |
619 if (var == NULL) { | |
620 return NGX_ERROR; | |
621 } | |
622 | |
623 var->get_handler = ngx_http_userid_reset_variable; | |
624 | |
625 n = ngx_http_get_variable_index(cf, &ngx_http_userid_reset); | |
626 if (n == NGX_ERROR) { | |
627 return NGX_ERROR; | |
628 } | |
629 | |
630 ngx_http_userid_reset_index = n; | |
586 | 631 |
587 return NGX_OK; | 632 return NGX_OK; |
588 } | 633 } |
589 | 634 |
590 | 635 |