Flushing responses in nginx modules
Johan Andersson
ng23 at firemail.cc
Sun Jul 9 17:43:05 UTC 2017
Hi everyone,
I have some issues writing my nginx modules.
I am on Debian Stretch, installed nginx with the default configuration,
and took the hello_world module. It works without a hitch. Then I
changed the handler to send three "hello world" responses, and sleep for
one second between each response.
However, when I look at the result in my browser, the page loads, pauses
for three seconds, and then displays all three "hello world" messages at
once.
Actually I was flushing each response, so I expected each "hello world"
message to appear one after the other, with one second pause between
them.
Am I doing something wrong? Is this event the correct way to achieve
this? All functions return NGX_OK. This is my code:
static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r)
{
ngx_buf_t *b;
ngx_chain_t out;
ngx_int_t result;
r->headers_out.content_type.len = sizeof("text/html") - 1;
r->headers_out.content_type.data = (u_char *) "text/html";
r->headers_out.status = NGX_HTTP_OK;
//r->headers_out.content_length_n = sizeof(ngx_hello_world);
ngx_http_send_header(r);
for(int i = 0; i < 3; i++)
{
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
out.buf = b;
out.next = NULL;
b->pos = ngx_hello_world;
b->last = ngx_hello_world + sizeof(ngx_hello_world);
b->memory = 1;
b->flush = 1;
b->last_buf = (i == 2);
result = ngx_http_output_filter(r, &out);
ngx_http_send_special(r, NGX_HTTP_FLUSH);
sleep(1);
}
return result;
}
Cheers
Johann
More information about the nginx
mailing list