Get $request_body before content handlers?

agentzh agentzh at
Mon Jul 9 04:51:09 UTC 2012


On Sun, Jul 8, 2012 at 2:38 PM, Mike Gagnon <mikegagnon at> wrote:
> Thanks for the pointer! I just looked at the source for
> form-input-nginx-module.
> It gives me the impression that the function
> ngx_http_read_client_request_body() isn't guaranteed to read the complete
> request_body before it is time to find the value for a set_form_input
> variable. In other words. it seems possible that the directive
> "set_form_input $data" might not work if the request_body isn't fully read
> before ngx_http_set_form_input() is called.

No, you're wrong here. The request body is read in ngx_form_input's
own rewrite phase handler, which is always run before the standard
ngx_rewrite module's handler. The ngx_http_set_form_input() function
is run in the ngx_rewrite's handler, by injecting callbacks into
ngx_rewrite's location configuration by means the ngx_devel_kit's
set_var submodule. That's the magic going on here.

The 3rd-party module ngx_eval also relies on the relative running
order with the standard ngx_rewrite module on the same "rewrite"

> I gathered this impression because it looks like ctx->done is only set after
> request_body is fully read. However, the function ngx_http_set_form_input()
> only advances to the function ngx_http_form_input_arg() if ctx->done is set.

The running order is already guaranteed here so this is totally fine :)

> I would like to ensure that the request_body is fully read before my module
> attempts to read it during the rewrite phase.

This is surely guaranteed or things can break very easily in
production environments ;)

> Thanks for the Lua tips. Lua has a reputation for speed, but I am worried it
> won't be as performant as a well engineered C module. Normally, I wouldn't
> be concerned with minor speed improvements but I am specifically trying
> building my module to withstand CPU overload attacks. Within my context, it
> is critical that my module be able to classify incoming requests quickly.

Most of the heavy lifting is already done in pure C in ngx_lua. Lua is
just the glue so the performance is totally fine especially when
you're using LuaJIT 2.0 with ngx_lua.

> Do you think I am under-estimating Lua's performance capability?

You can try it out on your side ;)

Best regards,

More information about the nginx-devel mailing list