Permalink
Browse files

Merge branch 'master' of github.com:agentzh/set-misc-nginx-module

  • Loading branch information...
agentzh committed Jul 25, 2011
2 parents 5792ec8 + 5c4a47f commit 2775016207a0b09c51c1aa96ba25e0958b1eba6a
Showing with 269 additions and 5 deletions.
  1. +1 −0 .gitignore
  2. +17 −1 README
  3. +2 −2 config
  4. +16 −0 src/ngx_http_set_misc_module.c
  5. +55 −0 src/ngx_http_set_random.c
  6. +7 −0 src/ngx_http_set_random.h
  7. +1 −2 t/base32.t
  8. +169 −0 t/rand.t
  9. +1 −0 util/build.sh
View
@@ -58,3 +58,4 @@ src/hmac.c
src/hmac.h
buildroot/
work/
+src/random.[ch]
View
18 README
@@ -50,7 +50,7 @@ Synopsis
set_unescape_uri $foo $foo;
# OR in-place editing:
- # set_quote_sql_str $foo;
+ # set_unescape_uri $foo;
# now $foo is: hello world
}
@@ -123,6 +123,17 @@ Synopsis
echo $signature;
}
+ location = /rand {
+ set $from 3;
+ set $to 15;
+ set_random $rand $from $to;
+
+ # or write directly
+ # set_random $rand 3 15;
+
+ echo $rand; # will print a random integer in the range [3, 15]
+ }
+
Description
Every directive provided by this module can be
mixed freely with other nginx rewrite module's
@@ -170,6 +181,11 @@ Directives
set_hmac_sha1 (only enabled when nginx uses openssl, as with ssl support)
+ set_random $res <from> <to>
+ Note that only non-negative numbers in the "from" to "to" argument are allowed.
+ A (psuedo) random number in the range [<from>, <to>] (inclusive) will be assigned
+ to $res. 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().
Caveats
Do not use $arg_XXX or $http_XXX or other special variables defined in
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_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_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"
if [ $USE_OPENSSL = YES ]; then
NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ngx_addon_dir/src/ngx_http_set_hmac.h"
@@ -16,6 +16,7 @@
#if NGX_OPENSSL
#include "ngx_http_set_hmac.h"
#endif
+#include "ngx_http_set_random.h"
#define NGX_UNESCAPE_URI_COMPONENT 0
@@ -129,6 +130,13 @@ static ndk_set_var_t ngx_http_set_misc_local_today_filter = {
NULL
};
+static ndk_set_var_t ngx_http_set_misc_set_random_filter = {
+ NDK_SET_VAR_MULTI_VALUE,
+ ngx_http_set_misc_set_random,
+ 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
@@ -281,6 +289,14 @@ static ngx_command_t ngx_http_set_misc_commands[] = {
0,
&ngx_http_set_misc_local_today_filter
},
+ { ngx_string ("set_random"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
+ |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE3,
+ ndk_set_var_multi_value,
+ 0,
+ 0,
+ &ngx_http_set_misc_set_random_filter
+ },
ngx_null_command
};
View
@@ -0,0 +1,55 @@
+#define DDEBUG 0
+#include "ddebug.h"
+#include <ndk.h>
+
+#include "ngx_http_set_random.h"
+#include <stdlib.h>
+
+
+ngx_int_t
+ngx_http_set_misc_set_random(ngx_http_request_t *r,
+ ngx_str_t *res, ngx_http_variable_value_t *v)
+{
+ ngx_http_variable_value_t *rand_from, *rand_to;
+ ngx_int_t int_from, int_to, tmp, random;
+
+ rand_from = v;
+ rand_to = v + 1;
+
+ int_from = ngx_atoi(rand_from->data, rand_from->len);
+ if (int_from == NGX_ERROR) {
+ ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+ "set_random: bad \"from\" argument: %v", rand_from);
+ return NGX_ERROR;
+ }
+
+ int_to = ngx_atoi(rand_to->data, rand_to->len);
+ if (int_to == NGX_ERROR) {
+ ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+ "set_random: bad \"to\" argument: %v", rand_to);
+ return NGX_ERROR;
+ }
+
+ if (int_from > int_to) {
+ tmp = int_from;
+ int_from = int_to;
+ int_to = tmp;
+ }
+
+ random = rand() % (int_to - int_from + 1) + 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", random) - res->data;
+
+ /* Set all required params */
+ v->valid = 1;
+ v->no_cacheable = 0;
+ v->not_found = 0;
+
+ return NGX_OK;
+}
+
@@ -0,0 +1,7 @@
+#include <ngx_core.h>
+#include <ngx_config.h>
+#include <ngx_http.h>
+
+ngx_int_t ngx_http_set_misc_set_random(ngx_http_request_t *r,
+ ngx_str_t *res, ngx_http_variable_value_t *v);
+
View
@@ -1,9 +1,8 @@
# vi:filetype=perl
-use lib 'lib';
use Test::Nginx::Socket;
-#repeat_each(3);
+repeat_each(3);
plan tests => repeat_each() * 2 * blocks();
View
169 t/rand.t
@@ -0,0 +1,169 @@
+# vi:filetype=perl
+
+use Test::Nginx::Socket;
+
+repeat_each(100);
+
+plan tests => repeat_each() * 2 * blocks();
+
+no_long_string();
+
+run_tests();
+
+#no_diff();
+
+__DATA__
+
+=== TEST 1: sanity
+--- config
+ location /rand {
+ set $from 5;
+ set $to 7;
+ set_random $res $from $to;
+
+ echo $res;
+ }
+--- request
+ GET /rand
+--- response_body_like: [5-7]
+
+
+
+=== TEST 2: sanity (two digits)
+--- config
+ location /rand {
+ set $from 35;
+ set $to 37;
+ set_random $res $from $to;
+
+ echo $res;
+ }
+--- request
+ GET /rand
+--- response_body_like: 3[5-7]
+
+
+
+=== TEST 3: sanity (two digits, from > to)
+--- config
+ location /rand {
+ set $from 37;
+ set $to 35;
+ set_random $res $from $to;
+
+ echo $res;
+ }
+--- request
+ GET /rand
+--- response_body_like: 3[5-7]
+
+
+
+=== TEST 4: sanity (two digits, from == to)
+--- config
+ location /rand {
+ set $from 117;
+ set $to 117;
+ set_random $res $from $to;
+
+ echo $res;
+ }
+--- request
+ GET /rand
+--- response_body
+117
+
+
+
+=== TEST 5: negative number not allowed in from arg
+--- config
+ location /rand {
+ set $from -2;
+ set $to 4;
+ set_random $res $from $to;
+
+ echo $res;
+ }
+--- request
+ GET /rand
+--- response_body_like: 500 Internal Server Error
+--- error_code: 500
+
+
+
+=== TEST 6: negative number not allowed in to arg
+--- config
+ location /rand {
+ set $from 2;
+ set $to -4;
+ set_random $res $from $to;
+
+ echo $res;
+ }
+--- request
+ GET /rand
+--- response_body_like: 500 Internal Server Error
+--- error_code: 500
+
+
+
+=== TEST 7: empty string not allowed in from arg
+--- config
+ location /rand {
+ set $from '';
+ set $to 4;
+ set_random $res $from $to;
+
+ echo $res;
+ }
+--- request
+ GET /rand
+--- response_body_like: 500 Internal Server Error
+--- error_code: 500
+
+
+
+=== TEST 8: empty string not allowed in to arg
+--- config
+ location /rand {
+ set $from 2;
+ set $to '';
+ set_random $res $from $to;
+
+ echo $res;
+ }
+--- request
+ GET /rand
+--- response_body_like: 500 Internal Server Error
+--- error_code: 500
+
+
+
+=== TEST 9: wrong number of arguments
+--- config
+ location /rand {
+ set $from 2;
+ set_random $res $from;
+
+ echo $res;
+ }
+--- request
+ GET /rand
+--- response_body_like: 500 Internal Server Error
+--- error_code: 500
+--- SKIP
+
+
+
+=== TEST 10: zero is fine
+--- config
+ location /rand {
+ set_random $res 0 0;
+
+ echo $res;
+ }
+--- request
+ GET /rand
+--- response_body
+0
+
View
@@ -31,6 +31,7 @@ fi
#tar -xzvf nginx-$version.tar.gz || exit 1
#cp $root/../no-pool-nginx/nginx-$version-no_pool.patch ./ || exit 1
#patch -p0 < nginx-$version-no_pool.patch || exit 1
+#patch -p0 < ~/work/nginx-$version-valgrind_fix.patch || exit 1
cd nginx-$version/ || exit 1

0 comments on commit 2775016

Please sign in to comment.