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