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