Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
32 changes: 32 additions & 0 deletions doc/HttpSetMiscModule.wiki
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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.

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.

== set_local_today ==
'''syntax:''' ''set_local_today $dst''

Expand Down
18 changes: 17 additions & 1 deletion src/ngx_http_set_misc_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
};
Expand Down
84 changes: 84 additions & 0 deletions src/ngx_http_set_rotate.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"

#include <ndk.h>
#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_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_current<int_from)
int_current = int_from;

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;

/* 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);
}
9 changes: 9 additions & 0 deletions src/ngx_http_set_rotate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#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);