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