Hello!
On Wed, Jul 25, 2012 at 8:08 PM, Daoyu Zhuang wrote:
> I'm quite new to nginx and c, I want achieve following goals: Write a nginx
> module to 1. set a variable , 2 add a set cookie http header.
>
> For the first task, I try the ndk example, and make some changes, the
> attachment is my code, here're the piece:
>
> static ngx_int_t
> ngx_http_set_var_concat1 (ngx_http_request_t *r, ngx_str_t *val,
> ngx_http_variable_value_t *v)
> {
> size_t len;
> u_char *p;
>
> len = v->len;
> ndk_palloc_re(p, r->pool, len);
>
> val->data = p;
> val->len = len;
>
> ngx_memzero (p, len);
> p = ngx_cpymem (p, v->data, v->len);
> return NGX_OK;
> }
>
> static char *
> ngx_http_abtest (ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
> {
> ngx_str_t *var_val, *var_name;
> ndk_set_var_t filter;
>
> var_name = cf->args->elts;
> var_name++;
> var_val = var_name + 1;
>
> filter.type = NDK_SET_VAR_VALUE;
> filter.func = ngx_http_set_var_concat1;
> filter.size = 1;
>
> return ndk_set_var_value_core(cf, var_name, var_val, &filter);
> }
>
> and I use it like this:
>
> http { server { location {
> set $cookie_ablocation 'a';
> abtest $cookie_ablocation 'end';
> } } }
>
> This works as expected, however, when I remove the "set $cookie_ablocation
> 'a';", try to use my custom set as nginx set, I get nginx error "worker
> process xxx exited on signal 11", seems In my module, I can't set a variable
> until it's created by set in conf. Is there any way to use my module to
> custom set a new variable?
>
> Also you may notice, I use ndk_set_var_value_core, to set a variable's
> value, I don't know whether this is the right way and whether I pass the
> right filter.func to it, since I change it from ndk example.
>
> I'm trying to add http header in NDK, could you give me some example on
> this? The document for ndk and nginx module development are quite poor I
> guess they are hiding in the code.
>
I suggest you take a look at the ngx_set_misc module:
https://github.com/agentzh/set-misc-nginx-module
It uses NDK set_var exclusively. Regarding nginx worker process
crashes, you can try gdb or valgrind/memcheck to investiage potential
memory problems in your code.
Sorry I do not have the time at the moment to read all of your C code.
Please just copy the way how ngx_set_misc handles various cases.
P.S. I'm cc'ing the openresty mailing list:
https://groups.google.com/group/openresty You're also very welcome to
join this list :)
Best regards,
-agentzh