processing a request without body

Muhammad Nuzaihan zaihan at unrealasia.net
Thu Jan 11 19:26:39 UTC 2024


Hi Maxim,

After searching the archives, I found the solution which you had
answered before:
https://www.ruby-forum.com/t/trouble-getting-the-request-body-of-a-http-post/180463/4

The code that reads the body is:
    rc = ngx_http_read_client_request_body(r,
                              ngx_http_foo_body_handler);

    if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
      return rc;
    }

Can i buy you coffee?

Thank you!
On Thu, Jan 11, 2024 at 1:06 PM Muhammad Nuzaihan <zaihan at unrealasia.net> wrote:
>
> Hi Maxim,
>
> Sorry for asking too many questions.
>
> I did looked at he mirror module and i couldn't find the code which
> reads the body (from what i understand i need a ngx_chain_t type of
> value to build the request body from buffer)
> https://github.com/nginx/nginx/blob/master/src/http/modules/ngx_http_mirror_module.c
>
> I'm going with point 1. I know it will break proxying but it's more
> simpler and i can think of doing 2. later on.
>
> Thank you,
> Zaihan
>
> On Thu, Dec 21, 2023 at 8:59 AM Maxim Dounin <mdounin at mdounin.ru> wrote:
> >
> > 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/
> > _______________________________________________
> > nginx-devel mailing list
> > nginx-devel at nginx.org
> > https://mailman.nginx.org/mailman/listinfo/nginx-devel


More information about the nginx-devel mailing list