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
|
10
|
72 #if 1
|
|
73 if (ngx_buf_size(cl->buf) == 0 && !ngx_buf_special(cl->buf)) {
|
|
74 ngx_debug_point();
|
|
75 }
|
|
76 #endif
|
|
77
|
0
|
78 size += ngx_buf_size(cl->buf);
|
|
79
|
|
80 if (cl->buf->flush || cl->buf->recycled) {
|
|
81 flush = size;
|
|
82 }
|
|
83
|
|
84 if (cl->buf->last_buf) {
|
|
85 last = 1;
|
|
86 }
|
|
87 }
|
|
88
|
|
89 /* add the new chain to the existent one */
|
|
90
|
|
91 for (ln = in; ln; ln = ln->next) {
|
10
|
92 if (!(cl = ngx_alloc_chain_link(r->pool))) {
|
|
93 return NGX_ERROR;
|
|
94 }
|
|
95
|
|
96 cl->buf = ln->buf;
|
0
|
97 *ll = cl;
|
|
98 ll = &cl->next;
|
|
99
|
10
|
100 #if 1
|
|
101 if (ngx_buf_size(cl->buf) == 0 && !ngx_buf_special(cl->buf)) {
|
|
102 ngx_debug_point();
|
|
103 }
|
|
104 #endif
|
|
105
|
0
|
106 size += ngx_buf_size(cl->buf);
|
|
107
|
|
108 if (cl->buf->flush || cl->buf->recycled) {
|
|
109 flush = size;
|
|
110 }
|
|
111
|
|
112 if (cl->buf->last_buf) {
|
|
113 last = 1;
|
|
114 }
|
|
115 }
|
|
116
|
10
|
117 *ll = NULL;
|
|
118
|
0
|
119 c = r->connection;
|
|
120
|
|
121 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
10
|
122 "http write filter: l:%d f:%O s:%O", last, flush, size);
|
0
|
123
|
|
124 clcf = ngx_http_get_module_loc_conf(r->main ? r->main : r,
|
|
125 ngx_http_core_module);
|
|
126
|
|
127 /*
|
|
128 * avoid the output if there is no last buf, no flush point,
|
|
129 * there are the incoming bufs and the size of all bufs
|
|
130 * is smaller than "postpone_output" directive
|
|
131 */
|
|
132
|
|
133 if (!last && flush == 0 && in && size < (off_t) clcf->postpone_output) {
|
|
134 return NGX_OK;
|
|
135 }
|
|
136
|
|
137 if (c->write->delayed) {
|
|
138 return NGX_AGAIN;
|
|
139 }
|
|
140
|
|
141 if (size == 0 && !c->buffered) {
|
|
142 if (!last) {
|
|
143 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
144 "the http output chain is empty");
|
10
|
145
|
|
146 ngx_debug_point();
|
|
147
|
4
|
148 return NGX_ERROR;
|
0
|
149 }
|
|
150 return NGX_OK;
|
|
151 }
|
|
152
|
|
153 sent = c->sent;
|
|
154
|
|
155 chain = c->send_chain(c, ctx->out,
|
|
156 clcf->limit_rate ? clcf->limit_rate: OFF_T_MAX_VALUE);
|
|
157
|
|
158 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
10
|
159 "http write filter %p", chain);
|
0
|
160
|
|
161 if (clcf->limit_rate) {
|
|
162 sent = c->sent - sent;
|
|
163 c->write->delayed = 1;
|
|
164 ngx_add_timer(r->connection->write,
|
|
165 (ngx_msec_t) (sent * 1000 / clcf->limit_rate));
|
|
166 }
|
|
167
|
|
168 if (chain == NGX_CHAIN_ERROR) {
|
|
169 return NGX_ERROR;
|
|
170 }
|
|
171
|
|
172 ctx->out = chain;
|
|
173
|
|
174 if (chain || c->buffered) {
|
|
175 return NGX_AGAIN;
|
|
176 }
|
|
177
|
|
178 return NGX_OK;
|
|
179 }
|
|
180
|
|
181
|
|
182 static ngx_int_t ngx_http_write_filter_init(ngx_cycle_t *cycle)
|
|
183 {
|
|
184 ngx_http_top_body_filter = ngx_http_write_filter;
|
|
185
|
|
186 return NGX_OK;
|
|
187 }
|