not sure how to associate a variable's get_handler with its context

Mauro Stettler mauro.stettler at gmail.com
Tue Jun 21 13:25:12 MSD 2011


hi

i've implemented a simple nginx module to uppercase/lowercase a
variable. but i'm not sure if i did it the right way. the code can be
found here:

https://github.com/replay/ngx_http_lower_upper_case/blob/master/ngx_http_lower_upper_case.c

the thing that i'm very uncertain about is that i want to set a
get_handler for the destination variable like the following:

168     v->get_handler = ngx_http_do_lower_upper;

the problem is that i will need to pass some data to this function
which is specific to this variable, and its possible that my
upper/lower casing directives are used multiple times in one nginx
config. so i created an array of ngx_http_lucase_t structs and point
to it from the module location config in order to be able to retrieve
it from the get_handler. then in the get_handler i have to search
through this array to find the right element for the variable that
wants to be read, based on the variable index. like this:

174 static ngx_int_t
175 ngx_http_do_lower_upper(ngx_http_request_t *r,
ngx_http_variable_value_t *dst_v, uintptr_t data)
176 {
177     ngx_http_lower_upper_case_conf_t       *lucf =
ngx_http_get_module_loc_conf(r, ngx_http_lower_upper_case_module);
178     ngx_uint_t                              i;
179     u_char                                 *tmp_void;
180     ngx_http_lucase_t                      *tmp_value;
181     ngx_http_lucase_t                      *lucase;
182
183     tmp_void = (u_char*) lucf->lucases->elts;
184
185     for (i = 0; i < lucf->lucases->nelts; i++) {
186         tmp_value = (ngx_http_lucase_t*) tmp_void;
187         if (tmp_value->dst_variable_index == data) {
188             lucase = tmp_value;
189             break;
190         }
191         tmp_void += lucf->lucases->size;
192     }
193     if (i == lucf->lucases->nelts) {
194         return NGX_ERROR;
195     }

this is working, but i think its very ugly. it would be much nicer if
i could pass some pointer to the get_handler which tells it which of
those array elements belongs to its variable, instead of having to
loop over the array.

a possible alternative solution would be to use the
ngx_http_variable_t struct's data element and store the array index
there, like that:

164     v->data = lucf->lucases->nelts - 1;

this would be convenient because the content of the data element is
always passed as the third argument to the get_handler. but i'm not
sure if its ok to store an array index into an element of type
uintptr_t.

what do you think, is there any way to do this in a nice way?

any help is appreciated



More information about the nginx-devel mailing list