Welcome! Log In Create A New Profile

Advanced

Flushing responses in nginx modules

Johan Andersson
July 09, 2017 01:44PM
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
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Subject Author Posted

Flushing responses in nginx modules

Johan Andersson July 09, 2017 01:44PM

Re: Flushing responses in nginx modules

A. Schulze July 09, 2017 04:42PM

Re: Flushing responses in nginx modules

tokers July 09, 2017 10:06PM

Re: Flushing responses in nginx modules

Johan Andersson July 11, 2017 10:58AM

Re: Flushing responses in nginx modules

tokers July 12, 2017 08:52AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 259
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready