pax_global_header00006660000000000000000000000064141153502200014503gustar00rootroot0000000000000052 comment=31c4ad67bb9e392a734e4e58ea8048e24012311f set-misc-nginx-module-0.33/000077500000000000000000000000001411535022000155605ustar00rootroot00000000000000set-misc-nginx-module-0.33/.gitattributes000066400000000000000000000000331411535022000204470ustar00rootroot00000000000000*.t linguist-language=Text set-misc-nginx-module-0.33/.gitignore000066400000000000000000000013011411535022000175430ustar00rootroot00000000000000reindex .libs *.swp *.slo *.la *.swo *.lo *~ *.o print.txt .rsync *.tar.gz dist build[789] build tags update-readme *.tmp test/Makefile test/blib test.sh all t/t.sh test/t/servroot/ releng reset *.t_ genmobi.sh *.mobi misc/chunked ctags src/base32.h src/uri.c src/module.c src/upstream.c src/upstream.h src/uri.c src/sql.c src/uri.h src/sql.h src/uri.h src/value.c src/base32.c src/value.h src/hash.h src/hash.c src/today.h src/today.c src/json.[ch] all.sh go t/servroot/ src/base64.c src/base64.h src/hex.c src/hex.h src/hmac.c src/hmac.h src/rotate.[ch] analyze buildroot/ src/module.h work/ src/random.[ch] build1[0-9] nginx analyze src/rotate.[ch] *.plist Makefile src/base64url.[ch] src/expired.[ch] set-misc-nginx-module-0.33/.travis.yml000066400000000000000000000041761411535022000177010ustar00rootroot00000000000000sudo: required dist: trusty branches: only: - "master" os: linux language: c compiler: gcc env: global: - LUAJIT_PREFIX=/opt/luajit21 - LUAJIT_LIB=$LUAJIT_PREFIX/lib - LD_LIBRARY_PATH=$LUAJIT_LIB:$LD_LIBRARY_PATH - LUAJIT_INC=$LUAJIT_PREFIX/include/luajit-2.1 - LUA_INCLUDE_DIR=$LUAJIT_INC - LUA_CMODULE_DIR=/lib - JOBS=3 - NGX_BUILD_JOBS=$JOBS - TEST_NGINX_SLEEP=0.006 matrix: - NGINX_VERSION=1.19.9 before_install: - sudo apt-get install -qq -y axel cpanminus libtest-base-perl libtext-diff-perl liburi-perl libwww-perl libtest-longstring-perl liblist-moreutils-perl > build.log 2>&1 || (cat build.log && exit 1) install: - git clone https://github.com/openresty/nginx-devel-utils.git - git clone https://github.com/openresty/openresty.git ../openresty - git clone https://github.com/openresty/no-pool-nginx.git ../no-pool-nginx - git clone https://github.com/simpl/ngx_devel_kit.git ../ndk-nginx-module - git clone https://github.com/openresty/test-nginx.git - git clone -b v2.1-agentzh https://github.com/openresty/luajit2.git - git clone https://github.com/openresty/lua-nginx-module.git ../lua-nginx-module - git clone https://github.com/openresty/lua-resty-core.git ../lua-resty-core - git clone https://github.com/openresty/lua-resty-lrucache.git ../lua-resty-lrucache - git clone https://github.com/openresty/echo-nginx-module.git ../echo-nginx-module - git clone https://github.com/calio/iconv-nginx-module.git ../iconv-nginx-module script: - export PATH=$PWD/work/nginx/sbin:$PWD/nginx-devel-utils:$PATH - ngx-releng > check.txt || true - lines=`wc -l check.txt | awk '{print $1}'`; if [ $lines -gt 3 ]; then cat check.txt; exit 1; fi - cd luajit2 - make -j$JOBS CCDEBUG=-g Q= PREFIX=$LUAJIT_PREFIX CC=$CC XCFLAGS='-DLUA_USE_APICHECK -DLUA_USE_ASSERT' > build.log 2>&1 || (cat build.log && exit 1) - sudo make install PREFIX=$LUAJIT_PREFIX > build.log 2>&1 || (cat build.log && exit 1) - cd ../test-nginx && sudo cpanm . && cd .. - export NGX_BUILD_CC=$CC - sh util/build.sh $NGINX_VERSION > build.log 2>&1 || (cat build.log && exit 1) - nginx -V - prove -r t set-misc-nginx-module-0.33/README.markdown000066400000000000000000001256511411535022000202730ustar00rootroot00000000000000 Name ==== **ngx_set_misc** - Various set_xxx directives added to nginx's rewrite module (md5/sha1, sql/json quoting, and many more) *This module is not distributed with the Nginx source.* See [the installation instructions](#installation). Table of Contents ================= * [Name](#name) * [Version](#version) * [Synopsis](#synopsis) * [Description](#description) * [Directives](#directives) * [set_if_empty](#set_if_empty) * [set_quote_sql_str](#set_quote_sql_str) * [set_quote_pgsql_str](#set_quote_pgsql_str) * [set_quote_json_str](#set_quote_json_str) * [set_unescape_uri](#set_unescape_uri) * [set_escape_uri](#set_escape_uri) * [set_hashed_upstream](#set_hashed_upstream) * [set_encode_base32](#set_encode_base32) * [set_base32_padding](#set_base32_padding) * [set_misc_base32_padding](#set_misc_base32_padding) * [set_base32_alphabet](#set_base32_alphabet) * [set_decode_base32](#set_decode_base32) * [set_encode_base64](#set_encode_base64) * [set_decode_base64](#set_decode_base64) * [set_encode_base64url](#set_encode_base64url) * [set_decode_base64url](#set_decode_base64url) * [set_encode_hex](#set_encode_hex) * [set_decode_hex](#set_decode_hex) * [set_sha1](#set_sha1) * [set_md5](#set_md5) * [set_hmac_sha1](#set_hmac_sha1) * [set_hmac_sha256](#set_hmac_sha256) * [set_random](#set_random) * [set_secure_random_alphanum](#set_secure_random_alphanum) * [set_secure_random_lcalpha](#set_secure_random_lcalpha) * [set_rotate](#set_rotate) * [set_local_today](#set_local_today) * [set_formatted_gmt_time](#set_formatted_gmt_time) * [set_formatted_local_time](#set_formatted_local_time) * [Caveats](#caveats) * [Installation](#installation) * [Building as a dynamic module](#building-as-a-dynamic-module) * [Compatibility](#compatibility) * [Report Bugs](#report-bugs) * [Source Repository](#source-repository) * [Changes](#changes) * [Test Suite](#test-suite) * [Getting involved](#getting-involved) * [Author](#author) * [Copyright & License](#copyright--license) * [See Also](#see-also) Version ======= This document describes ngx_set_misc [v0.32](https://github.com/openresty/set-misc-nginx-module/tags) released on 19 April 2018. Synopsis ======== ```nginx location /foo { set $a $arg_a; set_if_empty $a 56; # GET /foo?a=32 will yield $a == 32 # while GET /foo and GET /foo?a= will # yeild $a == 56 here. } location /bar { set $foo "hello\n\n'\"\\"; set_quote_sql_str $foo $foo; # for mysql # OR in-place editing: # set_quote_sql_str $foo; # now $foo is: 'hello\n\n\'\"\\' } location /bar { set $foo "hello\n\n'\"\\"; set_quote_pgsql_str $foo; # for PostgreSQL # now $foo is: E'hello\n\n\'\"\\' } location /json { set $foo "hello\n\n'\"\\"; set_quote_json_str $foo $foo; # OR in-place editing: # set_quote_json_str $foo; # now $foo is: "hello\n\n'\"\\" } location /baz { set $foo "hello%20world"; set_unescape_uri $foo $foo; # OR in-place editing: # set_unescape_uri $foo; # now $foo is: hello world } upstream_list universe moon sun earth; upstream moon { ... } upstream sun { ... } upstream earth { ... } location /foo { set_hashed_upstream $backend universe $arg_id; drizzle_pass $backend; # used with ngx_drizzle } location /base32 { set $a 'abcde'; set_encode_base32 $a; set_decode_base32 $b $a; # now $a == 'c5h66p35' and # $b == 'abcde' } location /base64 { set $a 'abcde'; set_encode_base64 $a; set_decode_base64 $b $a; # now $a == 'YWJjZGU=' and # $b == 'abcde' } location /hex { set $a 'abcde'; set_encode_hex $a; set_decode_hex $b $a; # now $a == '6162636465' and # $b == 'abcde' } # GET /sha1 yields the output # aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d location /sha1 { set_sha1 $a hello; echo $a; } # ditto location /sha1 { set $a hello; set_sha1 $a; echo $a; } # GET /today yields the date of today in local time using format 'yyyy-mm-dd' location /today { set_local_today $today; echo $today; } # GET /signature yields the hmac-sha-1 signature # given a secret and a string to sign # this example yields the base64 encoded singature which is # "HkADYytcoQQzqbjQX33k/ZBB/DQ=" location /signature { set $secret_key 'secret-key'; set $string_to_sign "some-string-to-sign"; set_hmac_sha1 $signature $secret_key $string_to_sign; set_encode_base64 $signature $signature; 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 =========== This module extends the standard HttpRewriteModule's directive set to provide more functionalities like URI escaping and unescaping, JSON quoting, Hexadecimal/MD5/SHA1/Base32/Base64 digest encoding and decoding, random number generator, and more! Every directive provided by this module can be mixed freely with other [ngx_http_rewrite_module](http://nginx.org/en/docs/http/ngx_http_rewrite_module.html)'s directives, like [if](http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#if) and [set](http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#set). (Thanks to the [Nginx Devel Kit](https://github.com/simpl/ngx_devel_kit)!) [Back to TOC](#table-of-contents) Directives ========== [Back to TOC](#table-of-contents) set_if_empty ------------ **syntax:** *set_if_empty $dst <src>* **default:** *no* **context:** *location, location if* **phase:** *rewrite* Assign the value of the argument `` if and only if variable `$dst` is empty (i.e., not found or has an empty string value). In the following example, ```nginx set $a 32; set_if_empty $a 56; ``` the variable `$dst` will take the value 32 at last. But in the sample ```nginx set $a ''; set $value "hello, world" set_if_empty $a $value; ``` `$a` will take the value `"hello, world"` at last. [Back to TOC](#table-of-contents) set_quote_sql_str ----------------- **syntax:** *set_quote_sql_str $dst <src>* **syntax:** *set_quote_sql_str $dst* **default:** *no* **context:** *location, location if* **phase:** *rewrite* **category:** *ndk_set_var_value* When taking two arguments, this directive will quote the value of the second argument `` by MySQL's string value quoting rule and assign the result into the first argument, variable `$dst`. For example, ```nginx location /test { set $value "hello\n\r'\"\\"; set_quote_sql_str $quoted $value; echo $quoted; } ``` Then request `GET /test` will yield the following output ```sql 'hello\n\r\'\"\\' ``` Please note that we're using [echo-nginx-module](http://github.com/openresty/echo-nginx-module)'s [echo directive](http://github.com/openresty/echo-nginx-module#echo) here to output values of nginx variables directly. When taking a single argument, this directive will do in-place modification of the argument variable. For example, ```nginx location /test { set $value "hello\n\r'\"\\"; set_quote_sql_str $value; echo $value; } ``` then request `GET /test` will give exactly the same output as the previous example. This directive is usually used to prevent SQL injection. This directive can be invoked by [lua-nginx-module](http://github.com/openresty/lua-nginx-module)'s [ndk.set_var.DIRECTIVE](http://github.com/openresty/lua-nginx-module#ndkset_vardirective) interface and [array-var-nginx-module](http://github.com/openresty/array-var-nginx-module)'s [array_map_op](http://github.com/openresty/array-var-nginx-module#array_map_op) directive. [Back to TOC](#table-of-contents) set_quote_pgsql_str ------------------- **syntax:** *set_quote_pgsql_str $dst <src>* **syntax:** *set_quote_pgsql_str $dst* **default:** *no* **context:** *location, location if* **phase:** *rewrite* **category:** *ndk_set_var_value* Very much like [set_quote_sql_str](#set_quote_sql_str), but with PostgreSQL quoting rules for SQL string literals. [Back to TOC](#table-of-contents) set_quote_json_str ------------------ **syntax:** *set_quote_json_str $dst <src>* **syntax:** *set_quote_json_str $dst* **default:** *no* **context:** *location, location if* **phase:** *rewrite* **category:** *ndk_set_var_value* When taking two arguments, this directive will quote the value of the second argument `` by JSON string value quoting rule and assign the result into the first argument, variable `$dst`. For example, ```nginx location /test { set $value "hello\n\r'\"\\"; set_quote_json_str $quoted $value; echo $quoted; } ``` Then request `GET /test` will yield the following output ```javascript "hello\n\r'\"\\" ``` Please note that we're using [echo-nginx-module](http://github.com/openresty/echo-nginx-module)'s [echo directive](http://github.com/openresty/echo-nginx-module#echo) here to output values of nginx variables directly. When taking a single argument, this directive will do in-place modification of the argument variable. For example, ```nginx location /test { set $value "hello\n\r'\"\\"; set_quote_json_str $value; echo $value; } ``` then request `GET /test` will give exactly the same output as the previous example. This directive can be invoked by [lua-nginx-module](http://github.com/openresty/lua-nginx-module)'s [ndk.set_var.DIRECTIVE](http://github.com/openresty/lua-nginx-module#ndkset_vardirective) interface and [array-var-nginx-module](http://github.com/openresty/array-var-nginx-module)'s [array_map_op](http://github.com/openresty/array-var-nginx-module#array_map_op) directive. [Back to TOC](#table-of-contents) set_unescape_uri ---------------- **syntax:** *set_unescape_uri $dst <src>* **syntax:** *set_unescape_uri $dst* **default:** *no* **context:** *location, location if* **phase:** *rewrite* **category:** *ndk_set_var_value* When taking two arguments, this directive will unescape the value of the second argument `` as a URI component and assign the result into the first argument, variable `$dst`. For example, ```nginx location /test { set_unescape_uri $key $arg_key; echo $key; } ``` Then request `GET /test?key=hello+world%21` will yield the following output ``` hello world! ``` The nginx standard [$arg_PARAMETER](http://nginx.org/en/docs/http/ngx_http_core_module.html#var_arg_) variable holds the raw (escaped) value of the URI parameter. So we need the `set_unescape_uri` directive to unescape it first. Please note that we're using [echo-nginx-module](http://github.com/openresty/echo-nginx-module)'s [echo directive](http://github.com/openresty/echo-nginx-module#echo) here to output values of nginx variables directly. When taking a single argument, this directive will do in-place modification of the argument variable. For example, ```nginx location /test { set $key $arg_key; set_unescape_uri $key; echo $key; } ``` then request `GET /test?key=hello+world%21` will give exactly the same output as the previous example. This directive can be invoked by [lua-nginx-module](http://github.com/openresty/lua-nginx-module)'s [ndk.set_var.DIRECTIVE](http://github.com/openresty/lua-nginx-module#ndkset_vardirective) interface and [array-var-nginx-module](http://github.com/openresty/array-var-nginx-module)'s [array_map_op](http://github.com/openresty/array-var-nginx-module#array_map_op) directive. [Back to TOC](#table-of-contents) set_escape_uri -------------- **syntax:** *set_escape_uri $dst <src>* **syntax:** *set_escape_uri $dst* **default:** *no* **context:** *location, location if* **phase:** *rewrite* **category:** *ndk_set_var_value* Very much like the [set_unescape_uri](#set_unescape_uri) directive, but does the conversion the other way around, i.e., URL component escaping. [Back to TOC](#table-of-contents) set_hashed_upstream ------------------- **syntax:** *set_hashed_upstream $dst <upstream_list_name> <src>* **default:** *no* **context:** *location, location if* **phase:** *rewrite* Hashes the string argument `` into one of the upstream name included in the upstream list named ``. The hash function being used is simple modulo. Here's an example, ```nginx upstream moon { ... } upstream sun { ... } upstream earth { ... } upstream_list universe moon sun earth; location /test { set_unescape_uri $key $arg_key; set $list_name universe; set_hashed_upstream $backend $list_name $key; echo $backend; } ``` Then `GET /test?key=blah` will output either "moon", "sun", or "earth", depending on the actual value of the `key` query argument. This directive is usually used to compute an nginx variable to be passed to [memc-nginx-module](http://github.com/openresty/memc-nginx-module)'s [memc_pass](http://github.com/openresty/memc-nginx-module#memc_pass) directive, [redis2-nginx-module](http://github.com/openresty/redis2-nginx-module)'s [[HttpRedis2Module#redis2_pass]] directive, and [ngx_http_proxy_module](http://nginx.org/en/docs/http/ngx_http_proxy_module.html)'s [proxy_pass](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass) directive, among others. [Back to TOC](#table-of-contents) set_encode_base32 ----------------- **syntax:** *set_encode_base32 $dst <src>* **syntax:** *set_encode_base32 $dst* **default:** *no* **context:** *location, location if* **phase:** *rewrite* **category:** *ndk_set_var_value* When taking two arguments, this directive will encode the value of the second argument `` to its base32(hex) digest and assign the result into the first argument, variable `$dst`. For example, ```nginx location /test { set $raw "abcde"; set_encode_base32 $digest $raw; echo $digest; } ``` Then request `GET /test` will yield the following output ``` c5h66p35 ``` Please note that we're using [echo-nginx-module](http://github.com/openresty/echo-nginx-module)'s [echo directive](http://github.com/openresty/echo-nginx-module#echo) here to output values of nginx variables directly. RFC forces the `[A-Z2-7]` RFC-3548 compliant encoding, but we are using the "base32hex" encoding (`[0-9a-v]`) by default. The [set_base32_alphabet](#set_base32_alphabet) directive (first introduced in `v0.28`) allows you to change the alphabet used for encoding/decoding so RFC-3548 compliant encoding is still possible by custom configurations. By default, the `=` character is used to pad the left-over bytes due to alignment. But the padding behavior can be completely disabled by setting [set_base32_padding](#set_base32_padding) `off`. When taking a single argument, this directive will do in-place modification of the argument variable. For example, ```nginx location /test { set $value "abcde"; set_encode_base32 $value; echo $value; } ``` then request `GET /test` will give exactly the same output as the previous example. This directive can be invoked by [lua-nginx-module](http://github.com/openresty/lua-nginx-module)'s [ndk.set_var.DIRECTIVE](http://github.com/openresty/lua-nginx-module#ndkset_vardirective) interface and [array-var-nginx-module](http://github.com/openresty/array-var-nginx-module)'s [array_map_op](http://github.com/openresty/array-var-nginx-module#array_map_op) directive. [Back to TOC](#table-of-contents) set_base32_padding ------------------ **syntax:** *set_base32_padding on|off* **default:** *on* **context:** *http, server, server if, location, location if* **phase:** *no* This directive can control whether to pad left-over bytes with the "=" character when encoding a base32 digest by the [set_encode_base32](#set_encode_base32) directive. This directive was first introduced in `v0.28`. If you use earlier versions of this module, then you should use [set_misc_base32_padding](#set_misc_base32_padding) instead. [Back to TOC](#table-of-contents) set_misc_base32_padding ----------------------- **syntax:** *set_misc_base32_padding on|off* **default:** *on* **context:** *http, server, server if, location, location if* **phase:** *no* This directive has been deprecated since `v0.28`. Use [set_base32_padding](#set_base32_padding) instead if you are using `v0.28+`. [Back to TOC](#table-of-contents) set_base32_alphabet ------------------- **syntax:** *set_base32_alphabet <alphabet>* **default:** *"0123456789abcdefghijklmnopqrstuv"* **context:** *http, server, server if, location, location if* **phase:** *no* This directive controls the alphabet used for encoding/decoding a base32 digest. It accepts a string containing the desired alphabet like "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" for standard alphabet. Extended (base32hex) alphabet is used by default. This directive was first introduced in `v0.28`. [Back to TOC](#table-of-contents) set_decode_base32 ----------------- **syntax:** *set_decode_base32 $dst <src>* **syntax:** *set_decode_base32 $dst* **default:** *no* **context:** *location, location if* **phase:** *rewrite* **category:** *ndk_set_var_value* Similar to the [set_encode_base32](#set_encode_base32) directive, but does exactly the opposite operation, .i.e, decoding a base32(hex) digest into its original form. [Back to TOC](#table-of-contents) set_encode_base64 ----------------- **syntax:** *set_encode_base64 $dst <src>* **syntax:** *set_encode_base64 $dst* **default:** *no* **context:** *location, location if* **phase:** *rewrite* **category:** *ndk_set_var_value* When taking two arguments, this directive will encode the value of the second argument `` to its base64 digest and assign the result into the first argument, variable `$dst`. For example, ```nginx location /test { set $raw "abcde"; set_encode_base64 $digest $raw; echo $digest; } ``` Then request `GET /test` will yield the following output ``` YWJjZGU= ``` Please note that we're using [echo-nginx-module](http://github.com/openresty/echo-nginx-module)'s [echo directive](http://github.com/openresty/echo-nginx-module#echo) here to output values of nginx variables directly. When taking a single argument, this directive will do in-place modification of the argument variable. For example, ```nginx location /test { set $value "abcde"; set_encode_base64 $value; echo $value; } ``` then request `GET /test` will give exactly the same output as the previous example. This directive can be invoked by [lua-nginx-module](http://github.com/openresty/lua-nginx-module)'s [ndk.set_var.DIRECTIVE](http://github.com/openresty/lua-nginx-module#ndkset_vardirective) interface and [array-var-nginx-module](http://github.com/openresty/array-var-nginx-module)'s [array_map_op](http://github.com/openresty/array-var-nginx-module#array_map_op) directive. [Back to TOC](#table-of-contents) set_encode_base64url ----------------- **syntax:** *set_encode_base64url $dst <src>* **syntax:** *set_encode_base64url $dst* **default:** *no* **context:** *location, location if* **phase:** *rewrite* **category:** *ndk_set_var_value* When taking two arguments, this directive will encode the value of the second argument `` to its base64 url safe digest and assign the result into the first argument, variable `$dst`. For example, ```nginx location /test { set $raw "abcde"; set_encode_base64url $digest $raw; echo $digest; } ``` Then request `GET /test` will yield the following output ``` YWJjZGU= ``` Please note that we're using [echo-nginx-module](http://github.com/openresty/echo-nginx-module)'s [echo directive](http://github.com/openresty/echo-nginx-module#echo) here to output values of nginx variables directly. When taking a single argument, this directive will do in-place modification of the argument variable. For example, ```nginx location /test { set $value "abcde"; set_encode_base64url $value; echo $value; } ``` then request `GET /test` will give exactly the same output as the previous example. This directive can be invoked by [lua-nginx-module](http://github.com/openresty/lua-nginx-module)'s [ndk.set_var.DIRECTIVE](http://github.com/openresty/lua-nginx-module#ndkset_vardirective) interface and [array-var-nginx-module](http://github.com/openresty/array-var-nginx-module)'s [array_map_op](http://github.com/openresty/array-var-nginx-module#array_map_op) directive. [Back to TOC](#table-of-contents) set_decode_base64 ----------------- **syntax:** *set_decode_base64 $dst <src>* **syntax:** *set_decode_base64 $dst* **default:** *no* **context:** *location, location if* **phase:** *rewrite* **category:** *ndk_set_var_value* Similar to the [set_encode_base64](#set_encode_base64) directive, but does exactly the opposite operation, .i.e, decoding a base64 digest into its original form. [Back to TOC](#table-of-contents) set_decode_base64url ----------------- **syntax:** *set_decode_base64url $dst <src>* **syntax:** *set_decode_base64url $dst* **default:** *no* **context:** *location, location if* **phase:** *rewrite* **category:** *ndk_set_var_value* Similar to the [set_encode_base64url](#set_encode_base64url) directive, but does exactly the the opposite operation, .i.e, decoding a base64 url safe digest into its original form. [Back to TOC](#table-of-contents) set_encode_hex -------------- **syntax:** *set_encode_hex $dst <src>* **syntax:** *set_encode_hex $dst* **default:** *no* **context:** *location, location if* **phase:** *rewrite* **category:** *ndk_set_var_value* When taking two arguments, this directive will encode the value of the second argument `` to its hexadecimal digest and assign the result into the first argument, variable `$dst`. For example, ```nginx location /test { set $raw "章亦春"; set_encode_hex $digest $raw; echo $digest; } ``` Then request `GET /test` will yield the following output ``` e7aba0e4baa6e698a5 ``` Please note that we're using [echo-nginx-module](http://github.com/openresty/echo-nginx-module)'s [echo directive](http://github.com/openresty/echo-nginx-module#echo) here to output values of nginx variables directly. When taking a single argument, this directive will do in-place modification of the argument variable. For example, ```nginx location /test { set $value "章亦春"; set_encode_hex $value; echo $value; } ``` then request `GET /test` will give exactly the same output as the previous example. This directive can be invoked by [lua-nginx-module](http://github.com/openresty/lua-nginx-module)'s [ndk.set_var.DIRECTIVE](http://github.com/openresty/lua-nginx-module#ndkset_vardirective) interface and [array-var-nginx-module](http://github.com/openresty/array-var-nginx-module)'s [array_map_op](http://github.com/openresty/array-var-nginx-module#array_map_op) directive. [Back to TOC](#table-of-contents) set_decode_hex -------------- **syntax:** *set_decode_hex $dst <src>* **syntax:** *set_decode_hex $dst* **default:** *no* **context:** *location, location if* **phase:** *rewrite* **category:** *ndk_set_var_value* Similar to the [set_encode_hex](#set_encode_hex) directive, but does exactly the opposite operation, .i.e, decoding a hexadecimal digest into its original form. [Back to TOC](#table-of-contents) set_sha1 -------- **syntax:** *set_sha1 $dst <src>* **syntax:** *set_sha1 $dst* **default:** *no* **context:** *location, location if* **phase:** *rewrite* **category:** *ndk_set_var_value* When taking two arguments, this directive will encode the value of the second argument `` to its [SHA-1](http://en.wikipedia.org/wiki/SHA-1) digest and assign the result into the first argument, variable `$dst`. The hexadecimal form of the `SHA-1` digest will be generated automatically, use [set_decode_hex](#set_decode_hex) to decode the result if you want the binary form of the `SHA-1` digest. For example, ```nginx location /test { set $raw "hello"; set_sha1 $digest $raw; echo $digest; } ``` Then request `GET /test` will yield the following output ``` aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d ``` Please note that we're using [echo-nginx-module](http://github.com/openresty/echo-nginx-module)'s [echo directive](http://github.com/openresty/echo-nginx-module#echo) here to output values of nginx variables directly. When taking a single argument, this directive will do in-place modification of the argument variable. For example, ```nginx location /test { set $value "hello"; set_sha1 $value; echo $value; } ``` then request `GET /test` will give exactly the same output as the previous example. This directive can be invoked by [lua-nginx-module](http://github.com/openresty/lua-nginx-module)'s [ndk.set_var.DIRECTIVE](http://github.com/openresty/lua-nginx-module#ndkset_vardirective) interface and [array-var-nginx-module](http://github.com/openresty/array-var-nginx-module)'s [array_map_op](http://github.com/openresty/array-var-nginx-module#array_map_op) directive. [Back to TOC](#table-of-contents) set_md5 ------- **syntax:** *set_md5 $dst <src>* **syntax:** *set_md5 $dst* **default:** *no* **context:** *location, location if* **phase:** *rewrite* **category:** *ndk_set_var_value* When taking two arguments, this directive will encode the value of the second argument `` to its [MD5](http://en.wikipedia.org/wiki/MD5) digest and assign the result into the first argument, variable `$dst`. The hexadecimal form of the `MD5` digest will be generated automatically, use [set_decode_hex](#set_decode_hex) to decode the result if you want the binary form of the `MD5` digest. For example, ```nginx location /test { set $raw "hello"; set_md5 $digest $raw; echo $digest; } ``` Then request `GET /test` will yield the following output 5d41402abc4b2a76b9719d911017c592 Please note that we're using [echo-nginx-module](http://github.com/openresty/echo-nginx-module)'s [echo directive](http://github.com/openresty/echo-nginx-module#echo) here to output values of nginx variables directly. When taking a single argument, this directive will do in-place modification of the argument variable. For example, ```nginx location /test { set $value "hello"; set_md5 $value; echo $value; } ``` then request `GET /test` will give exactly the same output as the previous example. This directive can be invoked by [lua-nginx-module](http://github.com/openresty/lua-nginx-module)'s [ndk.set_var.DIRECTIVE](http://github.com/openresty/lua-nginx-module#ndkset_vardirective) interface and [array-var-nginx-module](http://github.com/openresty/array-var-nginx-module)'s [array_map_op](http://github.com/openresty/array-var-nginx-module#array_map_op) directive. [Back to TOC](#table-of-contents) set_hmac_sha1 ------------- **syntax:** *set_hmac_sha1 $dst <secret_key> <src>* **syntax:** *set_hmac_sha1 $dst* **default:** *no* **context:** *location, location if* **phase:** *rewrite* Computes the [HMAC-SHA1](http://en.wikipedia.org/wiki/HMAC) digest of the argument `` and assigns the result into the argument variable `$dst` with the secret key ``. The raw binary form of the `HMAC-SHA1` digest will be generated, use [set_encode_base64](#set_encode_base64), for example, to encode the result to a textual representation if desired. For example, ```nginx location /test { set $secret 'thisisverysecretstuff'; set $string_to_sign 'some string we want to sign'; set_hmac_sha1 $signature $secret $string_to_sign; set_encode_base64 $signature $signature; echo $signature; } ``` Then request `GET /test` will yield the following output ``` R/pvxzHC4NLtj7S+kXFg/NePTmk= ``` Please note that we're using [echo-nginx-module](http://github.com/openresty/echo-nginx-module)'s [echo directive](http://github.com/openresty/echo-nginx-module#echo) here to output values of nginx variables directly. This directive requires the OpenSSL library enabled in your Nginx build (usually by passing the `--with-http_ssl_module` option to the `./configure` script). [Back to TOC](#table-of-contents) set_hmac_sha256 --------------- **syntax:** *set_hmac_sha256 $dst <secret_key> <src>* **syntax:** *set_hmac_sha256 $dst* **default:** *no* **context:** *location, location if* **phase:** *rewrite* Computes the [HMAC-SHA256](http://en.wikipedia.org/wiki/HMAC) digest of the argument `` and assigns the result into the argument variable `$dst` with the secret key ``. The raw binary form of the `HMAC-SHA256` digest will be generated, use [set_encode_base64](#set_encode_base64), for example, to encode the result to a textual representation if desired. For example, ```nginx location /test { set $secret 'thisisverysecretstuff'; set $string_to_sign 'some string we want to sign'; set_hmac_sha256 $signature $secret $string_to_sign; set_encode_base64 $signature $signature; echo $signature; } ``` Then request `GET /test` will yield the following output ``` 4pU3GRQrKKIoeLb9CqYsavHE2l6Hx+KMmRmesU+Cfrs= ``` Please note that we're using [echo-nginx-module](http://github.com/openresty/echo-nginx-module)'s [echo directive](http://github.com/openresty/echo-nginx-module#echo) here to output values of nginx variables directly. This directive requires the OpenSSL library enabled in your Nginx build (usually by passing the `--with-http_ssl_module` option to the `./configure` script). [Back to TOC](#table-of-contents) set_random ---------- **syntax:** *set_random $res <from> <to>* **default:** *no* **context:** *location, location if* **phase:** *rewrite* Generates a (pseudo) random number (in textual form) within the range `[<$from>, <$to>]` (inclusive). Only non-negative numbers are allowed for the `` and `` arguments. When `` is greater than ``, their values will be exchanged accordingly. For instance, ```nginx location /test { set $from 5; set $to 7; set_random $res $from $to; echo $res; } ``` then request `GET /test` will output a number between 5 and 7 (i.e., among 5, 6, 7). 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()`. This directive was first introduced in the `v0.22rc1` release. See also [set_secure_random_alphanum](#set_secure_random_alphanum) and [set_secure_random_lcalpha](#set_secure_random_lcalpha). [Back to TOC](#table-of-contents) set_secure_random_alphanum -------------------------- **syntax:** *set_secure_random_alphanum $res <length>* **default:** *no* **context:** *location, location if* **phase:** *rewrite* Generates a cryptographically-strong random string `` characters long with the alphabet `[a-zA-Z0-9]`. `` may be between 1 and 64, inclusive. For instance, ```nginx location /test { set_secure_random_alphanum $res 32; echo $res; } ``` then request `GET /test` will output a string like `ivVVRP2DGaAqDmdf3Rv4ZDJ7k0gOfASz`. This functionality depends on the presence of the `/dev/urandom` device, available on most UNIX-like systems. See also [set_secure_random_lcalpha](#set_secure_random_lcalpha) and [set_random](#set_random). This directive was first introduced in the `v0.22rc8` release. [Back to TOC](#table-of-contents) set_secure_random_lcalpha ------------------------- **syntax:** *set_secure_random_lcalpha $res <length>* **default:** *no* **context:** *location, location if* **phase:** *rewrite* Generates a cryptographically-strong random string `` characters long with the alphabet `[a-z]`. `` may be between 1 and 64, inclusive. For instance, ```nginx location /test { set_secure_random_lcalpha $res 32; echo $res; } ``` then request `GET /test` will output a string like `kcuxcddktffsippuekhshdaclaquiusj`. This functionality depends on the presence of the `/dev/urandom` device, available on most UNIX-like systems. This directive was first introduced in the `v0.22rc8` release. See also [set_secure_random_alphanum](#set_secure_random_alphanum) and [set_random](#set_random). [Back to TOC](#table-of-contents) set_rotate ---------- **syntax:** *set_rotate $value <from> <to>* **default:** *no* **context:** *location, location if* **phase:** *rewrite* Increments `$value` but keeps it in range from `` to ``. If `$value` is greater than `` or less than `` is will be set to `` 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 `` and `` arguments. When `` is greater than ``, their values will be exchanged accordingly. For instance, ```nginx 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: ```nginx 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. [Back to TOC](#table-of-contents) set_local_today --------------- **syntax:** *set_local_today $dst* **default:** *no* **context:** *location, location if* **phase:** *rewrite* Set today's date ("yyyy-mm-dd") in localtime to the argument variable `$dst`. Here's an example, ```nginx location /today { set_local_today $today; echo $today; } ``` then request `GET /today` will output something like ``` 2011-08-16 ``` and year, the actual date you get here will vary every day ;) Behind the scene, this directive utilizes the `ngx_time` API in the Nginx core, so usually no syscall is involved due to the time caching mechanism in the Nginx core. [Back to TOC](#table-of-contents) set_formatted_gmt_time ---------------------- **syntax:** *set_formatted_gmt_time $res <time-format>* **default:** *no* **context:** *location, location if* **phase:** *rewrite* Set a formatted GMT time to variable `$res` (as the first argument) using the format string in the second argument. All the conversion specification notations in the standard C function `strftime` are supported, like `%Y` (for 4-digit years) and `%M` (for minutes in decimal). See for a complete list of conversion specification symbols. Below is an example: ```nginx location = /t { set_formatted_gmt_time $timestr "%a %b %e %H:%M:%S %Y GMT"; echo $timestr; } ``` Accessing `/t` yields the output ``` Fri Dec 13 15:34:37 2013 GMT ``` This directive was first added in the `0.23` release. See also [set_formatted_local_time](#set_formatted_local_time). [Back to TOC](#table-of-contents) set_formatted_local_time ------------------------ **syntax:** *set_formatted_local_time $res <time-format>* **default:** *no* **context:** *location, location if* **phase:** *rewrite* Set a formatted local time to variable `$res` (as the first argument) using the format string in the second argument. All the conversion specification notations in the standard C function `strftime` are supported, like `%Y` (for 4-digit years) and `%M` (for minutes in decimal). See for a complete list of conversion specification symbols. Below is an example: ```nginx location = /t { set_formatted_local_time $timestr "%a %b %e %H:%M:%S %Y %Z"; echo $timestr; } ``` Accessing `/t` yields the output ``` Fri Dec 13 15:42:15 2013 PST ``` This directive was first added in the `0.23` release. See also [set_formatted_gmt_time](#set_formatted_gmt_time). [Back to TOC](#table-of-contents) Caveats ======= Do not use [$arg_PARAMETER](http://nginx.org/en/docs/http/ngx_http_core_module.html#var_arg_), [$cookie_COOKIE](http://nginx.org/en/docs/http/ngx_http_core_module.html#var_cookie_), [$http_HEADER](http://nginx.org/en/docs/http/ngx_http_core_module.html#var_http_) or other special variables defined in the Nginx core module as the target variable in this module's directives. For instance, ```nginx set_if_empty $arg_user 'foo'; # DO NOT USE THIS! ``` may lead to segmentation faults. [Back to TOC](#table-of-contents) Installation ============ This module is included and enabled by default in the [OpenResty bundle](http://openresty.org). If you want to install this module manually with your own Nginx source tarball, then follow the steps below: Grab the nginx source code from [nginx.org](http://nginx.org/), for example, the version 1.13.6 (see [nginx compatibility](#compatibility)), and then build the source with this module: ```bash wget 'http://nginx.org/download/nginx-1.13.6.tar.gz' tar -xzvf nginx-1.13.6.tar.gz cd nginx-1.13.6/ # Here we assume you would install you nginx under /opt/nginx/. ./configure --prefix=/opt/nginx \ --with-http_ssl_module \ --add-module=/path/to/ngx_devel_kit \ --add-module=/path/to/set-misc-nginx-module make -j2 make install ``` Download the latest version of the release tarball of this module from [set-misc-nginx-module file list](http://github.com/openresty/set-misc-nginx-module/tags), and the latest tarball for [ngx_devel_kit](https://github.com/simplresty/ngx_devel_kit) from its [file list](https://github.com/simplresty/ngx_devel_kit/tags). [Back to TOC](#table-of-contents) Building as a dynamic module ---------------------------- Starting from NGINX 1.9.11, you can also compile this module as a dynamic module, by using the `--add-dynamic-module=PATH` option instead of `--add-module=PATH` on the `./configure` command line above. And then you can explicitly load the module in your `nginx.conf` via the [load_module](http://nginx.org/en/docs/ngx_core_module.html#load_module) directive, for example, ```nginx load_module /path/to/modules/ndk_http_module.so; # assuming NDK is built as a dynamic module too load_module /path/to/modules/ngx_http_set_misc_module.so; ``` Also, this module is included and enabled by default in the [OpenResty bundle](http://openresty.org/). [Back to TOC](#table-of-contents) Compatibility ============= The following versions of Nginx should work with this module: * **1.13.x** (last tested: 1.13.6) * **1.12.x** * **1.11.x** (last tested: 1.11.2) * **1.10.x** * **1.9.x** (last tested: 1.9.15) * **1.8.x** * **1.7.x** (last tested: 1.7.10) * **1.6.x** * **1.5.x** (last tested: 1.5.8) * **1.4.x** (last tested: 1.4.4) * **1.2.x** (last tested: 1.2.9) * **1.1.x** (last tested: 1.1.5) * **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) If you find that any particular version of Nginx above 0.7.46 does not work with this module, please consider [reporting a bug](#report-bugs). [Back to TOC](#table-of-contents) Report Bugs =========== Although a lot of effort has been put into testing and code tuning, there must be some serious bugs lurking somewhere in this module. So whenever you are bitten by any quirks, please don't hesitate to 1. send a bug report or even patches to the [openresty-en mailing list](https://groups.google.com/group/openresty-en), 1. or create a ticket on the [issue tracking interface](http://github.com/openresty/set-misc-nginx-module/issues) provided by GitHub. [Back to TOC](#table-of-contents) Source Repository ================= Available on github at [openresty/set-misc-nginx-module](http://github.com/openresty/set-misc-nginx-module). [Back to TOC](#table-of-contents) Changes ======= The change logs for every release of this module can be obtained from the OpenResty bundle's change logs: [Back to TOC](#table-of-contents) Test Suite ========== This module comes with a Perl-driven test suite. The [test cases](http://github.com/openresty/set-misc-nginx-module/tree/master/t/) are [declarative](http://github.com/openresty/set-misc-nginx-module/blob/master/t/escape-uri.t) too. Thanks to the [Test::Nginx](http://search.cpan.org/perldoc?Test::Nginx) module in the Perl world. To run it on your side: ```bash $ PATH=/path/to/your/nginx-with-set-misc-module:$PATH prove -r t ``` You need to terminate any Nginx processes before running the test suite if you have changed the Nginx server binary. Because a single nginx server (by default, `localhost:1984`) is used across all the test scripts (`.t` files), it's meaningless to run the test suite in parallel by specifying `-jN` when invoking the `prove` utility. [Back to TOC](#table-of-contents) Getting involved ================ You'll be very welcomed to submit patches to the [author](#author) or just ask for a commit bit to the [source repository](#source-repository) on GitHub. [Back to TOC](#table-of-contents) Author ====== Yichun Zhang (agentzh) *<agentzh@gmail.com>*, OpenResty Inc. This wiki page is also maintained by the author himself, and everybody is encouraged to improve this page as well. [Back to TOC](#table-of-contents) Copyright & License =================== Copyright (C) 2009-2018, Yichun Zhang (章亦春) , OpenResty Inc. This module is licensed under the terms of the BSD license. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. [Back to TOC](#table-of-contents) See Also ======== * [Nginx Devel Kit](https://github.com/simpl/ngx_devel_kit) * [The OpenResty bundle](http://openresty.org) [Back to TOC](#table-of-contents) set-misc-nginx-module-0.33/config000077500000000000000000000075351411535022000167650ustar00rootroot00000000000000ngx_addon_name=ngx_http_set_misc_module if test -n "$ngx_module_link"; then if test -n "$NDK_SRCS"; then echo "found ngx_devel_kit for ngx_set_misc; looks good." else echo "error: ngx_devel_kit is required to build ngx_set_misc; please put it before ngx_set_misc." 1>&2 exit 1 fi else if echo $HTTP_MODULES | grep " ndk_http_module" > /dev/null; then echo "found ngx_devel_kit for ngx_set_misc; looks good." else echo "error: ngx_devel_kit is required to build ngx_set_misc; please put it before ngx_set_misc." 1>&2 exit 1 fi fi SET_MISC_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_base64url.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 " SET_MISC_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_base64url.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_secure_random.h \ $ngx_addon_dir/src/ngx_http_set_misc_module.h \ " if [ $USE_OPENSSL = YES -o $MAIL_SSL = YES ]; then SET_MISC_DEPS="$SET_MISC_DEPS $ngx_addon_dir/src/ngx_http_set_hmac.h" SET_MISC_SRCS="$SET_MISC_SRCS $ngx_addon_dir/src/ngx_http_set_hmac.c" fi CFLAGS="$CFLAGS -DNDK_SET_VAR -DNDK_UPSTREAM_LIST" USE_SHA1=YES USE_MD5=YES if test -n "$ngx_module_link"; then ngx_module_type=HTTP ngx_module_name=$ngx_addon_name ngx_module_incs= ngx_module_deps="$SET_MISC_DEPS" ngx_module_srcs="$SET_MISC_SRCS" ngx_module_libs= . auto/module else HTTP_MODULES="$HTTP_MODULES $ngx_addon_name" NGX_ADDON_SRCS="$NGX_ADDON_SRCS $SET_MISC_SRCS" NGX_ADDON_DEPS="$NGX_ADDON_DEPS $SET_MISC_DEPS" fi set-misc-nginx-module-0.33/src/000077500000000000000000000000001411535022000163475ustar00rootroot00000000000000set-misc-nginx-module-0.33/src/ddebug.h000066400000000000000000000030741411535022000177560ustar00rootroot00000000000000#ifndef DDEBUG_H #define DDEBUG_H #include #include #if defined(DDEBUG) && (DDEBUG) # if (NGX_HAVE_VARIADIC_MACROS) # define dd(...) fprintf(stderr, "set_misc *** "); \ fprintf(stderr, __VA_ARGS__); \ fprintf(stderr, " at %s line %d.\n", __FILE__, __LINE__) # else #include #include #include static ngx_inline void dd(const char *fmt, ...) { } # endif #else # if (NGX_HAVE_VARIADIC_MACROS) # define dd(...) # else #include static ngx_inline void dd(const char *fmt, ...) { } # endif #endif #if defined(DDEBUG) && (DDEBUG) #define dd_check_read_event_handler(r) \ dd("r->read_event_handler = %s", \ r->read_event_handler == ngx_http_block_reading ? \ "ngx_http_block_reading" : \ r->read_event_handler == ngx_http_test_reading ? \ "ngx_http_test_reading" : \ r->read_event_handler == ngx_http_request_empty_handler ? \ "ngx_http_request_empty_handler" : "UNKNOWN") #define dd_check_write_event_handler(r) \ dd("r->write_event_handler = %s", \ r->write_event_handler == ngx_http_handler ? \ "ngx_http_handler" : \ r->write_event_handler == ngx_http_core_run_phases ? \ "ngx_http_core_run_phases" : \ r->write_event_handler == ngx_http_request_empty_handler ? \ "ngx_http_request_empty_handler" : "UNKNOWN") #else #define dd_check_read_event_handler(r) #define dd_check_write_event_handler(r) #endif #endif /* DDEBUG_H */ set-misc-nginx-module-0.33/src/ngx_http_set_base32.c000066400000000000000000000144661411535022000223730ustar00rootroot00000000000000#ifndef DDEBUG #define DDEBUG 0 #endif #include "ddebug.h" #include #include "ngx_http_set_base32.h" #include "ngx_http_set_misc_module.h" #define base32_encoded_length(len) ((((len)+4)/5)*8) #define base32_decoded_length(len) ((((len)+7)/8)*5) static void encode_base32(size_t slen, u_char *src, size_t *dlen, u_char *dst, ngx_flag_t padding, ngx_str_t *alphabet); static int decode_base32(size_t slen, u_char *src, size_t *dlen, u_char *dst, u_char *basis32); ngx_int_t ngx_http_set_misc_encode_base32(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v) { size_t len; u_char *p; u_char *src, *dst; ngx_http_set_misc_loc_conf_t *conf; conf = ngx_http_get_module_loc_conf(r, ngx_http_set_misc_module); len = base32_encoded_length(v->len); dd("estimated dst len: %d", (int) len); p = ngx_palloc(r->pool, len); if (p == NULL) { return NGX_ERROR; } src = v->data; dst = p; encode_base32(v->len, src, &len, dst, conf->base32_padding, &conf->base32_alphabet); res->data = p; res->len = len; dd("res (len %d): %.*s", (int) res->len, (int) res->len, res->data); return NGX_OK; } ngx_int_t ngx_http_set_misc_decode_base32(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v) { size_t len; u_char *p; u_char *src, *dst; int ret; ngx_http_set_misc_loc_conf_t *conf; conf = ngx_http_get_module_loc_conf(r, ngx_http_set_misc_module); len = base32_decoded_length(v->len); dd("estimated dst len: %d", (int) len); p = ngx_palloc(r->pool, len); if (p == NULL) { return NGX_ERROR; } src = v->data; dst = p; ret = decode_base32(v->len, src, &len, dst, conf->basis32); if (ret == 0 /* OK */) { res->data = p; res->len = len; return NGX_OK; } /* failed to decode */ res->data = NULL; res->len = 0; return NGX_OK; } /* See the implementation in src/core/ngx_string.c's * ngx_(encode|decode)_base64() for details. */ static void encode_base32(size_t slen, u_char *src, size_t *dlen, u_char *dst, ngx_flag_t padding, ngx_str_t *alphabet) { unsigned char *basis32 = alphabet->data; size_t len; u_char *s; u_char *d; len = slen; s = src; d = dst; while (len > 4) { *d++ = basis32[s[0] >> 3]; *d++ = basis32[((s[0] & 0x07) << 2) | (s[1] >> 6)]; *d++ = basis32[(s[1] >> 1) & 0x1f]; *d++ = basis32[((s[1] & 1) << 4) | (s[2] >> 4)]; *d++ = basis32[((s[2] & 0x0f) << 1) | (s[3] >> 7)]; *d++ = basis32[(s[3] >> 2) & 0x1f]; *d++ = basis32[((s[3] & 0x03) << 3) | (s[4] >> 5)]; *d++ = basis32[s[4] & 0x1f]; s += 5; len -= 5; } if (len) { *d++ = basis32[s[0] >> 3]; if (len == 1) { /* 1 byte left */ *d++ = basis32[(s[0] & 0x07) << 2]; /* pad six '='s to the end */ if (padding) { *d++ = '='; *d++ = '='; *d++ = '='; *d++ = '='; *d++ = '='; } } else { *d++ = basis32[((s[0] & 0x07) << 2) | (s[1] >> 6)]; *d++ = basis32[(s[1] >> 1) & 0x1f]; if (len == 2) { /* 2 bytes left */ *d++ = basis32[(s[1] & 1) << 4]; /* pad four '='s to the end */ if (padding) { *d++ = '='; *d++ = '='; *d++ = '='; } } else { *d++ = basis32[((s[1] & 1) << 4) | (s[2] >> 4)]; if (len == 3) { /* 3 bytes left */ *d++ = basis32[(s[2] & 0x0f) << 1]; if (padding) { /* pad three '='s to the end */ *d++ = '='; *d++ = '='; } } else { /* 4 bytes left */ *d++ = basis32[((s[2] & 0x0f) << 1) | (s[3] >> 7)]; *d++ = basis32[(s[3] >> 2) & 0x1f]; *d++ = basis32[(s[3] & 0x03) << 3]; /* pad one '=' to the end */ } } } if (padding) { *d++ = '='; } } *dlen = (size_t) (d - dst); } static int decode_base32(size_t slen, u_char *src, size_t *dlen, u_char *dst, u_char *basis32) { size_t len, mod; u_char *s = src; u_char *d = dst; for (len = 0; len < slen; len++) { if (s[len] == '=') { break; } if (basis32[s[len]] == 77) { return -1; } } mod = len % 8; if (mod == 1 || mod == 3 || mod == 6) { /* bad Base32 digest length */ return -1; } while (len > 7) { *d++ = (basis32[s[0]] << 3) | ((basis32[s[1]] >> 2) & 0x07); *d++ = ((basis32[s[1]] & 0x03) << 6) | (basis32[s[2]] << 1) | ((basis32[s[3]] >> 4) & 1); *d++ = ((basis32[s[3]] & 0x0f) << 4) | ((basis32[s[4]] >> 1) & 0x0f); *d++ = ((basis32[s[4]] & 1) << 7) | ((basis32[s[5]] & 0x1f) << 2) | ((basis32[s[6]] >> 3) & 0x03); *d++ = ((basis32[s[6]] & 0x07) << 5) | (basis32[s[7]] & 0x1f); s += 8; len -= 8; } if (len) { /* 2 bytes left */ *d++ = (basis32[s[0]] << 3) | ((basis32[s[1]] >> 2) & 0x07); if (len > 2) { /* 4 bytes left */ *d++ = ((basis32[s[1]] & 0x03) << 6) | ((basis32[s[2]] & 0x1f) << 1) | ((basis32[s[3]] >> 4) & 1); if (len > 4) { /* 5 bytes left */ *d++ = ((basis32[s[3]] & 0x0f) << 4) | ((basis32[s[4]] >> 1) & 0x0f); if (len > 5) { /* 7 bytes left */ *d++ = ((basis32[s[4]] & 1) << 7) | ((basis32[s[5]] & 0x1f) << 2) | ((basis32[s[6]] >> 3) & 0x03); } } } } *dlen = (size_t) (d - dst); return 0; } set-misc-nginx-module-0.33/src/ngx_http_set_base32.h000066400000000000000000000006251411535022000223700ustar00rootroot00000000000000#ifndef NGX_HTTP_SET_BASE32 #define NGX_HTTP_SET_BASE32 #include #include #include ngx_int_t ngx_http_set_misc_encode_base32(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v); ngx_int_t ngx_http_set_misc_decode_base32(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v); #endif /* NGX_HTTP_SET_BASE32 */ set-misc-nginx-module-0.33/src/ngx_http_set_base64.c000066400000000000000000000017311411535022000223670ustar00rootroot00000000000000#ifndef DDEBUG #define DDEBUG 0 #endif #include "ddebug.h" #include #include "ngx_http_set_base64.h" ngx_int_t ngx_http_set_misc_set_decode_base64(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v) { ngx_str_t src; src.len = v->len; src.data = v->data; res->len = ngx_base64_decoded_length(v->len); ndk_palloc_re(res->data, r->pool, res->len); if (ngx_decode_base64(res, &src) != NGX_OK) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "set_decode_base64: invalid value"); return NGX_ERROR; } return NGX_OK; } ngx_int_t ngx_http_set_misc_set_encode_base64(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v) { ngx_str_t src; src.len = v->len; src.data = v->data; res->len = ngx_base64_encoded_length(v->len); ndk_palloc_re(res->data, r->pool, res->len); ngx_encode_base64(res, &src); return NGX_OK; } set-misc-nginx-module-0.33/src/ngx_http_set_base64.h000066400000000000000000000004771411535022000224020ustar00rootroot00000000000000#include #include #include ngx_int_t ngx_http_set_misc_set_encode_base64(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v); ngx_int_t ngx_http_set_misc_set_decode_base64(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v); set-misc-nginx-module-0.33/src/ngx_http_set_base64url.c000066400000000000000000000017551411535022000231200ustar00rootroot00000000000000#ifndef DDEBUG #define DDEBUG 0 #endif #include "ddebug.h" #include #include "ngx_http_set_base64url.h" ngx_int_t ngx_http_set_misc_set_decode_base64url(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v) { ngx_str_t src; src.len = v->len; src.data = v->data; res->len = ngx_base64_decoded_length(v->len); ndk_palloc_re(res->data, r->pool, res->len); if (ngx_decode_base64url(res, &src) != NGX_OK) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "set_decode_base64url: invalid value"); return NGX_ERROR; } return NGX_OK; } ngx_int_t ngx_http_set_misc_set_encode_base64url(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v) { ngx_str_t src; src.len = v->len; src.data = v->data; res->len = ngx_base64_encoded_length(v->len); ndk_palloc_re(res->data, r->pool, res->len); ngx_encode_base64url(res, &src); return NGX_OK; } set-misc-nginx-module-0.33/src/ngx_http_set_base64url.h000066400000000000000000000005051411535022000231150ustar00rootroot00000000000000#include #include #include ngx_int_t ngx_http_set_misc_set_encode_base64url(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v); ngx_int_t ngx_http_set_misc_set_decode_base64url(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v); set-misc-nginx-module-0.33/src/ngx_http_set_default_value.c000066400000000000000000000017131411535022000241230ustar00rootroot00000000000000#ifndef DDEBUG #define DDEBUG 0 #endif #include "ddebug.h" #include #include "ngx_http_set_default_value.h" ngx_int_t ngx_http_set_misc_set_if_empty(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v) { ngx_http_variable_value_t *cur_v, *default_v; cur_v = &v[0]; default_v = &v[1]; if (cur_v->not_found || cur_v->len == 0) { res->data = default_v->data; res->len = default_v->len; return NGX_OK; } res->data = cur_v->data; res->len = cur_v->len; return NGX_OK; } char * ngx_http_set_if_empty(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 = (void *) ngx_http_set_misc_set_if_empty; filter.size = 2; filter.data = NULL; return ndk_set_var_multi_value_core(cf, &value[1], &value[1], &filter); } set-misc-nginx-module-0.33/src/ngx_http_set_default_value.h000066400000000000000000000005731411535022000241330ustar00rootroot00000000000000#include #include #include #ifndef NGX_HTTP_SET_DEFAULT_VALUE #define NGX_HTTP_SET_DEFAULT_VALUE char *ngx_http_set_if_empty(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); ngx_int_t ngx_http_set_misc_set_if_empty(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v); #endif /* NGX_HTTP_SET_DEFAULT_VALUE */ set-misc-nginx-module-0.33/src/ngx_http_set_escape_uri.c000066400000000000000000000144551411535022000234310ustar00rootroot00000000000000#ifndef DDEBUG #define DDEBUG 0 #endif #include "ddebug.h" #include #include "ngx_http_set_escape_uri.h" #include "ngx_string.h" static uintptr_t ngx_escape_uri_patched(u_char *dst, u_char *src, size_t size, ngx_uint_t type); ngx_int_t ngx_http_set_misc_escape_uri(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v) { size_t len; uintptr_t escape; u_char *src, *dst; if (v->len == 0) { res->len = 0; res->data = NULL; return NGX_OK; } src = v->data; dd("before escape:%.*s", v->len, v->data); escape = 2 * ngx_escape_uri_patched(NULL, src, v->len, NGX_ESCAPE_URI); /* len = v->len + 2 * ngx_escape_uri(NULL, src, v->len, NGX_ESCAPE_URI); */ len = escape + v->len; dd("escaped string len:%zu", len); dst = ngx_palloc(r->pool, len); if (dst == NULL) { return NGX_ERROR; } if (escape == 0) { ngx_memcpy(dst, src, len); dd("escape == 0"); } else { ngx_escape_uri_patched(dst, src, v->len, NGX_ESCAPE_URI); } res->data = dst; res->len = len; dd("after eacape:%.*s", (int) res->len, res->data); return NGX_OK; } static uintptr_t ngx_escape_uri_patched(u_char *dst, u_char *src, size_t size, ngx_uint_t type) { ngx_uint_t n; uint32_t *escape; static u_char hex[] = "0123456789ABCDEF"; /* " ", "#", "%", "?", %00-%1F, %7F-%FF */ static uint32_t uri[] = { 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ 0xfc00886d, /* 1111 1100 0000 0000 1000 1000 0110 1101 */ /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ 0x78000000, /* 0111 1000 0000 0000 0000 0000 0000 0000 */ /* ~}| {zyx wvut srqp onml kjih gfed cba` */ 0xa8000000, /* 1010 1000 0000 0000 0000 0000 0000 0000 */ 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */ }; /* " ", "#", "%", "+", "?", %00-%1F, %7F-%FF */ static uint32_t args[] = { 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ 0x80000829, /* 1000 0000 0000 0000 0000 1000 0010 1001 */ /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ /* ~}| {zyx wvut srqp onml kjih gfed cba` */ 0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */ 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */ }; /* " ", "#", """, "%", "'", %00-%1F, %7F-%FF */ static uint32_t html[] = { 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ 0x000000ad, /* 0000 0000 0000 0000 0000 0000 1010 1101 */ /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ /* ~}| {zyx wvut srqp onml kjih gfed cba` */ 0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */ 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */ }; /* " ", """, "%", "'", %00-%1F, %7F-%FF */ static uint32_t refresh[] = { 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ 0x00000085, /* 0000 0000 0000 0000 0000 0000 1000 0101 */ /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ /* ~}| {zyx wvut srqp onml kjih gfed cba` */ 0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */ 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ 0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */ }; /* " ", "%", %00-%1F */ static uint32_t memcached[] = { 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ 0x00000021, /* 0000 0000 0000 0000 0000 0000 0010 0001 */ /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ /* ~}| {zyx wvut srqp onml kjih gfed cba` */ 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */ }; /* mail_auth is the same as memcached */ static uint32_t *map[] = { uri, args, html, refresh, memcached, memcached }; escape = map[type]; if (dst == NULL) { /* find the number of the characters to be escaped */ n = 0; while (size) { if (escape[*src >> 5] & (1 << (*src & 0x1f))) { n++; } src++; size--; } return (uintptr_t) n; } while (size) { if (escape[*src >> 5] & (1 << (*src & 0x1f))) { *dst++ = '%'; *dst++ = hex[*src >> 4]; *dst++ = hex[*src & 0xf]; src++; } else { *dst++ = *src++; } size--; } return (uintptr_t) dst; } set-misc-nginx-module-0.33/src/ngx_http_set_escape_uri.h000066400000000000000000000003321411535022000234230ustar00rootroot00000000000000#ifndef NGX_HTTP_SET_ESCAPE_URI #define NGX_HTTP_SET_ESCAPE_URI ngx_int_t ngx_http_set_misc_escape_uri(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v); #endif /* NGX_HTTP_SET_ESCAPE_URI */ set-misc-nginx-module-0.33/src/ngx_http_set_hash.c000066400000000000000000000030251411535022000222240ustar00rootroot00000000000000#ifndef DDEBUG #define DDEBUG 0 #endif #include "ddebug.h" #include "ngx_http_set_hash.h" #if NGX_HAVE_SHA1 #include "ngx_sha1.h" #ifndef SHA_DIGEST_LENGTH #define SHA_DIGEST_LENGTH 20 #endif #endif #include "ngx_md5.h" #ifndef MD5_DIGEST_LENGTH #define MD5_DIGEST_LENGTH 16 #endif enum { #if NGX_HAVE_SHA1 SHA_HEX_LENGTH = SHA_DIGEST_LENGTH * 2, #endif MD5_HEX_LENGTH = MD5_DIGEST_LENGTH * 2 }; #if NGX_HAVE_SHA1 ngx_int_t ngx_http_set_misc_set_sha1(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v) { u_char *p; ngx_sha1_t sha; u_char sha_buf[SHA_DIGEST_LENGTH]; p = ngx_palloc(r->pool, SHA_HEX_LENGTH); if (p == NULL) { return NGX_ERROR; } ngx_sha1_init(&sha); ngx_sha1_update(&sha, v->data, v->len); ngx_sha1_final(sha_buf, &sha); ngx_hex_dump(p, sha_buf, sizeof(sha_buf)); res->data = p; res->len = SHA_HEX_LENGTH; return NGX_OK; } #endif ngx_int_t ngx_http_set_misc_set_md5(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v) { u_char *p; ngx_md5_t md5; u_char md5_buf[MD5_DIGEST_LENGTH]; p = ngx_palloc(r->pool, MD5_HEX_LENGTH); if (p == NULL) { return NGX_ERROR; } ngx_md5_init(&md5); ngx_md5_update(&md5, v->data, v->len); ngx_md5_final(md5_buf, &md5); ngx_hex_dump(p, md5_buf, sizeof(md5_buf)); res->data = p; res->len = MD5_HEX_LENGTH; return NGX_OK; } set-misc-nginx-module-0.33/src/ngx_http_set_hash.h000066400000000000000000000005531411535022000222340ustar00rootroot00000000000000#ifndef NGX_HTTP_SET_HASH_H #define NGX_HTTP_SET_HASH_H #include "ngx_http_set_misc_module.h" ngx_int_t ngx_http_set_misc_set_sha1(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v); ngx_int_t ngx_http_set_misc_set_md5(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v); #endif /* NGX_HTTP_SET_HASH_H */ set-misc-nginx-module-0.33/src/ngx_http_set_hashed_upstream.c000066400000000000000000000066151411535022000244650ustar00rootroot00000000000000#ifndef DDEBUG #define DDEBUG 0 #endif #include "ddebug.h" #include "ngx_http_set_hashed_upstream.h" ngx_uint_t ngx_http_set_misc_apply_distribution(ngx_log_t *log, ngx_uint_t hash, ndk_upstream_list_t *ul, ngx_http_set_misc_distribution_t type) { switch (type) { case ngx_http_set_misc_distribution_modula: return (uint32_t) hash % (uint32_t) ul->nelts; default: ngx_log_error(NGX_LOG_ERR, log, 0, "apply_distribution: " "unknown distribution: %d", type); return 0; } /* impossible to reach here */ } ngx_int_t ngx_http_set_misc_set_hashed_upstream(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v, void *data) { ngx_str_t **u; ndk_upstream_list_t *ul = data; ngx_str_t ulname; ngx_uint_t hash, index; ngx_http_variable_value_t *key; if (ul == NULL) { ulname.data = v->data; ulname.len = v->len; dd("ulname: %.*s", (int) ulname.len, ulname.data); ul = ndk_get_upstream_list(ndk_http_get_main_conf(r), ulname.data, ulname.len); if (ul == NULL) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "set_hashed_upstream: upstream list \"%V\" " "not defined yet", &ulname); return NGX_ERROR; } key = v + 1; } else { key = v; } if (ul->nelts == 0) { res->data = NULL; res->len = 0; return NGX_OK; } u = ul->elts; dd("upstream list: %d upstreams found", (int) ul->nelts); if (ul->nelts == 1) { dd("only one upstream found in the list"); res->data = u[0]->data; res->len = u[0]->len; return NGX_OK; } dd("key: \"%.*s\"", key->len, key->data); hash = ngx_hash_key_lc(key->data, key->len); index = ngx_http_set_misc_apply_distribution(r->connection->log, hash, ul, ngx_http_set_misc_distribution_modula); res->data = u[index]->data; res->len = u[index]->len; return NGX_OK; } char * ngx_http_set_hashed_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_str_t *value; ndk_set_var_t filter; ngx_uint_t n; ngx_str_t *var; ngx_str_t *ulname; ndk_upstream_list_t *ul; ngx_str_t *v; value = cf->args->elts; var = &value[1]; ulname = &value[2]; n = ngx_http_script_variables_count(ulname); filter.func = (void *) ngx_http_set_misc_set_hashed_upstream; if (n) { /* upstream list name contains variables */ v = &value[2]; filter.size = 2; filter.data = NULL; filter.type = NDK_SET_VAR_MULTI_VALUE_DATA; return ndk_set_var_multi_value_core(cf, var, v, &filter); } ul = ndk_get_upstream_list(ndk_http_conf_get_main_conf(cf), ulname->data, ulname->len); if (ul == NULL) { ngx_log_error(NGX_LOG_ERR, cf->log, 0, "set_hashed_upstream: upstream list \"%V\" " "not defined yet", ulname); return NGX_CONF_ERROR; } v = &value[3]; filter.size = 1; filter.data = ul; filter.type = NDK_SET_VAR_VALUE_DATA; return ndk_set_var_value_core(cf, var, v, &filter); } set-misc-nginx-module-0.33/src/ngx_http_set_hashed_upstream.h000066400000000000000000000013421411535022000244620ustar00rootroot00000000000000#ifndef NGX_HTTP_SET_HASHED_UPSTREAM #define NGX_HTTP_SET_HASHED_UPSTREAM #include #include #include #include typedef enum { ngx_http_set_misc_distribution_modula, ngx_http_set_misc_distribution_random /* XXX not used */ } ngx_http_set_misc_distribution_t; ngx_uint_t ngx_http_set_misc_apply_distribution(ngx_log_t *log, ngx_uint_t hash, ndk_upstream_list_t *ul, ngx_http_set_misc_distribution_t type); char *ngx_http_set_hashed_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); ngx_int_t ngx_http_set_misc_set_hashed_upstream(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v, void *data); #endif /* NGX_HTTP_SET_HASHED_UPSTREAM */ set-misc-nginx-module-0.33/src/ngx_http_set_hex.c000066400000000000000000000024461411535022000220730ustar00rootroot00000000000000#ifndef DDEBUG #define DDEBUG 0 #endif #include "ddebug.h" #include #include "ngx_http_set_hex.h" ngx_int_t ngx_http_set_misc_set_decode_hex(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v) { u_char *p; ngx_int_t n; ngx_uint_t i; size_t len; if (v->len % 2 != 0) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "set_decode_hex: invalid value"); return NGX_ERROR; } p = v->data; len = v->len >> 1; res->data = ngx_palloc(r->pool, len); if (res->data == NULL) { return NGX_ERROR; } for (i = 0; i < len; i++) { n = ngx_hextoi(p, 2); if (n == NGX_ERROR || n > 255) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "set_decode_hex: invalid value"); return NGX_ERROR; } p += 2; res->data[i] = (u_char) n; } res->len = len; return NGX_OK; } ngx_int_t ngx_http_set_misc_set_encode_hex(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v) { res->len = v->len << 1; res->data = ngx_palloc(r->pool, res->len); if (res->data == NULL) { return NGX_ERROR; } ngx_hex_dump(res->data, v->data, v->len); return NGX_OK; } set-misc-nginx-module-0.33/src/ngx_http_set_hex.h000066400000000000000000000004701411535022000220730ustar00rootroot00000000000000#include #include #include ngx_int_t ngx_http_set_misc_set_decode_hex(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v); ngx_int_t ngx_http_set_misc_set_encode_hex(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v); set-misc-nginx-module-0.33/src/ngx_http_set_hmac.c000066400000000000000000000030331411535022000222100ustar00rootroot00000000000000#ifndef DDEBUG #define DDEBUG 0 #endif #include "ddebug.h" #include #include "ngx_http_set_hmac.h" #include #include /* this function's implementation is partly borrowed from * https://github.com/anomalizer/ngx_aws_auth */ static ngx_int_t ngx_http_set_misc_set_hmac(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v, const EVP_MD *evp_md) { ngx_http_variable_value_t *secret, *string_to_sign; unsigned int md_len = 0; unsigned char md[EVP_MAX_MD_SIZE]; secret = v; string_to_sign = v + 1; dd("secret=%.*s, string_to_sign=%.*s", (int) secret->len, secret->data, (int) string_to_sign->len, string_to_sign->data); HMAC(evp_md, secret->data, secret->len, string_to_sign->data, string_to_sign->len, md, &md_len); /* defensive test if there is something wrong with openssl */ if (md_len == 0 || md_len > EVP_MAX_MD_SIZE) { res->len = 0; return NGX_ERROR; } res->len = md_len; ndk_palloc_re(res->data, r->pool, md_len); ngx_memcpy(res->data, &md, md_len); return NGX_OK; } ngx_int_t ngx_http_set_misc_set_hmac_sha1(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v) { return ngx_http_set_misc_set_hmac(r, res, v, EVP_sha1()); } ngx_int_t ngx_http_set_misc_set_hmac_sha256(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v) { return ngx_http_set_misc_set_hmac(r, res, v, EVP_sha256()); } set-misc-nginx-module-0.33/src/ngx_http_set_hmac.h000066400000000000000000000004701411535022000222170ustar00rootroot00000000000000#include #include #include ngx_int_t ngx_http_set_misc_set_hmac_sha1(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v); ngx_int_t ngx_http_set_misc_set_hmac_sha256(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v); set-misc-nginx-module-0.33/src/ngx_http_set_local_today.c000066400000000000000000000042411411535022000235740ustar00rootroot00000000000000#ifndef DDEBUG #define DDEBUG 0 #endif #include "ddebug.h" #include #ifndef NGX_HTTP_SET_MISC_FMT_DATE_LEN #define NGX_HTTP_SET_MISC_FMT_DATE_LEN 256 #endif ngx_int_t ngx_http_set_local_today(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v) { time_t now; ngx_tm_t tm; u_char *p; /*t = ngx_timeofday();*/ now = ngx_time(); ngx_gmtime(now + ngx_cached_time->gmtoff * 60, &tm); dd("tm.ngx_tm_hour:%d", tm.ngx_tm_hour); p = ngx_palloc(r->pool, sizeof("yyyy-mm-dd") - 1); if (p == NULL) { return NGX_ERROR; } ngx_sprintf(p, "%04d-%02d-%02d", tm.ngx_tm_year, tm.ngx_tm_mon, tm.ngx_tm_mday); res->data = p; res->len = sizeof("yyyy-mm-dd") - 1; return NGX_OK; } ngx_int_t ngx_http_set_formatted_gmt_time(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v) { time_t now; u_char *p; struct tm tm; if (v->not_found || v->len == 0) { res->data = NULL; res->len = 0; return NGX_OK; } now = ngx_time(); ngx_libc_gmtime(now, &tm); p = ngx_palloc(r->pool, NGX_HTTP_SET_MISC_FMT_DATE_LEN); if (p == NULL) { return NGX_ERROR; } res->len = strftime((char *) p, NGX_HTTP_SET_MISC_FMT_DATE_LEN, (char *) v->data, &tm); if (res->len == 0) { return NGX_ERROR; } res->data = p; return NGX_OK; } ngx_int_t ngx_http_set_formatted_local_time(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v) { time_t now; u_char *p; struct tm tm; if (v->not_found || v->len == 0) { res->data = NULL; res->len = 0; return NGX_OK; } now = ngx_time(); ngx_libc_localtime(now, &tm); p = ngx_palloc(r->pool, NGX_HTTP_SET_MISC_FMT_DATE_LEN); if (p == NULL) { return NGX_ERROR; } res->len = strftime((char *) p, NGX_HTTP_SET_MISC_FMT_DATE_LEN, (char *) v->data, &tm); if (res->len == 0) { return NGX_ERROR; } res->data = p; return NGX_OK; } set-misc-nginx-module-0.33/src/ngx_http_set_local_today.h000066400000000000000000000006531411535022000236040ustar00rootroot00000000000000#include #include #include ngx_int_t ngx_http_set_local_today(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v); ngx_int_t ngx_http_set_formatted_gmt_time(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v); ngx_int_t ngx_http_set_formatted_local_time(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v); set-misc-nginx-module-0.33/src/ngx_http_set_misc_module.c000066400000000000000000000400111411535022000235750ustar00rootroot00000000000000#ifndef DDEBUG #define DDEBUG 0 #endif #include "ddebug.h" #include "ngx_http_set_misc_module.h" #include #include "ngx_http_set_base32.h" #include "ngx_http_set_default_value.h" #include "ngx_http_set_hashed_upstream.h" #include "ngx_http_set_unescape_uri.h" #include "ngx_http_set_quote_sql.h" #include "ngx_http_set_quote_json.h" #include "ngx_http_set_escape_uri.h" #include "ngx_http_set_local_today.h" #include "ngx_http_set_hash.h" #include "ngx_http_set_hex.h" #include "ngx_http_set_base64.h" #include "ngx_http_set_base64url.h" #if NGX_OPENSSL #include "ngx_http_set_hmac.h" #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 #define BASE32_ALPHABET_LEN 32 static void *ngx_http_set_misc_create_loc_conf(ngx_conf_t *cf); static char *ngx_http_set_misc_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child); static char *ngx_http_set_misc_base32_alphabet(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static ngx_conf_deprecated_t ngx_conf_deprecated_set_misc_base32_padding = { ngx_conf_deprecated, "set_misc_base32_padding", "set_base32_padding" }; static ndk_set_var_t ngx_http_set_misc_set_encode_base64_filter = { NDK_SET_VAR_VALUE, (void *) ngx_http_set_misc_set_encode_base64, 1, NULL }; static ndk_set_var_t ngx_http_set_misc_set_decode_base64_filter = { NDK_SET_VAR_VALUE, (void *) ngx_http_set_misc_set_decode_base64, 1, NULL }; static ndk_set_var_t ngx_http_set_misc_set_encode_base64url_filter = { NDK_SET_VAR_VALUE, (void *) ngx_http_set_misc_set_encode_base64url, 1, NULL }; static ndk_set_var_t ngx_http_set_misc_set_decode_base64url_filter = { NDK_SET_VAR_VALUE, (void *) ngx_http_set_misc_set_decode_base64url, 1, NULL }; static ndk_set_var_t ngx_http_set_misc_set_decode_hex_filter = { NDK_SET_VAR_VALUE, (void *) ngx_http_set_misc_set_decode_hex, 1, NULL }; static ndk_set_var_t ngx_http_set_misc_set_encode_hex_filter = { NDK_SET_VAR_VALUE, (void *) ngx_http_set_misc_set_encode_hex, 1, NULL }; #if NGX_OPENSSL static ndk_set_var_t ngx_http_set_misc_set_hmac_sha1_filter = { NDK_SET_VAR_MULTI_VALUE, (void *) ngx_http_set_misc_set_hmac_sha1, 2, NULL }; static ndk_set_var_t ngx_http_set_misc_set_hmac_sha256_filter = { NDK_SET_VAR_MULTI_VALUE, (void *) ngx_http_set_misc_set_hmac_sha256, 2, NULL }; #endif #ifndef NGX_HTTP_SET_HASH static ndk_set_var_t ngx_http_set_misc_set_md5_filter = { NDK_SET_VAR_VALUE, (void *) ngx_http_set_misc_set_md5, 1, NULL }; #if NGX_HAVE_SHA1 static ndk_set_var_t ngx_http_set_misc_set_sha1_filter = { NDK_SET_VAR_VALUE, (void *) ngx_http_set_misc_set_sha1, 1, NULL }; #endif #endif static ndk_set_var_t ngx_http_set_misc_unescape_uri_filter = { NDK_SET_VAR_VALUE, (void *) ngx_http_set_misc_unescape_uri, 1, NULL }; static ndk_set_var_t ngx_http_set_misc_escape_uri_filter = { NDK_SET_VAR_VALUE, (void *) ngx_http_set_misc_escape_uri, 1, NULL }; static ndk_set_var_t ngx_http_set_misc_decode_base32_filter = { NDK_SET_VAR_VALUE, (void *) ngx_http_set_misc_decode_base32, 1, NULL }; static ndk_set_var_t ngx_http_set_misc_quote_sql_str_filter = { NDK_SET_VAR_VALUE, (void *) ngx_http_set_misc_quote_sql_str, 1, NULL }; static ndk_set_var_t ngx_http_set_misc_quote_pgsql_str_filter = { NDK_SET_VAR_VALUE, (void *) ngx_http_set_misc_quote_pgsql_str, 1, NULL }; static ndk_set_var_t ngx_http_set_misc_quote_json_str_filter = { NDK_SET_VAR_VALUE, (void *) ngx_http_set_misc_quote_json_str, 1, NULL }; static ndk_set_var_t ngx_http_set_misc_encode_base32_filter = { NDK_SET_VAR_VALUE, (void *) ngx_http_set_misc_encode_base32, 1, NULL }; static ndk_set_var_t ngx_http_set_misc_local_today_filter = { NDK_SET_VAR_VALUE, (void *) ngx_http_set_local_today, 0, NULL }; static ndk_set_var_t ngx_http_set_misc_formatted_gmt_time_filter = { NDK_SET_VAR_VALUE, (void *) ngx_http_set_formatted_gmt_time, 2, NULL }; static ndk_set_var_t ngx_http_set_misc_formatted_local_time_filter = { NDK_SET_VAR_VALUE, (void *) ngx_http_set_formatted_local_time, 2, NULL }; static ndk_set_var_t ngx_http_set_misc_set_random_filter = { NDK_SET_VAR_MULTI_VALUE, (void *) ngx_http_set_misc_set_random, 2, NULL }; static ndk_set_var_t ngx_http_set_misc_set_secure_random_alphanum_filter = { NDK_SET_VAR_VALUE, (void *) ngx_http_set_misc_set_secure_random_alphanum, 1, NULL }; static ndk_set_var_t ngx_http_set_misc_set_secure_random_lcalpha_filter = { NDK_SET_VAR_VALUE, (void *) ngx_http_set_misc_set_secure_random_lcalpha, 1, 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 |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12, ndk_set_var_value, 0, 0, &ngx_http_set_misc_set_encode_base64_filter }, { ngx_string ("set_decode_base64"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12, ndk_set_var_value, 0, 0, &ngx_http_set_misc_set_decode_base64_filter }, { ngx_string ("set_encode_base64url"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12, ndk_set_var_value, 0, 0, &ngx_http_set_misc_set_encode_base64url_filter }, { ngx_string ("set_decode_base64url"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12, ndk_set_var_value, 0, 0, &ngx_http_set_misc_set_decode_base64url_filter }, { ngx_string ("set_decode_hex"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12, ndk_set_var_value, 0, 0, &ngx_http_set_misc_set_decode_hex_filter }, { ngx_string ("set_encode_hex"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12, ndk_set_var_value, 0, 0, &ngx_http_set_misc_set_encode_hex_filter }, #if NGX_OPENSSL { ngx_string ("set_hmac_sha1"), 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_hmac_sha1_filter }, { ngx_string ("set_hmac_sha256"), 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_hmac_sha256_filter }, #endif #ifndef NGX_HTTP_SET_HASH { ngx_string ("set_md5"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12, ndk_set_var_value, 0, 0, &ngx_http_set_misc_set_md5_filter }, #if NGX_HAVE_SHA1 { ngx_string ("set_sha1"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12, ndk_set_var_value, 0, 0, &ngx_http_set_misc_set_sha1_filter }, #endif #endif { ngx_string ("set_unescape_uri"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12, ndk_set_var_value, 0, 0, &ngx_http_set_misc_unescape_uri_filter }, { ngx_string ("set_escape_uri"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12, ndk_set_var_value, 0, 0, &ngx_http_set_misc_escape_uri_filter }, { ngx_string ("set_quote_sql_str"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12, ndk_set_var_value, 0, 0, &ngx_http_set_misc_quote_sql_str_filter }, { ngx_string ("set_quote_pgsql_str"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12, ndk_set_var_value, 0, 0, &ngx_http_set_misc_quote_pgsql_str_filter }, { ngx_string ("set_quote_json_str"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12, ndk_set_var_value, 0, 0, &ngx_http_set_misc_quote_json_str_filter }, { ngx_string ("set_if_empty"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE2, ngx_http_set_if_empty, 0, 0, NULL }, { ngx_string("set_hashed_upstream"), 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_hashed_upstream, 0, 0, NULL }, { /* this is now deprecated; use set_base32_padding instead */ ngx_string("set_misc_base32_padding"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_FLAG, ngx_conf_set_flag_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_set_misc_loc_conf_t, base32_padding), &ngx_conf_deprecated_set_misc_base32_padding, }, { ngx_string("set_base32_padding"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_FLAG, ngx_conf_set_flag_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_set_misc_loc_conf_t, base32_padding), NULL }, { ngx_string("set_base32_alphabet"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1, ngx_http_set_misc_base32_alphabet, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_set_misc_loc_conf_t, base32_alphabet), NULL }, { ngx_string("set_encode_base32"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12, ndk_set_var_value, 0, 0, &ngx_http_set_misc_encode_base32_filter }, { ngx_string("set_decode_base32"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12, ndk_set_var_value, 0, 0, &ngx_http_set_misc_decode_base32_filter }, { ngx_string("set_local_today"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1, ndk_set_var_value, 0, 0, &ngx_http_set_misc_local_today_filter }, { ngx_string("set_formatted_gmt_time"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE2, ndk_set_var_value, 0, 0, &ngx_http_set_misc_formatted_gmt_time_filter }, { ngx_string("set_formatted_local_time"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE2, ndk_set_var_value, 0, 0, &ngx_http_set_misc_formatted_local_time_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_string ("set_secure_random_alphanum"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12, ndk_set_var_value, 0, 0, &ngx_http_set_misc_set_secure_random_alphanum_filter }, { ngx_string ("set_secure_random_lcalpha"), NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE12, ndk_set_var_value, 0, 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 }; static ngx_http_module_t ngx_http_set_misc_module_ctx = { NULL, /* preconfiguration */ NULL, /* postconfiguration */ NULL, /* create main configuration */ NULL, /* init main configuration */ NULL, /* create server configuration */ NULL, /* merge server configuration */ ngx_http_set_misc_create_loc_conf, /* create location configuration */ ngx_http_set_misc_merge_loc_conf /* merge location configuration */ }; ngx_module_t ngx_http_set_misc_module = { NGX_MODULE_V1, &ngx_http_set_misc_module_ctx, /* module context */ ngx_http_set_misc_commands, /* module directives */ NGX_HTTP_MODULE, /* module type */ NULL, /* init master */ NULL, /* init module */ NULL, /* init process */ NULL, /* init thread */ NULL, /* exit thread */ NULL, /* exit process */ NULL, /* exit master */ NGX_MODULE_V1_PADDING }; void * ngx_http_set_misc_create_loc_conf(ngx_conf_t *cf) { ngx_http_set_misc_loc_conf_t *conf; conf = ngx_palloc(cf->pool, sizeof(ngx_http_set_misc_loc_conf_t)); if (conf == NULL) { return NULL; } conf->base32_padding = NGX_CONF_UNSET; conf->base32_alphabet.data = NULL; conf->base32_alphabet.len = 0; conf->current = NGX_CONF_UNSET; return conf; } char * ngx_http_set_misc_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) { ngx_uint_t i; ngx_http_set_misc_loc_conf_t *prev = parent; ngx_http_set_misc_loc_conf_t *conf = child; ngx_conf_merge_value(conf->base32_padding, prev->base32_padding, 1); ngx_conf_merge_str_value(conf->base32_alphabet, prev->base32_alphabet, "0123456789abcdefghijklmnopqrstuv"); ngx_conf_merge_value(conf->current, prev->current, NGX_CONF_UNSET); for (i = 0; i < BASE32_ALPHABET_LEN; i++) { conf->basis32[conf->base32_alphabet.data[i]] = (u_char) i; } return NGX_CONF_OK; } static char * ngx_http_set_misc_base32_alphabet(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_str_t *value; value = cf->args->elts; if (value[1].len != BASE32_ALPHABET_LEN) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "\"set_base32_alphabet\" directive takes an " "alphabet of %uz bytes but %d expected", value[1].len, BASE32_ALPHABET_LEN); return NGX_CONF_ERROR; } return ngx_conf_set_str_slot(cf, cmd, conf); } set-misc-nginx-module-0.33/src/ngx_http_set_misc_module.h000066400000000000000000000011071411535022000236050ustar00rootroot00000000000000#ifndef NGX_HTTP_SET_MISC_MODULE_H #define NGX_HTTP_SET_MISC_MODULE_H #include #include #include #include #ifndef NGX_HAVE_SHA1 # if (nginx_version >= 1011002) # define NGX_HAVE_SHA1 1 # endif #endif typedef struct { ngx_flag_t base32_padding; ngx_str_t base32_alphabet; u_char basis32[256]; ngx_int_t current; /* for set_rotate */ } ngx_http_set_misc_loc_conf_t; extern ngx_module_t ngx_http_set_misc_module; #endif /* NGX_HTTP_SET_MISC_MODULE_H */ set-misc-nginx-module-0.33/src/ngx_http_set_quote_json.c000066400000000000000000000066421411535022000234770ustar00rootroot00000000000000#ifndef DDEBUG #define DDEBUG 0 #endif #include "ddebug.h" #include #include "ngx_http_set_quote_json.h" ngx_int_t ngx_http_set_misc_quote_json_str(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v) { size_t len; u_char *p; size_t escape; if (v->not_found || v->len == 0) { res->data = (u_char *) "null"; res->len = sizeof("null") - 1; return NGX_OK; } escape = ngx_http_set_misc_escape_json_str(NULL, v->data, v->len); len = sizeof("''") - 1 + v->len + escape; p = ngx_palloc(r->pool, len); if (p == NULL) { return NGX_ERROR; } res->data = p; res->len = len; *p++ = '\"'; if (escape == 0) { p = ngx_copy(p, v->data, v->len); } else { p = (u_char *) ngx_http_set_misc_escape_json_str(p, v->data, v->len); } *p++ = '\"'; if (p != res->data + res->len) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "set_quote_sql_str: buffer error"); return NGX_ERROR; } return NGX_OK; } uintptr_t ngx_http_set_misc_escape_json_str(u_char *dst, u_char *src, size_t size) { ngx_uint_t n; static u_char hex[] = "0123456789abcdef"; if (dst == NULL) { /* find the number of characters to be escaped */ n = 0; while (size) { /* UTF-8 char has high bit of 1 */ if ((*src & 0x80) == 0) { switch (*src) { case '\r': case '\n': case '\\': case '"': case '\f': case '\b': case '\t': n++; break; default: if (*src < 32) { n += sizeof("\\u00xx") - 2; } break; } } src++; size--; } return (uintptr_t) n; } while (size) { if ((*src & 0x80) == 0) { switch (*src) { case '\r': *dst++ = '\\'; *dst++ = 'r'; break; case '\n': *dst++ = '\\'; *dst++ = 'n'; break; case '\\': *dst++ = '\\'; *dst++ = '\\'; break; case '"': *dst++ = '\\'; *dst++ = '"'; break; case '\f': *dst++ = '\\'; *dst++ = 'f'; break; case '\b': *dst++ = '\\'; *dst++ = 'b'; break; case '\t': *dst++ = '\\'; *dst++ = 't'; break; default: if (*src < 32) { /* control chars */ *dst++ = '\\'; *dst++ = 'u'; *dst++ = '0'; *dst++ = '0'; *dst++ = hex[*src >> 4]; *dst++ = hex[*src & 0x0f]; } else { *dst++ = *src; } break; } /* switch */ src++; } else { *dst++ = *src++; } size--; } return (uintptr_t) dst; } set-misc-nginx-module-0.33/src/ngx_http_set_quote_json.h000066400000000000000000000006121411535022000234730ustar00rootroot00000000000000#ifndef NGX_HTTP_SET_QUOTE_JSON_H #define NGX_HTTP_SET_QUOTE_JSON_H #include #include #include ngx_int_t ngx_http_set_misc_quote_json_str(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v); uintptr_t ngx_http_set_misc_escape_json_str(u_char *dst, u_char *src, size_t size); #endif /* NGX_HTTP_SET_QUOTE_JSON_H */ set-misc-nginx-module-0.33/src/ngx_http_set_quote_sql.c000066400000000000000000000171721411535022000233250ustar00rootroot00000000000000#ifndef DDEBUG #define DDEBUG 0 #endif #include "ddebug.h" #include #include "ngx_http_set_quote_sql.h" static ngx_int_t ngx_http_pg_utf_escape(ngx_http_request_t *r, ngx_str_t *res); static ngx_int_t ngx_http_pg_utf_islegal(const unsigned char *s, ngx_int_t len); static ngx_int_t ngx_http_pg_utf_mblen(const unsigned char *s); ngx_int_t ngx_http_set_misc_quote_pgsql_str(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v) { u_char *pstr; ngx_int_t length; if (v->not_found || v->len ==0) { res->data = (u_char *) "''"; res->len = sizeof("''") - 1; return NGX_OK; } ngx_http_set_misc_quote_sql_str(r, res, v); length = res->len; pstr = ngx_palloc(r->pool, length + 1); if (pstr == NULL) { return NGX_ERROR; } *pstr = 'E'; memcpy(pstr + 1, res->data, length); res->data = pstr; res->len = length + 1; if (ngx_http_pg_utf_islegal(res->data, res->len)) { return NGX_OK; } if (ngx_http_pg_utf_escape(r, res) != NGX_OK) { return NGX_ERROR; } return NGX_OK; } static ngx_int_t ngx_http_pg_utf_mblen(const unsigned char *s) { int len; if ((*s & 0x80) == 0) len = 1; else if ((*s & 0xe0) == 0xc0) len = 2; else if ((*s & 0xf0) == 0xe0) len = 3; else if ((*s & 0xf8) == 0xf0) len = 4; #ifdef NOT_USED else if ((*s & 0xfc) == 0xf8) len = 5; else if ((*s & 0xfe) == 0xfc) len = 6; #endif else len = 1; return len; } static ngx_int_t ngx_http_pg_utf_islegal(const unsigned char *s, ngx_int_t len) { ngx_int_t mblen; ngx_int_t slen; u_char a; slen = len; while (slen > 0) { mblen = ngx_http_pg_utf_mblen(s); if (slen < mblen) { return 0; } switch (mblen) { case 4: a = *(s + 3); if (a < 0x80 || a > 0xBF) { return 0; } break; case 3: a = *(s + 2); if (a < 0x80 || a > 0xBF) { return 0; } break; case 2: a = *(s + 1); switch (*s) { case 0xE0: if (a < 0xA0 || a > 0xBF) { return 0; } break; case 0xED: if (a < 0x80 || a > 0x9F) { return 0; } break; case 0xF0: if (a < 0x90 || a > 0xBF) { return 0; } break; case 0xF4: if (a < 0x80 || a > 0x8F) { return 0; } break; default: if (a < 0x80 || a > 0xBF) { return 0; } break; } break; case 1: a = *s; if (a >= 0x80 && a < 0xC2) { return 0; } if (a > 0xF4) { return 0; } break; default: return 0; } s += mblen; slen -= mblen; } return 1; } static ngx_int_t ngx_http_pg_utf_escape(ngx_http_request_t *r, ngx_str_t *res) { ngx_str_t *result; ngx_int_t l, count; u_char *d, *p, *p1; l = res->len; d = res->data; result = res; count = 0; while (l-- > 0) { if (*d & 0x80) { count += 4; } d++; count++; } d = res->data; l = res->len; p = ngx_palloc(r->pool, count); if (p == NULL) { return NGX_ERROR; } p1 = p; while (l-- > 0) { if ((*d & 0x80)) { *p++ = '\\'; *p++ = '\\'; *p++ = (*d >> 6) + '0'; *p++ = ((*d >> 3) & 07) + '0'; *p++ = (*d & 07) + '0'; } else { *p++ = *d; } d++; } result->len = count; result->data = p1; return NGX_OK; } ngx_int_t ngx_http_set_misc_quote_sql_str(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v) { size_t len; u_char *p; size_t escape; if (v->not_found || v->len == 0) { res->data = (u_char *) "''"; res->len = sizeof("''") - 1; return NGX_OK; } escape = ngx_http_set_misc_escape_sql_str(NULL, v->data, v->len); len = sizeof("''") - 1 + v->len + escape; p = ngx_palloc(r->pool, len); if (p == NULL) { return NGX_ERROR; } res->data = p; res->len = len; *p++ = '\''; if (escape == 0) { p = ngx_copy(p, v->data, v->len); } else { p = (u_char *) ngx_http_set_misc_escape_sql_str(p, v->data, v->len); } *p++ = '\''; if (p != res->data + res->len) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "set_quote_sql_str: buffer error"); return NGX_ERROR; } return NGX_OK; } uintptr_t ngx_http_set_misc_escape_sql_str(u_char *dst, u_char *src, size_t size) { ngx_uint_t n; if (dst == NULL) { /* find the number of chars to be escaped */ n = 0; while (size) { /* the highest bit of all the UTF-8 chars * is always 1 */ if ((*src & 0x80) == 0) { switch (*src) { case '\0': case '\b': case '\n': case '\r': case '\t': case '\\': case '\'': case '"': case '$': case 26: /* \Z */ n++; break; default: break; } } src++; size--; } return (uintptr_t) n; } while (size) { if ((*src & 0x80) == 0) { switch (*src) { case '\0': *dst++ = '\\'; *dst++ = '0'; break; case '\b': *dst++ = '\\'; *dst++ = 'b'; break; case '\n': *dst++ = '\\'; *dst++ = 'n'; break; case '\r': *dst++ = '\\'; *dst++ = 'r'; break; case '\t': *dst++ = '\\'; *dst++ = 't'; break; case '\\': *dst++ = '\\'; *dst++ = '\\'; break; case '\'': *dst++ = '\\'; *dst++ = '\''; break; case '"': *dst++ = '\\'; *dst++ = '"'; break; case '$': *dst++ = '\\'; *dst++ = '$'; break; case 26: *dst++ = '\\'; *dst++ = 'Z'; break; default: *dst++ = *src; break; } } else { *dst++ = *src; } src++; size--; } /* while (size) */ return (uintptr_t) dst; } set-misc-nginx-module-0.33/src/ngx_http_set_quote_sql.h000066400000000000000000000007631411535022000233300ustar00rootroot00000000000000#ifndef NGX_SET_QUOTE_SQL_H #define NGX_SET_QUOTE_SQL_H #include #include #include uintptr_t ngx_http_set_misc_escape_sql_str(u_char *dst, u_char *src, size_t size); ngx_int_t ngx_http_set_misc_quote_sql_str(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v); ngx_int_t ngx_http_set_misc_quote_pgsql_str(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v); #endif /* NGX_SET_QUOTE_SQL_H */ set-misc-nginx-module-0.33/src/ngx_http_set_random.c000066400000000000000000000025421411535022000225640ustar00rootroot00000000000000#ifndef DDEBUG #define DDEBUG 0 #endif #include "ddebug.h" #include #include "ngx_http_set_random.h" #include 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; } set-misc-nginx-module-0.33/src/ngx_http_set_random.h000066400000000000000000000004151411535022000225660ustar00rootroot00000000000000#ifndef NGX_SET_RANDOM_H #define NGX_SET_RANDOM_H #include #include #include ngx_int_t ngx_http_set_misc_set_random(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v); #endif /* NGX_SET_RANDOM_H */ set-misc-nginx-module-0.33/src/ngx_http_set_rotate.c000066400000000000000000000054411411535022000226030ustar00rootroot00000000000000#ifndef DDEBUG #define DDEBUG 0 #endif #include "ddebug.h" #include #include "ngx_http_set_rotate.h" #include "ngx_http_set_misc_module.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; 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 = (void *) 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); } set-misc-nginx-module-0.33/src/ngx_http_set_rotate.h000066400000000000000000000005721411535022000226100ustar00rootroot00000000000000#ifndef NGX_HTTP_SET_MISC_ROTATE_H #define NGX_HTTP_SET_MISC_ROTATE_H #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); #endif /* NGX_HTTP_SET_MISC_ROTATE_H */ set-misc-nginx-module-0.33/src/ngx_http_set_secure_random.c000066400000000000000000000052001411535022000241240ustar00rootroot00000000000000#ifndef DDEBUG #define DDEBUG 0 #endif #include "ddebug.h" #include #include "ngx_http_set_secure_random.h" #include enum { MAX_RANDOM_STRING = 64, ALPHANUM = 1, LCALPHA = 2 }; static ngx_int_t ngx_http_set_misc_set_secure_random_common(int alphabet_type, ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v); ngx_int_t ngx_http_set_misc_set_secure_random_alphanum(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v) { return ngx_http_set_misc_set_secure_random_common(ALPHANUM, r, res, v); } ngx_int_t ngx_http_set_misc_set_secure_random_lcalpha(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v) { return ngx_http_set_misc_set_secure_random_common(LCALPHA, r, res, v); } static ngx_int_t ngx_http_set_misc_set_secure_random_common(int alphabet_type, ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v) { static u_char alphabet[] = "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; u_char entropy[MAX_RANDOM_STRING]; u_char output[MAX_RANDOM_STRING]; ngx_int_t length, i; ngx_fd_t fd; ssize_t n; length = ngx_atoi(v->data, v->len); if (length == NGX_ERROR || length < 1 || length > MAX_RANDOM_STRING) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "set_random: bad \"length\" argument: %v", v); return NGX_ERROR; } fd = ngx_open_file((u_char *) "/dev/urandom", NGX_FILE_RDONLY, NGX_FILE_OPEN, 0); if (fd == NGX_INVALID_FILE) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "set_secure_random: could not open /dev/urandom"); return NGX_ERROR; } n = ngx_read_fd(fd, entropy, length); if (n != length) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "set_secure_random: could not read all %i byte(s) from " "/dev/urandom", length); ngx_close_file(fd); return NGX_ERROR; } ngx_close_file(fd); for (i = 0; i < length; i++) { if (alphabet_type == LCALPHA) { output[i] = entropy[i] % 26 + 'a'; } else { output[i] = alphabet[ entropy[i] % (sizeof alphabet - 1) ]; } } res->data = ngx_palloc(r->pool, length); if (res->data == NULL) { return NGX_ERROR; } ngx_memcpy(res->data, output, length); res->len = length; /* set all required params */ v->valid = 1; v->no_cacheable = 0; v->not_found = 0; return NGX_OK; } set-misc-nginx-module-0.33/src/ngx_http_set_secure_random.h000066400000000000000000000006671411535022000241450ustar00rootroot00000000000000#ifndef NGX_SET_SECURE_RANDOM_H #define NGX_SET_SECURE_RANDOM_H #include #include #include ngx_int_t ngx_http_set_misc_set_secure_random_alphanum(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v); ngx_int_t ngx_http_set_misc_set_secure_random_lcalpha(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v); #endif /* NGX_SET_SECURE_RANDOM_H */ set-misc-nginx-module-0.33/src/ngx_http_set_unescape_uri.c000066400000000000000000000075331411535022000237730ustar00rootroot00000000000000#ifndef DDEBUG #define DDEBUG 0 #endif #include "ddebug.h" #include #include "ngx_http_set_unescape_uri.h" #define NGX_UNESCAPE_URI_COMPONENT 0 static void ngx_unescape_uri_patched(u_char **dst, u_char **src, size_t size, ngx_uint_t type); ngx_int_t ngx_http_set_misc_unescape_uri(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v) { size_t len; u_char *p; u_char *src, *dst; /* the unescaped string can only be smaller */ len = v->len; p = ngx_palloc(r->pool, len); if (p == NULL) { return NGX_ERROR; } src = v->data; dst = p; ngx_unescape_uri_patched(&dst, &src, v->len, NGX_UNESCAPE_URI_COMPONENT); if (src != v->data + v->len) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "set_unescape_uri: input data not consumed completely"); return NGX_ERROR; } res->data = p; res->len = dst - p; return NGX_OK; } /* XXX we also decode '+' to ' ' */ static void ngx_unescape_uri_patched(u_char **dst, u_char **src, size_t size, ngx_uint_t type) { u_char *d, *s, ch, c, decoded; enum { sw_usual = 0, sw_quoted, sw_quoted_second } state; d = *dst; s = *src; state = 0; decoded = 0; while (size--) { ch = *s++; switch (state) { case sw_usual: if (ch == '?' && (type & (NGX_UNESCAPE_URI|NGX_UNESCAPE_REDIRECT))) { *d++ = ch; goto done; } if (ch == '%') { state = sw_quoted; break; } if (ch == '+') { *d++ = ' '; break; } *d++ = ch; break; case sw_quoted: if (ch >= '0' && ch <= '9') { decoded = (u_char) (ch - '0'); state = sw_quoted_second; break; } c = (u_char) (ch | 0x20); if (c >= 'a' && c <= 'f') { decoded = (u_char) (c - 'a' + 10); state = sw_quoted_second; break; } /* the invalid quoted character */ state = sw_usual; *d++ = ch; break; case sw_quoted_second: state = sw_usual; if (ch >= '0' && ch <= '9') { ch = (u_char) ((decoded << 4) + ch - '0'); if (type & NGX_UNESCAPE_REDIRECT) { if (ch > '%' && ch < 0x7f) { *d++ = ch; break; } *d++ = '%'; *d++ = *(s - 2); *d++ = *(s - 1); break; } *d++ = ch; break; } c = (u_char) (ch | 0x20); if (c >= 'a' && c <= 'f') { ch = (u_char) ((decoded << 4) + c - 'a' + 10); if (type & NGX_UNESCAPE_URI) { if (ch == '?') { *d++ = ch; goto done; } *d++ = ch; break; } if (type & NGX_UNESCAPE_REDIRECT) { if (ch == '?') { *d++ = ch; goto done; } if (ch > '%' && ch < 0x7f) { *d++ = ch; break; } *d++ = '%'; *d++ = *(s - 2); *d++ = *(s - 1); break; } *d++ = ch; break; } /* the invalid quoted character */ break; } } done: *dst = d; *src = s; } set-misc-nginx-module-0.33/src/ngx_http_set_unescape_uri.h000066400000000000000000000004561411535022000237750ustar00rootroot00000000000000 #ifndef NGX_HTTP_SET_UNESCAPE_URI #define NGX_HTTP_SET_UNESCAPE_URI #include #include #include ngx_int_t ngx_http_set_misc_unescape_uri(ngx_http_request_t *r, ngx_str_t *res, ngx_http_variable_value_t *v); #endif /* NGX_HTTP_SET_UNESCAPE_URI */ set-misc-nginx-module-0.33/t/000077500000000000000000000000001411535022000160235ustar00rootroot00000000000000set-misc-nginx-module-0.33/t/base32.t000066400000000000000000000235451411535022000173000ustar00rootroot00000000000000# vi:filetype= use Test::Nginx::Socket; repeat_each(3); plan tests => repeat_each() * 2 * blocks(); no_long_string(); run_tests(); #no_diff(); __DATA__ === TEST 1: base32 (5 bytes) --- config location /bar { set $a 'abcde'; set_encode_base32 $a; set $b $a; set_decode_base32 $b; echo $a; echo $b; } --- request GET /bar --- response_body c5h66p35 abcde === TEST 2: base32 (1 byte) --- config location /bar { set $a '!'; set_encode_base32 $a; set $b $a; set_decode_base32 $b; echo $a; echo $b; } --- request GET /bar --- response_body 44====== ! === TEST 3: base32 (1 byte) - not in-place editing --- config location /bar { set $a '!'; set_encode_base32 $a $a; set_decode_base32 $b $a; echo $a; echo $b; } --- request GET /bar --- response_body 44====== ! === TEST 4: base32 (hello world) --- config location /bar { set $a '"hello, world!\nhiya"'; set_encode_base32 $a; set $b $a; set_decode_base32 $b; echo $a; echo $b; } --- request GET /bar --- response_body 49k6ar3cdsm20trfe9m6888ad1knio92 "hello, world! hiya" === TEST 5: base32 (0 bytes left) --- config set_base32_padding on; location /bar { set $a '"hello, world!"'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body 49k6ar3cdsm20trfe9m68892 === TEST 6: base32 (6 bytes padded) --- config set_base32_padding on; location /bar { set $a '"hello, world!"a'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body 49k6ar3cdsm20trfe9m68892c4====== === TEST 7: base32 (4 bytes left) --- config set_base32_padding on; location /bar { set $a '"hello, world!"ab'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body 49k6ar3cdsm20trfe9m68892c5h0==== === TEST 8: base32 (3 bytes left) --- config set_base32_padding on; location /bar { set $a '"hello, world!"abc'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body 49k6ar3cdsm20trfe9m68892c5h66=== === TEST 9: base32 (1 bytes left) --- config set_base32_padding on; location /bar { set $a '"hello, world!"abcd'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body 49k6ar3cdsm20trfe9m68892c5h66p0= === TEST 10: base32 standard alphabet (5 bytes) --- config set_base32_alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; location /bar { set $a 'abcde'; set_encode_base32 $a; set $b $a; set_decode_base32 $b; echo $a; echo $b; } --- request GET /bar --- response_body MFRGGZDF abcde === TEST 11: base32 standard alphabet (1 byte) --- config set_base32_alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; location /bar { set $a '!'; set_encode_base32 $a; set $b $a; set_decode_base32 $b; echo $a; echo $b; } --- request GET /bar --- response_body EE====== ! === TEST 12: base32 standard alphabet (1 byte) - not in-place editing --- config set_base32_alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; location /bar { set $a '!'; set_encode_base32 $a $a; set_decode_base32 $b $a; echo $a; echo $b; } --- request GET /bar --- response_body EE====== ! === TEST 13: base32 standard alphabet (hello world) --- config set_base32_alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; location /bar { set $a '"hello, world!\nhiya"'; set_encode_base32 $a; set $b $a; set_decode_base32 $b; echo $a; echo $b; } --- request GET /bar --- response_body EJUGK3DMN4WCA53POJWGIIIKNBUXSYJC "hello, world! hiya" === TEST 14: base32 standard alphabet (0 bytes left) --- config set_base32_padding on; set_base32_alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; location /bar { set $a '"hello, world!"'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body EJUGK3DMN4WCA53POJWGIIJC === TEST 15: base32 standard alphabet (6 bytes padded) --- config set_base32_padding on; set_base32_alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; location /bar { set $a '"hello, world!"a'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body EJUGK3DMN4WCA53POJWGIIJCME====== === TEST 16: base32 standard alphabet (4 bytes left) --- config set_base32_padding on; set_base32_alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; location /bar { set $a '"hello, world!"ab'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body EJUGK3DMN4WCA53POJWGIIJCMFRA==== === TEST 17: base32 standard alphabet (3 bytes left) --- config set_base32_padding on; set_base32_alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; location /bar { set $a '"hello, world!"abc'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body EJUGK3DMN4WCA53POJWGIIJCMFRGG=== === TEST 18: base32 standard alphabet (1 bytes left) --- config set_base32_padding on; set_base32_alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; location /bar { set $a '"hello, world!"abcd'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body EJUGK3DMN4WCA53POJWGIIJCMFRGGZA= === TEST 19: base32 custom alphabet (5 bytes) --- config set_base32_alphabet "efghijklmnopqrstuvwxyz0123456789"; location /bar { set $a 'abcde'; set_encode_base32 $a; set $b $a; set_decode_base32 $b; echo $a; echo $b; } --- request GET /bar --- response_body qjvkk3hj abcde === TEST 20: base32 custom alphabet (1 byte) --- config set_base32_alphabet "efghijklmnopqrstuvwxyz0123456789"; location /bar { set $a '!'; set_encode_base32 $a; set $b $a; set_decode_base32 $b; echo $a; echo $b; } --- request GET /bar --- response_body ii====== ! === TEST 21: base32 custom alphabet (1 byte) - not in-place editing --- config set_base32_alphabet "efghijklmnopqrstuvwxyz0123456789"; location /bar { set $a '!'; set_encode_base32 $a $a; set_decode_base32 $b $a; echo $a; echo $b; } --- request GET /bar --- response_body ii====== ! === TEST 22: base32 custom alphabet (hello world) --- config set_base32_alphabet "efghijklmnopqrstuvwxyz0123456789"; location /bar { set $a '"hello, world!\nhiya"'; set_encode_base32 $a; set $b $a; set_decode_base32 $b; echo $a; echo $b; } --- request GET /bar --- response_body inyko5hqr60ge75tsn0kmmmorfy1w2ng "hello, world! hiya" === TEST 23: base32 custom alphabet (0 bytes left) --- config set_base32_padding on; set_base32_alphabet "efghijklmnopqrstuvwxyz0123456789"; location /bar { set $a '"hello, world!"'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body inyko5hqr60ge75tsn0kmmng === TEST 24: base32 custom alphabet (6 bytes padded) --- config set_base32_padding on; set_base32_alphabet "efghijklmnopqrstuvwxyz0123456789"; location /bar { set $a '"hello, world!"a'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body inyko5hqr60ge75tsn0kmmngqi====== === TEST 25: base32 custom alphabet (4 bytes left) --- config set_base32_padding on; set_base32_alphabet "efghijklmnopqrstuvwxyz0123456789"; location /bar { set $a '"hello, world!"ab'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body inyko5hqr60ge75tsn0kmmngqjve==== === TEST 26: base32 custom alphabet (3 bytes left) --- config set_base32_padding on; set_base32_alphabet "efghijklmnopqrstuvwxyz0123456789"; location /bar { set $a '"hello, world!"abc'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body inyko5hqr60ge75tsn0kmmngqjvkk=== === TEST 27: base32 custom alphabet (1 bytes left) --- config set_base32_padding on; set_base32_alphabet "efghijklmnopqrstuvwxyz0123456789"; location /bar { set $a '"hello, world!"abcd'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body inyko5hqr60ge75tsn0kmmngqjvkk3e= === TEST 28: use set_base32_alphabet in location --- config set_base32_padding on; location /bar { set_base32_alphabet "efghijklmnopqrstuvwxyz0123456789"; set $a '"hello, world!"abcd'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body inyko5hqr60ge75tsn0kmmngqjvkk3e= === TEST 29: one byte less in set_base32_alphabet --- config set_base32_padding on; location /bar { set_base32_alphabet "efghijklmnopqrstuvwxyz012345678"; set $a '"hello, world!"abcd?\/.;'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body inyko5hqr60ge75tsn0kmmngqjvkk3e= --- must_die --- error_log eval qr/\[emerg\] .*? "set_base32_alphabet" directive takes an alphabet of 31 bytes but 32 expected/ === TEST 30: one byte more in set_base32_alphabet --- config set_base32_padding on; location /bar { set_base32_alphabet "efghijklmnopqrstuvwxyz0123456789A"; set $a '"hello, world!"abcd?\/.;'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body inyko5hqr60ge75tsn0kmmngqjvkk3e= --- must_die --- error_log eval qr/\[emerg\] .*? "set_base32_alphabet" directive takes an alphabet of 33 bytes but 32 expected/ set-misc-nginx-module-0.33/t/base32_no_padding.t000066400000000000000000000222711411535022000214550ustar00rootroot00000000000000# vi:filetype= use Test::Nginx::Socket; repeat_each(3); plan tests => repeat_each() * 2 * blocks(); no_long_string(); run_tests(); #no_diff(); __DATA__ === TEST 1: base32 (5 bytes) --- config set_base32_padding off; location /bar { set $a 'abcde'; set_encode_base32 $a; set $b $a; set_decode_base32 $b; echo $a; echo $b; } --- request GET /bar --- response_body c5h66p35 abcde === TEST 2: base32 (1 byte) --- config set_base32_padding off; location /bar { set $a '!'; set_encode_base32 $a; set $b $a; set_decode_base32 $b; echo $a; echo $b; } --- request GET /bar --- response_body 44 ! === TEST 3: base32 (1 byte) - not in-place editing --- config location /bar { set_base32_padding off; set $a '!'; set_encode_base32 $a $a; set_decode_base32 $b $a; echo $a; echo $b; } --- request GET /bar --- response_body 44 ! === TEST 4: base32 (hello world) --- config set_base32_padding off; location /bar { set $a '"hello, world!\nhiya"'; set_encode_base32 $a; set $b $a; set_decode_base32 $b; echo $a; echo $b; } --- request GET /bar --- response_body 49k6ar3cdsm20trfe9m6888ad1knio92 "hello, world! hiya" === TEST 5: base32 (0 bytes left) --- config set_base32_padding off; location /bar { set $a '"hello, world!"'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body 49k6ar3cdsm20trfe9m68892 === TEST 6: base32 (6 bytes padded) --- config set_base32_padding off; location /bar { set $a '"hello, world!"a'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body 49k6ar3cdsm20trfe9m68892c4 === TEST 7: base32 (4 bytes left) --- config set_base32_padding off; location /bar { set $a '"hello, world!"ab'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body 49k6ar3cdsm20trfe9m68892c5h0 === TEST 8: base32 (3 bytes left) --- config set_base32_padding off; location /bar { set $a '"hello, world!"abc'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body 49k6ar3cdsm20trfe9m68892c5h66 === TEST 9: base32 (1 bytes left) --- config set_base32_padding off; location /bar { set $a '"hello, world!"abcd'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body 49k6ar3cdsm20trfe9m68892c5h66p0 === TEST 10: base32 standard alphabet (5 bytes) --- config set_base32_padding off; set_base32_alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; location /bar { set $a 'abcde'; set_encode_base32 $a; set $b $a; set_decode_base32 $b; echo $a; echo $b; } --- request GET /bar --- response_body MFRGGZDF abcde === TEST 11: base32 standard alphabet (1 byte) --- config set_base32_padding off; set_base32_alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; location /bar { set $a '!'; set_encode_base32 $a; set $b $a; set_decode_base32 $b; echo $a; echo $b; } --- request GET /bar --- response_body EE ! === TEST 12: base32 standard alphabet (1 byte) - not in-place editing --- config location /bar { set_base32_padding off; set_base32_alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; set $a '!'; set_encode_base32 $a $a; set_decode_base32 $b $a; echo $a; echo $b; } --- request GET /bar --- response_body EE ! === TEST 13: base32 standard alphabet (hello world) --- config set_base32_padding off; set_base32_alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; location /bar { set $a '"hello, world!\nhiya"'; set_encode_base32 $a; set $b $a; set_decode_base32 $b; echo $a; echo $b; } --- request GET /bar --- response_body EJUGK3DMN4WCA53POJWGIIIKNBUXSYJC "hello, world! hiya" === TEST 14: base32 standard alphabet (0 bytes left) --- config set_base32_padding off; set_base32_alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; location /bar { set $a '"hello, world!"'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body EJUGK3DMN4WCA53POJWGIIJC === TEST 15: base32 standard alphabet (6 bytes padded) --- config set_base32_padding off; set_base32_alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; location /bar { set $a '"hello, world!"a'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body EJUGK3DMN4WCA53POJWGIIJCME === TEST 16: base32 standard alphabet (4 bytes left) --- config set_base32_padding off; set_base32_alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; location /bar { set $a '"hello, world!"ab'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body EJUGK3DMN4WCA53POJWGIIJCMFRA === TEST 17: base32 standard alphabet (3 bytes left) --- config set_base32_padding off; set_base32_alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; location /bar { set $a '"hello, world!"abc'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body EJUGK3DMN4WCA53POJWGIIJCMFRGG === TEST 18: base32 standard alphabet (1 bytes left) --- config set_base32_padding off; set_base32_alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; location /bar { set $a '"hello, world!"abcd'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body EJUGK3DMN4WCA53POJWGIIJCMFRGGZA === TEST 19: base32 custom alphabet (5 bytes) --- config set_base32_padding off; set_base32_alphabet "cdefghijklmnopqrstuvwxyz12345678"; location /bar { set $a 'abcde'; set_encode_base32 $a; set $b $a; set_decode_base32 $b; echo $a; echo $b; } --- request GET /bar --- response_body ohtii2fh abcde === TEST 20: base32 custom alphabet (1 byte) --- config set_base32_padding off; set_base32_alphabet "cdefghijklmnopqrstuvwxyz12345678"; location /bar { set $a '!'; set_encode_base32 $a; set $b $a; set_decode_base32 $b; echo $a; echo $b; } --- request GET /bar --- response_body gg ! === TEST 21: base32 custom alphabet (1 byte) - not in-place editing --- config location /bar { set_base32_padding off; set_base32_alphabet "cdefghijklmnopqrstuvwxyz12345678"; set $a '!'; set_encode_base32 $a $a; set_decode_base32 $b $a; echo $a; echo $b; } --- request GET /bar --- response_body gg ! === TEST 22: base32 custom alphabet (hello world) --- config set_base32_padding off; set_base32_alphabet "cdefghijklmnopqrstuvwxyz12345678"; location /bar { set $a '"hello, world!\nhiya"'; set_encode_base32 $a; set $b $a; set_decode_base32 $b; echo $a; echo $b; } --- request GET /bar --- response_body glwim4fop5yec64rqlyikkkmpdwzu1le "hello, world! hiya" === TEST 23: base32 custom alphabet (0 bytes left) --- config set_base32_padding off; set_base32_alphabet "cdefghijklmnopqrstuvwxyz12345678"; location /bar { set $a '"hello, world!"'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body glwim4fop5yec64rqlyikkle === TEST 24: base32 custom alphabet (6 bytes padded) --- config set_base32_padding off; set_base32_alphabet "cdefghijklmnopqrstuvwxyz12345678"; location /bar { set $a '"hello, world!"a'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body glwim4fop5yec64rqlyikkleog === TEST 25: base32 custom alphabet (4 bytes left) --- config set_base32_padding off; set_base32_alphabet "cdefghijklmnopqrstuvwxyz12345678"; location /bar { set $a '"hello, world!"ab'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body glwim4fop5yec64rqlyikkleohtc === TEST 26: base32 custom alphabet (3 bytes left) --- config set_base32_padding off; set_base32_alphabet "cdefghijklmnopqrstuvwxyz12345678"; location /bar { set $a '"hello, world!"abc'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body glwim4fop5yec64rqlyikkleohtii === TEST 27: base32 custom alphabet (1 bytes left) --- config set_base32_padding off; set_base32_alphabet "cdefghijklmnopqrstuvwxyz12345678"; location /bar { set $a '"hello, world!"abcd'; set_encode_base32 $a; echo $a; } --- request GET /bar --- response_body glwim4fop5yec64rqlyikkleohtii2c === TEST 28: deprecated set_misc_base32_padding --- config set_misc_base32_padding off; location /bar { set $a 'abcde'; set_encode_base32 $a; set $b $a; set_decode_base32 $b; echo $a; echo $b; } --- request GET /bar --- response_body c5h66p35 abcde set-misc-nginx-module-0.33/t/base64.t000066400000000000000000000010171411535022000172730ustar00rootroot00000000000000# vi:filetype= use lib 'lib'; use Test::Nginx::Socket; #repeat_each(3); plan tests => repeat_each() * 2 * blocks(); no_long_string(); run_tests(); #no_diff(); __DATA__ === TEST 1: base64 encode --- config location /bar { set_encode_base64 $out "abcde"; echo $out; } --- request GET /bar --- response_body YWJjZGU= === TEST 2: base64 decode --- config location /bar { set_decode_base64 $out "YWJjZGU="; echo $out; } --- request GET /bar --- response_body abcde set-misc-nginx-module-0.33/t/base64url.t000066400000000000000000000010351411535022000200160ustar00rootroot00000000000000# vi:filetype= use lib 'lib'; use Test::Nginx::Socket; #repeat_each(3); plan tests => repeat_each() * 2 * blocks(); no_long_string(); run_tests(); #no_diff(); __DATA__ === TEST 1: base64url encode --- config location /bar { set_encode_base64url $out "?b> repeat_each() * 2 * blocks(); no_long_string(); run_tests(); #no_diff(); __DATA__ === TEST 1: set if empty --- config location /foo { set $a 32; set_if_empty $a 56; echo $a; set_if_empty $b 72; echo $b; } --- request GET /foo --- response_body 32 72 === TEST 2: set if empty --- config location /foo { set $bar $arg_bar; set_if_empty $bar 15; echo $bar; set $bah $arg_bah; set_if_empty $bah 25; echo $bah; } --- request GET /foo?bar=71 --- response_body 71 25 === TEST 3: set if empty --- config location /foo { set $bar $arg_bar; set_if_empty $bar 15; echo $bar; set $bah $arg_bah; set_if_empty $bah 25; echo $bah; } --- request GET /foo?bar= --- response_body 15 25 === TEST 4: set if empty (using arg_xxx directly) buggy? --- config location /foo { set_if_empty $arg_bar 15; echo $arg_bar; set_if_empty $arg_bah 25; echo $arg_bah; } --- request GET /foo?bar=71 --- response_body 71 25 set-misc-nginx-module-0.33/t/escape-uri.t000066400000000000000000000100101411535022000202350ustar00rootroot00000000000000# vi:filetype= use lib 'lib'; use Test::Nginx::Socket; repeat_each(3); plan tests => repeat_each() * 2 * blocks(); no_long_string(); #no_diff(); run_tests(); __DATA__ === TEST 1: set escape uri --- config location /foo { set $foo "hello world"; set_escape_uri $foo $foo; echo $foo; } --- request GET /foo --- response_body hello%20world === TEST 2: set escape uri(in-place) --- config location /foo { set $foo "hello world"; set_escape_uri $foo; echo $foo; } --- request GET /foo --- response_body hello%20world === TEST 3: blank string --- config location /foo { set $foo ""; set_escape_uri $foo; echo $foo; } --- request GET /foo --- response_body eval "\n" === TEST 4: blank string(in place) --- config location /foo { set $foo ""; set_escape_uri $foo; echo $foo; } --- request GET /foo --- response_body eval "\n" === TEST 5: eacape chinese character --- config location /foo { set $foo "你好"; set_escape_uri $foo; echo $foo; } --- request GET /foo --- response_body %E4%BD%A0%E5%A5%BD === TEST 6: escape long string --- config location /foo { set $foo "法规及饿哦物权法家哦低价非结果哦我二期界 附件饿哦武器 积分饿哦为契机佛i 该软件哦气氛 份额叫我起 国无二君哦气氛为界非ieowq结果哦而完全附件 份额叫我iqfjeowiqgjeriowqfjpdjfosadijfoiasdjf 附件饿哦武器界 份额叫我起界份额叫我起哦ifjefejwioq附件饿哦武器界非风格及去哦根据份额叫我起哦界份额为契机哦乳房阿基完全哦igqtewqo个人就去哦ieorjwrewqoi日哦额外起今天诶哦我亲热为特务前日哦我而哥特完全哦iijrtewmkdf 服务鄂潜江哦irewq"; set_escape_uri $foo; echo $foo; } --- request GET /foo --- response_body %E6%B3%95%E8%A7%84%E5%8F%8A%E9%A5%BF%E5%93%A6%E7%89%A9%E6%9D%83%E6%B3%95%E5%AE%B6%E5%93%A6%E4%BD%8E%E4%BB%B7%E9%9D%9E%E7%BB%93%E6%9E%9C%E5%93%A6%E6%88%91%E4%BA%8C%E6%9C%9F%E7%95%8C%20%20%E9%99%84%E4%BB%B6%E9%A5%BF%E5%93%A6%E6%AD%A6%E5%99%A8%20%20%E7%A7%AF%E5%88%86%E9%A5%BF%E5%93%A6%E4%B8%BA%E5%A5%91%E6%9C%BA%E4%BD%9Bi%20%E8%AF%A5%E8%BD%AF%E4%BB%B6%E5%93%A6%E6%B0%94%E6%B0%9B%20%20%E4%BB%BD%E9%A2%9D%E5%8F%AB%E6%88%91%E8%B5%B7%20%E5%9B%BD%E6%97%A0%E4%BA%8C%E5%90%9B%E5%93%A6%E6%B0%94%E6%B0%9B%E4%B8%BA%E7%95%8C%E9%9D%9Eieowq%E7%BB%93%E6%9E%9C%E5%93%A6%E8%80%8C%E5%AE%8C%E5%85%A8%E9%99%84%E4%BB%B6%20%20%E4%BB%BD%E9%A2%9D%E5%8F%AB%E6%88%91iqfjeowiqgjeriowqfjpdjfosadijfoiasdjf%20%E9%99%84%E4%BB%B6%E9%A5%BF%E5%93%A6%E6%AD%A6%E5%99%A8%E7%95%8C%20%E4%BB%BD%E9%A2%9D%E5%8F%AB%E6%88%91%E8%B5%B7%E7%95%8C%E4%BB%BD%E9%A2%9D%E5%8F%AB%E6%88%91%E8%B5%B7%E5%93%A6ifjefejwioq%E9%99%84%E4%BB%B6%E9%A5%BF%E5%93%A6%E6%AD%A6%E5%99%A8%E7%95%8C%E9%9D%9E%E9%A3%8E%E6%A0%BC%E5%8F%8A%E5%8E%BB%E5%93%A6%E6%A0%B9%E6%8D%AE%E4%BB%BD%E9%A2%9D%E5%8F%AB%E6%88%91%E8%B5%B7%E5%93%A6%E7%95%8C%E4%BB%BD%E9%A2%9D%E4%B8%BA%E5%A5%91%E6%9C%BA%E5%93%A6%E4%B9%B3%E6%88%BF%E9%98%BF%E5%9F%BA%E5%AE%8C%E5%85%A8%E5%93%A6igqtewqo%E4%B8%AA%E4%BA%BA%E5%B0%B1%E5%8E%BB%E5%93%A6ieorjwrewqoi%E6%97%A5%E5%93%A6%E9%A2%9D%E5%A4%96%E8%B5%B7%E4%BB%8A%E5%A4%A9%E8%AF%B6%E5%93%A6%E6%88%91%E4%BA%B2%E7%83%AD%E4%B8%BA%E7%89%B9%E5%8A%A1%E5%89%8D%E6%97%A5%E5%93%A6%E6%88%91%E8%80%8C%E5%93%A5%E7%89%B9%E5%AE%8C%E5%85%A8%E5%93%A6iijrtewmkdf%20%E6%9C%8D%E5%8A%A1%E9%84%82%E6%BD%9C%E6%B1%9F%E5%93%A6irewq === TEST 7: no need to escape --- config location /foo { set $foo 'welcometotheworldofnginx'; set_escape_uri $foo; echo $foo; } --- request GET /foo --- response_body welcometotheworldofnginx === TEST 8: fixed ngx_escape_uri issues: + and / should also be escaped --- config location /foo { set $foo '+/='; set_escape_uri $foo; echo $foo; } --- request GET /foo --- response_body %2B%2F%3D === TEST 9: fixed ngx_escape_uri issues: / {} : & [] and more --- config location /foo { set $foo '"a/b={}:<>;&[]\\^'; set_escape_uri $foo; echo $foo; } --- request GET /foo --- response_body %22a%2Fb%3D%7B%7D%3A%3C%3E%3B%26%5B%5D%5C%5E set-misc-nginx-module-0.33/t/formatted-time.t000066400000000000000000000030341411535022000211310ustar00rootroot00000000000000# vi:filetype= use lib 'lib'; use Test::Nginx::Socket; use POSIX qw(strftime); my $fmt="%a %b %e %H:%M:%S %Y"; our $str_local = (strftime $fmt, localtime time()).'|'.(strftime $fmt, localtime time()+1).'|'.(strftime $fmt, localtime time()+2); our $str_gmt = (strftime $fmt, gmtime time()).'|'.(strftime $fmt, gmtime time()+1).'|'.(strftime $fmt, gmtime time()+2); repeat_each(2); plan tests => repeat_each() * 2 * blocks(); log_level('warn'); run_tests(); #no_diff(); __DATA__ === TEST 1: local time format --- config location /foo { set_formatted_local_time $today "%a %b %e %H:%M:%S %Y"; echo $today; } --- request GET /foo --- response_body_like eval: $main::str_local === TEST 2: GMT time format --- config location /bar { set_formatted_gmt_time $today "%a %b %e %H:%M:%S %Y"; echo $today; } --- request GET /bar --- response_body_like eval: $main::str_gmt === TEST 3: set_formatted_gmt_time (empty formatter) --- config location /bar { set_formatted_gmt_time $today ""; echo "[$today]"; } --- request GET /bar --- response_body [] === TEST 4: set_formatted_local_time (empty formatter) --- config location /bar { set_formatted_local_time $today ""; echo "[$today]"; } --- request GET /bar --- response_body [] === TEST 5: set_formatted_local_time (constant formatter) --- config location /bar { set_formatted_local_time $today "hello world"; echo "[$today]"; } --- request GET /bar --- response_body [hello world] set-misc-nginx-module-0.33/t/hash.t000066400000000000000000000024751411535022000171430ustar00rootroot00000000000000# vi:filetype=perl use lib 'lib'; use Test::Nginx::Socket; #repeat_each(3); plan tests => repeat_each() * 2 * blocks(); no_long_string(); run_tests(); #no_diff(); __DATA__ === TEST 1: sha1 hello (copy) --- config location /sha1 { set_sha1 $a hello; echo $a; } --- request GET /sha1 --- response_body aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d === TEST 2: sha1 hello (in-place) --- config location /sha1 { set $a hello; set_sha1 $a; echo $a; } --- request GET /sha1 --- response_body aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d === TEST 3: sha1 (empty) --- config location /sha1 { set_sha1 $a ""; echo $a; } --- request GET /sha1 --- response_body da39a3ee5e6b4b0d3255bfef95601890afd80709 === TEST 4: md5 hello (copy) --- config location /md5 { set_md5 $a hello; echo $a; } --- request GET /md5 --- response_body 5d41402abc4b2a76b9719d911017c592 === TEST 5: md5 hello (in-place) --- config location /md5 { set $a hello; set_md5 $a; echo $a; } --- request GET /md5 --- response_body 5d41402abc4b2a76b9719d911017c592 === TEST 6: md5 (empty) --- config location /md5 { set_md5 $a ""; echo $a; } --- request GET /md5 --- response_body d41d8cd98f00b204e9800998ecf8427e set-misc-nginx-module-0.33/t/hashed-upstream.t000066400000000000000000000023761411535022000213120ustar00rootroot00000000000000# vi:filetype= use lib 'lib'; use Test::Nginx::Socket; #repeat_each(3); plan tests => repeat_each() * 2 * blocks(); no_long_string(); #master_on(); #log_level("warn"); run_tests(); #no_diff(); __DATA__ === TEST 1: set hashed upstream buggy? --- config upstream_list universe moon sun earth; location /foo { set_hashed_upstream $backend universe $arg_id; echo $backend; } location /main { echo_location_async /foo; echo_location_async /foo?id=hello; echo_location_async /foo?id=world; echo_location_async /foo?id=larry; echo_location_async /foo?id=audreyt; } --- request GET /main --- response_body moon sun moon earth earth === TEST 2: set hashed upstream (use var for upstream_list name) buggy? --- config upstream_list universe moon sun earth; location /foo { set $list_name universe; set_hashed_upstream $backend $list_name $arg_id; echo $backend; } location /main { echo_location_async /foo; echo_location_async /foo?id=hello; echo_location_async /foo?id=world; echo_location_async /foo?id=larry; echo_location_async /foo?id=audreyt; } --- request GET /main --- response_body moon sun moon earth earth set-misc-nginx-module-0.33/t/hex.t000066400000000000000000000014551411535022000170010ustar00rootroot00000000000000# vi:filetype=perl use lib 'lib'; use Test::Nginx::Socket; #repeat_each(3); plan tests => repeat_each() * 2 * blocks(); no_long_string(); run_tests(); #no_diff(); __DATA__ === TEST 1: hex encode --- config location /bar { set_encode_hex $out "abcde"; echo $out; } --- request GET /bar --- response_body 6162636465 === TEST 2: hex decode --- config location /bar { set_decode_hex $out "6162636465"; echo $out; } --- request GET /bar --- response_body abcde === TEST 3: hex encode (chinese) --- config location /bar { set $raw "章亦春"; set_encode_hex $digest $raw; set_decode_hex $hex $digest; echo $digest; echo $hex; } --- request GET /bar --- response_body e7aba0e4baa6e698a5 章亦春 set-misc-nginx-module-0.33/t/hmac.t000066400000000000000000000031231411535022000171170ustar00rootroot00000000000000# vi:filetype= use lib 'lib'; use Test::Nginx::Socket; #repeat_each(3); plan tests => repeat_each() * 2 * blocks(); no_long_string(); run_tests(); #no_diff(); __DATA__ === TEST 1: hmac_sha1 --- config location /bar { set $secret 'thisisverysecretstuff'; set $string_to_sign 'some string we want to sign'; set_hmac_sha1 $signature $secret $string_to_sign; set_encode_base64 $signature $signature; echo $signature; } --- request GET /bar --- response_body R/pvxzHC4NLtj7S+kXFg/NePTmk= === TEST 2: hmac_sha1 empty vars --- config location /bar { set $secret ''; set $string_to_sign ''; set_hmac_sha1 $signature $secret $string_to_sign; set_encode_base64 $signature $signature; echo $signature; } --- request GET /bar --- response_body +9sdGxiqbAgyS31ktx+3Y3BpDh0= === TEST 3: hmac_sha256 --- config location /bar { set $secret 'thisisverysecretstuff'; set $string_to_sign 'some string we want to sign'; set_hmac_sha256 $signature $secret $string_to_sign; set_encode_base64 $signature $signature; echo $signature; } --- request GET /bar --- response_body 4pU3GRQrKKIoeLb9CqYsavHE2l6Hx+KMmRmesU+Cfrs= === TEST 4: hmac_sha256 empty vars --- config location /bar { set $secret ''; set $string_to_sign ''; set_hmac_sha256 $signature $secret $string_to_sign; set_encode_base64 $signature $signature; echo $signature; } --- request GET /bar --- response_body thNnmggU2ex3L5XXeMNfxf8Wl8STcVZTxscSFEKSxa0= set-misc-nginx-module-0.33/t/local-today.t000066400000000000000000000007321411535022000204220ustar00rootroot00000000000000# vi:filetype=perl use lib 'lib'; use Test::Nginx::Socket; my ($sec, $min, $hour, $mday, $mon, $year) = localtime; our $str = sprintf("%04d-%02d-%02d\n", $year + 1900, $mon + 1, $mday); #repeat_each(3); plan tests => repeat_each() * 2 * blocks(); #no_long_string(); run_tests(); #no_diff(); __DATA__ === TEST 1: sanity --- config location /foo { set_local_today $today; echo $today; } --- request GET /foo --- response_body eval: $main::str set-misc-nginx-module-0.33/t/quote-json.t000066400000000000000000000017431411535022000203210ustar00rootroot00000000000000# vi:filetype=perl use lib 'lib'; use Test::Nginx::Socket; #repeat_each(3); plan tests => repeat_each() * 2 * blocks(); no_long_string(); run_tests(); #no_diff(); __DATA__ === TEST 1: set quote json value --- config location /foo { set $foo "hello\n\r'\"\\"; set_quote_json_str $foo $foo; echo $foo; } --- request GET /foo --- response_body "hello\n\r'\"\\" === TEST 2: set quote json value (in place) --- config location /foo { set $foo "hello\n\r'\"\\"; set_quote_json_str $foo; echo $foo; } --- request GET /foo --- response_body "hello\n\r'\"\\" === TEST 3: set quote empty json value --- config location /foo { set $foo ""; set_quote_json_str $foo; echo $foo; } --- request GET /foo --- response_body null === TEST 4: set quote null json value --- config location /foo { set_quote_json_str $foo; echo $foo; } --- request GET /foo --- response_body null set-misc-nginx-module-0.33/t/quote-sql.t000066400000000000000000000056171411535022000201530ustar00rootroot00000000000000# vi:filetype= use lib 'lib'; use Test::Nginx::Socket; #repeat_each(3); plan tests => repeat_each() * 2 * blocks(); no_long_string(); run_tests(); #no_diff(); __DATA__ === TEST 1: set quote sql value --- config location /foo { set $foo "hello\n\r'\"\\"; set_quote_sql_str $foo $foo; echo $foo; } --- request GET /foo --- response_body 'hello\n\r\'\"\\' === TEST 2: set quote sql value (in place) --- config location /foo { set $foo "hello\n\r'\"\\"; set_quote_sql_str $foo; echo $foo; } --- request GET /foo --- response_body 'hello\n\r\'\"\\' === TEST 3: set quote empty sql value --- config location /foo { set $foo ""; set_quote_sql_str $foo; echo $foo; } --- request GET /foo --- response_body '' === TEST 4: set quote null sql value --- config location /foo { set_quote_sql_str $foo; echo $foo; } --- request GET /foo --- response_body '' === TEST 5: set quote null pgsql value --- config location /foo { set_quote_pgsql_str $foo; echo $foo; } --- request GET /foo --- response_body '' === TEST 6: set quote pgsql value --- config location /foo { set $foo "hello\n\r'\"\\"; set_quote_pgsql_str $foo; echo $foo; } --- request GET /foo --- response_body E'hello\n\r\'\"\\' === TEST 7: set quote pgsql valid utf8 value --- config location /foo { set $foo "你好"; set_quote_pgsql_str $foo; echo $foo; } --- request GET /foo --- response_body E'你好' === TEST 8: set quote pgsql invalid utf8 value --- config location /foo { set $foo "你好"; set_iconv $foo $foo from=utf-8 to=gbk; set_quote_pgsql_str $foo; echo $foo; } --- request GET /foo --- response_body E'\\304\\343\\272\\303' === TEST 9: \0 for mysql --- config location /foo { set_unescape_uri $foo $arg_a; set_quote_sql_str $foo $foo; echo $foo; } --- request GET /foo?a=a%00b%00 --- response_body 'a\0b\0' === TEST 10: \b for mysql --- config location /foo { set_unescape_uri $foo $arg_a; set_quote_sql_str $foo $foo; echo $foo; } --- request GET /foo?a=a%08b%08 --- response_body 'a\bb\b' === TEST 11: \t for mysql --- config location /foo { set_unescape_uri $foo $arg_a; set_quote_sql_str $foo $foo; echo $foo; } --- request GET /foo?a=a%09b%09 --- response_body 'a\tb\t' === TEST 12: \Z for mysql --- config location /foo { set_unescape_uri $foo $arg_a; set_quote_sql_str $foo $foo; echo $foo; } --- request GET /foo?a=a%1ab%1a --- response_body 'a\Zb\Z' === TEST 13: set quote sql value --- config location /foo { set_unescape_uri $foo $arg_a; set_quote_sql_str $foo $foo; echo $foo; } --- request GET /foo?a=$$ --- response_body '\$\$' set-misc-nginx-module-0.33/t/rand.t000066400000000000000000000050731411535022000171410ustar00rootroot00000000000000# 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 set-misc-nginx-module-0.33/t/rotate.t000066400000000000000000000077711411535022000175220ustar00rootroot00000000000000# 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] set-misc-nginx-module-0.33/t/secure-random.t000066400000000000000000000033561411535022000207630ustar00rootroot00000000000000# vi:filetype= use Test::Nginx::Socket; repeat_each(2); plan tests => repeat_each() * 2 * blocks(); no_long_string(); run_tests(); #no_diff(); __DATA__ === TEST 1: a 32-character alphanum --- config location /alphanum { set_secure_random_alphanum $res 32; echo $res; } --- request GET /alphanum --- response_body_like: ^[a-zA-Z0-9]{32}$ === TEST 2: a 16-character alphanum --- config location /alphanum { set_secure_random_alphanum $res 16; echo $res; } --- request GET /alphanum --- response_body_like: ^[a-zA-Z0-9]{16}$ === TEST 3: a 1-character alphanum --- config location /alphanum { set_secure_random_alphanum $res 1; echo $res; } --- request GET /alphanum --- response_body_like: ^[a-zA-Z0-9]{1}$ === TEST 4: length less than <= 0 should fail --- config location /alphanum { set_secure_random_alphanum $res 0; echo $res; } --- request GET /alphanum --- response_body_like: 500 Internal Server Error --- error_code: 500 === TEST 5: length less than <= 0 should fail --- config location /alphanum { set_secure_random_alphanum $res -4; echo $res; } --- request GET /alphanum --- response_body_like: 500 Internal Server Error --- error_code: 500 === TEST 6: non-numeric length should fail --- config location /alphanum { set_secure_random_alphanum $res bob; echo $res; } --- request GET /alphanum --- response_body_like: 500 Internal Server Error --- error_code: 500 === TEST 7: a 16-character lcalpha --- config location /lcalpha { set_secure_random_lcalpha $res 16; echo $res; } --- request GET /lcalpha --- response_body_like: ^[a-z]{16}$ set-misc-nginx-module-0.33/t/unescape-uri.t000066400000000000000000000014421411535022000206110ustar00rootroot00000000000000# vi:filetype=perl use lib 'lib'; use Test::Nginx::Socket; #repeat_each(3); plan tests => repeat_each() * 2 * blocks(); no_long_string(); run_tests(); #no_diff(); __DATA__ === TEST 1: set unescape uri buggy? --- config location /foo { set $foo "hello%20world"; set_unescape_uri $foo $foo; echo $foo; } --- request GET /foo --- response_body hello world === TEST 2: set unescape uri (in-place) buggy? --- config location /foo { set $foo "hello%20world"; set_unescape_uri $foo; echo $foo; } --- request GET /foo --- response_body hello world === TEST 3: unescape '+' to ' ' --- config location /bar { set $a 'a+b'; set_unescape_uri $a; echo $a; } --- request GET /bar --- response_body a b set-misc-nginx-module-0.33/util/000077500000000000000000000000001411535022000165355ustar00rootroot00000000000000set-misc-nginx-module-0.33/util/build.sh000077500000000000000000000017251411535022000202000ustar00rootroot00000000000000#!/bin/bash # this file is mostly meant to be used by the author himself. root=`pwd` home=~ version=$1 force=$2 #--with-cc="gcc46" \ #--with-ld-opt="-rdynamic" \ #--with-mail \ #--with-mail_ssl_module \ ngx-build $force $version \ --with-cc-opt="-I$PCRE_INC -I$OPENSSL_INC" \ --with-ld-opt="-L$PCRE_LIB -L$OPENSSL_LIB -Wl,-rpath,$PCRE_LIB:$OPENSSL_LIB" \ --with-http_ssl_module \ --without-mail_pop3_module \ --without-mail_imap_module \ --without-mail_smtp_module \ --without-http_upstream_ip_hash_module \ --without-http_empty_gif_module \ --without-http_memcached_module \ --without-http_referer_module \ --without-http_autoindex_module \ --without-http_auth_basic_module \ --without-http_userid_module \ --add-module=$root/../echo-nginx-module \ --add-module=$root/../ndk-nginx-module \ --add-module=$root/../iconv-nginx-module \ --add-module=$root $opts \ --with-debug \ || exit 1 set-misc-nginx-module-0.33/valgrind.suppress000066400000000000000000000034571411535022000212050ustar00rootroot00000000000000{ Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_create_pool fun:ngx_http_init_request } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_create_pool fun:ngx_event_accept } { Memcheck:Addr4 fun:ngx_init_cycle fun:ngx_master_process_cycle fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_calloc fun:ngx_event_process_init } { exp-sgcheck:SorG fun:ngx_http_variables_init_vars fun:ngx_http_block } { exp-sgcheck:SorG fun:ngx_conf_parse } { exp-sgcheck:SorG fun:ngx_vslprintf fun:ngx_log_error_core } { exp-sgcheck:SorG fun:ngx_conf_parse fun:ngx_http_core_location } { nginx-core-process-init Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_event_process_init } { nginx-core-crc32-init Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_crc32_table_init fun:main } { libc-2.12.so Memcheck:Param epoll_ctl(event) fun:epoll_ctl } { Memcheck:Cond fun:index fun:expand_dynamic_string_token fun:_dl_map_object fun:map_doit fun:_dl_catch_error fun:do_preload fun:dl_main } { Memcheck:Leak match-leak-kinds: definite fun:malloc fun:ngx_alloc fun:ngx_set_environment fun:ngx_single_process_cycle } { Memcheck:Leak match-leak-kinds: definite fun:malloc fun:ngx_alloc fun:ngx_set_environment fun:ngx_worker_process_init fun:ngx_worker_process_cycle }