comparison src/http/ngx_http_filter.c @ 3:34a521b1a148

nginx-0.0.1-2002-08-20-18:48:28 import
author Igor Sysoev <igor@sysoev.ru>
date Tue, 20 Aug 2002 14:48:28 +0000
parents
children c5f071d376e5
comparison
equal deleted inserted replaced
2:ffffe1499bce 3:34a521b1a148
1
2
3
4 ngx_http_module_t ngx_http_filter_module;
5
6
7 /* STUB */
8 static ngx_http_filter_ctx_t module_ctx;
9
10 void ngx_http_filter_init()
11 {
12 module_ctx.buffer_output = 10240;
13 module_ctx.out = NULL;
14 module_ctx.next_filter = ngx_http_write_filter;
15
16 ngx_http_filter_module.ctx = &module_ctx;
17 }
18 /* */
19
20
21 /*
22 int ngx_http_filter(ngx_http_request_t *r, ngx_chain_t *in)
23 */
24
25 /*
26 flags NGX_HUNK_RECYCLED, NGX_HUNK_FLUSH, NGX_HUNK_LAST
27 */
28
29 int ngx_http_filter(ngx_http_request_t *r, ngx_hunk_t *hunk)
30 {
31 enum { NO = 0, COPY, FILE } temp;
32
33 ngx_http_write_ctx_t *ctx;
34
35 ctx = (ngx_http_filter_ctx_t *)
36 ngx_get_module_ctx(r->main ? r->main : r,
37 &ngx_http_filter_module);
38
39
40
41 if (hunk == NULL)
42 if (in == NULL)
43 next_filter(NULL);
44 else
45
46
47
48
49
50 if (hunk != NULL)
51 if (in == NULL)
52 if (temp == NO)
53 fast_chain = hunk;
54 next_filter(fast_chain);
55 else
56 if (ctx->hunk busy)
57 add hunk to ctx->in
58 next_filter(NULL);
59 else
60 if (hunk > ctx->hunk)
61 copy hunk part to ctx->hunk
62 add hunk to ctx->in
63 else
64 copy hunk to ctx->hunk
65 fast_chain = ctx->hunk
66 next_filter(fast_chain);
67
68 else /* in != NULL */
69 add hunk to ctx->in
70
71
72
73
74
75
76
77
78
79 if ((r->filter & NGX_FILT_NEED_IN_MEMORY) && (hunk->type & NGX_HUNK_FILE))
80 temp = FILE;
81
82 else if ((r->filter & NGX_FILT_NEED_TEMP)
83 && (hunk->type & NGX_HUNK_MEMORY|NGX_HUNK_MMAP))
84 temp = COPY;
85
86 if (temp) {
87 size = hunk->last.mem - hunk->pos.mem;
88
89 if (hunk->type & NGX_HUNK_LAST) {
90 if (size > ctx->hunk_size)
91 size = ctx->hunk_size;
92
93 hunk_size = size;
94
95 } else {
96 hunk_size = ctx->hunk_size;
97 }
98 }
99
100 if (!ctx->hunk)
101 ngx_test_null(ctx->hunk, ngx_create_temp_hunk(hunk_size), ...);
102
103 if (temp == FILE) {
104 n = ngx_read_file(hunk->fd, ctx->hunk->pos.mem, size);
105
106 if (n == -1) {
107 ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_errno,
108 ngx_read_file_n " failed for client");
109 return -1;
110
111 } else {
112 ngx_assert((n == size), /* void */ ; ,
113 r->connection->log, 0,
114 ngx_read_file_n " reads only %d of %d for client",
115 n, size);
116 }
117
118 hunk->pos.mem += n;
119 ctx->hunk->last.mem += n;
120
121 } else if (temp == COPY) {
122 ngx_memcpy(ctx->hunk->pos.mem, hunk->pos.mem, size);
123
124 hunk->pos.mem += size;
125 ctx->hunk->last.mem += size;
126 }
127
128
129
130
131
132
133 /* if no hunk is passed and there is no our hunk
134 or our hunk is still busy then call next filter */
135 if (hunk == NULL
136 && (ctx->hunk == NULL
137 || ((ctx->hunk != NULL)
138 && (ctx->hunk->pos.mem < ctx->hunk->last.mem))
139 )
140 )
141 ctx->next_filter(r, NULL);
142 }
143
144 /* hunk != NULL || ctx->hunk->pos.mem == ctx->hunk->last.mem */
145
146 /* find last link of saved chain */
147 prev = &ctx->out;
148 for (ch = ctx->out; ch; ch = ch->next) {
149 prev = &ch->next;
150 }
151
152 if hunk
153 if need our hunk - alloc it and add to our queue
154 else add hunk to our queue
155
156 /*
157 size += ch->hunk->last.file - ch->hunk->pos.file;
158
159 ngx_log_debug(r->connection->log, "old chunk: %x %qx %qd" _
160 ch->hunk->type _ ch->hunk->pos.file _
161 ch->hunk->last.file - ch->hunk->pos.file);
162
163 if (ch->hunk->type & NGX_HUNK_FLUSH)
164 flush = size;
165
166 if (ch->hunk->type & NGX_HUNK_LAST)
167 last = 1;
168 }
169 */
170
171 /* add new chain to existent one */
172 for (/* void */; in; in = in->next) {
173 ngx_test_null(ch, ngx_palloc(r->pool, sizeof(ngx_chain_t)),
174 NGX_HTTP_FILTER_ERROR);
175
176 ch->hunk = h;
177 ch->next = NULL;
178 *prev = ch;
179 prev = &ch->next;
180 size += ch->hunk->last.file - ch->hunk->pos.file;
181
182 ngx_log_debug(r->connection->log, "new chunk: %x %qx %qd" _
183 ch->hunk->type _ ch->hunk->pos.file _
184 ch->hunk->last.file - ch->hunk->pos.file);
185
186 if (ch->hunk->type & NGX_HUNK_FLUSH)
187 flush = size;
188
189 if (ch->hunk->type & NGX_HUNK_LAST)
190 last = 1;
191 }
192
193
194
195
196
197 /*
198 !(HAVE_SENDFILE) == NGX_FILT_NEED_IN_MEMORY
199 */
200
201 if ((r->filter & NGX_FILT_NEED_IN_MEMORY) && (h->type & NGX_HUNK_FILE)) {
202
203 size = h->last.mem - h->pos.mem;
204 if (size > ctx->hunk_size)
205 size = ctx->hunk_size;
206
207 if (!ctx->hunk)
208 ngx_test_null(ctx->hunk, ngx_create_temp_hunk(size), ...);
209
210 ngx_read_file(h->fd, ctx->hunk->pos.mem, size);
211
212 h->hunk->pos.mem += size;
213 }
214
215 if ((r->filter & NGX_FILT_NEED_TEMP)
216 && (h->type & NGX_HUNK_MEMORY|NGX_HUNK_MMAP))
217 {
218 size = h->last.mem - h->pos.mem;
219 if (size > ctx->hunk_size)
220 size = ctx->hunk_size;
221
222 if (!ctx->hunk)
223 ngx_test_null(ctx->hunk, ngx_create_temp_hunk(size), ...);
224
225 ngx_memcpy(ctx->hunk->pos.mem, h->pos.mem, size);
226
227 h->hunk->pos.mem += size;
228 }
229
230
231
232
233
234
235 rc = ctx->next_filter(r, ch);
236
237 /* STUB */
238 rc = ngx_http_write_filter(r, ch);
239 }