Permalink
Browse files

various fixes in the patches from viperet for the new set_rotate dire…

…ctive.
  • Loading branch information...
1 parent f091d5b commit 9e061396a121cffa8139cf2d44f719c9700fcb47 @agentzh agentzh committed Apr 17, 2012
Showing with 191 additions and 17 deletions.
  1. +2 −0 .gitignore
  2. +32 −0 README
  3. +33 −0 README.markdown
  4. +1 −8 src/ngx_http_set_misc_module.c
  5. +12 −9 src/ngx_http_set_rotate.c
  6. +9 −0 src/ngx_http_set_rotate.h
  7. +102 −0 t/rotate.t
View
2 .gitignore
@@ -56,6 +56,8 @@ src/hex.c
src/hex.h
src/hmac.c
src/hmac.h
+src/rotate.[ch]
+analyze
buildroot/
src/module.h
work/
View
32 README
@@ -793,6 +793,38 @@ Directives
Behind the scene, it makes use of the standard C function "rand()".
+ set_rotate
+ syntax: *set_random $value <from> <to>*
+
+ default: *no*
+
+ context: *location, location if*
+
+ phase: *rewrite*
+
+ Increments $value but keeps it in range from $from to $to. If $value is
+ greater than $to or less than $from is will be set to $from value.
+
+ Only non-negative numbers are allowed for the "<from>" and "<to>"
+ arguments.
+
+ When "<$from>" is greater than "<$to>", their values will be exchanged
+ accordingly.
+
+ For instance,
+
+ location /rotate {
+ default_type text/plain;
+ set $counter $cookie_counter;
+ set_rotate $counter 1 5;
+ echo $counter;
+ add_header Set-Cookie counter=$counter;
+ }
+
+ then request "GET /rotate" will output next number between 1 and 5
+ (i.e., 1, 2, 3, 4, 5) on each refresh of the page. This directive may be
+ userful for banner rotation purposes.
+
set_local_today
syntax: *set_local_today $dst*
View
33 README.markdown
@@ -771,6 +771,39 @@ For now, there's no way to configure a custom random generator seed.
Behind the scene, it makes use of the standard C function `rand()`.
+set_rotate
+----------
+**syntax:** *set_random $value &lt;from&gt; &lt;to&gt;*
+
+**default:** *no*
+
+**context:** *location, location if*
+
+**phase:** *rewrite*
+
+Increments `$value` but keeps it in range from `$from` to `$to`.
+If `$value` is greater than `$to` or less than `$from` is will be
+set to `$from` value.
+
+Only non-negative numbers are allowed for the `<from>` and `<to>` arguments.
+
+When `<$from>` is greater than `<$to>`, their values will be exchanged accordingly.
+
+For instance,
+
+
+ location /rotate {
+ default_type text/plain;
+ set $counter $cookie_counter;
+ set_rotate $counter 1 5;
+ echo $counter;
+ add_header Set-Cookie counter=$counter;
+ }
+
+
+then request `GET /rotate` will output next number between 1 and 5 (i.e., 1, 2, 3, 4, 5) on each
+refresh of the page. This directive may be userful for banner rotation purposes.
+
set_local_today
---------------
**syntax:** *set_local_today $dst*
View
9 src/ngx_http_set_misc_module.c
@@ -147,14 +147,7 @@ static ndk_set_var_t ngx_http_set_misc_set_random_filter = {
2,
NULL
};
-/*
-static ndk_set_var_t ngx_http_set_misc_set_rotate_filter = {
- NDK_SET_VAR_VALUE,
- ngx_http_set_misc_set_rotate,
- 2,
- NULL
-};
-*/
+
static ngx_command_t ngx_http_set_misc_commands[] = {
{ ngx_string ("set_encode_base64"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
View
21 src/ngx_http_set_rotate.c
@@ -7,9 +7,10 @@
#include "ngx_http_set_rotate.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_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;
@@ -18,20 +19,19 @@ ngx_http_set_misc_set_rotate(ngx_http_request_t *r,
rotate_from = &v[1];
rotate_to = &v[2];
-// rotate_from = v;
-// rotate_to = v + 1;
-
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: %v", rotate_from);
+ "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: %v", rotate_to);
+ "set_rotate: bad \"to\" argument value: \"%v\"",
+ rotate_to);
return NGX_ERROR;
}
@@ -44,13 +44,15 @@ ngx_http_set_misc_set_rotate(ngx_http_request_t *r,
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 \"default\" argument: %v", rotate_to);
+ "set_rotate: bad current value: \"%v\"", rotate_num);
int_current = int_from;
}
int_current++;
- if(int_current>int_to || int_current<int_from)
+
+ if (int_current > int_to || int_current < int_from) {
int_current = int_from;
+ }
res->data = ngx_palloc(r->pool, NGX_INT_T_LEN);
if (res->data == NULL) {
@@ -59,14 +61,14 @@ ngx_http_set_misc_set_rotate(ngx_http_request_t *r,
res->len = ngx_sprintf(res->data, "%i", int_current) - res->data;
- /* Set all required params */
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)
{
@@ -82,3 +84,4 @@ ngx_http_set_rotate(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
return ndk_set_var_multi_value_core(cf, &value[1], &value[1], &filter);
}
+
View
9 src/ngx_http_set_rotate.h
@@ -1,9 +1,18 @@
+#ifndef NGX_HTTP_SET_MISC_ROTATE_H
+#define NGX_HTTP_SET_MISC_ROTATE_H
+
+
#include <ngx_core.h>
#include <ngx_config.h>
#include <ngx_http.h>
+
char * ngx_http_set_rotate(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
ngx_int_t ngx_http_set_misc_set_rotate(ngx_http_request_t *r,
ngx_str_t *res, ngx_http_variable_value_t *v);
+
+
+#endif /* NGX_HTTP_SET_MISC_ROTATE_H */
+
View
102 t/rotate.t
@@ -0,0 +1,102 @@
+# vi:filetype=
+
+use Test::Nginx::Socket;
+
+repeat_each(2);
+
+plan tests => repeat_each() * (3 * blocks());
+
+no_long_string();
+
+run_tests();
+
+#no_diff();
+
+__DATA__
+
+=== TEST 1: sanity
+--- config
+ location /bar {
+ set $a 1;
+ set_rotate $a 1 3;
+
+ set $b 2;
+ set_rotate $b 1 3;
+
+ set $c 3;
+ set_rotate $c 1 3;
+
+ set $d 0;
+ set_rotate $d 1 3;
+
+ set $e 1;
+ set_rotate $e 3 5;
+
+ echo "a = $a";
+ echo "b = $b";
+ echo "c = $c";
+ echo "d = $d";
+ echo "e = $e";
+ }
+--- request
+ GET /bar
+--- response_body
+a = 2
+b = 3
+c = 1
+d = 1
+e = 3
+--- no_error_log
+[error]
+
+
+
+=== TEST 2: bad current value
+--- config
+ location /bar {
+ set $a abc;
+ set_rotate $a 1 3;
+
+ echo "a = $a";
+ }
+--- request
+ GET /bar
+--- response_body
+a = 2
+--- error_log
+set_rotate: bad current value: "abc"
+
+
+
+=== TEST 3: bad "from" value
+--- config
+ location /bar {
+ set $a 2;
+ set_rotate $a abc 3;
+
+ echo "a = $a";
+ }
+--- request
+ GET /bar
+--- response_body_like: 500 Internal Server Error
+--- error_code: 500
+--- error_log
+set_rotate: bad "from" argument value: "abc"
+
+
+
+=== TEST 4: bad "to" argument value
+--- config
+ location /bar {
+ set $a 2;
+ set_rotate $a 1 abc;
+
+ echo "a = $a";
+ }
+--- request
+ GET /bar
+--- response_body_like: 500 Internal Server Error
+--- error_code: 500
+--- error_log
+set_rotate: bad "to" argument value: "abc"
+

0 comments on commit 9e06139

Please sign in to comment.