3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
12 static ngx_int_t ngx_http_write_filter_init(ngx_conf_t *cf);
15 static ngx_http_module_t ngx_http_write_filter_module_ctx = {
16 NULL, /* preconfiguration */
17 ngx_http_write_filter_init, /* postconfiguration */
19 NULL, /* create main configuration */
20 NULL, /* init main configuration */
22 NULL, /* create server configuration */
23 NULL, /* merge server configuration */
25 NULL, /* create location configuration */
26 NULL, /* merge location configuration */
30 ngx_module_t ngx_http_write_filter_module = {
32 &ngx_http_write_filter_module_ctx, /* module context */
33 NULL, /* module directives */
34 NGX_HTTP_MODULE, /* module type */
35 NULL, /* init master */
36 NULL, /* init module */
37 NULL, /* init process */
38 NULL, /* init thread */
39 NULL, /* exit thread */
40 NULL, /* exit process */
41 NULL, /* exit master */
47 ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in)
49 off_t size, sent, limit;
50 ngx_uint_t last, flush;
52 ngx_chain_t *cl, *ln, **ll, *chain;
54 ngx_http_core_loc_conf_t *clcf;
67 /* find the size, the flush point and the last link of the saved chain */
69 for (cl = r->out; cl; cl = cl->next) {
72 ngx_log_debug7(NGX_LOG_DEBUG_EVENT, c->log, 0,
73 "write old buf t:%d f:%d %p, pos %p, size: %z "
75 cl->buf->temporary, cl->buf->in_file,
76 cl->buf->start, cl->buf->pos,
77 cl->buf->last - cl->buf->pos,
79 cl->buf->file_last - cl->buf->file_pos);
82 if (ngx_buf_size(cl->buf) == 0 && !ngx_buf_special(cl->buf)) {
83 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
84 "zero size buf in writer "
85 "t:%d r:%d f:%d %p %p-%p %p %O-%O",
101 size += ngx_buf_size(cl->buf);
103 if (cl->buf->flush || cl->buf->recycled) {
107 if (cl->buf->last_buf) {
112 /* add the new chain to the existent one */
114 for (ln = in; ln; ln = ln->next) {
115 cl = ngx_alloc_chain_link(r->pool);
124 ngx_log_debug7(NGX_LOG_DEBUG_EVENT, c->log, 0,
125 "write new buf t:%d f:%d %p, pos %p, size: %z "
126 "file: %O, size: %z",
127 cl->buf->temporary, cl->buf->in_file,
128 cl->buf->start, cl->buf->pos,
129 cl->buf->last - cl->buf->pos,
131 cl->buf->file_last - cl->buf->file_pos);
134 if (ngx_buf_size(cl->buf) == 0 && !ngx_buf_special(cl->buf)) {
135 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
136 "zero size buf in writer "
137 "t:%d r:%d f:%d %p %p-%p %p %O-%O",
153 size += ngx_buf_size(cl->buf);
155 if (cl->buf->flush || cl->buf->recycled) {
159 if (cl->buf->last_buf) {
166 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
167 "http write filter: l:%d f:%d s:%O", last, flush, size);
169 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
172 * avoid the output if there are no last buf, no flush point,
173 * there are the incoming bufs and the size of all bufs
174 * is smaller than "postpone_output" directive
177 if (!last && !flush && in && size < (off_t) clcf->postpone_output) {
181 if (c->write->delayed) {
182 c->buffered |= NGX_HTTP_WRITE_BUFFERED;
186 if (size == 0 && !(c->buffered & NGX_LOWLEVEL_BUFFERED)) {
189 c->buffered &= ~NGX_HTTP_WRITE_BUFFERED;
196 r->out = r->out->next;
199 c->buffered &= ~NGX_HTTP_WRITE_BUFFERED;
204 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
205 "the http output chain is empty");
213 limit = r->limit_rate * (ngx_time() - r->start_sec + 1) - c->sent;
216 c->write->delayed = 1;
217 ngx_add_timer(c->write,
218 (ngx_msec_t) (- limit * 1000 / r->limit_rate + 1));
220 c->buffered |= NGX_HTTP_WRITE_BUFFERED;
225 } else if (clcf->sendfile_max_chunk) {
226 limit = clcf->sendfile_max_chunk;
234 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
235 "http write filter limit %O", limit);
237 chain = c->send_chain(c, r->out, limit);
239 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
240 "http write filter %p", chain);
242 if (chain == NGX_CHAIN_ERROR) {
248 delay = (ngx_msec_t) ((c->sent - sent) * 1000 / r->limit_rate + 1);
251 c->write->delayed = 1;
252 ngx_add_timer(c->write, delay);
255 } else if (c->write->ready
256 && clcf->sendfile_max_chunk
257 && (size_t) (c->sent - sent)
258 >= clcf->sendfile_max_chunk - 2 * ngx_pagesize)
260 c->write->delayed = 1;
261 ngx_add_timer(c->write, 1);
264 for (cl = r->out; cl && cl != chain; /* void */) {
267 ngx_free_chain(r->pool, ln);
273 c->buffered |= NGX_HTTP_WRITE_BUFFERED;
277 c->buffered &= ~NGX_HTTP_WRITE_BUFFERED;
279 if ((c->buffered & NGX_LOWLEVEL_BUFFERED) && r->postponed == NULL) {
288 ngx_http_write_filter_init(ngx_conf_t *cf)
290 ngx_http_top_body_filter = ngx_http_write_filter;