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? <br>
<br>static u_char ngx_hello_string[] = "Hello, world!";<br><br>static ngx_int_t ngx_http_alpha_handler_top(ngx_http_request_t *r) {<br> ngx_int_t rc;<br> ngx_http_alpha_loc_conf_t *ctx;<br><br> /* we response to 'GET' and 'HEAD' requests only */<br>
if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {<br> return(NGX_HTTP_NOT_ALLOWED);<br> }<br> /* discard request body, since we don't need it here */<br> rc = ngx_http_discard_request_body(r);<br><br> if (rc != NGX_OK) {<br>
return(rc);<br> }<br><br> ctx = ngx_http_get_module_loc_conf(r, ngx_http_alpha_module);<br> if (ctx->event == NULL) {<br> ctx->event = ngx_calloc(sizeof(ngx_event_t), r->connection->log);<br> if (ctx->event == NULL) {<br>
return NGX_HTTP_INTERNAL_SERVER_ERROR;<br> }<br> }<br><br> ngx_memzero(ctx->event, sizeof(ngx_event_t));<br> ctx->event->handler = ngx_http_alpha_handler_bottom;<br> ctx->event->data = r;<br> ctx->event->log = r->connection->log;<br>
<br> ngx_add_timer(ctx->event, 10 * 1000);<br><br> r->main->count++;<br> return(NGX_DONE);<br>}<br><br>static ngx_int_t ngx_http_alpha_handler_bottom(ngx_event_t *ev) {<br> ngx_http_request_t *r;<br> ngx_int_t rc;<br>
ngx_buf_t *b;<br> ngx_chain_t out;<br><br> r = (ngx_http_request_t *)ev->data;<br><br> /* set the 'Content-type' header */<br> r->headers_out.content_type_len = sizeof("text/html") - 1;<br>
r->headers_out.content_type.len = sizeof("text/html") - 1;<br> r->headers_out.content_type.data = (u_char *) "text/html";<br><br> /* send the header only, if the request type is http 'HEAD' */<br>
if (r->method == NGX_HTTP_HEAD) {<br> r->headers_out.status = NGX_HTTP_OK;<br> r->headers_out.content_length_n = sizeof(ngx_hello_string) - 1;<br><br> return(ngx_http_send_header(r));<br> }<br><br> /* allocate a buffer for your response body */<br>
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));<br> if (b == NULL) {<br> return(NGX_HTTP_INTERNAL_SERVER_ERROR);<br> }<br><br> /* attach this buffer to the buffer chain */<br> out.buf = b;<br> out.next = NULL;<br>
<br> /* adjust the pointers of the buffer */<br> b->pos = ngx_hello_string;<br> b->last = ngx_hello_string + sizeof(ngx_hello_string) - 1;<br> b->memory = 1; /* this buffer is in memory */<br> b->last_buf = 1; /* this is the last buffer in the buffer chain */<br>
<br> /* set the status line */<br> r->headers_out.status = NGX_HTTP_OK;<br> r->headers_out.content_length_n = sizeof(ngx_hello_string) - 1;<br><br> /* send the headers of your response */<br> rc = ngx_http_send_header(r);<br>
<br> if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {<br> return(rc);<br> }<br><br> /* send the buffer chain of your response */<br> return(ngx_http_output_filter(r, &out));<br>}<br><br>