From 472f1b5d935b0e82fe08a09c8c6f69c21479d197 Mon Sep 17 00:00:00 2001 From: xoxo Date: Sun, 11 Jan 2015 11:56:43 +0800 Subject: [PATCH 1/2] add set_encode_hex_dash --- .gitignore | 72 ---------------------------------- src/ngx_http_set_hex.c | 35 ++++++++++++++++- src/ngx_http_set_hex.h | 3 ++ src/ngx_http_set_misc_module.c | 14 +++++++ 4 files changed, 51 insertions(+), 73 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 7500112..0000000 --- a/.gitignore +++ /dev/null @@ -1,72 +0,0 @@ -reindex -.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] diff --git a/src/ngx_http_set_hex.c b/src/ngx_http_set_hex.c index 6bdaa4b..93fde30 100644 --- a/src/ngx_http_set_hex.c +++ b/src/ngx_http_set_hex.c @@ -7,6 +7,40 @@ #include "ngx_http_set_hex.h" +ngx_int_t +ngx_http_set_misc_set_encode_hex_dash(ngx_http_request_t *r, ngx_str_t *res, + ngx_http_variable_value_t *v) +{ + + u_char *p; + ngx_uint_t i; + size_t len; + + if (v->len % 2 != 0) { + ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, + "set_encode_hex_dash: 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=i+2) { + res->data[(i*2)+0] = (u_char) 92; + res->data[(i*2)+1] = (u_char) 120; + res->data[(i*2)+2] = p[i]; + res->data[(i*2)+3] = p[i+1]; + } + + res->len = len; + return NGX_OK; +} + 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) @@ -47,7 +81,6 @@ ngx_http_set_misc_set_decode_hex(ngx_http_request_t *r, ngx_str_t *res, 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) diff --git a/src/ngx_http_set_hex.h b/src/ngx_http_set_hex.h index e8b77e0..a6deef2 100644 --- a/src/ngx_http_set_hex.h +++ b/src/ngx_http_set_hex.h @@ -2,6 +2,9 @@ #include #include +ngx_int_t ngx_http_set_misc_set_encode_hex_dash(ngx_http_request_t *r, + ngx_str_t *res, ngx_http_variable_value_t *v); + 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); diff --git a/src/ngx_http_set_misc_module.c b/src/ngx_http_set_misc_module.c index 226741e..441e605 100644 --- a/src/ngx_http_set_misc_module.c +++ b/src/ngx_http_set_misc_module.c @@ -54,6 +54,12 @@ static ndk_set_var_t ngx_http_set_misc_set_decode_base64_filter = { NULL }; +static ndk_set_var_t ngx_http_set_misc_set_encode_hex_dash_filter = { + NDK_SET_VAR_VALUE, + (void *) ngx_http_set_misc_set_encode_hex_dash, + 1, + NULL +}; static ndk_set_var_t ngx_http_set_misc_set_decode_hex_filter = { NDK_SET_VAR_VALUE, @@ -222,6 +228,14 @@ static ngx_command_t ngx_http_set_misc_commands[] = { 0, &ngx_http_set_misc_set_decode_base64_filter }, + { ngx_string ("set_encode_hex_dash"), + 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_dash_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, From d820b7b8a26e63125a2dad8f8866dd2027f78077 Mon Sep 17 00:00:00 2001 From: xoxo Date: Sun, 11 Jan 2015 12:02:49 +0800 Subject: [PATCH 2/2] update readme --- README.markdown | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/README.markdown b/README.markdown index 609aca4..bfb2002 100644 --- a/README.markdown +++ b/README.markdown @@ -32,6 +32,7 @@ Table of Contents * [set_encode_base64](#set_encode_base64) * [set_decode_base64](#set_decode_base64) * [set_encode_hex](#set_encode_hex) + * [set_encode_hex_dash](#set_encode_hex_dash) * [set_decode_hex](#set_decode_hex) * [set_sha1](#set_sha1) * [set_md5](#set_md5) @@ -714,6 +715,41 @@ then request `GET /test` will give exactly the same output as the previous examp 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_hex_dash +----------------- +**syntax:** *set_encode_hex_dash $dst <src>* + +**syntax:** *set_encode_hex_dash $dst* + +**default:** *no* + +**context:** *location, location if* + +**phase:** *rewrite* + +**category:** *ndk_set_var_value* + +```nginx + + location /test { + set $raw "章亦春"; + set_encode_hex $digest $raw; + set_encode_hex_dash $digest_dashed $digest; + + echo $digest_dashed; + } +``` + +Then request `GET /test` will yield the following output + + + \xe7\xab\xa0\xe4\xba\xa6\xe6\x98\xa5 + + + [Back to TOC](#table-of-contents) set_decode_hex