0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_event.h>
|
|
10 #include <ngx_http.h>
|
|
11
|
|
12
|
|
13 typedef struct {
|
|
14 ngx_chain_t *out;
|
|
15 } ngx_http_write_filter_ctx_t;
|
|
16
|
|
17
|
|
18 static ngx_int_t ngx_http_write_filter_init(ngx_cycle_t *cycle);
|
|
19
|
|
20
|
|
21 ngx_http_module_t ngx_http_write_filter_module_ctx = {
|
|
22 NULL, /* pre conf */
|
|
23
|
|
24 NULL, /* create main configuration */
|
|
25 NULL, /* init main configuration */
|
|
26
|
|
27 NULL, /* create server configuration */
|
|
28 NULL, /* merge server configuration */
|
|
29
|
|
30 NULL, /* create location configuration */
|
|
31 NULL, /* merge location configuration */
|
|
32 };
|
|
33
|
|
34
|
|
35 ngx_module_t ngx_http_write_filter_module = {
|
|
36 NGX_MODULE,
|
|
37 &ngx_http_write_filter_module_ctx, /* module context */
|
|
38 NULL, /* module directives */
|
|
39 NGX_HTTP_MODULE, /* module type */
|
|
40 ngx_http_write_filter_init, /* init module */
|
|
41 NULL /* init process */
|
|
42 };
|
|
43
|
|
44
|
|
45 ngx_int_t ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in)
|
|
46 {
|
|
47 int last;
|
|
48 off_t size, flush, sent;
|
|
49 ngx_chain_t *cl, *ln, **ll, *chain;
|
|
50 ngx_connection_t *c;
|
|
51 ngx_http_core_loc_conf_t *clcf;
|
|
52 ngx_http_write_filter_ctx_t *ctx;
|
|
53
|
|
54 ctx = ngx_http_get_module_ctx(r->main ? r->main : r,
|
|
55 ngx_http_write_filter_module);
|
|
56
|
|
57 if (ctx == NULL) {
|
|
58 ngx_http_create_ctx(r, ctx, ngx_http_write_filter_module,
|
|
59 sizeof(ngx_http_write_filter_ctx_t), NGX_ERROR);
|
|
60 }
|
|
61
|
|
62 size = 0;
|
|
63 flush = 0;
|
|
64 last = 0;
|
|
65 ll = &ctx->out;
|
|
66
|
|
67 /* find the size, the flush point and the last link of the saved chain */
|
|
68
|
|
69 for (cl = ctx->out; cl; cl = cl->next) {
|
|
70 ll = &cl->next;
|
|
71
|
28
|
72 ngx_log_debug7(NGX_LOG_DEBUG_EVENT, r->connection->log, 0,
|
|
73 "write old buf t:%d f:%d %p, pos %p, size: %z "
|
|
74 "file: %O, size: %z",
|
|
75 cl->buf->temporary, cl->buf->in_file,
|
|
76 cl->buf->start, cl->buf->pos,
|
|
77 cl->buf->last - cl->buf->pos,
|
|
78 cl->buf->file_pos,
|
|
79 cl->buf->file_last - cl->buf->file_pos);
|
|
80
|
10
|
81 #if 1
|
|
82 if (ngx_buf_size(cl->buf) == 0 && !ngx_buf_special(cl->buf)) {
|
28
|
83 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
32
|
84 "zero size buf in writer "
|
|
85 "t:%d r:%d f:%d %p %p-%p %p %O-%O",
|
|
86 cl->buf->temporary,
|
|
87 cl->buf->recycled,
|
|
88 cl->buf->in_file,
|
|
89 cl->buf->start,
|
|
90 cl->buf->pos,
|
|
91 cl->buf->last,
|
|
92 cl->buf->file,
|
|
93 cl->buf->file_pos,
|
|
94 cl->buf->file_last);
|
|
95
|
10
|
96 ngx_debug_point();
|
38
|
97 return NGX_ERROR;
|
10
|
98 }
|
|
99 #endif
|
|
100
|
0
|
101 size += ngx_buf_size(cl->buf);
|
|
102
|
|
103 if (cl->buf->flush || cl->buf->recycled) {
|
|
104 flush = size;
|
|
105 }
|
|
106
|
|
107 if (cl->buf->last_buf) {
|
|
108 last = 1;
|
|
109 }
|
|
110 }
|
|
111
|
|
112 /* add the new chain to the existent one */
|
|
113
|
|
114 for (ln = in; ln; ln = ln->next) {
|
10
|
115 if (!(cl = ngx_alloc_chain_link(r->pool))) {
|
|
116 return NGX_ERROR;
|
|
117 }
|
|
118
|
|
119 cl->buf = ln->buf;
|
0
|
120 *ll = cl;
|
|
121 ll = &cl->next;
|
|
122
|
28
|
123 ngx_log_debug7(NGX_LOG_DEBUG_EVENT, r->connection->log, 0,
|
|
124 "write new buf t:%d f:%d %p, pos %p, size: %z "
|
|
125 "file: %O, size: %z",
|
|
126 cl->buf->temporary, cl->buf->in_file,
|
|
127 cl->buf->start, cl->buf->pos,
|
|
128 cl->buf->last - cl->buf->pos,
|
|
129 cl->buf->file_pos,
|
|
130 cl->buf->file_last - cl->buf->file_pos);
|
|
131
|
10
|
132 #if 1
|
|
133 if (ngx_buf_size(cl->buf) == 0 && !ngx_buf_special(cl->buf)) {
|
28
|
134 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
32
|
135 "zero size buf in writer "
|
|
136 "t:%d r:%d f:%d %p %p-%p %p %O-%O",
|
|
137 cl->buf->temporary,
|
|
138 cl->buf->recycled,
|
|
139 cl->buf->in_file,
|
|
140 cl->buf->start,
|
|
141 cl->buf->pos,
|
|
142 cl->buf->last,
|
|
143 cl->buf->file,
|
|
144 cl->buf->file_pos,
|
|
145 cl->buf->file_last);
|
|
146
|
10
|
147 ngx_debug_point();
|
38
|
148 return NGX_ERROR;
|
10
|
149 }
|
|
150 #endif
|
|
151
|
0
|
152 size += ngx_buf_size(cl->buf);
|
|
153
|
|
154 if (cl->buf->flush || cl->buf->recycled) {
|
|
155 flush = size;
|
|
156 }
|
|
157
|
|
158 if (cl->buf->last_buf) {
|
|
159 last = 1;
|
|
160 }
|
|
161 }
|
|
162
|
10
|
163 *ll = NULL;
|
|
164
|
0
|
165 c = r->connection;
|
|
166
|
|
167 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
10
|
168 "http write filter: l:%d f:%O s:%O", last, flush, size);
|
0
|
169
|
|
170 clcf = ngx_http_get_module_loc_conf(r->main ? r->main : r,
|
|
171 ngx_http_core_module);
|
|
172
|
|
173 /*
|
|
174 * avoid the output if there is no last buf, no flush point,
|
|
175 * there are the incoming bufs and the size of all bufs
|
|
176 * is smaller than "postpone_output" directive
|
|
177 */
|
|
178
|
|
179 if (!last && flush == 0 && in && size < (off_t) clcf->postpone_output) {
|
|
180 return NGX_OK;
|
|
181 }
|
|
182
|
|
183 if (c->write->delayed) {
|
|
184 return NGX_AGAIN;
|
|
185 }
|
|
186
|
|
187 if (size == 0 && !c->buffered) {
|
32
|
188 if (last) {
|
|
189 return NGX_OK;
|
|
190 }
|
10
|
191
|
32
|
192 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
193 "the http output chain is empty");
|
10
|
194
|
32
|
195 ngx_debug_point();
|
|
196
|
|
197 return NGX_ERROR;
|
0
|
198 }
|
|
199
|
|
200 sent = c->sent;
|
|
201
|
22
|
202 chain = c->send_chain(c, ctx->out, clcf->limit_rate);
|
0
|
203
|
|
204 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
10
|
205 "http write filter %p", chain);
|
0
|
206
|
|
207 if (clcf->limit_rate) {
|
|
208 sent = c->sent - sent;
|
|
209 c->write->delayed = 1;
|
|
210 ngx_add_timer(r->connection->write,
|
|
211 (ngx_msec_t) (sent * 1000 / clcf->limit_rate));
|
|
212 }
|
|
213
|
|
214 if (chain == NGX_CHAIN_ERROR) {
|
|
215 return NGX_ERROR;
|
|
216 }
|
|
217
|
|
218 ctx->out = chain;
|
|
219
|
32
|
220 if (chain || (last && c->buffered)) {
|
0
|
221 return NGX_AGAIN;
|
|
222 }
|
|
223
|
|
224 return NGX_OK;
|
|
225 }
|
|
226
|
|
227
|
|
228 static ngx_int_t ngx_http_write_filter_init(ngx_cycle_t *cycle)
|
|
229 {
|
|
230 ngx_http_top_body_filter = ngx_http_write_filter;
|
|
231
|
|
232 return NGX_OK;
|
|
233 }
|