processing a request without body

Maxim Dounin mdounin at mdounin.ru
Thu Dec 21 00:59:26 UTC 2023


Hello!

On Tue, Dec 19, 2023 at 10:11:04PM +0800, Muhammad Nuzaihan wrote:

> Thanks Maxim, Vasility,
> 
> The problem i was going to solve is to i needed to run my specific
> function that takes  the data of request URL path, Headers and request
> body and determine and validate that all that data is correct before
> sending upstream, or else i would deny the request with 4xx code
> errors.
> 
> Handlers can only handle (from what i know) URL path and headers.
> 
> Request body requires a request chain (ngx_chain_t)) to piece out the
> request body and handlers doesn't seem to have t ngx_chain_t unlike
> request body filters.
> 
> Or maybe i am wrong in this case?

It looks like you are trying to do something which simply cannot 
be done.  For example, consider a configuration with 
"proxy_request_buffering off;" - in such a configuration request 
body is being read _after_ the request is passed to the upstream 
server, and you simply cannot validate request body before passing 
request headers to the upstream server.

As long as you have to examine both request body and request 
headers, I think there can be two possible solutions:

1. Install a phase handler, in which read the request body 
yourself, and check both request headers and request body once 
it's read.  See the mirror module as an example on how to read the 
body in a phase handler and properly resume processing after it.  
This will break proxying without request buffering, though might 
be good enough for your particular task.

2. Install a phase handler to check request headers, and a request 
body filter to check the request body.  Do checking in both 
places, and abort request processing when you see that data aren't 
correct.  This will work with proxying without request buffering, 
but will be generally more complex to implement.  And, obviously, 
this in case of proxying without request buffering this won't let 
you to validate request body before the request headers are sent 
to upstream server.

Hope this helps.

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


More information about the nginx-devel mailing list