Could someone please review this code snippet and tell me what I've missed?

J.Q. S. q2011oct at gmail.com
Sun Dec 25 23:08:02 UTC 2011


As an exercise I've written a module which receives a request, sets a 10
second timer, then replies "Hello World!" when the timer expires. I'm
running nginx with deamon off/master off to make things as simple as
possible. The first time through everything seems to work perfectly.
However, the second time I don't get any response at all - from any url the
server normally handles. This code does work though multiple invocations if
I call bottom handler function directly from the top handler function,
without using the timer. I was wondering if someone could spot check my
code and tell me what I'm doing wrong?

static u_char ngx_hello_string[] = "Hello, world!";

static ngx_int_t ngx_http_alpha_handler_top(ngx_http_request_t *r) {
  ngx_int_t    rc;
  ngx_http_alpha_loc_conf_t *ctx;

  /* we response to 'GET' and 'HEAD' requests only */
  if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
    return(NGX_HTTP_NOT_ALLOWED);
  }
  /* discard request body, since we don't need it here */
  rc = ngx_http_discard_request_body(r);

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

  ctx = ngx_http_get_module_loc_conf(r, ngx_http_alpha_module);
  if (ctx->event == NULL) {
    ctx->event = ngx_calloc(sizeof(ngx_event_t), r->connection->log);
    if (ctx->event == NULL) {
      return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }
  }

  ngx_memzero(ctx->event, sizeof(ngx_event_t));
  ctx->event->handler = ngx_http_alpha_handler_bottom;
  ctx->event->data = r;
  ctx->event->log = r->connection->log;

  ngx_add_timer(ctx->event, 10 * 1000);

  r->main->count++;
  return(NGX_DONE);
}

static ngx_int_t ngx_http_alpha_handler_bottom(ngx_event_t *ev) {
  ngx_http_request_t *r;
  ngx_int_t    rc;
  ngx_buf_t   *b;
  ngx_chain_t  out;

  r = (ngx_http_request_t *)ev->data;

  /* set the 'Content-type' header */
  r->headers_out.content_type_len = sizeof("text/html") - 1;
  r->headers_out.content_type.len = sizeof("text/html") - 1;
  r->headers_out.content_type.data = (u_char *) "text/html";

  /* send the header only, if the request type is http 'HEAD' */
  if (r->method == NGX_HTTP_HEAD) {
    r->headers_out.status = NGX_HTTP_OK;
    r->headers_out.content_length_n = sizeof(ngx_hello_string) - 1;

    return(ngx_http_send_header(r));
  }

  /* allocate a buffer for your response body */
  b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
  if (b == NULL) {
    return(NGX_HTTP_INTERNAL_SERVER_ERROR);
  }

  /* attach this buffer to the buffer chain */
  out.buf = b;
  out.next = NULL;

  /* adjust the pointers of the buffer */
  b->pos = ngx_hello_string;
  b->last = ngx_hello_string + sizeof(ngx_hello_string) - 1;
  b->memory = 1;    /* this buffer is in memory */
  b->last_buf = 1;  /* this is the last buffer in the buffer chain */

  /* set the status line */
  r->headers_out.status = NGX_HTTP_OK;
  r->headers_out.content_length_n = sizeof(ngx_hello_string) - 1;

  /* send the headers of your response */
  rc = ngx_http_send_header(r);

  if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
    return(rc);
  }

  /* send the buffer chain of your response */
  return(ngx_http_output_filter(r, &out));
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20111225/ae3ca417/attachment.html>


More information about the nginx-devel mailing list