June 21, 2011 05:26AM
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

_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

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

replay 2714 June 21, 2011 05:26AM

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

Maxim Dounin 932 June 21, 2011 06:14AM

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

replay 1817 June 21, 2011 06:18AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 257
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready