custom handler module - dynamic response with unknown content length

Maxim Dounin mdounin at mdounin.ru
Sat Mar 1 00:11:24 UTC 2014


Hello!

On Fri, Feb 28, 2014 at 10:44:38PM +0330, Yasser Zamani wrote:

> Hi there,
> 
> I learned some about how to write a handler module from [1] and [2].
> [1] http://blog.zhuzhaoyuan.com/2009/08/creating-a-hello-world-nginx-module/
> [2] http://www.evanmiller.org/nginx-modules-guide.html#compiling
> 
> But I need to rewrite [1] to send dynamically generated octect stream to
> client with unknown content length but it'll be large usually. Firstly I
> tried:

[...]

>     /* send the buffer chain of your response */
> 	int i;
> 	for(i=1;i<10000000;i++){b->flush =
> (0==(i%1000));rc=ngx_http_output_filter(r,
> &out);if(rc!=NGX_OK){ngx_log_error(NGX_LOG_ALERT,r->connection->log,0,"bad
> rc, rc:%d", rc);return rc;}}
> 	b->last_buf = 1;
>     return ngx_http_output_filter(r, &out);
> 
> But it simply fails with following errors:
> 
> 2014/02/28 22:17:25 [alert] 25115#0: *1 zero size buf in writer t:0 r:0 f:0
> 00000000 080C7431-080C7431 00000000 0-0, client: 127.0.0.1, server:
> localhost, request: "GET / HTTP/1.1", host: "localhost:8080"
> 2014/02/28 22:17:25 [alert] 25115#0: *1 bad rc, rc:-1, client: 127.0.0.1,
> server: localhost, request: "GET / HTTP/1.1", host: "localhost:8080"

You've tried to send the same chain with the same buffer multiple 
times.  After a buffer is sent for the first time, its pointers 
are adjusted to indicate it was sent - b->pos moved to b->last, and 
buffer's size become zero.  Second attempt to send the same buffer 
will expectedly trigger the "zero size buf" check.

> WHAT IS THE CORRECT WAY TO ACCOMPLISH MY NEED? (I searched a lot but I only
> found [3] which has rc=-2 rather than -1)
> [3] http://web.archiveorange.com/archive/v/yKUXMLzGBexXA3PccJa6

Trivial aproach is to prepare full output chain, and then send it 
using single ngx_http_output_filter() call.

-- 
Maxim Dounin
http://nginx.org/



More information about the nginx mailing list