Skip to content

Commit 9792e8d

Browse files
committed
feature: the set_rotate directive will always make the current value persistent and make use of it when either the current value is not given or the current value is invalid.
1 parent 16c7b48 commit 9792e8d

File tree

6 files changed

+161
-7
lines changed

6 files changed

+161
-7
lines changed

src/ngx_http_set_base32.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ ngx_int_t ngx_http_set_misc_decode_base32(ngx_http_request_t *r,
1515

1616

1717
#endif /* NGX_HTTP_SET_BASE32 */
18+

src/ngx_http_set_misc_module.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ ngx_http_set_misc_create_loc_conf(ngx_conf_t *cf)
378378
}
379379

380380
conf->base32_padding = NGX_CONF_UNSET;
381+
conf->current = NGX_CONF_UNSET;
381382

382383
return conf;
383384
}
@@ -391,6 +392,8 @@ ngx_http_set_misc_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
391392

392393
ngx_conf_merge_value(conf->base32_padding, prev->base32_padding, 1);
393394

395+
ngx_conf_merge_value(conf->current, prev->current, NGX_CONF_UNSET);
396+
394397
return NGX_CONF_OK;
395398
}
396399

src/ngx_http_set_misc_module.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
typedef struct {
66
ngx_flag_t base32_padding;
7+
ngx_int_t current; /* for set_rotate */
78
} ngx_http_set_misc_loc_conf_t;
89

910

src/ngx_http_set_rotate.c

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <ndk.h>
77
#include "ngx_http_set_rotate.h"
8+
#include "ngx_http_set_misc_module.h"
89
#include <stdlib.h>
910

1011

@@ -15,6 +16,8 @@ ngx_http_set_misc_set_rotate(ngx_http_request_t *r,
1516
ngx_http_variable_value_t *rotate_from, *rotate_to, *rotate_num;
1617
ngx_int_t int_from, int_to, tmp, int_current;
1718

19+
ngx_http_set_misc_loc_conf_t *conf;
20+
1821
rotate_num = &v[0];
1922
rotate_from = &v[1];
2023
rotate_to = &v[2];
@@ -41,11 +44,32 @@ ngx_http_set_misc_set_rotate(ngx_http_request_t *r,
4144
int_to = tmp;
4245
}
4346

44-
int_current = ngx_atoi(rotate_num->data, rotate_num->len);
45-
if (int_current == NGX_ERROR) {
46-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
47-
"set_rotate: bad current value: \"%v\"", rotate_num);
48-
int_current = int_from;
47+
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+
if (rotate_num->len == 0) {
52+
if (conf->current != NGX_CONF_UNSET) {
53+
int_current = conf->current;
54+
55+
} else {
56+
int_current = int_from - 1;
57+
}
58+
59+
} else {
60+
61+
int_current = ngx_atoi(rotate_num->data, rotate_num->len);
62+
if (int_current == NGX_ERROR) {
63+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
64+
"set_rotate: bad current value: \"%v\"", rotate_num);
65+
66+
if (conf->current != NGX_CONF_UNSET) {
67+
int_current = conf->current;
68+
69+
} else {
70+
int_current = int_from - 1;
71+
}
72+
}
4973
}
5074

5175
int_current++;
@@ -54,6 +78,8 @@ ngx_http_set_misc_set_rotate(ngx_http_request_t *r,
5478
int_current = int_from;
5579
}
5680

81+
conf->current = int_current;
82+
5783
res->data = ngx_palloc(r->pool, NGX_INT_T_LEN);
5884
if (res->data == NULL) {
5985
return NGX_ERROR;

t/rotate.t

Lines changed: 123 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ e = 3
6161
}
6262
--- request
6363
GET /bar
64-
--- response_body
65-
a = 2
64+
--- response_body_like: ^a = [12]$
6665
--- error_log
6766
set_rotate: bad current value: "abc"
6867

@@ -100,3 +99,125 @@ set_rotate: bad "from" argument value: "abc"
10099
--- error_log
101100
set_rotate: bad "to" argument value: "abc"
102101

102+
103+
104+
=== TEST 5: when no current value is given
105+
--- config
106+
location /incr {
107+
set_rotate $a 1 3;
108+
109+
echo "a = $a";
110+
}
111+
112+
location /t {
113+
echo_location /incr;
114+
echo_location /incr;
115+
echo_location /incr;
116+
echo_location /incr;
117+
echo_location /incr;
118+
echo_location /incr;
119+
}
120+
--- request
121+
GET /t
122+
--- response_body
123+
a = 1
124+
a = 2
125+
a = 3
126+
a = 1
127+
a = 2
128+
a = 3
129+
--- no_error_log
130+
[error]
131+
132+
133+
134+
=== TEST 6: when no current value is given (starting from 0)
135+
--- config
136+
location /incr {
137+
set_rotate $a 0 2;
138+
139+
echo "a = $a";
140+
}
141+
142+
location /t {
143+
echo_location /incr;
144+
echo_location /incr;
145+
echo_location /incr;
146+
echo_location /incr;
147+
echo_location /incr;
148+
echo_location /incr;
149+
}
150+
--- request
151+
GET /t
152+
--- response_body
153+
a = 0
154+
a = 1
155+
a = 2
156+
a = 0
157+
a = 1
158+
a = 2
159+
--- no_error_log
160+
[error]
161+
162+
163+
164+
=== TEST 7: when a non-integer string value is given
165+
--- config
166+
location /incr {
167+
set $a "hello";
168+
set_rotate $a 0 2;
169+
170+
echo "a = $a";
171+
}
172+
173+
location /t {
174+
echo_location /incr;
175+
echo_location /incr;
176+
echo_location /incr;
177+
echo_location /incr;
178+
echo_location /incr;
179+
echo_location /incr;
180+
}
181+
--- request
182+
GET /t
183+
--- response_body
184+
a = 0
185+
a = 1
186+
a = 2
187+
a = 0
188+
a = 1
189+
a = 2
190+
--- error_log
191+
set_rotate: bad current value: "hello"
192+
193+
194+
195+
=== TEST 8: when an empty string value is given
196+
--- config
197+
location /incr {
198+
set $a "";
199+
set_rotate $a 0 2;
200+
201+
echo "a = $a";
202+
}
203+
204+
location /t {
205+
echo_location /incr;
206+
echo_location /incr;
207+
echo_location /incr;
208+
echo_location /incr;
209+
echo_location /incr;
210+
echo_location /incr;
211+
}
212+
--- request
213+
GET /t
214+
--- response_body
215+
a = 0
216+
a = 1
217+
a = 2
218+
a = 0
219+
a = 1
220+
a = 2
221+
--- no_error_log
222+
[error]
223+

util/build.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ home=~
77
version=$1
88
force=$2
99

10+
#--with-cc="gcc46" \
11+
1012
ngx-build $force $version \
1113
--with-http_ssl_module \
1214
--without-mail_pop3_module \

0 commit comments

Comments
 (0)