comparison src/http/ngx_http_output_filter.c @ 67:5a7d1aaa1618

nginx-0.0.1-2003-03-11-23:38:13 import
author Igor Sysoev <igor@sysoev.ru>
date Tue, 11 Mar 2003 20:38:13 +0000
parents 8ccba41a678e
children e43f406e4525
comparison
equal deleted inserted replaced
66:4876cd4a36bb 67:5a7d1aaa1618
24 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, 24 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
25 ngx_conf_set_size_slot, 25 ngx_conf_set_size_slot,
26 NGX_HTTP_LOC_CONF_OFFSET, 26 NGX_HTTP_LOC_CONF_OFFSET,
27 offsetof(ngx_http_output_filter_conf_t, hunk_size)}, 27 offsetof(ngx_http_output_filter_conf_t, hunk_size)},
28 28
29 {ngx_string(""), 0, NULL, 0, 0} 29 {ngx_null_string, 0, NULL, 0, 0}
30 }; 30 };
31 31
32 32
33 static ngx_http_module_t ngx_http_output_filter_module_ctx = { 33 static ngx_http_module_t ngx_http_output_filter_module_ctx = {
34 NGX_HTTP_MODULE, 34 NGX_HTTP_MODULE,
61 #define next_filter ngx_http_output_filter_module_ctx.next_output_body_filter 61 #define next_filter ngx_http_output_filter_module_ctx.next_output_body_filter
62 62
63 #define need_to_copy(r, hunk) \ 63 #define need_to_copy(r, hunk) \
64 (((r->filter & NGX_HTTP_FILTER_NEED_IN_MEMORY) \ 64 (((r->filter & NGX_HTTP_FILTER_NEED_IN_MEMORY) \
65 && (hunk->type & NGX_HUNK_FILE)) \ 65 && (hunk->type & NGX_HUNK_FILE)) \
66 || ((r->filter & NGX_HTTP_FILTER_NEED_TEMP) \ 66 || ((r->filter & NGX_HTTP_FILTER_NEED_TEMP) \
67 && (hunk->type & (NGX_HUNK_MEMORY|NGX_HUNK_MMAP)))) 67 && (hunk->type & (NGX_HUNK_MEMORY|NGX_HUNK_MMAP))))
68 68
69 69
70 int ngx_http_output_filter(ngx_http_request_t *r, ngx_hunk_t *hunk) 70 int ngx_http_output_filter(ngx_http_request_t *r, ngx_hunk_t *hunk)
71 { 71 {
72 int rc; 72 int rc;
79 ngx_http_get_module_ctx(r->main ? r->main : r, 79 ngx_http_get_module_ctx(r->main ? r->main : r,
80 ngx_http_output_filter_module); 80 ngx_http_output_filter_module);
81 81
82 if (ctx == NULL) { 82 if (ctx == NULL) {
83 ngx_http_create_ctx(r, ctx, ngx_http_output_filter_module, 83 ngx_http_create_ctx(r, ctx, ngx_http_output_filter_module,
84 sizeof(ngx_http_output_filter_ctx_t)); 84 sizeof(ngx_http_output_filter_ctx_t), NGX_ERROR);
85 } 85 }
86 86
87 /* the short path for the case when the chain ctx->incoming is empty 87 /* the short path for the case when the chain ctx->incoming is empty
88 and there is no hunk or the hunk does not require the copy */ 88 and there is no hunk or the hunk does not require the copy */
89 if (ctx->incoming == NULL) { 89 if (ctx->incoming == NULL) {
122 conf = (ngx_http_output_filter_conf_t *) 122 conf = (ngx_http_output_filter_conf_t *)
123 ngx_http_get_module_loc_conf(r->main ? r->main : r, 123 ngx_http_get_module_loc_conf(r->main ? r->main : r,
124 ngx_http_output_filter_module); 124 ngx_http_output_filter_module);
125 125
126 if (hunk->type & NGX_HUNK_LAST) { 126 if (hunk->type & NGX_HUNK_LAST) {
127 size = hunk->last.mem - hunk->pos.mem; 127 if (hunk->type & NGX_HUNK_IN_MEMORY) {
128 size = hunk->last - hunk->pos;
129 } else {
130 size = hunk->file_last - hunk->file_pos;
131 }
132
128 if (size > conf->hunk_size) { 133 if (size > conf->hunk_size) {
129 size = conf->hunk_size; 134 size = conf->hunk_size;
130 } 135 }
131 136
132 } else { 137 } else {
138 NGX_ERROR); 143 NGX_ERROR);
139 ctx->hunk->type |= NGX_HUNK_RECYCLED; 144 ctx->hunk->type |= NGX_HUNK_RECYCLED;
140 145
141 146
142 /* our hunk is still busy */ 147 /* our hunk is still busy */
143 } else if (ctx->hunk->pos.mem < ctx->hunk->last.mem) { 148 } else if (ctx->hunk->pos < ctx->hunk->last) {
144 rc = next_filter(r, NULL); 149 rc = next_filter(r, NULL);
145 150
146 if (rc == NGX_ERROR || rc == NGX_AGAIN) { 151 if (rc == NGX_ERROR || rc == NGX_AGAIN) {
147 return rc; 152 return rc;
148 } 153 }
149 154
150 /* NGX_OK */ 155 /* NGX_OK */
151 /* set our hunk free */ 156 /* set our hunk free */
152 ctx->hunk->pos.mem = ctx->hunk->last.mem = ctx->hunk->start; 157 ctx->hunk->pos = ctx->hunk->last = ctx->hunk->start;
153 } 158 }
154 159
155 #if (NGX_SUPPRESS_WARN) 160 #if (NGX_SUPPRESS_WARN)
156 le = NULL; 161 le = NULL;
157 #endif 162 #endif
207 return rc; 212 return rc;
208 } 213 }
209 214
210 /* NGX_OK */ 215 /* NGX_OK */
211 /* set our hunk free */ 216 /* set our hunk free */
212 ctx->hunk->pos.mem = ctx->hunk->last.mem = ctx->hunk->start; 217 ctx->hunk->pos = ctx->hunk->last = ctx->hunk->start;
213 218
214 /* repeat until we will have copied the whole first hunk from 219 /* repeat until we will have copied the whole first hunk from
215 the chain ctx->incoming */ 220 the chain ctx->incoming */
216 } while (ctx->incoming->hunk->pos.mem < ctx->incoming->hunk->last.mem); 221
222 if (ctx->incoming->hunk->type & NGX_HUNK_IN_MEMORY) {
223 size = ctx->incoming->hunk->last - ctx->incoming->hunk->pos;
224
225 } else {
226 size = ctx->incoming->hunk->file_last
227 - ctx->incoming->hunk->file_pos;
228 }
229
230 } while (size);
217 231
218 /* delete the completed hunk from the incoming chain */ 232 /* delete the completed hunk from the incoming chain */
219 ctx->incoming = ctx->incoming->next; 233 ctx->incoming = ctx->incoming->next;
220 234
221 /* repeat until we will have processed the whole chain ctx->incoming */ 235 /* repeat until we will have processed the whole chain ctx->incoming */
227 241
228 static int ngx_http_output_filter_copy_hunk(ngx_hunk_t *dst, ngx_hunk_t *src) 242 static int ngx_http_output_filter_copy_hunk(ngx_hunk_t *dst, ngx_hunk_t *src)
229 { 243 {
230 ssize_t n, size; 244 ssize_t n, size;
231 245
232 size = src->last.mem - src->pos.mem; 246 if (src->type & NGX_HUNK_IN_MEMORY) {
233 if (size > (dst->end - dst->pos.mem)) { 247 size = src->last - src->pos;
234 size = dst->end - dst->pos.mem; 248 } else {
235 } 249 size = src->file_last - src->file_pos;
236 250 }
237 if (src->type & NGX_HUNK_FILE) { 251
238 n = ngx_read_file(src->file, dst->pos.mem, size, src->pos.file); 252 if (size > (dst->end - dst->pos)) {
253 size = dst->end - dst->pos;
254 }
255
256 if (src->type & NGX_HUNK_IN_MEMORY) {
257 ngx_memcpy(src->pos, dst->pos, size);
258 src->pos += size;
259 dst->last += size;
260
261 if (src->type & NGX_HUNK_FILE) {
262 src->file_pos += n;
263 }
264
265 if (src->type & NGX_HUNK_LAST && src->pos == src->last) {
266 dst->type |= NGX_HUNK_LAST;
267 }
268
269 } else {
270 n = ngx_read_file(src->file, dst->pos, size, src->file_pos);
239 271
240 if (n == NGX_ERROR) { 272 if (n == NGX_ERROR) {
241 return n; 273 return n;
242 } 274 }
243 275
254 if (n == 0) { 286 if (n == 0) {
255 return NGX_ERROR; 287 return NGX_ERROR;
256 } 288 }
257 } 289 }
258 290
259 src->pos.mem += n; 291 src->file_pos += n;
260 dst->last.mem += n; 292 dst->last += n;
261 293
262 } else { 294 if (src->type & NGX_HUNK_LAST && src->file_pos == src->file_last) {
263 ngx_memcpy(src->pos.mem, dst->pos.mem, size); 295 dst->type |= NGX_HUNK_LAST;
264 src->pos.mem += size; 296 }
265 dst->last.mem += size;
266 }
267
268 if (src->type & NGX_HUNK_LAST && src->pos.mem == src->last.mem) {
269 dst->type |= NGX_HUNK_LAST;
270 } 297 }
271 298
272 return NGX_OK; 299 return NGX_OK;
273 } 300 }
274 301