Hey all,
I’m writing an authentication module that is structurally very similar
to
ngx_http_auth_request_module
(https://github.com/PiotrSikora/ngx_http_auth_request_module/blob/master/ngx_http_auth_request_module.c).
It basically sends a subrequest to a service for auth. For testing, I
put a
hello_world module module
(https://github.com/perusio/nginx-hello-world-module/blob/master/ngx_http_hello_world_module.c)
behind it like:
location /hello_world {
auth_request /auth;
hello_world;
hello_world_string “Hello World!”;
}
Everything works fine EXCEPT I found today that the log phase is not
excuted. I traced it down to ngx_http_close_request function in
ngx_http_request.c (as it calls ngx_http_free_request(r, rc); to excute
log
handlers) where I found r->count = 2 so after r->count–; it’s still
non-zero thus ngx_http_close_request simply returns. I did not touch
request
count in my code.
Are we supposed to handle r->acount (either directly or through
ngx_http_finalize_request) when using subrequest? I didn’t see the
original
ngx_http_auth_request_module does so. If not, then why the request count
is
incorrect?
Thanks,
W
Posted at Nginx Forum:
BTW, I can add “r->count–;” in my module and it seems to work. But I
don’t
feel it is the right thing especially when I’m not sure how it works.
Posted at Nginx Forum:
Hello!
On Sat, Oct 11, 2014 at 12:28:08AM -0400, wangweixun wrote:
location /hello_world {
count in my code.
Are we supposed to handle r->acount (either directly or through
ngx_http_finalize_request) when using subrequest? I didn’t see the original
ngx_http_auth_request_module does so. If not, then why the request count is
incorrect?
My best quess is that in your module you are calling
ngx_http_read_client_request_body(), which will increment r->count.
–
Maxim D.
http://nginx.org/
Hello!
On Mon, Oct 13, 2014 at 12:41:04PM -0400, wangweixun wrote:
Maxim,
You are right. Since I need to make a hash of the original request’s body, I
do make call to ngx_http_read_client_request_body().
What’s the best way to decrement the count then? Simply “r->count–;” before
returning from the module.
The ngx_http_read_client_request_body() increments r->count as it
expects that r->main->count will be decremented by twice:
-
right after the ngx_http_read_client_request_body() call, by
ngx_http_finalize_request() as called automatically on you return
from content handlers;
-
later in post_handler, by another ngx_http_finalize_request()
call.
If you do call ngx_http_read_client_request_body() from an access
handler, it should be fine to do “r->main->count–” in
post_handler directly.
(Note well that nginx generally doesn’t assume that the request
body will be read before the content phase, and there may be
problems associated with this if you’ll try reading request body
in an access handler.)
By the way, in my access phase module, I need to send an subrequest and wait
for its response asynchornously. ngx_http_read_client_request_body() also
works asynchornously with a callback function, which apparently is not able
to send a subrequest. What I did is registering two handlers in access
phase. The first one does nothing but reading the request body only. The
second one does the real work (subrequest, etc.). Does it sound right?
No, it doesn’t. You should do the real work in post_handler.
–
Maxim D.
http://nginx.org/
Maxim,
You are right. Since I need to make a hash of the original request’s
body, I
do make call to ngx_http_read_client_request_body().
What’s the best way to decrement the count then? Simply “r->count–;”
before
returning from the module.
By the way, in my access phase module, I need to send an subrequest and
wait
for its response asynchornously. ngx_http_read_client_request_body()
also
works asynchornously with a callback function, which apparently is not
able
to send a subrequest. What I did is registering two handlers in access
phase. The first one does nothing but reading the request body only. The
second one does the real work (subrequest, etc.). Does it sound right?
Thanks!!
Posted at Nginx Forum:
Hello!
On Mon, Oct 13, 2014 at 02:43:07PM -0400, wangweixun wrote:
Thanks for your prompt reply.
Why do I have to do the work in the post handler? Now I have a stub post
handler. ngx_http_read_client_request_body() does nothing but load the
request body into the original request, which is used by the next handler.
Everything seems to be working fine to me.
If you don’t need a request body in your module, it’s not clear
why at all you are trying to read it. If, in contrast, you do
need the request body, you shouldn’t try to do the real work till
the post_handler is called.
Technically, you can use multiple access-phase handlers to handle
things, as long as proper order is maintained by using flags set
in post_handler. But there is no need to use multiple handlers to
do this, as there is no real difference as long as you anyway
maintain flags set in post_handler. On the other hand, if you
indeed using a stub post_handler, it means that things are working
by chance.
–
Maxim D.
http://nginx.org/
Thanks for your prompt reply.
Why do I have to do the work in the post handler? Now I have a stub post
handler. ngx_http_read_client_request_body() does nothing but load the
request body into the original request, which is used by the next
handler.
Everything seems to be working fine to me.
I’m not sure how to use the post handler to send a subrequest like a
real
handler which is polled multiple times until the response is arrived.
Posted at Nginx Forum: