<div dir="ltr">@Hung Nguyen,<div>Sorry did not see your response. Looks like my email settings were incorrect and I was not receiving the emails.</div><div><br></div><div>I am looking for a way to send custom response without having to change the config.</div><div><br></div><div>Dk.</div></div><br><div class="gmail_quote"><div dir="ltr">On Fri, Jul 6, 2018 at 2:33 PM Dk Jack <<a href="mailto:dnj0496@gmail.com">dnj0496@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi,<div>I thought I solved my issue but it looks like I haven't. It just took some time for the log messages to show up.</div><div>I've attached my code below. In my  code, the call HttpModuleProcessRequest returns a non-zero value if</div><div>the requests needs to be blocked.<font face="monospace, monospace"> </font>Would appreciate if some one can help. Thanks</div><div><br></div><div>-----------------------------------------------------------------------------------------------------------------------------------------------</div><div>PS: The issue seems to happen for request that my module blocked, so it seems to be related to the send header call.</div><div><br></div><div><div><font face="monospace, monospace">SendResponseBuffer(ngx_http_request_t *r, ngx_uint_t http_status, char *reason, ngx_int_t rlen)</font></div><div><font face="monospace, monospace">{</font></div><div><font face="monospace, monospace">  ngx_int_t rc;</font></div><div><font face="monospace, monospace">  ngx_log_t *log = r->connection->log;</font></div><div><font face="monospace, monospace">  ngx_buf_t *buf = ngx_create_temp_buf(r->pool, (rlen+16)); // pad.</font></div><div><font face="monospace, monospace">  if (NULL == buf) {</font></div><div><font face="monospace, monospace">    ngx_log_error(NGX_LOG_ERR, log, 0, "%s: Failed to allocate buffer", __FUNCTION__);</font></div><div><font face="monospace, monospace">    return NGX_ERROR;</font></div><div><font face="monospace, monospace">  }</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">  buf->last = ngx_copy(buf->start, (unsigned char*) data, dlen);</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">  rc = ngx_http_discard_request_body(r);</font></div><div><font face="monospace, monospace">  if (rc != NGX_OK) {</font></div><div><font face="monospace, monospace">    ngx_log_error(NGX_LOG_ERR, log, 0, "%s: Discard req. body failed. rc=%i", __FUNCTION__, rc);</font></div><div><font face="monospace, monospace">    return rc;</font></div><div><font face="monospace, monospace">  }</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">  ngx_chain_t *out_chain = ngx_alloc_chain_link(r->pool);</font></div><div><font face="monospace, monospace">  if (NULL == out_chain) {</font></div><div><font face="monospace, monospace">    ngx_log_error(NGX_LOG_ERR, log, 0, "%s: Buffer chain alloc failed", __FUNCTION__);</font></div><div><font face="monospace, monospace">    return NGX_ERROR;</font></div><div><font face="monospace, monospace">  }</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">  out_chain->buf = buf;</font></div><div><font face="monospace, monospace">  out_chain->next = NULL;</font></div><div><font face="monospace, monospace">  buf->last_buf = 1;</font></div><div><font face="monospace, monospace">  buf->last_in_chain = 1;</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">  r->headers_out.status = http_status;</font></div><div><font face="monospace, monospace">  r->headers_out.content_length_n = buf->last - buf->pos;</font></div><div><font face="monospace, monospace">  r->headers_out.content_type.len = sizeof("text/plain") - 1;</font></div><div><font face="monospace, monospace">  r->headers_out.content_type.data = (u_char *) "text/plain";</font></div><div><font face="monospace, monospace">  r->header_only = 1;</font></div><div><font face="monospace, monospace">  r->headers_out.content_length_n = 0;</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">  rc = ngx_http_send_header(r);</font></div><div><font face="monospace, monospace">  if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {</font></div><div><font face="monospace, monospace">    ngx_log_error(NGX_LOG_ERR, log, 0, "%s: Send header failed. rc=%i", __FUNCTION__, rc);</font></div><div><font face="monospace, monospace">    return rc;</font></div><div><font face="monospace, monospace">  }</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">  return ngx_http_output_filter(r, out_chain);</font></div><div><font face="monospace, monospace">}</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">typedef struct {</font></div><div><font face="monospace, monospace">  unsigned done:1;</font></div><div><font face="monospace, monospace">} ngx_http_mod_ctx_t;</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">static ngx_int_t</font></div><div><font face="monospace, monospace">ngx_http_request_handler(ngx_http_request_t *r)</font></div><div><font face="monospace, monospace">{</font></div><div><font face="monospace, monospace">  ngx_http_mod_ctx_t  *ctx = ngx_http_get_module_ctx(r, nginx_http_module);</font></div><div><font face="monospace, monospace">  if (ctx) {</font></div><div><font face="monospace, monospace">    ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "duplicate invokation");</font></div><div><font face="monospace, monospace">    return NGX_DECLINED;</font></div><div><font face="monospace, monospace">  } else {</font></div><div><font face="monospace, monospace">    ctx = ngx_palloc(r->connection->pool, sizeof(ngx_http_mod_ctx_t));</font></div><div><font face="monospace, monospace">    if (ctx == NULL) {</font></div><div><font face="monospace, monospace">      ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,</font></div><div><font face="monospace, monospace">                    "Out of memory. Cannot allocate context");</font></div><div><font face="monospace, monospace">      return NGX_ERROR;</font></div><div><font face="monospace, monospace">    }</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">    ctx->done = 0;</font></div><div><font face="monospace, monospace">    ngx_http_set_ctx(r, ctx, nginx_http_module);</font></div><div><font face="monospace, monospace">  }</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">  ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0,</font></div><div><font face="monospace, monospace">                "%s: uri:\"%V\" uuri: \"%V\" call-count:%ud",</font></div><div><font face="monospace, monospace">                __FUNCTION__, &r->uri, &r->unparsed_uri, r->main->count);</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">  ngx_int_t rc = 0;</font></div><div><font face="monospace, monospace">  if (!ctx->done) {</font></div><div><font face="monospace, monospace">    char buf[512];</font></div><div><font face="monospace, monospace">    ngx_uint_t len;</font></div><div><font face="monospace, monospace">    ngx_uint_t http_status;</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">    rc = HttpModuleProcessRequest(r, &http_status, buf, &len);</font></div><div><font face="monospace, monospace">    if (!rc) {</font></div><div><font face="monospace, monospace">      rc = SendResponseBuffer(r, http_status, buf, len);</font></div><div><font face="monospace, monospace">    }</font></div><div><font face="monospace, monospace">  }</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">  ctx->done = 1;</font></div><div><font face="monospace, monospace">  //ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,</font></div><div><font face="monospace, monospace">  //              "%s: rc = %d, uri:\"%V\"", __FUNCTION__, rc, &r->uri);</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">  if ((rc != 0) && (rc != NGX_DECLINED)) {</font></div><div><font face="monospace, monospace">    return rc;</font></div><div><font face="monospace, monospace">  }</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">  // per </font><a href="http://www.nginxguts.com/2011/01/phases/" target="_blank">http://www.nginxguts.com/2011/01/phases/</a>, ACCESS_PHASE handler should return NGX_OK here.</div><div><font face="monospace, monospace">  // however, returning NGX_OK causes nginx to hang.</font></div><div><font face="monospace, monospace">  return NGX_DECLINED;</font></div><div><font face="monospace, monospace">}</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">static ngx_int_t</font></div><div><font face="monospace, monospace">ngx_http_module_init (ngx_conf_t *cf)</font></div><div><font face="monospace, monospace">{</font></div><div><font face="monospace, monospace">  ngx_http_handler_pt       *hptr;</font></div><div><font face="monospace, monospace">  ngx_http_core_main_conf_t *cmcf;</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">  cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);</font></div><div><font face="monospace, monospace">  hptr = ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">  if (hptr == NULL) {</font></div><div><font face="monospace, monospace">    ngx_log_error(NGX_LOG_ERR, cf->log, 0, "Cannot retrieve Nginx access phase handler pointer");</font></div><div><font face="monospace, monospace">    return NGX_ERROR;</font></div><div><font face="monospace, monospace">  }</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">  *hptr = ngx_http_request_handler;</font></div><div><font face="monospace, monospace">  ngx_log_error(NGX_LOG_INFO, cf->log, 0, "[init] Installed request handler");</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">  return NGX_OK;</font></div><div><font face="monospace, monospace">}</font></div></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Jul 5, 2018 at 5:44 PM Dk Jack <<a href="mailto:dnj0496@gmail.com" target="_blank">dnj0496@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi,<div>I have an nginx module. The purpose of my module is to inspect every request and make a binary decision (i.e drop or allow). When dropping, I am sending a custom status code and response message. I've setup my module handler to be called in the NGX_HTTP_REWRITE_PHASE. Things seem to be working fine for the most part except for a small issue.</div><div><br></div><div>I am seeing 'header already sent' messages in the nginx error.log. In my handler, I create a context for my module and attach it request using the ngx_http_set_ctx call. I am using the context to store some info and also to prevent my module from processing the same request multiple times. My module processes the request only once.</div><div><br></div><div>I get the 'header already sent' message only for a fraction of the requests. The messages seem to be generated only for requests that I allowed in my module. In my module handler, I return an NGX_DECLINED return code when I allow a request. The message is not generated for every allowed request but only for some of the allowed requests. When the error is generated it is always for POST request and not all POST requests cause this error message to be logged. </div><div><br></div><div>Could someone explain what could be going on here or where I should look at next to debug this further? Any pointers are appreciated. Thanks.</div><div><br></div><div>Dk.</div><div><br></div></div>
</blockquote></div>
</blockquote></div>