Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

merged master into the secure-random-setters branch from Jeremy Wohl.

  • Loading branch information...
commit c193183208ad2b290e034961920ac4b3c64473fc 2 parents 5441a7e + 87d0ab2
@agentzh agentzh authored
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
83 README
@@ -6,9 +6,9 @@ Name
installation instructions.
Version
- This document describes set-misc-nginx-module v0.22rc3
- (<https://github.com/agentzh/set-misc-nginx-module/tags>) released on 10
- October 2011.
+ This document describes set-misc-nginx-module v0.22rc7
+ (<https://github.com/agentzh/set-misc-nginx-module/tags>) released on 17
+ April 2012.
Synopsis
location /foo {
@@ -756,7 +756,9 @@ Directives
(HttpEchoModule#echo)] here to output values of nginx variables
directly.
- This directive requires the OpenSSL library enabled in your Nignx build.
+ This directive requires the OpenSSL library enabled in your Nignx build
+ (usually by passing the "--with-http_ssl_module" option to the
+ "./configure" script).
set_random
syntax: *set_random $res <from> <to>*
@@ -773,7 +775,7 @@ Directives
Only non-negative numbers are allowed for the "<from>" and "<to>"
arguments.
- When "<$from>" is greater than "<$to>", their values will be exchanged
+ When "<from>" is greater than "<to>", their values will be exchanged
accordingly.
For instance,
@@ -793,6 +795,57 @@ 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.
+
+ The current value after running this directive will always be saved on a
+ per-location basis. And the this saved value will be used for
+ incrementation when the $value is not initialized or has a bad 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.
+
+ Another example is to use server-side value persistence to do simple
+ round-robin:
+
+ location /rotate {
+ default_type text/plain;
+ set_rotate $counter 0 3;
+ echo $counter;
+ }
+
+ And accessing "/rotate" will also output integer sequence 0, 1, 2, 3, 0,
+ 1, 2, 3, and so on.
+
+ This directive was first introduced in the "v0.22rc7" release.
+
set_local_today
syntax: *set_local_today $dst*
@@ -823,22 +876,22 @@ Directives
mechanism in the Nginx core.
Caveats
- Do not use $arg_PARAMETER or $http_HEADER or other special variables
- defined in the nginx core module as the target variable in this module's
- directives. For instance,
+ Do not use $arg_PARAMETER, $cookie_COOKIE, $http_HEADER or other special
+ variables defined in the Nginx core module as the target variable in
+ this module's directives. For instance,
set_if_empty $arg_user 'foo'; # DO NOT USE THIS!
- may lead to data corruption.
+ may lead to segmentation faults.
Installation
Grab the nginx source code from nginx.org (<http://nginx.org/>), for
- example, the version 1.0.8 (see nginx compatibility), and then build the
- source with this module:
+ example, the version 1.0.15 (see nginx compatibility), and then build
+ the source with this module:
- wget 'http://nginx.org/download/nginx-1.0.8.tar.gz'
- tar -xzvf nginx-1.0.8.tar.gz
- cd nginx-1.0.8/
+ wget 'http://nginx.org/download/nginx-1.0.15.tar.gz'
+ tar -xzvf nginx-1.0.15.tar.gz
+ cd nginx-1.0.15/
# Here we assume you would install you nginx under /opt/nginx/.
./configure --prefix=/opt/nginx \
@@ -863,7 +916,7 @@ Compatibility
* 1.1.x (last tested: 1.1.5)
- * 1.0.x (last tested: 1.0.8)
+ * 1.0.x (last tested: 1.0.15)
* 0.9.x (last tested: 0.9.4)
View
69 README.markdown
@@ -8,7 +8,7 @@ Name
Version
=======
-This document describes set-misc-nginx-module [v0.22rc3](https://github.com/agentzh/set-misc-nginx-module/tags) released on 10 October 2011.
+This document describes set-misc-nginx-module [v0.22rc7](https://github.com/agentzh/set-misc-nginx-module/tags) released on 17 April 2012.
Synopsis
========
@@ -735,7 +735,7 @@ Then request `GET /test` will yield the following output
Please note that we're using [HttpEchoModule](http://wiki.nginx.org/HttpEchoModule)'s [echo directive](http://wiki.nginx.org/HttpEchoModule#echo) here to output values of nginx variables directly.
-This directive requires the OpenSSL library enabled in your Nignx build.
+This directive requires the OpenSSL library enabled in your Nignx build (usually by passing the `--with-http_ssl_module` option to the `./configure` script).
set_random
----------
@@ -751,7 +751,7 @@ Generates a (pseudo) random number (in textual form) within the range `[<$from>,
Only non-negative numbers are allowed for the `<from>` and `<to>` arguments.
-When `<$from>` is greater than `<$to>`, their values will be exchanged accordingly.
+When `<from>` is greater than `<to>`, their values will be exchanged accordingly.
For instance,
@@ -827,6 +827,55 @@ then request `GET /test` will output a string like "kcuxcddktffsippuekhshdaclaqu
This function depends on the presence of the "/dev/urandom" device, available on most UNIX-like systems.
+set_rotate
+----------
+**syntax:** *set_rotate $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.
+
+The current value after running this directive will always be saved on a per-location basis. And the this saved value will be used for incrementation when the `$value` is not initialized or has a bad 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.
+
+Another example is to use server-side value persistence to do simple round-robin:
+
+
+ location /rotate {
+ default_type text/plain;
+ set_rotate $counter 0 3;
+ echo $counter;
+ }
+
+
+And accessing `/rotate` will also output integer sequence 0, 1, 2, 3, 0, 1, 2, 3, and so on.
+
+This directive was first introduced in the `v0.22rc7` release.
+
set_local_today
---------------
**syntax:** *set_local_today $dst*
@@ -861,24 +910,24 @@ Behind the scene, this directive utilizes the `ngx_time` API in the Nginx core,
Caveats
=======
-Do not use [$arg_PARAMETER](http://wiki.nginx.org/HttpCoreModule#.24arg_PARAMETER) or [$http_HEADER](http://wiki.nginx.org/HttpCoreModule#.24http_HEADER) or other special variables defined in the nginx core module as the target variable in this module's directives. For instance,
+Do not use [$arg_PARAMETER](http://wiki.nginx.org/HttpCoreModule#.24arg_PARAMETER), [$cookie_COOKIE](http://wiki.nginx.org/HttpCoreModule#.24cookie_COOKIE), [$http_HEADER](http://wiki.nginx.org/HttpCoreModule#.24http_HEADER) or other special variables defined in the Nginx core module as the target variable in this module's directives. For instance,
set_if_empty $arg_user 'foo'; # DO NOT USE THIS!
-may lead to data corruption.
+may lead to segmentation faults.
Installation
============
Grab the nginx source code from [nginx.org](http://nginx.org/), for example,
-the version 1.0.8 (see [nginx compatibility](http://wiki.nginx.org/HttpSetMiscModule#Compatibility)), and then build the source with this module:
+the version 1.0.15 (see [nginx compatibility](http://wiki.nginx.org/HttpSetMiscModule#Compatibility)), and then build the source with this module:
- wget 'http://nginx.org/download/nginx-1.0.8.tar.gz'
- tar -xzvf nginx-1.0.8.tar.gz
- cd nginx-1.0.8/
+ wget 'http://nginx.org/download/nginx-1.0.15.tar.gz'
+ tar -xzvf nginx-1.0.15.tar.gz
+ cd nginx-1.0.15/
# Here we assume you would install you nginx under /opt/nginx/.
./configure --prefix=/opt/nginx \
@@ -900,7 +949,7 @@ Compatibility
The following versions of Nginx should work with this module:
* **1.1.x** (last tested: 1.1.5)
-* **1.0.x** (last tested: 1.0.8)
+* **1.0.x** (last tested: 1.0.15)
* **0.9.x** (last tested: 0.9.4)
* **0.8.x** (last tested: 0.8.54)
* **0.7.x >= 0.7.46** (last tested: 0.7.68)
View
4 config
@@ -7,8 +7,8 @@ fi
ngx_addon_name=ngx_http_set_misc_module
HTTP_AUX_FILTER_MODULES="$HTTP_AUX_FILTER_MODULES ngx_http_set_misc_module"
-NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/src/ngx_http_set_base32.c $ngx_addon_dir/src/ngx_http_set_default_value.c $ngx_addon_dir/src/ngx_http_set_hashed_upstream.c $ngx_addon_dir/src/ngx_http_set_quote_sql.c $ngx_addon_dir/src/ngx_http_set_quote_json.c $ngx_addon_dir/src/ngx_http_set_unescape_uri.c $ngx_addon_dir/src/ngx_http_set_misc_module.c $ngx_addon_dir/src/ngx_http_set_escape_uri.c $ngx_addon_dir/src/ngx_http_set_hash.c $ngx_addon_dir/src/ngx_http_set_local_today.c $ngx_addon_dir/src/ngx_http_set_hex.c $ngx_addon_dir/src/ngx_http_set_base64.c $ngx_addon_dir/src/ngx_http_set_random.c $ngx_addon_dir/src/ngx_http_set_secure_random.c"
-NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ngx_addon_dir/src/ddebug.h $ngx_addon_dir/src/ngx_http_set_default_value.h $ngx_addon_dir/src/ngx_http_set_hashed_upstream.h $ngx_addon_dir/src/ngx_http_set_quote_sql.h $ngx_addon_dir/src/ngx_http_set_quote_json.h $ngx_addon_dir/src/ngx_http_set_unescape_uri.h $ngx_addon_dir/src/ngx_http_set_escape_uri.h $ngx_addon_dir/src/ngx_http_set_hash.h $ngx_addon_dir/src/ngx_http_set_local_today.h $ngx_addon_dir/src/ngx_http_set_hex.h $ngx_addon_dir/src/ngx_http_set_base64.h $ngx_addon_dir/src/ngx_http_set_random.h $ngx_addon_dir/src/ngx_http_set_misc_module.h"
+NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/src/ngx_http_set_base32.c $ngx_addon_dir/src/ngx_http_set_default_value.c $ngx_addon_dir/src/ngx_http_set_hashed_upstream.c $ngx_addon_dir/src/ngx_http_set_quote_sql.c $ngx_addon_dir/src/ngx_http_set_quote_json.c $ngx_addon_dir/src/ngx_http_set_unescape_uri.c $ngx_addon_dir/src/ngx_http_set_misc_module.c $ngx_addon_dir/src/ngx_http_set_escape_uri.c $ngx_addon_dir/src/ngx_http_set_hash.c $ngx_addon_dir/src/ngx_http_set_local_today.c $ngx_addon_dir/src/ngx_http_set_hex.c $ngx_addon_dir/src/ngx_http_set_base64.c $ngx_addon_dir/src/ngx_http_set_random.c $ngx_addon_dir/src/ngx_http_set_secure_random.c $ngx_addon_dir/src/ngx_http_set_rotate.c"
+NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ngx_addon_dir/src/ddebug.h $ngx_addon_dir/src/ngx_http_set_default_value.h $ngx_addon_dir/src/ngx_http_set_hashed_upstream.h $ngx_addon_dir/src/ngx_http_set_quote_sql.h $ngx_addon_dir/src/ngx_http_set_quote_json.h $ngx_addon_dir/src/ngx_http_set_unescape_uri.h $ngx_addon_dir/src/ngx_http_set_escape_uri.h $ngx_addon_dir/src/ngx_http_set_hash.h $ngx_addon_dir/src/ngx_http_set_local_today.h $ngx_addon_dir/src/ngx_http_set_hex.h $ngx_addon_dir/src/ngx_http_set_base64.h $ngx_addon_dir/src/ngx_http_set_random.h $ngx_addon_dir/src/ngx_http_set_rotate.h $ngx_addon_dir/src/ngx_http_set_misc_module.h"
if [ $USE_OPENSSL = YES ]; then
NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ngx_addon_dir/src/ngx_http_set_hmac.h"
View
68 doc/HttpSetMiscModule.wiki
@@ -6,7 +6,7 @@
= Version =
-This document describes set-misc-nginx-module [https://github.com/agentzh/set-misc-nginx-module/tags v0.22rc3] released on 10 October 2011.
+This document describes set-misc-nginx-module [https://github.com/agentzh/set-misc-nginx-module/tags v0.22rc7] released on 17 April 2012.
= Synopsis =
@@ -713,7 +713,7 @@ R/pvxzHC4NLtj7S+kXFg/NePTmk=
Please note that we're using [[HttpEchoModule]]'s [[HttpEchoModule#echo|echo directive]] here to output values of nginx variables directly.
-This directive requires the OpenSSL library enabled in your Nignx build.
+This directive requires the OpenSSL library enabled in your Nignx build (usually by passing the <code>--with-http_ssl_module</code> option to the <code>./configure</code> script).
== set_random ==
'''syntax:''' ''set_random $res <from> <to>''
@@ -728,7 +728,7 @@ Generates a (pseudo) random number (in textual form) within the range <code>[<$f
Only non-negative numbers are allowed for the <code><from></code> and <code><to></code> arguments.
-When <code><$from></code> is greater than <code><$to></code>, their values will be exchanged accordingly.
+When <code><from></code> is greater than <code><to></code>, their values will be exchanged accordingly.
For instance,
@@ -748,6 +748,54 @@ For now, there's no way to configure a custom random generator seed.
Behind the scene, it makes use of the standard C function <code>rand()</code>.
+== set_rotate ==
+'''syntax:''' ''set_random $value <from> <to>''
+
+'''default:''' ''no''
+
+'''context:''' ''location, location if''
+
+'''phase:''' ''rewrite''
+
+Increments <code>$value</code> but keeps it in range from <code><from></code> to <code><to></code>.
+If <code>$value</code> is greater than <code><to></code> or less than <code><from></code> is will be
+set to <code><from></code> value.
+
+The current value after running this directive will always be saved on a per-location basis. And the this saved value will be used for incrementation when the <code>$value</code> is not initialized or has a bad value.
+
+Only non-negative numbers are allowed for the <code><from></code> and <code><to></code> arguments.
+
+When <code><from></code> is greater than <code><to></code>, their values will be exchanged accordingly.
+
+For instance,
+
+<geshi lang="nginx">
+ location /rotate {
+ default_type text/plain;
+ set $counter $cookie_counter;
+ set_rotate $counter 1 5;
+ echo $counter;
+ add_header Set-Cookie counter=$counter;
+ }
+</geshi>
+
+then request <code>GET /rotate</code> 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.
+
+Another example is to use server-side value persistence to do simple round-robin:
+
+<geshi lang="nginx">
+ location /rotate {
+ default_type text/plain;
+ set_rotate $counter 0 3;
+ echo $counter;
+ }
+</geshi>
+
+And accessing <code>/rotate</code> will also output integer sequence 0, 1, 2, 3, 0, 1, 2, 3, and so on.
+
+This directive was first introduced in the <code>v0.22rc7</code> release.
+
== set_local_today ==
'''syntax:''' ''set_local_today $dst''
@@ -780,23 +828,23 @@ Behind the scene, this directive utilizes the <code>ngx_time</code> API in the N
= Caveats =
-Do not use [[HttpCoreModule#$arg_PARAMETER|$arg_PARAMETER]] or [[HttpCoreModule#$http_HEADER|$http_HEADER]] or other special variables defined in the nginx core module as the target variable in this module's directives. For instance,
+Do not use [[HttpCoreModule#$arg_PARAMETER|$arg_PARAMETER]], [[HttpCoreModule#$cookie_COOKIE|$cookie_COOKIE]], [[HttpCoreModule#$http_HEADER|$http_HEADER]] or other special variables defined in the Nginx core module as the target variable in this module's directives. For instance,
<geshi lang="nginx">
set_if_empty $arg_user 'foo'; # DO NOT USE THIS!
</geshi>
-may lead to data corruption.
+may lead to segmentation faults.
= Installation =
Grab the nginx source code from [http://nginx.org/ nginx.org], for example,
-the version 1.0.8 (see [[#Compatibility|nginx compatibility]]), and then build the source with this module:
+the version 1.0.15 (see [[#Compatibility|nginx compatibility]]), and then build the source with this module:
<geshi lang="bash">
- wget 'http://nginx.org/download/nginx-1.0.8.tar.gz'
- tar -xzvf nginx-1.0.8.tar.gz
- cd nginx-1.0.8/
+ wget 'http://nginx.org/download/nginx-1.0.15.tar.gz'
+ tar -xzvf nginx-1.0.15.tar.gz
+ cd nginx-1.0.15/
# Here we assume you would install you nginx under /opt/nginx/.
./configure --prefix=/opt/nginx \
@@ -817,7 +865,7 @@ Also, this module is included and enabled by default in the [http://openresty.or
The following versions of Nginx should work with this module:
* '''1.1.x''' (last tested: 1.1.5)
-* '''1.0.x''' (last tested: 1.0.8)
+* '''1.0.x''' (last tested: 1.0.15)
* '''0.9.x''' (last tested: 0.9.4)
* '''0.8.x''' (last tested: 0.8.54)
* '''0.7.x >= 0.7.46''' (last tested: 0.7.68)
View
1  src/ngx_http_set_base32.h
@@ -15,3 +15,4 @@ ngx_int_t ngx_http_set_misc_decode_base32(ngx_http_request_t *r,
#endif /* NGX_HTTP_SET_BASE32 */
+
View
2  src/ngx_http_set_hash.c
@@ -22,7 +22,9 @@
#endif
enum {
+#if NGX_HAVE_SHA1
SHA_HEX_LENGTH = SHA_DIGEST_LENGTH * 2,
+#endif
MD5_HEX_LENGTH = MD5_DIGEST_LENGTH * 2
};
View
13 src/ngx_http_set_misc_module.c
@@ -21,6 +21,8 @@
#endif
#include "ngx_http_set_random.h"
#include "ngx_http_set_secure_random.h"
+#include "ngx_http_set_rotate.h"
+
#define NGX_UNESCAPE_URI_COMPONENT 0
@@ -366,6 +368,14 @@ static ngx_command_t ngx_http_set_misc_commands[] = {
0,
&ngx_http_set_misc_set_secure_random_lcalpha_filter
},
+ { ngx_string ("set_rotate"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
+ |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE3,
+ ngx_http_set_rotate,
+ 0,
+ 0,
+ NULL
+ },
ngx_null_command
};
@@ -413,6 +423,7 @@ ngx_http_set_misc_create_loc_conf(ngx_conf_t *cf)
}
conf->base32_padding = NGX_CONF_UNSET;
+ conf->current = NGX_CONF_UNSET;
return conf;
}
@@ -426,6 +437,8 @@ ngx_http_set_misc_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
ngx_conf_merge_value(conf->base32_padding, prev->base32_padding, 1);
+ ngx_conf_merge_value(conf->current, prev->current, NGX_CONF_UNSET);
+
return NGX_CONF_OK;
}
View
1  src/ngx_http_set_misc_module.h
@@ -4,6 +4,7 @@
typedef struct {
ngx_flag_t base32_padding;
+ ngx_int_t current; /* for set_rotate */
} ngx_http_set_misc_loc_conf_t;
View
113 src/ngx_http_set_rotate.c
@@ -0,0 +1,113 @@
+#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);
+}
+
View
18 src/ngx_http_set_rotate.h
@@ -0,0 +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
271 t/rotate.t
@@ -0,0 +1,271 @@
+# 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_like: ^a = [12]$
+--- 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"
+
+
+
+=== TEST 5: when no current value is given
+--- config
+ location /incr {
+ set_rotate $a 1 3;
+
+ echo "a = $a";
+ }
+
+ location /t {
+ echo_location /incr;
+ echo_location /incr;
+ echo_location /incr;
+ echo_location /incr;
+ echo_location /incr;
+ echo_location /incr;
+ }
+--- request
+ GET /t
+--- response_body
+a = 1
+a = 2
+a = 3
+a = 1
+a = 2
+a = 3
+--- no_error_log
+[error]
+
+
+
+=== TEST 6: when no current value is given (starting from 0)
+--- config
+ location /incr {
+ set_rotate $a 0 2;
+
+ echo "a = $a";
+ }
+
+ location /t {
+ echo_location /incr;
+ echo_location /incr;
+ echo_location /incr;
+ echo_location /incr;
+ echo_location /incr;
+ echo_location /incr;
+ }
+--- request
+ GET /t
+--- response_body
+a = 0
+a = 1
+a = 2
+a = 0
+a = 1
+a = 2
+--- no_error_log
+[error]
+
+
+
+=== TEST 7: when a non-integer string value is given
+--- config
+ location /incr {
+ set $a "hello";
+ set_rotate $a 0 2;
+
+ echo "a = $a";
+ }
+
+ location /t {
+ echo_location /incr;
+ echo_location /incr;
+ echo_location /incr;
+ echo_location /incr;
+ echo_location /incr;
+ echo_location /incr;
+ }
+--- request
+ GET /t
+--- response_body
+a = 0
+a = 1
+a = 2
+a = 0
+a = 1
+a = 2
+--- error_log
+set_rotate: bad current value: "hello"
+
+
+
+=== TEST 8: when an empty string value is given
+--- config
+ location /incr {
+ set $a "";
+ set_rotate $a 0 2;
+
+ echo "a = $a";
+ }
+
+ location /t {
+ echo_location /incr;
+ echo_location /incr;
+ echo_location /incr;
+ echo_location /incr;
+ echo_location /incr;
+ echo_location /incr;
+ }
+--- request
+ GET /t
+--- response_body
+a = 0
+a = 1
+a = 2
+a = 0
+a = 1
+a = 2
+--- no_error_log
+[error]
+
+
+
+=== TEST 9: value persistence is per-location
+--- config
+ location /incr {
+ set_rotate $a 0 2;
+
+ echo "a = $a";
+ }
+
+ location /incr2 {
+ set_rotate $a 0 2;
+
+ echo "a = $a";
+ }
+
+ location /t {
+ echo_location /incr;
+ echo_location /incr2;
+ echo_location /incr;
+ echo_location /incr2;
+ echo_location /incr;
+ echo_location /incr2;
+ echo_location /incr;
+ echo_location /incr2;
+ echo_location /incr;
+ echo_location /incr2;
+ echo_location /incr;
+ echo_location /incr2;
+ }
+--- request
+ GET /t
+--- response_body
+a = 0
+a = 0
+a = 1
+a = 1
+a = 2
+a = 2
+a = 0
+a = 0
+a = 1
+a = 1
+a = 2
+a = 2
+--- no_error_log
+[error]
+
View
2  util/build.sh
@@ -7,6 +7,8 @@ home=~
version=$1
force=$2
+ #--with-cc="gcc46" \
+
ngx-build $force $version \
--with-http_ssl_module \
--without-mail_pop3_module \
View
9 valgrind.suppress
@@ -1,5 +1,14 @@
{
<insert_a_suppression_name_here>
+ Memcheck:Leak
+ fun:malloc
+ fun:ngx_alloc
+ fun:ngx_calloc
+ fun:ngx_event_process_init
+ fun:ngx_single_process_cycle
+}
+{
+ <insert_a_suppression_name_here>
exp-sgcheck:SorG
fun:ngx_http_variables_init_vars
fun:ngx_http_block
Please sign in to comment.
Something went wrong with that request. Please try again.