0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_event.h>
|
|
10 #include <ngx_event_pipe.h>
|
|
11
|
|
12
|
|
13 static ngx_int_t ngx_event_pipe_read_upstream(ngx_event_pipe_t *p);
|
|
14 static ngx_int_t ngx_event_pipe_write_to_downstream(ngx_event_pipe_t *p);
|
|
15
|
|
16 static ngx_int_t ngx_event_pipe_write_chain_to_temp_file(ngx_event_pipe_t *p);
|
16
|
17 static ngx_inline void ngx_event_pipe_remove_shadow_links(ngx_buf_t *buf);
|
|
18 static ngx_inline void ngx_event_pipe_free_shadow_raw_buf(ngx_chain_t **free,
|
0
|
19 ngx_buf_t *buf);
|
|
20 static ngx_int_t ngx_event_pipe_drain_chains(ngx_event_pipe_t *p);
|
|
21
|
|
22
|
|
23 ngx_int_t ngx_event_pipe(ngx_event_pipe_t *p, int do_write)
|
|
24 {
|
|
25 u_int flags;
|
|
26 ngx_event_t *rev, *wev;
|
|
27
|
|
28 for ( ;; ) {
|
|
29 if (do_write) {
|
32
|
30 p->log->action = "sending to client";
|
|
31
|
0
|
32 if (ngx_event_pipe_write_to_downstream(p) == NGX_ABORT) {
|
|
33 return NGX_ABORT;
|
|
34 }
|
|
35 }
|
|
36
|
|
37 p->read = 0;
|
|
38 p->upstream_blocked = 0;
|
|
39
|
32
|
40 p->log->action = "reading upstream";
|
|
41
|
0
|
42 if (ngx_event_pipe_read_upstream(p) == NGX_ABORT) {
|
|
43 return NGX_ABORT;
|
|
44 }
|
|
45
|
|
46 if (!p->read && !p->upstream_blocked) {
|
|
47 break;
|
|
48 }
|
|
49
|
|
50 do_write = 1;
|
|
51 }
|
|
52
|
|
53 if (p->upstream->fd != -1) {
|
|
54 rev = p->upstream->read;
|
|
55
|
|
56 flags = (rev->eof || rev->error) ? NGX_CLOSE_EVENT : 0;
|
|
57
|
|
58 if (ngx_handle_read_event(rev, flags) == NGX_ERROR) {
|
|
59 return NGX_ABORT;
|
|
60 }
|
|
61
|
|
62 if (rev->active) {
|
|
63 ngx_add_timer(rev, p->read_timeout);
|
|
64 }
|
|
65 }
|
|
66
|
|
67 if (p->downstream->fd != -1) {
|
|
68 wev = p->downstream->write;
|
2
|
69 if (ngx_handle_write_event(wev, p->send_lowat) == NGX_ERROR) {
|
0
|
70 return NGX_ABORT;
|
|
71 }
|
|
72
|
|
73 if (wev->active) {
|
|
74 ngx_add_timer(wev, p->send_timeout);
|
|
75 }
|
|
76 }
|
|
77
|
|
78 return NGX_OK;
|
|
79 }
|
|
80
|
|
81
|
32
|
82 static ngx_int_t ngx_event_pipe_read_upstream(ngx_event_pipe_t *p)
|
0
|
83 {
|
10
|
84 ssize_t n, size;
|
|
85 ngx_int_t rc;
|
0
|
86 ngx_buf_t *b;
|
56
|
87 ngx_chain_t *chain, *cl, *ln;
|
0
|
88
|
|
89 if (p->upstream_eof || p->upstream_error || p->upstream_done) {
|
|
90 return NGX_OK;
|
|
91 }
|
|
92
|
|
93 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
|
|
94 "pipe read upstream: %d", p->upstream->read->ready);
|
|
95
|
|
96 for ( ;; ) {
|
|
97
|
|
98 if (p->upstream_eof || p->upstream_error || p->upstream_done) {
|
|
99 break;
|
|
100 }
|
|
101
|
|
102 if (p->preread_bufs == NULL && !p->upstream->read->ready) {
|
|
103 break;
|
|
104 }
|
|
105
|
|
106 if (p->preread_bufs) {
|
|
107
|
|
108 /* use the pre-read bufs if they exist */
|
|
109
|
|
110 chain = p->preread_bufs;
|
|
111 p->preread_bufs = NULL;
|
|
112 n = p->preread_size;
|
|
113
|
|
114 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
|
10
|
115 "pipe preread: %z", n);
|
0
|
116
|
|
117 if (n) {
|
|
118 p->read = 1;
|
|
119 }
|
|
120
|
|
121 } else {
|
|
122
|
|
123 /*
|
|
124 * kqueue notifies about the end of file or a pending error.
|
|
125 * This test allows not to allocate a buf on these conditions
|
|
126 * and not to call ngx_recv_chain().
|
|
127 */
|
|
128
|
|
129 if (p->upstream->read->available == 0
|
|
130 && p->upstream->read->pending_eof)
|
|
131 {
|
|
132 p->upstream->read->ready = 0;
|
|
133 p->upstream->read->eof = 0;
|
|
134 p->upstream_eof = 1;
|
|
135 p->read = 1;
|
|
136
|
18
|
137 #if (NGX_HAVE_KQUEUE)
|
0
|
138 if (p->upstream->read->kq_errno) {
|
|
139 p->upstream->read->error = 1;
|
|
140 p->upstream_error = 1;
|
|
141 p->upstream_eof = 0;
|
|
142
|
|
143 ngx_log_error(NGX_LOG_ERR, p->log,
|
|
144 p->upstream->read->kq_errno,
|
|
145 "readv() failed");
|
|
146 }
|
|
147 #endif
|
|
148
|
|
149 break;
|
|
150 }
|
|
151
|
|
152 if (p->free_raw_bufs) {
|
|
153
|
|
154 /* use the free bufs if they exist */
|
|
155
|
|
156 chain = p->free_raw_bufs;
|
|
157 if (p->single_buf) {
|
|
158 p->free_raw_bufs = p->free_raw_bufs->next;
|
|
159 chain->next = NULL;
|
|
160 } else {
|
|
161 p->free_raw_bufs = NULL;
|
|
162 }
|
|
163
|
|
164 } else if (p->allocated < p->bufs.num) {
|
|
165
|
|
166 /* allocate a new buf if it's still allowed */
|
|
167
|
50
|
168 b = ngx_create_temp_buf(p->pool, p->bufs.size);
|
|
169 if (b == NULL) {
|
0
|
170 return NGX_ABORT;
|
|
171 }
|
|
172
|
|
173 p->allocated++;
|
|
174
|
50
|
175 chain = ngx_alloc_chain_link(p->pool);
|
|
176 if (chain == NULL) {
|
32
|
177 return NGX_ABORT;
|
|
178 }
|
|
179
|
|
180 chain->buf = b;
|
|
181 chain->next = NULL;
|
0
|
182
|
|
183 } else if (!p->cachable && p->downstream->write->ready) {
|
|
184
|
|
185 /*
|
|
186 * if the bufs are not needed to be saved in a cache and
|
|
187 * a downstream is ready then write the bufs to a downstream
|
|
188 */
|
|
189
|
|
190 p->upstream_blocked = 1;
|
|
191
|
|
192 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, p->log, 0,
|
|
193 "pipe downstream ready");
|
|
194
|
|
195 break;
|
|
196
|
|
197 } else if (p->cachable
|
|
198 || p->temp_file->offset < p->max_temp_file_size)
|
|
199 {
|
|
200
|
|
201 /*
|
32
|
202 * if it is allowed, then save some bufs from r->in
|
0
|
203 * to a temporary file, and add them to a r->out chain
|
|
204 */
|
|
205
|
|
206 rc = ngx_event_pipe_write_chain_to_temp_file(p);
|
|
207
|
|
208 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
|
10
|
209 "pipe temp offset: %O", p->temp_file->offset);
|
0
|
210
|
|
211 if (rc == NGX_AGAIN) {
|
|
212 if (ngx_event_flags & NGX_USE_LEVEL_EVENT
|
|
213 && p->upstream->read->active
|
|
214 && p->upstream->read->ready)
|
|
215 {
|
|
216 if (ngx_del_event(p->upstream->read, NGX_READ_EVENT, 0)
|
|
217 == NGX_ERROR)
|
|
218 {
|
|
219 return NGX_ABORT;
|
|
220 }
|
|
221 }
|
|
222 }
|
|
223
|
|
224 if (rc != NGX_OK) {
|
|
225 return rc;
|
|
226 }
|
|
227
|
|
228 chain = p->free_raw_bufs;
|
|
229 if (p->single_buf) {
|
|
230 p->free_raw_bufs = p->free_raw_bufs->next;
|
|
231 chain->next = NULL;
|
|
232 } else {
|
|
233 p->free_raw_bufs = NULL;
|
|
234 }
|
|
235
|
|
236 } else {
|
|
237
|
32
|
238 /* there are no bufs to read in */
|
0
|
239
|
|
240 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, p->log, 0,
|
|
241 "no pipe bufs to read in");
|
|
242
|
|
243 break;
|
|
244 }
|
|
245
|
|
246 n = ngx_recv_chain(p->upstream, chain);
|
|
247
|
|
248 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
|
10
|
249 "pipe recv chain: %z", n);
|
0
|
250
|
|
251 if (p->free_raw_bufs) {
|
|
252 chain->next = p->free_raw_bufs;
|
|
253 }
|
|
254 p->free_raw_bufs = chain;
|
|
255
|
|
256 if (n == NGX_ERROR) {
|
|
257 p->upstream_error = 1;
|
|
258 return NGX_ERROR;
|
|
259 }
|
|
260
|
|
261 if (n == NGX_AGAIN) {
|
|
262 if (p->single_buf) {
|
|
263 ngx_event_pipe_remove_shadow_links(chain->buf);
|
|
264 }
|
|
265
|
|
266 break;
|
|
267 }
|
|
268
|
|
269 p->read = 1;
|
|
270
|
|
271 if (n == 0) {
|
|
272 p->upstream_eof = 1;
|
|
273 break;
|
|
274 }
|
|
275 }
|
|
276
|
|
277 p->read_length += n;
|
|
278 cl = chain;
|
|
279
|
|
280 while (cl && n > 0) {
|
|
281
|
|
282 ngx_event_pipe_remove_shadow_links(cl->buf);
|
|
283
|
|
284 size = cl->buf->end - cl->buf->last;
|
|
285
|
|
286 if (n >= size) {
|
|
287 cl->buf->last = cl->buf->end;
|
|
288
|
|
289 /* STUB */ cl->buf->num = p->num++;
|
|
290
|
|
291 if (p->input_filter(p, cl->buf) == NGX_ERROR) {
|
|
292 return NGX_ABORT;
|
|
293 }
|
|
294
|
|
295 n -= size;
|
56
|
296 ln = cl;
|
0
|
297 cl = cl->next;
|
56
|
298 ngx_free_chain(p->pool, ln);
|
0
|
299
|
|
300 } else {
|
|
301 cl->buf->last += n;
|
|
302 n = 0;
|
|
303 }
|
|
304 }
|
|
305
|
|
306 p->free_raw_bufs = cl;
|
|
307 }
|
|
308
|
|
309 #if (NGX_DEBUG)
|
|
310
|
2
|
311 for (cl = p->busy; cl; cl = cl->next) {
|
32
|
312 ngx_log_debug8(NGX_LOG_DEBUG_EVENT, p->log, 0,
|
|
313 "pipe buf busy s:%d t:%d f:%d "
|
|
314 "%p, pos %p, size: %z "
|
|
315 "file: %O, size: %z",
|
|
316 (cl->buf->shadow ? 1 : 0),
|
|
317 cl->buf->temporary, cl->buf->in_file,
|
0
|
318 cl->buf->start, cl->buf->pos,
|
32
|
319 cl->buf->last - cl->buf->pos,
|
|
320 cl->buf->file_pos,
|
|
321 cl->buf->file_last - cl->buf->file_pos);
|
0
|
322 }
|
|
323
|
2
|
324 for (cl = p->out; cl; cl = cl->next) {
|
32
|
325 ngx_log_debug8(NGX_LOG_DEBUG_EVENT, p->log, 0,
|
|
326 "pipe buf out s:%d t:%d f:%d "
|
|
327 "%p, pos %p, size: %z "
|
|
328 "file: %O, size: %z",
|
|
329 (cl->buf->shadow ? 1 : 0),
|
|
330 cl->buf->temporary, cl->buf->in_file,
|
|
331 cl->buf->start, cl->buf->pos,
|
|
332 cl->buf->last - cl->buf->pos,
|
|
333 cl->buf->file_pos,
|
|
334 cl->buf->file_last - cl->buf->file_pos);
|
2
|
335 }
|
|
336
|
|
337 for (cl = p->in; cl; cl = cl->next) {
|
32
|
338 ngx_log_debug8(NGX_LOG_DEBUG_EVENT, p->log, 0,
|
|
339 "pipe buf in s:%d t:%d f:%d "
|
|
340 "%p, pos %p, size: %z "
|
|
341 "file: %O, size: %z",
|
|
342 (cl->buf->shadow ? 1 : 0),
|
|
343 cl->buf->temporary, cl->buf->in_file,
|
0
|
344 cl->buf->start, cl->buf->pos,
|
32
|
345 cl->buf->last - cl->buf->pos,
|
|
346 cl->buf->file_pos,
|
|
347 cl->buf->file_last - cl->buf->file_pos);
|
0
|
348 }
|
|
349
|
|
350 for (cl = p->free_raw_bufs; cl; cl = cl->next) {
|
32
|
351 ngx_log_debug8(NGX_LOG_DEBUG_EVENT, p->log, 0,
|
|
352 "pipe buf free s:%d t:%d f:%d "
|
|
353 "%p, pos %p, size: %z "
|
|
354 "file: %O, size: %z",
|
|
355 (cl->buf->shadow ? 1 : 0),
|
|
356 cl->buf->temporary, cl->buf->in_file,
|
|
357 cl->buf->start, cl->buf->pos,
|
|
358 cl->buf->last - cl->buf->pos,
|
|
359 cl->buf->file_pos,
|
|
360 cl->buf->file_last - cl->buf->file_pos);
|
0
|
361 }
|
|
362
|
|
363 #endif
|
|
364
|
|
365 if ((p->upstream_eof || p->upstream_error) && p->free_raw_bufs) {
|
|
366
|
|
367 /* STUB */ p->free_raw_bufs->buf->num = p->num++;
|
|
368
|
|
369 if (p->input_filter(p, p->free_raw_bufs->buf) == NGX_ERROR) {
|
|
370 return NGX_ABORT;
|
|
371 }
|
|
372
|
|
373 p->free_raw_bufs = p->free_raw_bufs->next;
|
|
374
|
|
375 if (p->free_bufs) {
|
|
376 for (cl = p->free_raw_bufs; cl; cl = cl->next) {
|
2
|
377 if (cl->buf->shadow == NULL) {
|
|
378 ngx_pfree(p->pool, cl->buf->start);
|
|
379 }
|
0
|
380 }
|
|
381 }
|
|
382 }
|
|
383
|
|
384 if (p->cachable && p->in) {
|
|
385 if (ngx_event_pipe_write_chain_to_temp_file(p) == NGX_ABORT) {
|
|
386 return NGX_ABORT;
|
|
387 }
|
|
388 }
|
|
389
|
|
390 return NGX_OK;
|
|
391 }
|
|
392
|
|
393
|
32
|
394 static ngx_int_t ngx_event_pipe_write_to_downstream(ngx_event_pipe_t *p)
|
0
|
395 {
|
28
|
396 size_t bsize;
|
0
|
397 ngx_uint_t flush;
|
32
|
398 ngx_chain_t *out, **ll, *cl;
|
0
|
399
|
|
400 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
|
|
401 "pipe write downstream: %d", p->downstream->write->ready);
|
|
402
|
|
403 for ( ;; ) {
|
|
404 if (p->downstream_error) {
|
|
405 return ngx_event_pipe_drain_chains(p);
|
|
406 }
|
|
407
|
|
408 if (p->upstream_eof || p->upstream_error || p->upstream_done) {
|
|
409
|
|
410 /* pass the p->out and p->in chains to the output filter */
|
|
411
|
|
412 if (p->out) {
|
|
413 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, p->log, 0,
|
|
414 "pipe write downstream flush out");
|
|
415
|
|
416 if (p->output_filter(p->output_ctx, p->out) == NGX_ERROR) {
|
|
417 p->downstream_error = 1;
|
|
418 return ngx_event_pipe_drain_chains(p);
|
|
419 }
|
|
420
|
|
421 p->out = NULL;
|
|
422 }
|
|
423
|
|
424 if (p->in) {
|
|
425 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, p->log, 0,
|
|
426 "pipe write downstream flush in");
|
|
427
|
|
428 if (p->output_filter(p->output_ctx, p->in) == NGX_ERROR) {
|
|
429 p->downstream_error = 1;
|
|
430 return ngx_event_pipe_drain_chains(p);
|
|
431 }
|
|
432
|
|
433 p->in = NULL;
|
|
434 }
|
|
435
|
|
436 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, p->log, 0,
|
|
437 "pipe write downstream done");
|
|
438
|
|
439 /* TODO: free unused bufs */
|
|
440
|
|
441 p->downstream_done = 1;
|
|
442 break;
|
|
443 }
|
|
444
|
|
445 if (!p->downstream->write->ready) {
|
|
446 break;
|
|
447 }
|
|
448
|
28
|
449 /* bsize is the size of the busy recycled bufs */
|
0
|
450
|
|
451 bsize = 0;
|
|
452
|
|
453 for (cl = p->busy; cl; cl = cl->next) {
|
28
|
454 if (cl->buf->recycled) {
|
|
455 bsize += cl->buf->end - cl->buf->start;
|
|
456 }
|
0
|
457 }
|
|
458
|
|
459 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
|
28
|
460 "pipe write busy: %uz", bsize);
|
0
|
461
|
|
462 out = NULL;
|
|
463 ll = NULL;
|
|
464 flush = 0;
|
|
465
|
|
466 for ( ;; ) {
|
|
467 if (p->out) {
|
|
468 cl = p->out;
|
|
469
|
28
|
470 if (cl->buf->recycled
|
|
471 && bsize + cl->buf->last - cl->buf->pos > p->busy_size)
|
|
472 {
|
0
|
473 flush = 1;
|
|
474 break;
|
|
475 }
|
|
476
|
|
477 p->out = p->out->next;
|
28
|
478
|
|
479 ngx_event_pipe_free_shadow_raw_buf(&p->free_raw_bufs, cl->buf);
|
0
|
480
|
|
481 } else if (!p->cachable && p->in) {
|
|
482 cl = p->in;
|
|
483
|
28
|
484 if (cl->buf->recycled
|
|
485 && bsize + cl->buf->last - cl->buf->pos > p->busy_size)
|
|
486 {
|
0
|
487 flush = 1;
|
|
488 break;
|
|
489 }
|
|
490
|
|
491 p->in = p->in->next;
|
|
492
|
|
493 } else {
|
|
494 break;
|
|
495 }
|
|
496
|
28
|
497 if (cl->buf->recycled) {
|
|
498 bsize += cl->buf->last - cl->buf->pos;
|
|
499 }
|
|
500
|
0
|
501 cl->next = NULL;
|
50
|
502
|
|
503 if (out) {
|
|
504 *ll = cl;
|
|
505 } else {
|
|
506 out = cl;
|
|
507 }
|
|
508 ll = &cl->next;
|
0
|
509 }
|
|
510
|
|
511 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, p->log, 0,
|
10
|
512 "pipe write: out:%p, f:%d", out, flush);
|
0
|
513
|
|
514 if (out == NULL && !flush) {
|
|
515 break;
|
|
516 }
|
|
517
|
|
518 if (p->output_filter(p->output_ctx, out) == NGX_ERROR) {
|
|
519 p->downstream_error = 1;
|
|
520 return ngx_event_pipe_drain_chains(p);
|
|
521 }
|
|
522
|
|
523 ngx_chain_update_chains(&p->free, &p->busy, &out, p->tag);
|
|
524
|
|
525 for (cl = p->free; cl; cl = cl->next) {
|
|
526
|
|
527 if (cl->buf->temp_file) {
|
|
528 if (p->cachable || !p->cyclic_temp_file) {
|
|
529 continue;
|
|
530 }
|
|
531
|
|
532 /* reset p->temp_offset if all bufs had been sent */
|
|
533
|
|
534 if (cl->buf->file_last == p->temp_file->offset) {
|
|
535 p->temp_file->offset = 0;
|
|
536 }
|
|
537 }
|
|
538
|
|
539 /* TODO: free buf if p->free_bufs && upstream done */
|
|
540
|
|
541 /* add the free shadow raw buf to p->free_raw_bufs */
|
|
542
|
|
543 if (cl->buf->last_shadow) {
|
32
|
544 if (ngx_event_pipe_add_free_buf(p, cl->buf->shadow) != NGX_OK) {
|
|
545 return NGX_ABORT;
|
|
546 }
|
0
|
547
|
|
548 cl->buf->last_shadow = 0;
|
|
549 }
|
|
550
|
|
551 cl->buf->shadow = NULL;
|
|
552 }
|
|
553 }
|
|
554
|
|
555 return NGX_OK;
|
|
556 }
|
|
557
|
|
558
|
|
559 static ngx_int_t ngx_event_pipe_write_chain_to_temp_file(ngx_event_pipe_t *p)
|
|
560 {
|
|
561 ssize_t size, bsize;
|
|
562 ngx_buf_t *b;
|
|
563 ngx_chain_t *cl, *tl, *next, *out, **ll, **last_free, fl;
|
|
564
|
|
565 if (p->buf_to_file) {
|
|
566 fl.buf = p->buf_to_file;
|
|
567 fl.next = p->in;
|
|
568 out = &fl;
|
|
569
|
|
570 } else {
|
|
571 out = p->in;
|
|
572 }
|
|
573
|
|
574 if (!p->cachable) {
|
|
575
|
|
576 size = 0;
|
|
577 cl = out;
|
|
578 ll = NULL;
|
|
579
|
|
580 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
|
10
|
581 "pipe offset: %O", p->temp_file->offset);
|
0
|
582
|
|
583 do {
|
|
584 bsize = cl->buf->last - cl->buf->pos;
|
|
585
|
|
586 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, p->log, 0,
|
10
|
587 "pipe buf %p, pos %p, size: %z",
|
0
|
588 cl->buf->start, cl->buf->pos, bsize);
|
|
589
|
|
590 if ((size + bsize > p->temp_file_write_size)
|
|
591 || (p->temp_file->offset + size + bsize > p->max_temp_file_size))
|
|
592 {
|
|
593 break;
|
|
594 }
|
|
595
|
|
596 size += bsize;
|
|
597 ll = &cl->next;
|
|
598 cl = cl->next;
|
|
599
|
|
600 } while (cl);
|
|
601
|
10
|
602 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0, "size: %z", size);
|
0
|
603
|
|
604 if (cl) {
|
|
605 p->in = cl;
|
|
606 *ll = NULL;
|
|
607
|
|
608 } else {
|
|
609 p->in = NULL;
|
|
610 p->last_in = &p->in;
|
|
611 }
|
|
612
|
|
613 } else {
|
|
614 p->in = NULL;
|
|
615 p->last_in = &p->in;
|
|
616 }
|
|
617
|
|
618 if (ngx_write_chain_to_temp_file(p->temp_file, out) == NGX_ERROR) {
|
|
619 return NGX_ABORT;
|
|
620 }
|
|
621
|
|
622 for (last_free = &p->free_raw_bufs;
|
|
623 *last_free != NULL;
|
|
624 last_free = &(*last_free)->next)
|
|
625 {
|
|
626 /* void */
|
|
627 }
|
|
628
|
|
629 if (p->buf_to_file) {
|
|
630 p->temp_file->offset = p->buf_to_file->last - p->buf_to_file->pos;
|
|
631 p->buf_to_file = NULL;
|
|
632 out = out->next;
|
|
633 }
|
|
634
|
|
635 for (cl = out; cl; cl = next) {
|
|
636 next = cl->next;
|
|
637 cl->next = NULL;
|
|
638
|
|
639 b = cl->buf;
|
|
640 b->file = &p->temp_file->file;
|
|
641 b->file_pos = p->temp_file->offset;
|
|
642 p->temp_file->offset += b->last - b->pos;
|
|
643 b->file_last = p->temp_file->offset;
|
|
644
|
|
645 b->in_file = 1;
|
|
646 b->temp_file = 1;
|
|
647
|
50
|
648 if (p->out) {
|
|
649 *p->last_out = cl;
|
|
650 } else {
|
|
651 p->out = cl;
|
|
652 }
|
|
653 p->last_out = &cl->next;
|
0
|
654
|
|
655 if (b->last_shadow) {
|
32
|
656
|
50
|
657 tl = ngx_alloc_chain_link(p->pool);
|
|
658 if (tl == NULL) {
|
32
|
659 return NGX_ABORT;
|
|
660 }
|
|
661
|
|
662 tl->buf = b->shadow;
|
|
663 tl->next = NULL;
|
|
664
|
|
665 *last_free = tl;
|
|
666 last_free = &tl->next;
|
|
667
|
2
|
668 b->shadow->pos = b->shadow->start;
|
|
669 b->shadow->last = b->shadow->start;
|
|
670
|
32
|
671 ngx_event_pipe_remove_shadow_links(b->shadow);
|
0
|
672 }
|
|
673 }
|
|
674
|
|
675 return NGX_OK;
|
|
676 }
|
|
677
|
|
678
|
|
679 /* the copy input filter */
|
|
680
|
|
681 ngx_int_t ngx_event_pipe_copy_input_filter(ngx_event_pipe_t *p, ngx_buf_t *buf)
|
|
682 {
|
|
683 ngx_buf_t *b;
|
|
684 ngx_chain_t *cl;
|
|
685
|
|
686 if (buf->pos == buf->last) {
|
|
687 return NGX_OK;
|
|
688 }
|
|
689
|
|
690 if (p->free) {
|
56
|
691 cl = p->free;
|
|
692 b = cl->buf;
|
|
693 p->free = cl->next;
|
|
694 ngx_free_chain(p->pool, cl);
|
0
|
695
|
|
696 } else {
|
50
|
697 b = ngx_alloc_buf(p->pool);
|
|
698 if (b == NULL) {
|
0
|
699 return NGX_ERROR;
|
|
700 }
|
|
701 }
|
|
702
|
|
703 ngx_memcpy(b, buf, sizeof(ngx_buf_t));
|
|
704 b->shadow = buf;
|
|
705 b->tag = p->tag;
|
|
706 b->last_shadow = 1;
|
|
707 b->recycled = 1;
|
|
708 buf->shadow = b;
|
|
709
|
50
|
710 cl = ngx_alloc_chain_link(p->pool);
|
|
711 if (cl == NULL) {
|
32
|
712 return NGX_ERROR;
|
|
713 }
|
|
714
|
|
715 cl->buf = b;
|
|
716 cl->next = NULL;
|
0
|
717
|
28
|
718 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0, "input buf #%d", b->num);
|
0
|
719
|
50
|
720 if (p->in) {
|
|
721 *p->last_in = cl;
|
|
722 } else {
|
|
723 p->in = cl;
|
|
724 }
|
|
725 p->last_in = &cl->next;
|
0
|
726
|
|
727 return NGX_OK;
|
|
728 }
|
|
729
|
|
730
|
16
|
731 static ngx_inline void ngx_event_pipe_remove_shadow_links(ngx_buf_t *buf)
|
0
|
732 {
|
|
733 ngx_buf_t *b, *next;
|
|
734
|
2
|
735 b = buf->shadow;
|
|
736
|
|
737 if (b == NULL) {
|
0
|
738 return;
|
|
739 }
|
|
740
|
|
741 while (!b->last_shadow) {
|
|
742 next = b->shadow;
|
|
743
|
2
|
744 b->temporary = 0;
|
|
745 b->recycled = 0;
|
0
|
746
|
|
747 b->shadow = NULL;
|
|
748 b = next;
|
|
749 }
|
|
750
|
2
|
751 b->temporary = 0;
|
|
752 b->recycled = 0;
|
0
|
753 b->last_shadow = 0;
|
|
754
|
|
755 b->shadow = NULL;
|
|
756
|
|
757 buf->shadow = NULL;
|
|
758 }
|
|
759
|
|
760
|
16
|
761 static ngx_inline void ngx_event_pipe_free_shadow_raw_buf(ngx_chain_t **free,
|
0
|
762 ngx_buf_t *buf)
|
|
763 {
|
|
764 ngx_buf_t *s;
|
|
765 ngx_chain_t *cl, **ll;
|
|
766
|
|
767 if (buf->shadow == NULL) {
|
|
768 return;
|
|
769 }
|
|
770
|
|
771 for (s = buf->shadow; !s->last_shadow; s = s->shadow) { /* void */ }
|
|
772
|
|
773 ll = free;
|
|
774
|
|
775 for (cl = *free ; cl; cl = cl->next) {
|
|
776 if (cl->buf == s) {
|
|
777 *ll = cl->next;
|
|
778 break;
|
|
779 }
|
|
780
|
|
781 if (cl->buf->shadow) {
|
|
782 break;
|
|
783 }
|
|
784
|
|
785 ll = &cl->next;
|
|
786 }
|
|
787 }
|
|
788
|
|
789
|
32
|
790 ngx_int_t ngx_event_pipe_add_free_buf(ngx_event_pipe_t *p, ngx_buf_t *b)
|
0
|
791 {
|
32
|
792 ngx_chain_t *cl;
|
|
793
|
50
|
794 cl = ngx_alloc_chain_link(p->pool);
|
|
795 if (cl == NULL) {
|
32
|
796 return NGX_ERROR;
|
0
|
797 }
|
|
798
|
32
|
799 b->pos = b->start;
|
|
800 b->last = b->start;
|
|
801 b->shadow = NULL;
|
|
802
|
|
803 cl->buf = b;
|
|
804
|
|
805 if (p->free_raw_bufs == NULL) {
|
|
806 p->free_raw_bufs = cl;
|
|
807 cl->next = NULL;
|
|
808
|
|
809 return NGX_OK;
|
|
810 }
|
|
811
|
|
812 if (p->free_raw_bufs->buf->pos == p->free_raw_bufs->buf->last) {
|
0
|
813
|
32
|
814 /* add the free buf to the list start */
|
|
815
|
|
816 cl->next = p->free_raw_bufs;
|
|
817 p->free_raw_bufs = cl;
|
|
818
|
|
819 return NGX_OK;
|
0
|
820 }
|
32
|
821
|
|
822 /* the first free buf is partialy filled, thus add the free buf after it */
|
|
823
|
|
824 cl->next = p->free_raw_bufs->next;
|
|
825 p->free_raw_bufs->next = cl;
|
|
826
|
|
827 return NGX_OK;
|
0
|
828 }
|
|
829
|
|
830
|
|
831 static ngx_int_t ngx_event_pipe_drain_chains(ngx_event_pipe_t *p)
|
|
832 {
|
|
833 ngx_chain_t *cl, *tl;
|
|
834
|
|
835 for ( ;; ) {
|
|
836 if (p->busy) {
|
|
837 cl = p->busy;
|
|
838 p->busy = NULL;
|
|
839
|
|
840 } else if (p->out) {
|
|
841 cl = p->out;
|
|
842 p->out = NULL;
|
|
843
|
|
844 } else if (p->in) {
|
|
845 cl = p->in;
|
|
846 p->in = NULL;
|
|
847
|
|
848 } else {
|
|
849 return NGX_OK;
|
|
850 }
|
|
851
|
|
852 while (cl) {
|
|
853 if (cl->buf->last_shadow) {
|
32
|
854 if (ngx_event_pipe_add_free_buf(p, cl->buf->shadow) != NGX_OK) {
|
28
|
855 return NGX_ABORT;
|
|
856 }
|
|
857
|
0
|
858 cl->buf->last_shadow = 0;
|
|
859 }
|
|
860
|
|
861 cl->buf->shadow = NULL;
|
|
862 tl = cl->next;
|
|
863 cl->next = p->free;
|
|
864 p->free = cl;
|
|
865 cl = tl;
|
|
866 }
|
|
867 }
|
|
868 }
|