error ngx_http_send_header

Ranier Vf ranier.vf at gmail.com
Wed May 16 17:21:37 UTC 2018


Hi,
I'm not sure that help.
But, I suggest this:

int
SendResponse(ngx_http_request_t *r, ngx_uint_t http_status,
             const char *data, unsigned int dlen)
{
  ngx_buf_t *buf;
  ngx_int_t rc;

  rc = ngx_http_discard_request_body(r);
  if (rc != NGX_OK) {
     return rc;
  }

  buf = ngx_create_temp_buf(r->pool, 512); /* Sure 512 is enough? */
  if (NULL == buf) {
    ngx_log_error(NGX_LOG_ERR, log, 0,
                  "allocation failure");
    return NGX_HTTP_INTERNAL_SERVER_ERROR;
  }

  buf->last = ngx_copy(buf->start, (unsigned char*) data, dlen);
  ngx_log_t *log = r->connection->log;

  ngx_chain_t *out_chain = ngx_alloc_chain_link(r->pool);
  if (NULL == out_chain) {
    ngx_log_error(NGX_LOG_ERR, log, 0,
                  "failed to alloc buffer chain");
    return NGX_HTTP_INTERNAL_SERVER_ERROR;
  }

  out_chain->buf = buf;
  out_chain->next = NULL;
  buf->last_buf = 1;
  buf->last_in_chain = 1;

  r->headers_out.status = http_status;
  r->headers_out.content_length_n = buf->last - buf->pos; /* Use buf->pos */
  r->headers_out.content_type.len = sizeof("text/plain") - 1;
  r->headers_out.content_type.data = (u_char *) "text/plain";
  rc = ngx_http_send_header(r);

  if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
    ngx_log_error(NGX_LOG_ERR, log, 0,
                  "send header failed. rc=%i", rc);
    return NGX_HTTP_INTERNAL_SERVER_ERROR;
  }

  rc = ngx_http_output_filter(r, out_chain);
  if (rc != NGX_OK) {
    ngx_log_error(NGX_LOG_ERR, log, 0,
                  "send response buffer failed. rc=%i", rc);
    return NGX_HTTP_INTERNAL_SERVER_ERROR;
  }

  return rc;
}


<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
Livre
de vírus. www.avast.com
<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>.
<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

2018-05-16 14:02 GMT-03:00 Dk Jack <dnj0496 at gmail.com>:

> Hi,
> 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:
>
>   server {
>     listen 9999;
>       location /__my_module {
>       set_mymodule_location;
>       log_not_found off;
>     }
>   }
>
> 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:
>
> 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"
>
> 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...
>
> 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
>
> Dk.
>
> int
> SendResponse(ngx_http_request_t *r, ngx_uint_t http_status,
>              const char *data, unsigned int dlen)
> {
>   ngx_buf_t *buf = ngx_create_temp_buf(r->pool, 512);
>
>   if (NULL == buf) {
>     ngx_log_error(NGX_LOG_ERR, log, 0,
>                   "allocation failure");
>     return NGX_HTTP_INTERNAL_SERVER_ERROR;
>   }
>
>   buf->last = ngx_copy(buf->start, (unsigned char*) data, dlen);
>   ngx_log_t *log = r->connection->log;
>
>   ngx_chain_t *out_chain = ngx_alloc_chain_link(r->pool);
>   if (NULL == out_chain) {
>     ngx_log_error(NGX_LOG_ERR, log, 0,
>                   "failed to alloc buffer chain");
>     return NGX_HTTP_INTERNAL_SERVER_ERROR;
>   }
>
>   out_chain->buf = buf;
>   out_chain->next = NULL;
>   buf->last_buf = 1;
>   buf->last_in_chain = 1;
>
>   ngx_int_t rc;
>   r->headers_out.status = http_status;
>   r->headers_out.content_length_n = buf->last - buf->start;
>   r->headers_out.content_type.len = sizeof("text/plain") - 1;
>   r->headers_out.content_type.data = (u_char *) "text/plain";
>   rc = ngx_http_send_header(r);
>
> #if 0 // send_header always returns NGX_ERROR.
>   if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
>     ngx_log_error(NGX_LOG_ERR, log, 0,
>                   "send header failed. rc=%i", rc);
>     return NGX_HTTP_INTERNAL_SERVER_ERROR;
>   }
> #endif
>
>   rc = ngx_http_output_filter(r, out_chain);
>   if (rc != NGX_OK) {
>     ngx_log_error(NGX_LOG_ERR, log, 0,
>                   "send response buffer failed. rc=%i", rc);
>     return NGX_HTTP_INTERNAL_SERVER_ERROR;
>   }
>
>   return 0;
> }
>
>
> _______________________________________________
> nginx-devel mailing list
> nginx-devel at nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20180516/7fc76f95/attachment.html>


More information about the nginx-devel mailing list