Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 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
19 changes: 19 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Table of Contents
* [set_hashed_upstream](#set_hashed_upstream)
* [set_encode_base32](#set_encode_base32)
* [set_misc_base32_padding](#set_misc_base32_padding)
* [set_misc_base32_alphabet](#set_misc_base32_alphabet)
* [set_decode_base32](#set_decode_base32)
* [set_encode_base64](#set_encode_base64)
* [set_decode_base64](#set_decode_base64)
Expand Down Expand Up @@ -505,6 +506,8 @@ Please note that we're using [echo-nginx-module](https://github.com/openresty/ech

RFC forces the `[A-Z2-7]` RFC-3548 compliant encoding, but we're using the "base32hex" encoding (`[0-9a-v]`).

The set_misc_base32_alphabet directive allows you to change the alphabet used for encoding/decoding so RFC-3548 compliant encoding is still possible.

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_misc_base32_padding](#set_misc_base32_padding) `off`.

When taking a single argument, this directive will do in-place modification of the argument variable. For example,
Expand Down Expand Up @@ -539,6 +542,22 @@ This directive can control whether to pad left-over bytes with the "=" character

[Back to TOC](#table-of-contents)

set_misc_base32_alphabet
-----------------------
**syntax:** *set_misc_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.

[Back to TOC](#table-of-contents)

set_decode_base32
-----------------
**syntax:** *set_decode_base32 $dst <src>*
Expand Down
15 changes: 15 additions & 0 deletions doc/HttpSetMiscModule.wiki
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ Please note that we're using [[HttpEchoModule]]'s [[HttpEchoModule#echo|echo dir

RFC forces the <code>[A-Z2-7]</code> RFC-3548 compliant encoding, but we're using the "base32hex" encoding (<code>[0-9a-v]</code>).

The [[#set_misc_base32_alphabet|set_misc_base32_alphabet]] directive allows you to change the alphabet used for encoding/decoding so RFC-3548 compliant encoding is still possible.

By default, the <code>=</code> character is used to pad the left-over bytes due to alignment. But the padding behavior can be completely disabled by setting [[#set_misc_base32_padding|set_misc_base32_padding]] <code>off</code>.

When taking a single argument, this directive will do in-place modification of the argument variable. For example,
Expand Down Expand Up @@ -441,6 +443,19 @@ This directive can be invoked by [[HttpLuaModule]]'s [[HttpLuaModule#ndk.set_var

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.

== set_misc_base32_alphabet ==
'''syntax:''' ''set_misc_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.

== set_decode_base32 ==
'''syntax:''' ''set_decode_base32 $dst <src>''

Expand Down
76 changes: 19 additions & 57 deletions src/ngx_http_set_base32.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@


static void encode_base32(size_t slen, u_char *src, size_t *dlen, u_char *dst,
ngx_flag_t padding);
static int decode_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
Expand All @@ -41,7 +42,7 @@ ngx_http_set_misc_encode_base32(ngx_http_request_t *r, ngx_str_t *res,

src = v->data; dst = p;

encode_base32(v->len, src, &len, dst, conf->base32_padding);
encode_base32(v->len, src, &len, dst, conf->base32_padding, &conf->base32_alphabet);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line also exceeds 80 columns :)

BTW, I usually use the ngx-releng script to do such checks automatically before committing: https://github.com/openresty/nginx-devel-utils/blob/master/ngx-releng Just FYI :)

Thanks!


res->data = p;
res->len = len;
Expand All @@ -60,6 +61,12 @@ ngx_http_set_misc_decode_base32(ngx_http_request_t *r, ngx_str_t *res,
u_char *p;
u_char *src, *dst;
int ret;
static unsigned char basis32[256] = { 77 };
ngx_uint_t i;

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

Expand All @@ -72,7 +79,11 @@ ngx_http_set_misc_decode_base32(ngx_http_request_t *r, ngx_str_t *res,

src = v->data; dst = p;

ret = decode_base32(v->len, src, &len, dst);
for (i = 0; i < 32; i++) {
basis32[conf->base32_alphabet.data[i]] = i;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to move this loop into the configuration phase as well?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pardon my ignorance, I guess didn't quite understand what you meant by the configuration phase and moved the code to the wrong place.

Did you mean we should initialize the array in ngx_http_set_misc_create_loc_conf and put it in the conf struct directly instead? This actually makes more sense now that I look at it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@blablacio Yes, by "configuration phase", I mean the phase where nginx loads its configuration (in the master process) instead of serving actual requests in the worker processes. Yeah, I mean initializing that array only once in the xxx_loc_conf_t struct (or something like that) :)

}

ret = decode_base32(v->len, src, &len, dst, &basis32);

if (ret == 0 /* OK */) {
res->data = p;
Expand Down Expand Up @@ -113,9 +124,9 @@ ngx_http_set_misc_decode_base32(ngx_http_request_t *r, ngx_str_t *res,
* */
static void
encode_base32(size_t slen, u_char *src, size_t *dlen, u_char *dst,
ngx_flag_t padding)
ngx_flag_t padding, ngx_str_t *alphabet)
{
static unsigned char basis32[] = "0123456789abcdefghijklmnopqrstuv";
unsigned char *basis32 = alphabet->data;

size_t len;
u_char *s;
Expand Down Expand Up @@ -204,58 +215,9 @@ encode_base32(size_t slen, u_char *src, size_t *dlen, u_char *dst,


static int
decode_base32(size_t slen, u_char *src, size_t *dlen, u_char *dst)
decode_base32(size_t slen, u_char *src, size_t *dlen, u_char *dst,
u_char *basis32)
{
static unsigned char basis32[] = {
/* 0 - 15 */
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,

/* 16 - 31 */
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,

/* 32 - 47 */
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,

/* 48 - 63 */
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 77, 77, 77, 77, 77, 77,

/* 64 - 79 */
77, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,

/* 80 - 95 */
25, 26, 27, 28, 29, 30, 31, 77, 77, 77, 77, 77, 77, 77, 77, 77,

/* 96 - 111 */
77, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,

/* 112 - 127 */
25, 26, 27, 28, 29, 30, 31, 77, 77, 77, 77, 77, 77, 77, 77, 77,

/* 128 - 143 */
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,

/* 144 - 159 */
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,

/* 160 - 175 */
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,

/* 176 - 191 */
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,

/* 192 - 207 */
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,

/* 208 - 223 */
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,

/* 224 - 239 */
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,

/* 240 - 255 */
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77
};

size_t len, mod;
u_char *s = src;
u_char *d = dst;
Expand Down
11 changes: 11 additions & 0 deletions src/ngx_http_set_misc_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,15 @@ static ngx_command_t ngx_http_set_misc_commands[] = {
offsetof(ngx_http_set_misc_loc_conf_t, base32_padding),
NULL
},
{
ngx_string("set_misc_base32_alphabet"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like the directive name to be simpler: set_base32_alphabet. Yes, I'm aware of the existing set_misc_base32_padding directive and I'll deprecate the old form and introduce the new form set_base32_padding, which sounds better :) I'll do this change myself during the merge :)

NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF
|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
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
Expand Down Expand Up @@ -471,6 +480,8 @@ ngx_http_set_misc_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)

ngx_conf_merge_value(conf->base32_padding, prev->base32_padding, 1);

ngx_conf_merge_str_value(conf->base32_alphabet, prev->base32_alphabet, "0123456789abcdefghijklmnopqrstuv");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line exceeds 80 columns. Please fix it :)


ngx_conf_merge_value(conf->current, prev->current, NGX_CONF_UNSET);

return NGX_CONF_OK;
Expand Down
1 change: 1 addition & 0 deletions src/ngx_http_set_misc_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

typedef struct {
ngx_flag_t base32_padding;
ngx_str_t base32_alphabet;
ngx_int_t current; /* for set_rotate */
} ngx_http_set_misc_loc_conf_t;

Expand Down
Loading