Welcome! Log In Create A New Profile

Advanced

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

J.Q. S.
December 25, 2011 06:10PM
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));
}
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

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

J.Q. S. 1305 December 25, 2011 06:10PM

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

Alexandr Gomoliako 499 December 25, 2011 07:22PM

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

wandenberg 663 December 25, 2011 08:34PM

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

Alexandr Gomoliako 491 December 25, 2011 09:30PM

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

J.Q. S. 529 December 25, 2011 10:24PM

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

J.Q. S. 679 December 26, 2011 12:08AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 285
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready