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();
|
|
97 }
|
|
98 #endif
|
|
99
|
0
|
100 size += ngx_buf_size(cl->buf);
|
|
101
|
|
102 if (cl->buf->flush || cl->buf->recycled) {
|
|
103 flush = size;
|
|
104 }
|
|
105
|
|
106 if (cl->buf->last_buf) {
|
|
107 last = 1;
|
|
108 }
|
|
109 }
|
|
110
|
|
111 /* add the new chain to the existent one */
|
|
112
|
|
113 for (ln = in; ln; ln = ln->next) {
|
10
|
114 if (!(cl = ngx_alloc_chain_link(r->pool))) {
|
|
115 return NGX_ERROR;
|
|
116 }
|
|
117
|
|
118 cl->buf = ln->buf;
|
0
|
119 *ll = cl;
|
|
120 ll = &cl->next;
|
|
121
|
28
|
122 ngx_log_debug7(NGX_LOG_DEBUG_EVENT, r->connection->log, 0,
|
|
123 "write new buf t:%d f:%d %p, pos %p, size: %z "
|
|
124 "file: %O, size: %z",
|
|
125 cl->buf->temporary, cl->buf->in_file,
|
|
126 cl->buf->start, cl->buf->pos,
|
|
127 cl->buf->last - cl->buf->pos,
|
|
128 cl->buf->file_pos,
|
|
129 cl->buf->file_last - cl->buf->file_pos);
|
|
130
|
10
|
131 #if 1
|
|
132 if (ngx_buf_size(cl->buf) == 0 && !ngx_buf_special(cl->buf)) {
|
28
|
133 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
32
|
134 "zero size buf in writer "
|
|
135 "t:%d r:%d f:%d %p %p-%p %p %O-%O",
|
|
136 cl->buf->temporary,
|
|
137 cl->buf->recycled,
|
|
138 cl->buf->in_file,
|
|
139 cl->buf->start,
|
|
140 cl->buf->pos,
|
|
141 cl->buf->last,
|
|
142 cl->buf->file,
|
|
143 cl->buf->file_pos,
|
|
144 cl->buf->file_last);
|
|
145
|
10
|
146 ngx_debug_point();
|
|
147 }
|
|
148 #endif
|
|
149
|
0
|
150 size += ngx_buf_size(cl->buf);
|
|
151
|
|
152 if (cl->buf->flush || cl->buf->recycled) {
|
|
153 flush = size;
|
|
154 }
|
|
155
|
|
156 if (cl->buf->last_buf) {
|
|
157 last = 1;
|
|
158 }
|
|
159 }
|
|
160
|
10
|
161 *ll = NULL;
|
|
162
|
0
|
163 c = r->connection;
|
|
164
|
|
165 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
10
|
166 "http write filter: l:%d f:%O s:%O", last, flush, size);
|
0
|
167
|
|
168 clcf = ngx_http_get_module_loc_conf(r->main ? r->main : r,
|
|
169 ngx_http_core_module);
|
|
170
|
|
171 /*
|
|
172 * avoid the output if there is 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
|
|
175 */
|
|
176
|
|
177 if (!last && flush == 0 && in && size < (off_t) clcf->postpone_output) {
|
|
178 return NGX_OK;
|
|
179 }
|
|
180
|
|
181 if (c->write->delayed) {
|
|
182 return NGX_AGAIN;
|
|
183 }
|
|
184
|
|
185 if (size == 0 && !c->buffered) {
|
32
|
186 if (last) {
|
|
187 return NGX_OK;
|
|
188 }
|
10
|
189
|
32
|
190 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
191 "the http output chain is empty");
|
10
|
192
|
32
|
193 ngx_debug_point();
|
|
194
|
|
195 return NGX_ERROR;
|
0
|
196 }
|
|
197
|
|
198 sent = c->sent;
|
|
199
|
22
|
200 chain = c->send_chain(c, ctx->out, clcf->limit_rate);
|
0
|
201
|
|
202 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
10
|
203 "http write filter %p", chain);
|
0
|
204
|
|
205 if (clcf->limit_rate) {
|
|
206 sent = c->sent - sent;
|
|
207 c->write->delayed = 1;
|
|
208 ngx_add_timer(r->connection->write,
|
|
209 (ngx_msec_t) (sent * 1000 / clcf->limit_rate));
|
|
210 }
|
|
211
|
|
212 if (chain == NGX_CHAIN_ERROR) {
|
|
213 return NGX_ERROR;
|
|
214 }
|
|
215
|
|
216 ctx->out = chain;
|
|
217
|
32
|
218 if (chain || (last && c->buffered)) {
|
0
|
219 return NGX_AGAIN;
|
|
220 }
|
|
221
|
|
222 return NGX_OK;
|
|
223 }
|
|
224
|
|
225
|
|
226 static ngx_int_t ngx_http_write_filter_init(ngx_cycle_t *cycle)
|
|
227 {
|
|
228 ngx_http_top_body_filter = ngx_http_write_filter;
|
|
229
|
|
230 return NGX_OK;
|
|
231 }
|