custom handler module - dynamic response with unknown content length
    Yasser Zamani 
    yasser.zamani at live.com
       
    Fri Feb 28 19:14:38 UTC 2014
    
    
  
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:
     /* allocate a buffer for your response body */
     b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
     if (b == NULL) {
         return NGX_HTTP_INTERNAL_SERVER_ERROR;
     }
     /* attach this buffer to the buffer chain */
     out.buf = b;
     out.next = NULL;
     /* adjust the pointers of the buffer */
     b->pos = ngx_hello_string;
     b->last = ngx_hello_string + sizeof(ngx_hello_string) - 1;
     b->memory = 1;    /* this buffer is in memory */
     b->last_buf = 0;  /* this is the last buffer in the buffer chain */
     /* set the status line */
     r->headers_out.status = NGX_HTTP_OK;
     //r->headers_out.content_length_n = sizeof(ngx_hello_string) - 1;
     /* send the headers of your response */
     rc = ngx_http_send_header(r);
     if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
         return rc;
     }
     /* 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"
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
Thanks in advance!
    
    
More information about the nginx
mailing list