diff --git a/config b/config index 0f8f878..5d3f094 100755 --- a/config +++ b/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_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_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" diff --git a/doc/HttpSetMiscModule.wiki b/doc/HttpSetMiscModule.wiki index cc4289d..8025145 100644 --- a/doc/HttpSetMiscModule.wiki +++ b/doc/HttpSetMiscModule.wiki @@ -748,6 +748,38 @@ 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 '' + +'''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 and 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'' diff --git a/src/ngx_http_set_misc_module.c b/src/ngx_http_set_misc_module.c index e7e3420..57fd5cb 100755 --- a/src/ngx_http_set_misc_module.c +++ b/src/ngx_http_set_misc_module.c @@ -20,6 +20,7 @@ #include "ngx_http_set_hmac.h" #endif #include "ngx_http_set_random.h" +#include "ngx_http_set_rotate.h" #define NGX_UNESCAPE_URI_COMPONENT 0 @@ -146,7 +147,14 @@ 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 @@ -316,6 +324,14 @@ static ngx_command_t ngx_http_set_misc_commands[] = { 0, &ngx_http_set_misc_set_random_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 }; diff --git a/src/ngx_http_set_rotate.c b/src/ngx_http_set_rotate.c new file mode 100644 index 0000000..0a21293 --- /dev/null +++ b/src/ngx_http_set_rotate.c @@ -0,0 +1,84 @@ +#ifndef DDEBUG +#define DDEBUG 0 +#endif +#include "ddebug.h" + +#include +#include "ngx_http_set_rotate.h" +#include + +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; + + rotate_num = &v[0]; + 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); + 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); + return NGX_ERROR; + } + + if (int_from > int_to) { + tmp = int_from; + int_from = int_to; + int_to = tmp; + } + + 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); + int_current = int_from; + } + + int_current++; + if(int_current>int_to || int_currentdata = 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; + + /* 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) +{ + 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); +} diff --git a/src/ngx_http_set_rotate.h b/src/ngx_http_set_rotate.h new file mode 100644 index 0000000..f1476a1 --- /dev/null +++ b/src/ngx_http_set_rotate.h @@ -0,0 +1,9 @@ +#include +#include +#include + +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);