1
2/*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
5 */
6
7
8#include <ngx_config.h>
9#include <ngx_core.h>
10#include <ngx_http.h>
11
12
13static ngx_int_t ngx_http_write_filter_init(ngx_conf_t *cf);
14
15
16static ngx_http_module_t  ngx_http_write_filter_module_ctx = {
17    NULL,                                  /* preconfiguration */
18    ngx_http_write_filter_init,            /* postconfiguration */
19
20    NULL,                                  /* create main configuration */
21    NULL,                                  /* init main configuration */
22
23    NULL,                                  /* create server configuration */
24    NULL,                                  /* merge server configuration */
25
26    NULL,                                  /* create location configuration */
27    NULL,                                  /* merge location configuration */
28};
29
30
31ngx_module_t  ngx_http_write_filter_module = {
32    NGX_MODULE_V1,
33    &ngx_http_write_filter_module_ctx,     /* module context */
34    NULL,                                  /* module directives */
35    NGX_HTTP_MODULE,                       /* module type */
36    NULL,                                  /* init master */
37    NULL,                                  /* init module */
38    NULL,                                  /* init process */
39    NULL,                                  /* init thread */
40    NULL,                                  /* exit thread */
41    NULL,                                  /* exit process */
42    NULL,                                  /* exit master */
43    NGX_MODULE_V1_PADDING
44};
45
46
47ngx_int_t
48ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in)
49{
50    off_t                      size, sent, nsent, limit;
51    ngx_uint_t                 last, flush, sync;
52    ngx_msec_t                 delay;
53    ngx_chain_t               *cl, *ln, **ll, *chain;
54    ngx_connection_t          *c;
55    ngx_http_core_loc_conf_t  *clcf;
56
57    c = r->connection;
58
59    if (c->error) {
60        return NGX_ERROR;
61    }
62
63    size = 0;
64    flush = 0;
65    sync = 0;
66    last = 0;
67    ll = &r->out;
68
69    /* find the size, the flush point and the last link of the saved chain */
70
71    for (cl = r->out; cl; cl = cl->next) {
72        ll = &cl->next;
73
74        ngx_log_debug7(NGX_LOG_DEBUG_EVENT, c->log, 0,
75                       "write old buf t:%d f:%d %p, pos %p, size: %z "
76                       "file: %O, size: %O",
77                       cl->buf->temporary, cl->buf->in_file,
78                       cl->buf->start, cl->buf->pos,
79                       cl->buf->last - cl->buf->pos,
80                       cl->buf->file_pos,
81                       cl->buf->file_last - cl->buf->file_pos);
82
83#if 1
84        if (ngx_buf_size(cl->buf) == 0 && !ngx_buf_special(cl->buf)) {
85            ngx_log_error(NGX_LOG_ALERT, c->log, 0,
86                          "zero size buf in writer "
87                          "t:%d r:%d f:%d %p %p-%p %p %O-%O",
88                          cl->buf->temporary,
89                          cl->buf->recycled,
90                          cl->buf->in_file,
91                          cl->buf->start,
92                          cl->buf->pos,
93                          cl->buf->last,
94                          cl->buf->file,
95                          cl->buf->file_pos,
96                          cl->buf->file_last);
97
98            ngx_debug_point();
99            return NGX_ERROR;
100        }
101#endif
102
103        size += ngx_buf_size(cl->buf);
104
105        if (cl->buf->flush || cl->buf->recycled) {
106            flush = 1;
107        }
108
109        if (cl->buf->sync) {
110            sync = 1;
111        }
112
113        if (cl->buf->last_buf) {
114            last = 1;
115        }
116    }
117
118    /* add the new chain to the existent one */
119
120    for (ln = in; ln; ln = ln->next) {
121        cl = ngx_alloc_chain_link(r->pool);
122        if (cl == NULL) {
123            return NGX_ERROR;
124        }
125
126        cl->buf = ln->buf;
127        *ll = cl;
128        ll = &cl->next;
129
130        ngx_log_debug7(NGX_LOG_DEBUG_EVENT, c->log, 0,
131                       "write new buf t:%d f:%d %p, pos %p, size: %z "
132                       "file: %O, size: %O",
133                       cl->buf->temporary, cl->buf->in_file,
134                       cl->buf->start, cl->buf->pos,
135                       cl->buf->last - cl->buf->pos,
136                       cl->buf->file_pos,
137                       cl->buf->file_last - cl->buf->file_pos);
138
139#if 1
140        if (ngx_buf_size(cl->buf) == 0 && !ngx_buf_special(cl->buf)) {
141            ngx_log_error(NGX_LOG_ALERT, c->log, 0,
142                          "zero size buf in writer "
143                          "t:%d r:%d f:%d %p %p-%p %p %O-%O",
144                          cl->buf->temporary,
145                          cl->buf->recycled,
146                          cl->buf->in_file,
147                          cl->buf->start,
148                          cl->buf->pos,
149                          cl->buf->last,
150                          cl->buf->file,
151                          cl->buf->file_pos,
152                          cl->buf->file_last);
153
154            ngx_debug_point();
155            return NGX_ERROR;
156        }
157#endif
158
159        size += ngx_buf_size(cl->buf);
160
161        if (cl->buf->flush || cl->buf->recycled) {
162            flush = 1;
163        }
164
165        if (cl->buf->sync) {
166            sync = 1;
167        }
168
169        if (cl->buf->last_buf) {
170            last = 1;
171        }
172    }
173
174    *ll = NULL;
175
176    ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
177                   "http write filter: l:%ui f:%ui s:%O", last, flush, size);
178
179    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
180
181    /*
182     * avoid the output if there are no last buf, no flush point,
183     * there are the incoming bufs and the size of all bufs
184     * is smaller than "postpone_output" directive
185     */
186
187    if (!last && !flush && in && size < (off_t) clcf->postpone_output) {
188        return NGX_OK;
189    }
190
191    if (c->write->delayed) {
192        c->buffered |= NGX_HTTP_WRITE_BUFFERED;
193        return NGX_AGAIN;
194    }
195
196    if (size == 0
197        && !(c->buffered & NGX_LOWLEVEL_BUFFERED)
198        && !(last && c->need_last_buf))
199    {
200        if (last || flush || sync) {
201            for (cl = r->out; cl; /* void */) {
202                ln = cl;
203                cl = cl->next;
204                ngx_free_chain(r->pool, ln);
205            }
206
207            r->out = NULL;
208            c->buffered &= ~NGX_HTTP_WRITE_BUFFERED;
209
210            return NGX_OK;
211        }
212
213        ngx_log_error(NGX_LOG_ALERT, c->log, 0,
214                      "the http output chain is empty");
215
216        ngx_debug_point();
217
218        return NGX_ERROR;
219    }
220
221    if (r->limit_rate) {
222        if (r->limit_rate_after == 0) {
223            r->limit_rate_after = clcf->limit_rate_after;
224        }
225
226        limit = (off_t) r->limit_rate * (ngx_time() - r->start_sec + 1)
227                - (c->sent - r->limit_rate_after);
228
229        if (limit <= 0) {
230            c->write->delayed = 1;
231            delay = (ngx_msec_t) (- limit * 1000 / r->limit_rate + 1);
232            ngx_add_timer(c->write, delay);
233
234            c->buffered |= NGX_HTTP_WRITE_BUFFERED;
235
236            return NGX_AGAIN;
237        }
238
239        if (clcf->sendfile_max_chunk
240            && (off_t) clcf->sendfile_max_chunk < limit)
241        {
242            limit = clcf->sendfile_max_chunk;
243        }
244
245    } else {
246        limit = clcf->sendfile_max_chunk;
247    }
248
249    sent = c->sent;
250
251    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
252                   "http write filter limit %O", limit);
253
254    chain = c->send_chain(c, r->out, limit);
255
256    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
257                   "http write filter %p", chain);
258
259    if (chain == NGX_CHAIN_ERROR) {
260        c->error = 1;
261        return NGX_ERROR;
262    }
263
264    if (r->limit_rate) {
265
266        nsent = c->sent;
267
268        if (r->limit_rate_after) {
269
270            sent -= r->limit_rate_after;
271            if (sent < 0) {
272                sent = 0;
273            }
274
275            nsent -= r->limit_rate_after;
276            if (nsent < 0) {
277                nsent = 0;
278            }
279        }
280
281        delay = (ngx_msec_t) ((nsent - sent) * 1000 / r->limit_rate);
282
283        if (delay > 0) {
284            limit = 0;
285            c->write->delayed = 1;
286            ngx_add_timer(c->write, delay);
287        }
288    }
289
290    if (limit
291        && c->write->ready
292        && c->sent - sent >= limit - (off_t) (2 * ngx_pagesize))
293    {
294        c->write->delayed = 1;
295        ngx_add_timer(c->write, 1);
296    }
297
298    for (cl = r->out; cl && cl != chain; /* void */) {
299        ln = cl;
300        cl = cl->next;
301        ngx_free_chain(r->pool, ln);
302    }
303
304    r->out = chain;
305
306    if (chain) {
307        c->buffered |= NGX_HTTP_WRITE_BUFFERED;
308        return NGX_AGAIN;
309    }
310
311    c->buffered &= ~NGX_HTTP_WRITE_BUFFERED;
312
313    if ((c->buffered & NGX_LOWLEVEL_BUFFERED) && r->postponed == NULL) {
314        return NGX_AGAIN;
315    }
316
317    return NGX_OK;
318}
319
320
321static ngx_int_t
322ngx_http_write_filter_init(ngx_conf_t *cf)
323{
324    ngx_http_top_body_filter = ngx_http_write_filter;
325
326    return NGX_OK;
327}
328