September 30, 2022 12:16PM
Thanks Dounin,
After stepping through the code, I came to the same conclusion but wanted
to check just in case I missed something. Thanks for the help.

-Dk


On Fri, Sep 30, 2022 at 2:32 AM Maxim Dounin <mdounin@mdounin.ru> wrote:

> Hello!
>
> On Thu, Sep 29, 2022 at 04:47:43PM -0700, Dk Jack wrote:
>
> > Hi,
> > In my module I have a body filter. In the body filter, I respond with a
> 4XX
> > if the body contains a matching pattern. This is working correctly.
> >
> > However, I would like to send a custom response message when the above
> > situation occurs. I tried doing this by attaching a buffer to the output
> > headers and calling send header before returning from the filter handler
> as
> > shown below.
> >
> > -----------------------------------------------------------------------
> > r->err_status = http_status;
> > r->headers_out.status = http_status;
> > r->headers_out.content_length_n = buf->last - buf->pos;
> > ngx_str_set(&r->headers_out.content_type, "text/plain");
> >
> > rc = ngx_http_send_header(r);
> > -----------------------------------------------------------------------
> >
> > However, this causes the "header already sent" error to be raised in the
> > error.log. The reason for this is because, ngx_http_core_content_phase
> > calls ngx_http_finalize_request after returning from the body filter.
> >
> > ngx_http_finalize_request calls ngx_http_special_response_handler if the
> > body filter returns any codes greater than NGX_HTTP_SPECIAL_RESPONSE. The
> > ngx_http_special_response_handler in turn calls
> > ngx_http_send_special_response which again tries to send the response
> > header using the stock error message in the ngx_http_error_pages array
> for
> > the return code in question. The alert is raised since we already sent
> it.
> >
> > Is there a way to prevent the special_response handler from either
> sending
> > this message again or use the buffer attached to the request in the body
> > filter?
>
> It is not possible to return a response body from a request body
> filter, you can only return an error code.
>
> If you want to return a custom response, consider configuring an
> error_page and return an appropriate custom response message from
> there.
>
> --
> Maxim Dounin
> http://mdounin.ru/
> _______________________________________________
> nginx-devel mailing list -- nginx-devel@nginx.org
> To unsubscribe send an email to nginx-devel-leave@nginx.org
>
_______________________________________________
nginx-devel mailing list -- nginx-devel@nginx.org
To unsubscribe send an email to nginx-devel-leave@nginx.org
Subject Author Views Posted

Send custom response in req. body filter.

dnj0496 484 September 29, 2022 07:50PM

Re: Send custom response in req. body filter.

Maxim Dounin 176 September 30, 2022 05:32AM

Re: Send custom response in req. body filter.

dnj0496 231 September 30, 2022 12:16PM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 151
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready