How to handle NGX_AGAIN returned by ngx_http_read_client_request_body() within handler module?

agentzh agentzh at gmail.com
Tue Nov 8 06:56:17 UTC 2011


On Tue, Nov 8, 2011 at 12:31 PM, CarlWang <nginx-forum at nginx.us> wrote:
> Here are my code:
> static void v8_embed_handler ( ngx_http_request_t * r )
> {
>        ...// generating out chain.
>        rc = ngx_http_output_filter ( r , out );
>        while( rc == NGX_AGAIN ) {
>                if( out->next == NULL )
>                        break;
>                rc = ngx_http_output_filter ( r , out->next );
>                out = out->next;
>        }
>        ngx_http_finalize_request ( r , rc );
> }

Please note that you shouldn't call ngx_http_output_filter on your
data again when NGX_AGAIN is returned. The underlying copy and writer
filters will buffer the output chains when the system send buffer is
full. Furthermore, the ngx_http_finalize_request function will
automatically register the ngx_http_writer as the write event handler
to actually emit the outputs at every write event for you. And that's
why Maxim suggested the simple call

    ngx_http_finalize_request(r, ngx_http_output_filter(r, out))

> static ngx_int_t ngx_http_v8_handler_request(ngx_http_request_t *r)
> {
>        ngx_int_t rc = NGX_DONE ;
>        rc = ngx_http_read_client_request_body ( r , v8_embed_handler ) ; //
> call the v8_embed_handler handler to process the post data
>        if ( rc >= NGX_HTTP_SPECIAL_RESPONSE )
>                return rc;
>        return NGX_DONE;
> }
>

Which phase is your ngx_http_v8_handler_request function running in?
If it's running in the access or rewrite phase, then the coding
structure can be quite different here.

I suggest you take a look at how our ngx_lua module handle all of this
in access, write, and content phases:

    http://wiki.nginx.org/HttpLuaModule

BTW, taking a close look at the error.log when building nginx with
--with-debug and enabling the debug error log level will be very
helpful to debug such issues ;)

Regards,
-agentzh



More information about the nginx mailing list