Content does not get replaced

Maxim Dounin mdounin at mdounin.ru
Tue May 31 02:27:22 MSD 2011


Hello!

On Mon, May 30, 2011 at 07:35:17PM +0100, Adelino Monteiro wrote:

> Hello,
> 
> I'm writing my first module (actually a copy from mod_strip with some simple
> modifications)
> 
> I have this simple function that for testing purposes should simply make a
> simple substitution of all chars to A. I used gdb and chain_link->buf->start
> indeed has all the  characters replaced with A. However the output in the
> end is the original file without any other modification.
> 
> Could someone shed some light on this mistery?
> 
> Thanks
> 
> AM
> 
> static ngx_int_t
> ngx_http_strip_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
> {
>     ngx_http_strip_ctx_t *ctx;
>     ngx_chain_t          *chain_link;
> 
> 
>     u_char *reader;
>     u_char *writer;
> 
>     ngx_log_t   *log;
> 
>     log = r->connection->log;
> 
>     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "In strip strip body filter 1
> ");
> 
>     ctx = ngx_http_get_module_ctx(r, ngx_http_strip_filter_module);
>     if (ctx == NULL) {
>         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "In strip strip body
> filter 2 ");
>         return ngx_http_next_body_filter(r, in);
>     }
>     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "In strip strip body filter 3
> ");
> 
> 
>     for (chain_link = in; chain_link; chain_link = chain_link->next) {
>         for (writer = chain_link->buf->pos, reader = chain_link->buf->pos;
> reader < chain_link->buf->last; reader++) {
>             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "w: \"%c\"",
> *reader);
> 
>             *reader = 'A' ;
>             if (reader < chain_link->buf->last)
>                         *writer++ = *reader;
>         }
>             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "In strip_body_filter
> writer: \"%s\"", writer);
> 
>         chain_link->buf->last = writer;
> 
>     }
> 
>     return ngx_http_next_body_filter(r, in);
> }

If you want to modify buffer data in place, you have to:

1. Request buffers to be in memory via r->filter_need_in_memory 
flag.

2. Request buffers to be in temporary (i.e. modifiable) memory 
buffers via r->filter_need_temporary flag.

Both (1) and (2) must be done at header filter stage.

3. Reset buf->in_file flag as long as you modified data to stop 
nginx from sending file data (if any and enabled via sendfile 
directive) instead of corresponding memory contents.

Please refer to charset filter sources for an example.

Maxim Dounin



More information about the nginx-devel mailing list