Line data Source code
1 : #ifndef DDEBUG
2 : #define DDEBUG 0
3 : #endif
4 : #include "ddebug.h"
5 :
6 : #include <ndk.h>
7 : #include "ngx_http_set_rotate.h"
8 : #include "ngx_http_set_misc_module.h"
9 : #include <stdlib.h>
10 :
11 :
12 : ngx_int_t
13 0 : ngx_http_set_misc_set_rotate(ngx_http_request_t *r, ngx_str_t *res,
14 : ngx_http_variable_value_t *v)
15 : {
16 : ngx_http_variable_value_t *rotate_from, *rotate_to, *rotate_num;
17 : ngx_int_t int_from, int_to, tmp, int_current;
18 :
19 : ngx_http_set_misc_loc_conf_t *conf;
20 :
21 0 : rotate_num = &v[0];
22 0 : rotate_from = &v[1];
23 0 : rotate_to = &v[2];
24 :
25 0 : int_from = ngx_atoi(rotate_from->data, rotate_from->len);
26 0 : if (int_from == NGX_ERROR) {
27 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
28 : "set_rotate: bad \"from\" argument value: \"%v\"",
29 : rotate_from);
30 0 : return NGX_ERROR;
31 : }
32 :
33 0 : int_to = ngx_atoi(rotate_to->data, rotate_to->len);
34 0 : if (int_to == NGX_ERROR) {
35 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
36 : "set_rotate: bad \"to\" argument value: \"%v\"",
37 : rotate_to);
38 0 : return NGX_ERROR;
39 : }
40 :
41 0 : if (int_from > int_to) {
42 0 : tmp = int_from;
43 0 : int_from = int_to;
44 0 : int_to = tmp;
45 : }
46 :
47 0 : conf = ngx_http_get_module_loc_conf(r, ngx_http_set_misc_module);
48 :
49 : dd("current value not found: %d", (int) rotate_num->not_found);
50 :
51 0 : if (rotate_num->len == 0) {
52 0 : if (conf->current != NGX_CONF_UNSET) {
53 0 : int_current = conf->current;
54 :
55 : } else {
56 0 : int_current = int_from - 1;
57 : }
58 :
59 : } else {
60 :
61 0 : int_current = ngx_atoi(rotate_num->data, rotate_num->len);
62 0 : if (int_current == NGX_ERROR) {
63 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
64 : "set_rotate: bad current value: \"%v\"", rotate_num);
65 :
66 0 : if (conf->current != NGX_CONF_UNSET) {
67 0 : int_current = conf->current;
68 :
69 : } else {
70 0 : int_current = int_from - 1;
71 : }
72 : }
73 : }
74 :
75 0 : int_current++;
76 :
77 0 : if (int_current > int_to || int_current < int_from) {
78 0 : int_current = int_from;
79 : }
80 :
81 0 : conf->current = int_current;
82 :
83 0 : res->data = ngx_palloc(r->pool, NGX_INT_T_LEN);
84 0 : if (res->data == NULL) {
85 0 : return NGX_ERROR;
86 : }
87 :
88 0 : res->len = ngx_sprintf(res->data, "%i", int_current) - res->data;
89 :
90 0 : v->valid = 1;
91 0 : v->no_cacheable = 0;
92 0 : v->not_found = 0;
93 :
94 0 : return NGX_OK;
95 : }
96 :
97 :
98 : char *
99 0 : ngx_http_set_rotate(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
100 : {
101 : ngx_str_t *value;
102 : ndk_set_var_t filter;
103 :
104 0 : value = cf->args->elts;
105 :
106 0 : filter.type = NDK_SET_VAR_MULTI_VALUE;
107 0 : filter.func = (void *) ngx_http_set_misc_set_rotate;
108 0 : filter.size = 3;
109 0 : filter.data = NULL;
110 :
111 0 : return ndk_set_var_multi_value_core(cf, &value[1], &value[1], &filter);
112 : }
113 :
|