Reading request body in PRE_ACCESS phase

Yichun Zhang (agentzh) agentzh at gmail.com
Thu Aug 28 20:46:37 UTC 2014


Hello!

On Thu, Aug 28, 2014 at 10:34 AM, Fasih wrote:
> I am trying to read the request body in pre_access phase. This seems
> like a regular requirement but I dont seem to find a good way to do
> this. Since the request body is read asynchronously, I have to do
> phases++ and core_run_phases myself in the read_completion callback. I
> also have to set the r->write_event_handler to empty because otherwise
> a write event calls run_phases which I obviously dont want till I read
> the body. I am trying to understand what is the correct way to do this
> since I seem to be hacking my way around this.
>

Incrementing r->phase_handler manually in your "post_read" handler is
a bad idea. You should just call ngx_http_core_run_phases() there if
it is not a recursive call (when you read the body in a single run).
And in your pre-access handler, just return appropriate return values
according to the state in your "ctx". If the request body is done, you
set a flag in your "ctx" in your "post_read" handler, and your
pre-access phase handler checks this flag and decides whether to
return NGX_DECLINED to proceed to the next phase handler, or just
return NGX_DONE to yield the control back to the nginx event loop.

The ngx_form_input module reads the request body in the rewrite phase,
which should be similar:

    https://github.com/calio/form-input-nginx-module

Regards,
-agentzh



More information about the nginx-devel mailing list