comparison src/http/ngx_http_write_filter.c @ 391:b670db10cbbd

nginx-0.0.7-2004-07-14-20:01:42 import
author Igor Sysoev <igor@sysoev.ru>
date Wed, 14 Jul 2004 16:01:42 +0000
parents f2755a2885c8
children d1222d46b3f9
comparison
equal deleted inserted replaced
390:1471c6fb108a 391:b670db10cbbd
4 #include <ngx_event.h> 4 #include <ngx_event.h>
5 #include <ngx_http.h> 5 #include <ngx_http.h>
6 6
7 7
8 typedef struct { 8 typedef struct {
9 size_t postpone_output; /* postpone_output */
10 size_t limit_rate; /* limit_rate */
11 } ngx_http_write_filter_conf_t;
12
13
14 typedef struct {
15 ngx_chain_t *out; 9 ngx_chain_t *out;
16 } ngx_http_write_filter_ctx_t; 10 } ngx_http_write_filter_ctx_t;
17 11
18 12
19 static void *ngx_http_write_filter_create_conf(ngx_conf_t *cf);
20 static char *ngx_http_write_filter_merge_conf(ngx_conf_t *cf,
21 void *parent, void *child);
22 static ngx_int_t ngx_http_write_filter_init(ngx_cycle_t *cycle); 13 static ngx_int_t ngx_http_write_filter_init(ngx_cycle_t *cycle);
23
24
25 static ngx_command_t ngx_http_write_filter_commands[] = {
26
27 { ngx_string("postpone_output"),
28 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
29 ngx_conf_set_size_slot,
30 NGX_HTTP_LOC_CONF_OFFSET,
31 offsetof(ngx_http_write_filter_conf_t, postpone_output),
32 NULL },
33
34 { ngx_string("limit_rate"),
35 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
36 ngx_conf_set_size_slot,
37 NGX_HTTP_LOC_CONF_OFFSET,
38 offsetof(ngx_http_write_filter_conf_t, limit_rate),
39 NULL },
40
41 ngx_null_command
42 };
43 14
44 15
45 ngx_http_module_t ngx_http_write_filter_module_ctx = { 16 ngx_http_module_t ngx_http_write_filter_module_ctx = {
46 NULL, /* pre conf */ 17 NULL, /* pre conf */
47 18
49 NULL, /* init main configuration */ 20 NULL, /* init main configuration */
50 21
51 NULL, /* create server configuration */ 22 NULL, /* create server configuration */
52 NULL, /* merge server configuration */ 23 NULL, /* merge server configuration */
53 24
54 ngx_http_write_filter_create_conf, /* create location configuration */ 25 NULL, /* create location configuration */
55 ngx_http_write_filter_merge_conf /* merge location configuration */ 26 NULL, /* merge location configuration */
56 }; 27 };
57 28
58 29
59 ngx_module_t ngx_http_write_filter_module = { 30 ngx_module_t ngx_http_write_filter_module = {
60 NGX_MODULE, 31 NGX_MODULE,
61 &ngx_http_write_filter_module_ctx, /* module context */ 32 &ngx_http_write_filter_module_ctx, /* module context */
62 ngx_http_write_filter_commands, /* module directives */ 33 NULL, /* module directives */
63 NGX_HTTP_MODULE, /* module type */ 34 NGX_HTTP_MODULE, /* module type */
64 ngx_http_write_filter_init, /* init module */ 35 ngx_http_write_filter_init, /* init module */
65 NULL /* init process */ 36 NULL /* init process */
66 }; 37 };
67 38
68 39
69 ngx_int_t ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in) 40 ngx_int_t ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in)
70 { 41 {
71 int last; 42 int last;
72 off_t size, flush, sent; 43 off_t size, flush, sent;
73 ngx_chain_t *cl, *ln, **ll, *chain; 44 ngx_chain_t *cl, *ln, **ll, *chain;
74 ngx_http_write_filter_ctx_t *ctx; 45 ngx_http_core_loc_conf_t *clcf;
75 ngx_http_write_filter_conf_t *conf; 46 ngx_http_write_filter_ctx_t *ctx;
76 47
77 ctx = ngx_http_get_module_ctx(r->main ? r->main : r, 48 ctx = ngx_http_get_module_ctx(r->main ? r->main : r,
78 ngx_http_write_filter_module); 49 ngx_http_write_filter_module);
79 50
80 if (ctx == NULL) { 51 if (ctx == NULL) {
123 94
124 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 95 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
125 "http write filter: l:%d f:" OFF_T_FMT " s:" OFF_T_FMT, 96 "http write filter: l:%d f:" OFF_T_FMT " s:" OFF_T_FMT,
126 last, flush, size); 97 last, flush, size);
127 98
128 conf = ngx_http_get_module_loc_conf(r->main ? r->main : r, 99 clcf = ngx_http_get_module_loc_conf(r->main ? r->main : r,
129 ngx_http_write_filter_module); 100 ngx_http_core_module);
130 101
131 /* 102 /*
132 * avoid the output if there is no last buf, no flush point, 103 * avoid the output if there is no last buf, no flush point,
133 * there are the incoming bufs and the size of all bufs 104 * there are the incoming bufs and the size of all bufs
134 * is smaller than "postpone_output" directive 105 * is smaller than "postpone_output" directive
135 */ 106 */
136 107
137 if (!last && flush == 0 && in && size < (off_t) conf->postpone_output) { 108 if (!last && flush == 0 && in && size < (off_t) clcf->postpone_output) {
138 return NGX_OK; 109 return NGX_OK;
139 } 110 }
140 111
141 if (r->connection->write->delayed) { 112 if (r->connection->write->delayed) {
142 return NGX_AGAIN; 113 return NGX_AGAIN;
151 } 122 }
152 123
153 sent = r->connection->sent; 124 sent = r->connection->sent;
154 125
155 chain = ngx_write_chain(r->connection, ctx->out, 126 chain = ngx_write_chain(r->connection, ctx->out,
156 conf->limit_rate ? conf->limit_rate: 127 clcf->limit_rate ? clcf->limit_rate:
157 OFF_T_MAX_VALUE); 128 OFF_T_MAX_VALUE);
158 129
159 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 130 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
160 "http write filter %X", chain); 131 "http write filter %X", chain);
161 132
162 if (conf->limit_rate) { 133 if (clcf->limit_rate) {
163 sent = r->connection->sent - sent; 134 sent = r->connection->sent - sent;
164 r->connection->write->delayed = 1; 135 r->connection->write->delayed = 1;
165 ngx_add_timer(r->connection->write, 136 ngx_add_timer(r->connection->write,
166 (ngx_msec_t) sent * 1000 / conf->limit_rate); 137 (ngx_msec_t) (sent * 1000 / clcf->limit_rate));
167 } 138 }
168 139
169 if (chain == NGX_CHAIN_ERROR) { 140 if (chain == NGX_CHAIN_ERROR) {
170 return NGX_ERROR; 141 return NGX_ERROR;
171 } 142 }
178 149
179 return NGX_AGAIN; 150 return NGX_AGAIN;
180 } 151 }
181 152
182 153
183 static void *ngx_http_write_filter_create_conf(ngx_conf_t *cf)
184 {
185 ngx_http_write_filter_conf_t *conf;
186
187 ngx_test_null(conf,
188 ngx_palloc(cf->pool, sizeof(ngx_http_write_filter_conf_t)),
189 NULL);
190
191 conf->postpone_output = NGX_CONF_UNSET_SIZE;
192 conf->limit_rate = NGX_CONF_UNSET_SIZE;
193
194 return conf;
195 }
196
197
198 static char *ngx_http_write_filter_merge_conf(ngx_conf_t *cf,
199 void *parent, void *child)
200 {
201 ngx_http_write_filter_conf_t *prev = parent;
202 ngx_http_write_filter_conf_t *conf = child;
203
204 ngx_conf_merge_size_value(conf->postpone_output, prev->postpone_output,
205 1460);
206
207 ngx_conf_merge_size_value(conf->limit_rate, prev->limit_rate, 0);
208
209 return NULL;
210 }
211
212
213 static ngx_int_t ngx_http_write_filter_init(ngx_cycle_t *cycle) 154 static ngx_int_t ngx_http_write_filter_init(ngx_cycle_t *cycle)
214 { 155 {
215 ngx_http_top_body_filter = ngx_http_write_filter; 156 ngx_http_top_body_filter = ngx_http_write_filter;
216 157
217 return NGX_OK; 158 return NGX_OK;