<div dir="ltr"><div><div>Hi,<br></div>I'm not sure that help.<br></div>But, I suggest this:<br><br><div><font face="monospace, monospace">int</font></div><div><font face="monospace, monospace">SendResponse(ngx_http_request_<wbr>t *r, ngx_uint_t http_status,</font></div><div><font face="monospace, monospace"> const char *data, unsigned int dlen)</font></div><div><font face="monospace, monospace">{</font></div><div><font face="monospace, monospace"> ngx_buf_t *buf;</font></div><div><div><font face="monospace, monospace"> ngx_int_t rc;<br><br></font></div><font face="monospace, monospace"> rc = ngx_http_discard_request_body(r);<br> if (rc != NGX_OK) {<br> return rc;<br> }<br><br></font></div><div><div><div><font face="monospace, monospace"> buf = ngx_create_temp_buf(r->pool, 512); /* Sure 512 is enough? */<br></font></div><font face="monospace, monospace"> if (NULL == buf) {</font></div><div><font face="monospace, monospace"> ngx_log_error(NGX_LOG_ERR, log, 0,</font></div><div><font face="monospace, monospace"> "allocation failure");</font></div><div><font face="monospace, monospace"> return NGX_HTTP_INTERNAL_SERVER_ERROR<wbr>;</font></div><div><font face="monospace, monospace"> }</font></div><div><font face="monospace, monospace"><br></font></div></div><div><font face="monospace, monospace"> buf->last = ngx_copy(buf->start, (unsigned char*) data, dlen);</font></div><div><div><font face="monospace, monospace"> ngx_log_t *log = r->connection->log;</font></div><div><font face="monospace, monospace"><br></font></div><div><span style="font-family:monospace,monospace"> ngx_chain_t *out_chain = ngx_alloc_chain_link(r->pool);</span></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,</font></div><div><font face="monospace, monospace"> "failed to alloc buffer chain");</font></div><div><font face="monospace, monospace"> return NGX_HTTP_INTERNAL_SERVER_ERROR<wbr>;</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><font face="monospace, monospace"> r->headers_out.status = http_status;</font><div><font face="monospace, monospace"> r->headers_out.content_length_<wbr>n = buf->last - buf->pos; /* Use buf->pos */<br></font></div><div><font face="monospace, monospace"> r->headers_out.content_type.le<wbr>n = sizeof("text/plain") - 1;</font></div><div><font face="monospace, monospace"> r->headers_out.content_type.da<wbr>ta = (u_char *) "text/plain";</font></div><div><font face="monospace, monospace"> rc = ngx_http_send_header(r);</font></div></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {</font><div><font face="monospace, monospace"> ngx_log_error(NGX_LOG_ERR, log, 0,</font></div><div><font face="monospace, monospace"> "send header failed. rc=%i", rc);</font></div><div><font face="monospace, monospace"> return NGX_HTTP_INTERNAL_SERVER_ERROR<wbr>;</font></div><div><font face="monospace, monospace"> }<br><br></font></div><div><font face="monospace, monospace"> rc = ngx_http_output_filter(r, out_chain);</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,</font></div><div><font face="monospace, monospace"> "send response buffer failed. rc=%i", </font><span style="font-family:monospace,monospace">rc);</span></div><div><font face="monospace, monospace"> return NGX_HTTP_INTERNAL_SERVER_ERROR<wbr>;</font></div><div><font face="monospace, monospace"> }</font></div><div><font face="monospace, monospace"></font><br></div><div><font face="monospace, monospace"> return rc;</font></div></div><div><font face="monospace, monospace">}</font></div><div><br></div></div><div id="DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2"><br> <table style="border-top:1px solid #d3d4de">
<tr>
<td style="width:55px;padding-top:18px"><a href="https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail" target="_blank"><img src="https://ipmcdn.avast.com/images/icons/icon-envelope-tick-round-orange-animated-no-repeat-v1.gif" alt="" width="46" height="29" style="width: 46px; height: 29px;"></a></td>
<td style="width:470px;padding-top:17px;color:#41424e;font-size:13px;font-family:Arial,Helvetica,sans-serif;line-height:18px">Livre de vírus. <a href="https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail" target="_blank" style="color:#4453ea">www.avast.com</a>. </td>
</tr>
</table>
<a href="#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2" width="1" height="1"></a></div><div class="gmail_extra"><br><div class="gmail_quote">2018-05-16 14:02 GMT-03:00 Dk Jack <span dir="ltr"><<a href="mailto:dnj0496@gmail.com" target="_blank">dnj0496@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><span style="font-size:12.800000190734863px">Hi,</span><div style="font-size:12.800000190734863px">I am trying to send a custom response in my module when I encounter a request for a specific location. For example, I have setup my location as follows:</div><div style="font-size:12.800000190734863px"><br></div><div style="font-size:12.800000190734863px"><div><font face="monospace, monospace"> server {</font></div><div><font face="monospace, monospace"> listen 9999;</font></div><div><font face="monospace, monospace"> location /__my_module {</font></div><div><font face="monospace, monospace"> set_mymodule_location;</font></div><div><font face="monospace, monospace"> log_not_found off;</font></div><div><font face="monospace, monospace"> }<br></font></div><div><font face="monospace, monospace"> }</font></div></div><div style="font-size:12.800000190734863px"><br></div><div style="font-size:12.800000190734863px">In my location handler, I am trying to respond to a request for this location. I get the response I expect at the client. However, I am seeing the following error message logged in error.log:</div><div style="font-size:12.800000190734863px"><br></div><div style="font-size:12.800000190734863px"><font face="monospace, monospace">2018/05/16 00:38:07 [alert] 225#225: *158 header already sent, client: 127.0.0.1, server: , request: "GET /__my_module HTTP/1.1", host: "localhost:9999"<br></font></div><div style="font-size:12.800000190734863px"><br></div><div style="font-size:12.800000190734863px">Can someone let me know how I can prevent this error from showing up. Also, the return value from ngx_http_send_header is always an NGX_ERROR. Not sure why...</div><div style="font-size:12.800000190734863px"><br></div><div style="font-size:12.800000190734863px">FWIW, my response buffer is always less than 512 bytes. My code is included at the end of this email. Any help is appreciated. Thanks<br></div><div style="font-size:12.800000190734863px"><br></div><div style="font-size:12.800000190734863px">Dk.</div><div style="font-size:12.800000190734863px"><br></div><div style="font-size:12.800000190734863px"><div><font face="monospace, monospace">int</font></div><div><font face="monospace, monospace">SendResponse(ngx_http_request_<wbr>t *r, ngx_uint_t http_status,</font></div><div><font face="monospace, monospace"> const char *data, unsigned int dlen)</font></div><div><font face="monospace, monospace">{</font></div><div><font face="monospace, monospace"> ngx_buf_t *buf = ngx_create_temp_buf(r->pool, 512);</font></div><div><font face="monospace, monospace"><br></font></div><div><div><font face="monospace, monospace"> if (NULL == buf) {</font></div><div><font face="monospace, monospace"> ngx_log_error(NGX_LOG_ERR, log, 0,</font></div><div><font face="monospace, monospace"> "allocation failure");</font></div><div><font face="monospace, monospace"> return NGX_HTTP_INTERNAL_SERVER_ERROR<wbr>;</font></div><div><font face="monospace, monospace"> }</font></div><div><font face="monospace, monospace"><br></font></div></div><div><font face="monospace, monospace"> buf->last = ngx_copy(buf->start, (unsigned char*) data, dlen);</font></div><div><div><font face="monospace, monospace"> ngx_log_t *log = r->connection->log;</font></div><div><font face="monospace, monospace"><br></font></div><div><span style="font-family:monospace,monospace"> ngx_chain_t *out_chain = ngx_alloc_chain_link(r->pool);</span></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,</font></div><div><font face="monospace, monospace"> "failed to alloc buffer chain");</font></div><div><font face="monospace, monospace"> return NGX_HTTP_INTERNAL_SERVER_ERROR<wbr>;</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"> ngx_int_t rc;</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_<wbr>n = buf->last - buf->start;</font></div><div><font face="monospace, monospace"> r->headers_out.content_type.le<wbr>n = sizeof("text/plain") - 1;</font></div><div><font face="monospace, monospace"> r->headers_out.content_type.da<wbr>ta = (u_char *) "text/plain";</font></div><div><font face="monospace, monospace"> rc = ngx_http_send_header(r);</font></div></div><div><font face="monospace, monospace"><br></font></div><div><div><font face="monospace, monospace">#if 0 // send_header always returns NGX_ERROR.</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,</font></div><div><font face="monospace, monospace"> "send header failed. rc=%i", rc);</font></div><div><font face="monospace, monospace"> return NGX_HTTP_INTERNAL_SERVER_ERROR<wbr>;</font></div><div><font face="monospace, monospace"> }</font></div><div><font face="monospace, monospace">#endif</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> rc = ngx_http_output_filter(r, out_chain);</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,</font></div><div><font face="monospace, monospace"> "send response buffer failed. rc=%i", </font><span style="font-family:monospace,monospace">rc);</span></div><div><font face="monospace, monospace"> return NGX_HTTP_INTERNAL_SERVER_ERROR<wbr>;</font></div><div><font face="monospace, monospace"> }</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> return 0;</font></div></div><div><font face="monospace, monospace">}</font></div><div><font face="monospace, monospace"><br></font></div></div></div>
<br>______________________________<wbr>_________________<br>
nginx-devel mailing list<br>
<a href="mailto:nginx-devel@nginx.org">nginx-devel@nginx.org</a><br>
<a href="http://mailman.nginx.org/mailman/listinfo/nginx-devel" rel="noreferrer" target="_blank">http://mailman.nginx.org/<wbr>mailman/listinfo/nginx-devel</a><br></blockquote></div><br></div>