module which handles POST request help
Maxim Dounin
mdounin at mdounin.ru
Tue Sep 19 00:45:37 UTC 2023
Hello!
On Mon, Sep 18, 2023 at 09:27:36AM -0600, Ben Mesander via nginx wrote:
> I'm working on an nginx module which will handle POST requests. I've never
> written an nginx module before, but I have worked on apache modules. My
> goal is to have it do unbuffered reads and process the data being posted as
> it arrives. Initially, I'm doing just a simple blocking read of the POST
> body. I've simplified things down almost to the "hello world" level.
>
> When I do a POST, my module gets called, but I get a 404 not found, I am
> not sure why. I do see the file I post being uploaded and stored in a file
> in the nginx temporary file area. I do not see an entry in the access log
> showing the POST, again I am not sure why. How can I fix these things?
>
> Source to my module:
> https://github.com/benmesander/ngx-dashll-module/tree/main
In no particular order:
Your module registers an access phase handler, which means it is
to either accept or reject requests for later processing with
other handlers. Unless you do something to handle the request,
the 404 response code is the most expected one, and likely the
reason why you are getting 404 response.
Your module fails to properly account for request reference
counting as needed during request body reading: you fail to call
ngx_http_finalize_request() after calling
ngx_http_read_client_request_body() during phase processing before
the content phase. Due to reference count leak the request will
hang during termination, waiting for the
ngx_http_finalize_request() call (which will never happen).
That's why you are not seeing the request in the access log - the
request is still running.
Also, your module fails to properly restore phase processing after
you've stopped it to read the request body: in addition to calling
ngx_http_core_run_phases() you have to restore
r->write_event_handler, since it can be overwritten during request
body reading.
In general, if you want to read the request body during the
access phase, consider looking into the mirror module request body
handling:
http://hg.nginx.org/nginx/file/tip/src/http/modules/ngx_http_mirror_module.c#l104
If you actually want to handle such requests yourself,
consider instead using a content handler. In a content handler
request finalization happens automatically, and there is no need
to recover phase processing, so reading the request body is more
or less trivial. An example code can be seen in the development
guide:
http://nginx.org/en/docs/dev/development_guide.html#http_request_body
Hope this helps.
--
Maxim Dounin
http://mdounin.ru/
More information about the nginx
mailing list