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