Got "http reading blocked" on second request with same connection

Maxim Dounin mdounin at mdounin.ru
Wed Nov 10 19:27:53 UTC 2021


Hello!

On Tue, Nov 09, 2021 at 06:40:01PM -0500, frdcybermatrix wrote:

> Hi I'm having problem with my custom nginx module.
> I created nginx module to detect wether the request is authorized or not,
> and I need to check request body. So I use ngx_http_read_client_request_body
> in NGX_HTTP_ACCESS_PHASE.
> Modules work perfectly on one request at a time. For example: 'curl -v
> localhost'
> But the problem happen when second request happen using same connection. For
> example: 'curl -v localhost localhost'.
> 
> When I try to debug I got  "http reading blocked". But when I remove
> ngx_http_read_client_request_body, and leave body empty, it's work
> 
> static ngx_int_t
> ngx_http_ai_inspect_handler(ngx_http_request_t *r)
> {
>     ngx_chain_t  *in;
>     ngx_http_read_client_request_body(r, ngx_http_ai_inspect_post_handler);
>     off_t len = 0;
>     if (r->request_body != NULL) {
>         for (in = r->request_body->bufs; in; in = in->next) {
>             len += ngx_buf_size(in->buf);
>         }
>     }
> 
> }

First of all, it is not correct to use the request body after the 
ngx_http_read_client_request_body() call: the request body might 
not be available yet at this point.  Instead, you should wait till 
the post handler is called, and only use the body after it's 
called.  See the development guide for details:

http://nginx.org/en/docs/dev/development_guide.html#http_request_body

Second, when you are reading the request body from phase handlers, 
there are additional things to consider.  In particular:

- Things to do after the ngx_http_read_client_request_body() might 
  differ from what one normally does in content handler, notably 
  you'll have to call ngx_http_finalize_request(NGX_DONE) yourself;

- It is important to restore phase processing after the request 
  body is read and you've done with it, so you'll have to 
  set r->write_event_handler back to ngx_http_core_run_phases in 
  the post handler and run ngx_http_core_run_phases() when you're 
  done.

Refer to the src/http/modules/ngx_http_mirror_module.c for an 
example.

-- 
Maxim Dounin
http://mdounin.ru/


More information about the nginx mailing list