How to modfiy headers in and headers out

Hello all.

I want to write a balancer module to balance requests based on cookie.
In the phase of upstream_init_peer, I lookup special cookie from
headers_in.headers. If not found, I’ll generate a new one, insert it
into headers_in.headers which would be packed to backend fastcgi
processes. After that, I need to modify headers_out.headers, so that the
newly generated cookie can be sent to clients.

The problem is I could’t modfiy headers_out.headers in the phase of
upstream_init_peer. If I call ngx_list_push(&r->headers_out.headers),
there would be a segment fault. I ever tried to modify it in the header
filter phase, but the hook for filter and the hook for balancer seem not
working together.

Is there any way to work around?

Thanks.

After changed the order of my filter module defined in
objs/ngx_modules.c which should go after ngx_http_header_filter module,
it works. But I still get a segment fault when calling
ngx_list_push(&r->headers_out.headers) in filter hook function.

Anyone could give me a explanation?

Thanks very much.


yingyuan

Yingyuan C. дµÀ:

On Wed, Oct 31, 2007 at 07:45:39PM +0800, Yingyuan C. wrote:

After changed the order of my filter module defined in
objs/ngx_modules.c which should go after ngx_http_header_filter module,
it works. But I still get a segment fault when calling
ngx_list_push(&r->headers_out.headers) in filter hook function.

Anyone could give me a explanation?

Thanks very much.

ngx_list_push(&r->headers_out.headers) should not cause segfault in this
phase.
Could you show more code ?

Hello Igor.

I tracked executing path using gdb, found the content of param “r”
(ngx_http_request_t) past by hook routine was a awful mess, and
r.headers_out.headers.last pointed to 0x22, which was in a protected
memory region. When executing path left my module and went to other
modules, the same param “r” was in its normal status. After I changed
the module code layout, such as moving “#include <openssl/md5>” after
nginx including headers, the problem diminished. Is it a compiling
problem? The configuring line is:

CFLAGS="-g" ./configure --with-debug
–add-module=/home/yingyuan/workspace/ngx_http_sessid_filter/

ngx_http_sessid_filter_module “borrows” code snippets from
ngx_http_userid_filter_module and ngx_http_upstream_ip_hash_module.
First it acts as a load balancer, dispatching requests to backends based
on cookie. If the cookie not found in request, it’ll generate a new one.
Then in the filter phase, it sends the cookie to client if it was
generated newly in balancing phase. So we can make the client always
dispatched to the same backend.

Another question I want to ask: May I change request headers_out in
upstream_init_peer phase?

Thank you very much.

Best Regards


yingyuan

Igor S. 写道:

On Thu, Nov 01, 2007 at 11:22:22AM +0800, Yingyuan C. wrote:

problem? The configuring line is:
I suspect that you did it on Linux or Solaris, where default off_t
is 32-bit size. nginx headers set “#define _FILE_OFFSET_BITS 64” and
nginx is built with 64-bit off_t.

If you include openssl/md5.h before nginx headers then your module
is built with 32-bit off_t, whereas all another obj files are built
with 64-bit off_t.

The general rule: include custom headers after nginx headers.

Another question I want to ask: May I change request headers_out in
upstream_init_peer phase?

No, it’s too early.

ngx_list_push(&r->headers_out.headers) should not cause segfault in this
phase.
Could you show more code ?

ngx_addon_name=ngx_http_sessid_filter_module
HTTP_MODULES="$HTTP_MODULES ngx_http_sessid_filter_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS /home/yingyuan/workspace/ngx_http_sessid_filter/ngx_http_sessid_filter_module.c"
CORE_LIBS="$CORE_LIBS -lssl"