Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add set_encode_hex_dash for set-misc #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 0 additions & 72 deletions .gitignore

This file was deleted.

36 changes: 36 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
35 changes: 34 additions & 1 deletion src/ngx_http_set_hex.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions src/ngx_http_set_hex.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#include <ngx_config.h>
#include <ngx_http.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);

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);

Expand Down
14 changes: 14 additions & 0 deletions src/ngx_http_set_misc_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down