Send custom response in req. body filter.

Dk Jack dnj0496 at gmail.com
Thu Sep 29 23:47:43 UTC 2022


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20220929/3ee211ed/attachment.htm>


More information about the nginx-devel mailing list