Mercurial > hg > nginx
comparison src/http/ngx_http_output_filter.c @ 9:6f58641241bb
nginx-0.0.1-2002-09-07-14:14:25 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Sat, 07 Sep 2002 10:14:25 +0000 |
parents | 708f8bb772ec |
children | 4f3879d9b6f6 |
comparison
equal
deleted
inserted
replaced
8:708f8bb772ec | 9:6f58641241bb |
---|---|
6 #include <ngx_hunk.h> | 6 #include <ngx_hunk.h> |
7 #include <ngx_http.h> | 7 #include <ngx_http.h> |
8 #include <ngx_http_output_filter.h> | 8 #include <ngx_http_output_filter.h> |
9 | 9 |
10 | 10 |
11 static int ngx_http_output_filter_copy_hunk(ngx_hunk_t *dst, ngx_hunk_t *src); | |
12 static int ngx_http_output_filter_init( | |
13 int (*next_filter)(ngx_http_request_t *r, ngx_chain_t *ch)); | |
14 static void *ngx_http_output_filter_create_conf(ngx_pool_t *pool); | |
15 static void *ngx_http_output_filter_set_hunk_size(ngx_pool_t *pool, void *conf, | |
16 char *size); | |
17 | |
18 | |
19 ngx_http_module_t ngx_http_output_filter_module = { | |
20 NGX_HTTP_MODULE | |
21 }; | |
22 | |
23 | |
24 static int (*ngx_http_output_next_filter)(ngx_http_request_t *r, | |
25 ngx_chain_t *ch); | |
26 | |
11 /* STUB */ | 27 /* STUB */ |
12 int ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in); | 28 int ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *ch); |
13 | 29 |
14 | 30 int ngx_http_output_filter_stub_init(ngx_pool_t *pool, void *loc_conf) |
15 | 31 { |
16 static int ngx_http_output_filter_copy_hunk(ngx_hunk_t *dst, ngx_hunk_t *src); | 32 ngx_http_output_filter_conf_t *conf; |
17 | 33 |
18 ngx_http_module_t ngx_http_output_filter_module; | 34 ngx_http_output_filter_init(ngx_http_write_filter); |
19 | 35 conf = ngx_http_output_filter_create_conf(pool); |
20 | 36 ngx_http_output_filter_set_hunk_size(pool, conf, "32"); |
21 /* STUB */ | 37 |
22 static ngx_http_output_filter_ctx_t module_ctx; | 38 loc_conf = conf; |
23 | |
24 void ngx_http_output_filter_init() | |
25 { | |
26 module_ctx.hunk_size = 32 * 1024; | |
27 module_ctx.out.hunk = NULL; | |
28 module_ctx.out.next = NULL; | |
29 module_ctx.next_filter = ngx_http_write_filter; | |
30 | |
31 ngx_http_output_filter_module.ctx = &module_ctx; | |
32 } | 39 } |
33 /* */ | 40 /* */ |
34 | 41 |
35 | 42 |
36 /* | |
37 flags NGX_HUNK_RECYCLED, NGX_HUNK_FLUSH, NGX_HUNK_LAST | |
38 */ | |
39 | |
40 int ngx_http_output_filter(ngx_http_request_t *r, ngx_hunk_t *hunk) | 43 int ngx_http_output_filter(ngx_http_request_t *r, ngx_hunk_t *hunk) |
41 { | 44 { |
42 int rc, first; | 45 int rc, once; |
43 size_t size; | 46 size_t size; |
44 ssize_t n; | 47 ssize_t n; |
45 ngx_chain_t *ce; | 48 ngx_chain_t *ce; |
46 ngx_http_output_filter_ctx_t *ctx; | 49 ngx_http_output_filter_ctx_t *ctx; |
50 ngx_http_output_filter_conf_t *conf; | |
47 | 51 |
48 ctx = (ngx_http_output_filter_ctx_t *) | 52 ctx = (ngx_http_output_filter_ctx_t *) |
49 ngx_get_module_ctx(r->main ? r->main : r, | 53 ngx_get_module_ctx(r->main ? r->main : r, |
50 &ngx_http_output_filter_module); | 54 ngx_http_output_filter_module); |
55 | |
56 if (ctx == NULL) { | |
57 ngx_test_null(ctx, | |
58 ngx_pcalloc(r->pool, sizeof(ngx_http_output_filter_ctx_t)), | |
59 NGX_ERROR); | |
60 | |
61 ctx->next_filter = ngx_http_output_next_filter; | |
62 } | |
63 | |
64 ngx_log_debug(r->connection->log, "HUNK: x%x CTX-IN: x%x CTX->HUNK: x%x" _ | |
65 hunk _ ctx->in _ ctx->hunk); | |
51 | 66 |
52 if (hunk && (hunk->type & NGX_HUNK_LAST)) | 67 if (hunk && (hunk->type & NGX_HUNK_LAST)) |
53 ctx->last = 1; | 68 ctx->last = 1; |
54 | 69 |
55 first = 1; | 70 for (once = 1; once || ctx->in; once--) { |
56 | |
57 while (first || ctx->in) { | |
58 | 71 |
59 /* input chain is not empty */ | 72 /* input chain is not empty */ |
60 if (ctx->in) { | 73 if (ctx->in) { |
61 | 74 |
62 /* add hunk to input chain */ | 75 /* add hunk to input chain */ |
63 if (first && hunk) { | 76 if (once && hunk) { |
64 for (ce = ctx->in; ce->next; ce = ce->next) | 77 for (ce = ctx->in; ce->next; ce = ce->next) |
65 /* void */ ; | 78 /* void */ ; |
66 | 79 |
67 ngx_add_hunk_to_chain(ce->next, hunk, r->pool, NGX_ERROR); | 80 ngx_add_hunk_to_chain(ce->next, hunk, r->pool, NGX_ERROR); |
68 } | 81 } |
69 | |
70 first = 0; | |
71 | 82 |
72 /* our hunk is still busy */ | 83 /* our hunk is still busy */ |
73 if (ctx->hunk->pos.mem < ctx->hunk->last.mem) { | 84 if (ctx->hunk->pos.mem < ctx->hunk->last.mem) { |
74 rc = ctx->next_filter(r, NULL); | 85 rc = ctx->next_filter(r, NULL); |
75 | 86 |
120 return rc; | 131 return rc; |
121 | 132 |
122 /* input chain is empty */ | 133 /* input chain is empty */ |
123 } else { | 134 } else { |
124 | 135 |
125 first = 0; | |
126 | |
127 if (hunk == NULL) { | 136 if (hunk == NULL) { |
128 rc = ctx->next_filter(r, NULL); | 137 rc = ctx->next_filter(r, NULL); |
129 | 138 |
130 } else { | 139 } else { |
131 | 140 |
145 | 154 |
146 } else { | 155 } else { |
147 if (ctx->hunk == NULL) { | 156 if (ctx->hunk == NULL) { |
148 | 157 |
149 if (hunk->type & NGX_HUNK_LAST) { | 158 if (hunk->type & NGX_HUNK_LAST) { |
159 | |
160 conf = (ngx_http_output_filter_conf_t *) | |
161 ngx_get_module_loc_conf(r->main ? r->main : r, | |
162 ngx_http_output_filter_module); | |
163 | |
150 size = hunk->last.mem - hunk->pos.mem; | 164 size = hunk->last.mem - hunk->pos.mem; |
151 if (size > ctx->hunk_size) | 165 if (size > conf->hunk_size) |
152 size = ctx->hunk_size; | 166 size = conf->hunk_size; |
153 | 167 |
154 } else { | 168 } else { |
155 size = ctx->hunk_size; | 169 size = conf->hunk_size; |
156 } | 170 } |
157 | 171 |
158 ngx_test_null(ctx->hunk, | 172 ngx_test_null(ctx->hunk, |
159 ngx_create_temp_hunk(r->pool, size, | 173 ngx_create_temp_hunk(r->pool, size, |
160 50, 50), | 174 50, 50), |
249 dst->last.mem += size; | 263 dst->last.mem += size; |
250 } | 264 } |
251 | 265 |
252 return NGX_OK; | 266 return NGX_OK; |
253 } | 267 } |
268 | |
269 | |
270 static int ngx_http_output_filter_init( | |
271 int (*next_filter)(ngx_http_request_t *r, ngx_chain_t *ch)) | |
272 { | |
273 ngx_http_output_next_filter = next_filter; | |
274 | |
275 return NGX_OK; | |
276 } | |
277 | |
278 static void *ngx_http_output_filter_create_conf(ngx_pool_t *pool) | |
279 { | |
280 ngx_http_output_filter_conf_t *conf; | |
281 | |
282 ngx_test_null(conf, | |
283 ngx_pcalloc(pool, sizeof(ngx_http_output_filter_conf_t)), | |
284 NULL); | |
285 | |
286 conf->hunk_size = 16384; | |
287 } | |
288 | |
289 static void *ngx_http_output_filter_set_hunk_size(ngx_pool_t *pool, void *conf, | |
290 char *size) | |
291 { | |
292 ngx_http_output_filter_conf_t *cf = (ngx_http_output_filter_conf_t *) conf; | |
293 | |
294 cf->hunk_size = atoi(size); | |
295 if (cf->hunk_size <= 0) | |
296 return "Error"; | |
297 | |
298 cf->hunk_size *= 1024; | |
299 return NULL; | |
300 } |