Welcome! Log In Create A New Profile

Advanced

Send custom response in req. body filter.

September 29, 2022 07:50PM
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?

Thanks and regards,
Dk.
_______________________________________________
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 522 September 29, 2022 07:50PM

Re: Send custom response in req. body filter.

Maxim Dounin 203 September 30, 2022 05:32AM

Re: Send custom response in req. body filter.

dnj0496 253 September 30, 2022 12:16PM



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

Online Users

Guests: 324
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