comparison src/http/ngx_http_write_filter.c @ 71:59229033ae93

nginx-0.0.1-2003-04-08-19:40:10 import
author Igor Sysoev <igor@sysoev.ru>
date Tue, 08 Apr 2003 15:40:10 +0000
parents e43f406e4525
children 869b10be682f
comparison
equal deleted inserted replaced
70:e320bf51c4e3 71:59229033ae93
14 14
15 15
16 static void *ngx_http_write_filter_create_conf(ngx_pool_t *pool); 16 static void *ngx_http_write_filter_create_conf(ngx_pool_t *pool);
17 static char *ngx_http_write_filter_merge_conf(ngx_pool_t *pool, 17 static char *ngx_http_write_filter_merge_conf(ngx_pool_t *pool,
18 void *parent, void *child); 18 void *parent, void *child);
19 static void ngx_http_write_filter_init(ngx_pool_t *pool, 19 static int ngx_http_write_filter_init(ngx_pool_t *pool);
20 ngx_http_conf_filter_t *cf);
21 20
22 21
23 static ngx_command_t ngx_http_write_filter_commands[] = { 22 static ngx_command_t ngx_http_write_filter_commands[] = {
24 23
25 {ngx_string("write_buffer"), 24 {ngx_string("buffer_output"),
26 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, 25 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
27 ngx_conf_set_size_slot, 26 ngx_conf_set_size_slot,
28 NGX_HTTP_LOC_CONF_OFFSET, 27 NGX_HTTP_LOC_CONF_OFFSET,
29 offsetof(ngx_http_write_filter_conf_t, buffer_output)}, 28 offsetof(ngx_http_write_filter_conf_t, buffer_output)},
30 29
31 {ngx_string(""), 0, NULL, 0, 0} 30 {ngx_null_string, 0, NULL, 0, 0}
32 }; 31 };
33 32
34 33
35 ngx_http_module_t ngx_http_write_filter_module_ctx = { 34 ngx_http_module_t ngx_http_write_filter_module_ctx = {
35 NGX_HTTP_MODULE,
36
36 NULL, /* create server config */ 37 NULL, /* create server config */
37 NULL, /* init server config */ 38 NULL, /* init server config */
38 39
39 ngx_http_write_filter_create_conf, /* create location config */ 40 ngx_http_write_filter_create_conf, /* create location config */
40 ngx_http_write_filter_merge_conf, /* merge location config */ 41 ngx_http_write_filter_merge_conf /* merge location config */
41
42 ngx_http_write_filter_init /* init filters */
43 }; 42 };
44 43
45 44
46 ngx_module_t ngx_http_write_filter_module = { 45 ngx_module_t ngx_http_write_filter_module = {
47 0, /* module index */ 46 0, /* module index */
48 &ngx_http_write_filter_module_ctx, /* module context */ 47 &ngx_http_write_filter_module_ctx, /* module context */
49 ngx_http_write_filter_commands, /* module directives */ 48 ngx_http_write_filter_commands, /* module directives */
50 NGX_HTTP_MODULE_TYPE, /* module type */ 49 NGX_HTTP_MODULE_TYPE, /* module type */
51 NULL /* init module */ 50 ngx_http_write_filter_init /* init module */
52 }; 51 };
53 52
54 53
55 int ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in) 54 int ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in)
56 { 55 {
57 int last; 56 int last;
58 off_t size, flush; 57 off_t size, flush;
59 ngx_chain_t *ce, **le, *chain; 58 ngx_chain_t *ce, **le, *chain;
60 ngx_http_write_filter_ctx_t *ctx; 59 ngx_http_write_filter_ctx_t *ctx;
61 ngx_http_write_filter_conf_t *conf; 60 ngx_http_write_filter_conf_t *conf;
62 61
63 62
64 ctx = (ngx_http_write_filter_ctx_t *) 63 ctx = (ngx_http_write_filter_ctx_t *)
65 ngx_http_get_module_ctx(r->main ? r->main : r, 64 ngx_http_get_module_ctx(r->main ? r->main : r,
66 ngx_http_write_filter_module); 65 ngx_http_write_filter_module_ctx);
67 if (ctx == NULL) { 66 if (ctx == NULL) {
68 ngx_http_create_ctx(r, ctx, ngx_http_write_filter_module, 67 ngx_http_create_ctx(r, ctx, ngx_http_write_filter_module_ctx,
69 sizeof(ngx_http_write_filter_ctx_t), NGX_ERROR); 68 sizeof(ngx_http_write_filter_ctx_t), NGX_ERROR);
70 } 69 }
71 70
72 size = flush = 0; 71 size = flush = 0;
73 last = 0; 72 last = 0;
74 le = &ctx->out; 73 le = &ctx->out;
75 74
76 /* find the size, the flush point and the last entry of saved chain */ 75 /* find the size, the flush point and the last entry of the saved chain */
76
77 for (ce = ctx->out; ce; ce = ce->next) { 77 for (ce = ctx->out; ce; ce = ce->next) {
78 le = &ce->next; 78 le = &ce->next;
79 79
80 if (ce->hunk->type & NGX_HUNK_IN_MEMORY) { 80 if (ce->hunk->type & NGX_HUNK_IN_MEMORY) {
81 size += ce->hunk->last - ce->hunk->pos; 81 size += ce->hunk->last - ce->hunk->pos;
91 last = 1; 91 last = 1;
92 } 92 }
93 } 93 }
94 94
95 /* add the new chain to the existent one */ 95 /* add the new chain to the existent one */
96
96 for (/* void */; in; in = in->next) { 97 for (/* void */; in; in = in->next) {
97 ngx_test_null(ce, ngx_palloc(r->pool, sizeof(ngx_chain_t)), NGX_ERROR); 98 ngx_test_null(ce, ngx_palloc(r->pool, sizeof(ngx_chain_t)), NGX_ERROR);
98 99
99 ce->hunk = in->hunk; 100 ce->hunk = in->hunk;
100 ce->next = NULL; 101 ce->next = NULL;
116 } 117 }
117 } 118 }
118 119
119 conf = (ngx_http_write_filter_conf_t *) 120 conf = (ngx_http_write_filter_conf_t *)
120 ngx_http_get_module_loc_conf(r->main ? r->main : r, 121 ngx_http_get_module_loc_conf(r->main ? r->main : r,
121 ngx_http_write_filter_module); 122 ngx_http_write_filter_module_ctx);
122 123
123 #if (NGX_DEBUG_WRITE_FILTER) 124 #if (NGX_DEBUG_WRITE_FILTER)
124 ngx_log_debug(r->connection->log, "write filter: last:%d flush:%d" _ 125 ngx_log_debug(r->connection->log, "write filter: last:%d flush:%d" _
125 last _ flush); 126 last _ flush);
126 #endif 127 #endif
127 128
128 /* avoid the output if there is no last hunk, no flush point and 129 /* avoid the output if there is no last hunk, no flush point and
129 size of the hunks is smaller then 'write_buffer' */ 130 size of the hunks is smaller then "buffer_output" */
131
130 if (!last && flush == 0 && size < conf->buffer_output) { 132 if (!last && flush == 0 && size < conf->buffer_output) {
131 return NGX_OK; 133 return NGX_OK;
132 } 134 }
133 135
134 chain = ngx_write_chain(r->connection, ctx->out, flush); 136 chain = ngx_write_chain(r->connection, ctx->out, flush);
147 return NGX_OK; 149 return NGX_OK;
148 150
149 } else { 151 } else {
150 return NGX_AGAIN; 152 return NGX_AGAIN;
151 } 153 }
152 }
153
154
155 static void ngx_http_write_filter_init(ngx_pool_t *pool,
156 ngx_http_conf_filter_t *cf)
157 {
158 cf->output_body_filter = ngx_http_write_filter;
159 } 154 }
160 155
161 156
162 static void *ngx_http_write_filter_create_conf(ngx_pool_t *pool) 157 static void *ngx_http_write_filter_create_conf(ngx_pool_t *pool)
163 { 158 {
184 ngx_conf_size_merge(conf->buffer_output, prev->buffer_output, 1460); 179 ngx_conf_size_merge(conf->buffer_output, prev->buffer_output, 1460);
185 180
186 return NULL; 181 return NULL;
187 } 182 }
188 183
184
185 static int ngx_http_write_filter_init(ngx_pool_t *pool)
186 {
187 ngx_http_top_body_filter = ngx_http_write_filter;
188
189 return NGX_OK;
190 }