nginx/src/event/ngx_event_pipe.c

771 lines
20 KiB
C
Raw Normal View History

2003-04-15 01:04:58 +08:00
2003-10-07 23:30:05 +08:00
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_event.h>
2003-10-22 00:49:56 +08:00
#include <ngx_event_pipe.h>
2003-04-15 01:04:58 +08:00
2003-10-22 00:49:56 +08:00
static ngx_int_t ngx_event_pipe_read_upstream(ngx_event_pipe_t *p);
static ngx_int_t ngx_event_pipe_write_to_downstream(ngx_event_pipe_t *p);
2003-10-22 00:49:56 +08:00
static ngx_int_t ngx_event_pipe_write_chain_to_temp_file(ngx_event_pipe_t *p);
ngx_inline static void ngx_event_pipe_remove_shadow_links(ngx_buf_t *buf);
ngx_inline static void ngx_event_pipe_free_shadow_raw_buf(ngx_chain_t **free,
ngx_buf_t *buf);
ngx_inline static void ngx_event_pipe_add_free_buf(ngx_chain_t **chain,
ngx_chain_t *cl);
static ngx_int_t ngx_event_pipe_drain_chains(ngx_event_pipe_t *p);
2003-10-17 04:19:16 +08:00
2003-04-15 01:04:58 +08:00
ngx_int_t ngx_event_pipe(ngx_event_pipe_t *p, int do_write)
2003-10-20 03:57:23 +08:00
{
2003-11-21 14:30:49 +08:00
u_int flags;
2003-10-29 16:30:44 +08:00
ngx_event_t *rev, *wev;
2003-10-20 03:57:23 +08:00
for ( ;; ) {
if (do_write) {
2003-10-22 00:49:56 +08:00
if (ngx_event_pipe_write_to_downstream(p) == NGX_ABORT) {
2003-10-20 03:57:23 +08:00
return NGX_ABORT;
}
}
p->read = 0;
2003-10-22 15:05:29 +08:00
p->upstream_blocked = 0;
2003-10-20 03:57:23 +08:00
2003-10-22 00:49:56 +08:00
if (ngx_event_pipe_read_upstream(p) == NGX_ABORT) {
2003-10-20 03:57:23 +08:00
return NGX_ABORT;
}
2003-10-22 15:05:29 +08:00
if (!p->read && !p->upstream_blocked) {
2003-10-20 03:57:23 +08:00
break;
}
do_write = 1;
}
2003-11-03 06:56:18 +08:00
if (p->upstream->fd != -1) {
rev = p->upstream->read;
2003-10-29 16:30:44 +08:00
2003-11-19 00:49:00 +08:00
flags = (rev->eof || rev->error) ? NGX_CLOSE_EVENT : 0;
if (ngx_handle_read_event(rev, flags) == NGX_ERROR) {
2003-11-03 06:56:18 +08:00
return NGX_ABORT;
}
2003-10-20 03:57:23 +08:00
2003-11-03 06:56:18 +08:00
if (rev->active) {
ngx_add_timer(rev, p->read_timeout);
}
2003-10-29 16:30:44 +08:00
}
2003-11-03 06:56:18 +08:00
if (p->downstream->fd != -1) {
wev = p->downstream->write;
2003-11-19 00:49:00 +08:00
wev->available = p->send_lowat;
if (ngx_handle_write_event(wev, NGX_LOWAT_EVENT) == NGX_ERROR) {
2003-11-03 06:56:18 +08:00
return NGX_ABORT;
}
2003-10-20 03:57:23 +08:00
2003-11-03 06:56:18 +08:00
if (wev->active) {
ngx_add_timer(wev, p->send_timeout);
}
2003-10-29 16:30:44 +08:00
}
2003-10-20 03:57:23 +08:00
return NGX_OK;
}
ngx_int_t ngx_event_pipe_read_upstream(ngx_event_pipe_t *p)
2003-04-15 01:04:58 +08:00
{
int n, rc, size;
ngx_buf_t *b;
2003-10-23 00:38:26 +08:00
ngx_chain_t *chain, *cl, *tl;
2003-04-15 23:06:52 +08:00
2003-10-21 15:47:21 +08:00
if (p->upstream_eof || p->upstream_error || p->upstream_done) {
return NGX_OK;
}
2004-02-12 01:08:49 +08:00
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
"pipe read upstream: %d", p->upstream->read->ready);
2003-04-15 01:04:58 +08:00
2003-10-21 15:47:21 +08:00
for ( ;; ) {
if (p->upstream_eof || p->upstream_error || p->upstream_done) {
break;
}
if (p->preread_bufs == NULL && !p->upstream->read->ready) {
2003-10-21 15:47:21 +08:00
break;
}
if (p->preread_bufs) {
2003-10-14 00:32:29 +08:00
/* use the pre-read bufs if they exist */
2003-10-14 00:32:29 +08:00
chain = p->preread_bufs;
p->preread_bufs = NULL;
2003-04-18 01:59:35 +08:00
n = p->preread_size;
2004-02-12 01:08:49 +08:00
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
"pipe preread: %d", n);
2003-10-14 00:32:29 +08:00
2004-04-02 13:14:40 +08:00
if (n) {
p->read = 1;
}
2003-04-18 01:59:35 +08:00
} else {
2003-10-14 00:32:29 +08:00
/*
* kqueue notifies about the end of file or a pending error.
* This test allows not to allocate a buf on these conditions
2003-10-14 00:32:29 +08:00
* and not to call ngx_recv_chain().
*/
2003-04-22 23:02:58 +08:00
2003-10-30 16:51:06 +08:00
if (p->upstream->read->available == 0
&& (p->upstream->read->kq_eof || p->upstream->read->aio_eof))
{
p->upstream->read->ready = 0;
p->upstream->read->eof = 0;
p->upstream_eof = 1;
p->read = 1;
2003-10-28 23:45:41 +08:00
2003-10-30 16:51:06 +08:00
#if (HAVE_KQUEUE)
2003-10-28 23:45:41 +08:00
if (p->upstream->read->kq_errno) {
2003-10-30 16:51:06 +08:00
p->upstream->read->error = 1;
p->upstream_error = 1;
p->upstream_eof = 0;
2003-10-28 23:45:41 +08:00
ngx_log_error(NGX_LOG_ERR, p->log,
p->upstream->read->kq_errno,
2003-04-22 23:02:58 +08:00
"readv() failed");
}
2003-10-28 23:45:41 +08:00
#endif
2003-10-30 16:51:06 +08:00
break;
2003-04-22 23:02:58 +08:00
}
2003-04-18 01:59:35 +08:00
if (p->free_raw_bufs) {
2003-10-14 00:32:29 +08:00
/* use the free bufs if they exist */
2003-10-14 00:32:29 +08:00
chain = p->free_raw_bufs;
2003-10-30 16:51:06 +08:00
if (p->single_buf) {
p->free_raw_bufs = p->free_raw_bufs->next;
2003-10-30 16:51:06 +08:00
chain->next = NULL;
} else {
p->free_raw_bufs = NULL;
2003-10-30 16:51:06 +08:00
}
2003-04-15 01:04:58 +08:00
} else if (p->allocated < p->bufs.num) {
2003-04-15 01:04:58 +08:00
/* allocate a new buf if it's still allowed */
2003-04-15 01:04:58 +08:00
if (!(b = ngx_create_temp_buf(p->pool, p->bufs.size))) {
return NGX_ABORT;
}
p->allocated++;
2003-04-21 22:55:47 +08:00
ngx_alloc_link_and_set_buf(tl, b, p->pool, NGX_ABORT);
2003-10-23 00:38:26 +08:00
chain = tl;
2003-04-15 01:04:58 +08:00
2003-10-14 13:26:00 +08:00
} else if (!p->cachable && p->downstream->write->ready) {
2003-04-18 01:59:35 +08:00
2003-10-14 00:32:29 +08:00
/*
* if the bufs are not needed to be saved in a cache and
* a downstream is ready then write the bufs to a downstream
2003-10-14 00:32:29 +08:00
*/
2003-10-22 15:05:29 +08:00
p->upstream_blocked = 1;
2004-02-12 01:08:49 +08:00
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, p->log, 0,
"pipe downstream ready");
2003-04-18 01:59:35 +08:00
2003-04-21 22:55:47 +08:00
break;
2003-04-18 01:59:35 +08:00
2003-11-03 06:56:18 +08:00
} else if (p->cachable
|| p->temp_file->offset < p->max_temp_file_size)
{
2003-10-14 00:32:29 +08:00
/*
* if it's allowed then save some bufs from r->in
2003-10-22 00:49:56 +08:00
* to a temporary file, and add them to a r->out chain
2003-10-14 00:32:29 +08:00
*/
2003-10-22 00:49:56 +08:00
rc = ngx_event_pipe_write_chain_to_temp_file(p);
2003-04-15 01:04:58 +08:00
2004-02-12 01:08:49 +08:00
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
"pipe temp offset: %d", p->temp_file->offset);
2003-04-15 01:04:58 +08:00
2003-10-14 23:06:38 +08:00
if (rc == NGX_AGAIN) {
if (ngx_event_flags & NGX_USE_LEVEL_EVENT
&& p->upstream->read->active
&& p->upstream->read->ready)
{
if (ngx_del_event(p->upstream->read, NGX_READ_EVENT, 0)
== NGX_ERROR)
{
return NGX_ABORT;
}
}
}
2003-04-18 01:59:35 +08:00
if (rc != NGX_OK) {
return rc;
}
2003-04-15 01:04:58 +08:00
chain = p->free_raw_bufs;
2003-10-30 16:51:06 +08:00
if (p->single_buf) {
p->free_raw_bufs = p->free_raw_bufs->next;
2003-10-30 16:51:06 +08:00
chain->next = NULL;
} else {
p->free_raw_bufs = NULL;
2003-10-30 16:51:06 +08:00
}
2003-04-15 01:04:58 +08:00
2003-04-18 01:59:35 +08:00
} else {
2003-10-14 00:32:29 +08:00
/* if there're no bufs to read in then disable a level event */
2003-04-21 22:55:47 +08:00
2004-02-12 01:08:49 +08:00
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, p->log, 0,
2004-06-01 14:04:46 +08:00
"no pipe bufs to read in");
2003-04-18 01:59:35 +08:00
break;
2003-04-15 01:04:58 +08:00
}
2003-04-18 01:59:35 +08:00
n = ngx_recv_chain(p->upstream, chain);
2003-04-15 01:04:58 +08:00
2004-02-12 01:08:49 +08:00
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
"pipe recv chain: %d", n);
2003-04-15 01:04:58 +08:00
if (p->free_raw_bufs) {
chain->next = p->free_raw_bufs;
2003-10-30 16:51:06 +08:00
}
p->free_raw_bufs = chain;
2003-10-21 01:14:07 +08:00
2003-10-14 00:32:29 +08:00
if (n == NGX_ERROR) {
p->upstream_error = 1;
return NGX_ERROR;
}
2003-04-15 01:04:58 +08:00
2003-10-14 00:32:29 +08:00
if (n == NGX_AGAIN) {
2003-10-30 16:51:06 +08:00
if (p->single_buf) {
ngx_event_pipe_remove_shadow_links(chain->buf);
2003-10-30 16:51:06 +08:00
}
2003-10-14 00:32:29 +08:00
break;
2003-04-15 01:04:58 +08:00
}
2003-10-20 03:57:23 +08:00
p->read = 1;
2003-10-14 00:32:29 +08:00
if (n == 0) {
p->upstream_eof = 1;
break;
2003-04-15 23:06:52 +08:00
}
2003-04-15 01:04:58 +08:00
}
2003-11-06 01:03:41 +08:00
p->read_length += n;
2003-10-23 00:38:26 +08:00
cl = chain;
2003-10-21 01:14:07 +08:00
2003-10-23 00:38:26 +08:00
while (cl && n > 0) {
2003-10-14 23:06:38 +08:00
ngx_event_pipe_remove_shadow_links(cl->buf);
2003-10-17 04:19:16 +08:00
size = cl->buf->end - cl->buf->last;
2003-10-14 23:06:38 +08:00
if (n >= size) {
cl->buf->last = cl->buf->end;
2003-10-14 23:06:38 +08:00
/* STUB */ cl->buf->num = p->num++;
2003-10-22 15:05:29 +08:00
if (p->input_filter(p, cl->buf) == NGX_ERROR) {
2003-10-14 23:06:38 +08:00
return NGX_ABORT;
}
n -= size;
2003-10-23 00:38:26 +08:00
cl = cl->next;
2003-04-15 01:04:58 +08:00
} else {
cl->buf->last += n;
2003-04-15 01:04:58 +08:00
n = 0;
}
}
p->free_raw_bufs = cl;
2003-04-15 01:04:58 +08:00
}
#if (NGX_DEBUG)
2004-04-02 13:14:40 +08:00
if (p->in || p->busy || p->free_raw_bufs) {
2004-04-02 13:14:40 +08:00
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, p->log, 0, "pipe buf");
}
for (cl = p->in; cl; cl = cl->next) {
ngx_log_debug3(NGX_LOG_DEBUG_EVENT, p->log, 0,
"pipe buf in " PTR_FMT ", pos " PTR_FMT ", size: %d",
cl->buf->start, cl->buf->pos,
cl->buf->last - cl->buf->pos);
2004-04-02 13:14:40 +08:00
}
for (cl = p->busy; cl; cl = cl->next) {
ngx_log_debug3(NGX_LOG_DEBUG_EVENT, p->log, 0,
"pipe buf busy " PTR_FMT ", pos " PTR_FMT ", size: %d",
cl->buf->start, cl->buf->pos,
cl->buf->last - cl->buf->pos);
2004-04-02 13:14:40 +08:00
}
for (cl = p->free_raw_bufs; cl; cl = cl->next) {
2004-04-02 13:14:40 +08:00
ngx_log_debug3(NGX_LOG_DEBUG_EVENT, p->log, 0,
"pipe buf free " PTR_FMT ", last " PTR_FMT ", size: %d",
cl->buf->start, cl->buf->last,
cl->buf->end - cl->buf->last);
2004-04-02 13:14:40 +08:00
}
#endif
if ((p->upstream_eof || p->upstream_error) && p->free_raw_bufs) {
2004-03-19 13:25:53 +08:00
/* STUB */ p->free_raw_bufs->buf->num = p->num++;
2004-03-19 13:25:53 +08:00
if (p->input_filter(p, p->free_raw_bufs->buf) == NGX_ERROR) {
2003-10-17 04:19:16 +08:00
return NGX_ABORT;
2003-04-15 01:04:58 +08:00
}
p->free_raw_bufs = p->free_raw_bufs->next;
2003-10-30 16:51:06 +08:00
2003-10-31 15:10:36 +08:00
if (p->free_bufs) {
for (cl = p->free_raw_bufs; cl; cl = cl->next) {
ngx_pfree(p->pool, cl->buf->start);
2003-10-31 15:10:36 +08:00
}
2003-10-30 16:51:06 +08:00
}
2003-04-15 01:04:58 +08:00
}
2003-10-20 03:57:23 +08:00
if (p->cachable && p->in) {
2003-10-22 00:49:56 +08:00
if (ngx_event_pipe_write_chain_to_temp_file(p) == NGX_ABORT) {
2003-10-10 23:10:50 +08:00
return NGX_ABORT;
2003-04-15 01:04:58 +08:00
}
}
2003-10-17 04:19:16 +08:00
return NGX_OK;
2003-04-15 01:04:58 +08:00
}
ngx_int_t ngx_event_pipe_write_to_downstream(ngx_event_pipe_t *p)
2003-10-14 13:26:00 +08:00
{
2004-04-02 13:14:40 +08:00
size_t bsize;
ngx_uint_t flush;
ngx_buf_t *b;
2003-10-23 00:38:26 +08:00
ngx_chain_t *out, **ll, *cl, *tl;
2003-10-14 13:26:00 +08:00
2004-02-12 01:08:49 +08:00
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
"pipe write downstream: %d", p->downstream->write->ready);
2003-10-20 03:57:23 +08:00
for ( ;; ) {
2003-10-21 15:47:21 +08:00
if (p->downstream_error) {
2003-10-23 00:38:26 +08:00
return ngx_event_pipe_drain_chains(p);
2003-10-21 15:47:21 +08:00
}
2003-10-20 03:57:23 +08:00
2004-03-19 13:25:53 +08:00
if (p->upstream_eof || p->upstream_error || p->upstream_done) {
/* pass the p->out and p->in chains to the output filter */
if (p->out) {
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, p->log, 0,
"pipe write downstream flush out");
if (p->output_filter(p->output_ctx, p->out) == NGX_ERROR) {
p->downstream_error = 1;
return ngx_event_pipe_drain_chains(p);
}
p->out = NULL;
}
if (p->in) {
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, p->log, 0,
"pipe write downstream flush in");
if (p->output_filter(p->output_ctx, p->in) == NGX_ERROR) {
p->downstream_error = 1;
return ngx_event_pipe_drain_chains(p);
}
p->in = NULL;
}
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, p->log, 0,
"pipe write downstream done");
/* TODO: free unused bufs */
2004-03-19 13:25:53 +08:00
2003-10-20 03:57:23 +08:00
p->downstream_done = 1;
break;
}
if (!p->downstream->write->ready) {
break;
}
2003-10-14 13:26:00 +08:00
/* bsize is the size of the busy bufs */
2003-10-23 00:38:26 +08:00
bsize = 0;
2003-10-21 15:47:21 +08:00
2004-04-02 13:14:40 +08:00
for (cl = p->busy; cl; cl = cl->next) {
bsize += cl->buf->end - cl->buf->start;
2003-10-21 15:47:21 +08:00
}
2004-04-02 13:14:40 +08:00
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
"pipe write busy: " SIZE_T_FMT, bsize);
2003-10-21 15:47:21 +08:00
2003-10-22 00:49:56 +08:00
out = NULL;
2003-10-23 00:38:26 +08:00
ll = NULL;
2004-04-02 13:14:40 +08:00
flush = 0;
2003-10-21 15:47:21 +08:00
2003-10-22 00:49:56 +08:00
for ( ;; ) {
if (p->out) {
2003-10-23 00:38:26 +08:00
cl = p->out;
2003-10-14 23:06:38 +08:00
if (bsize + ngx_buf_size(cl->buf) > p->busy_size) {
2004-04-02 13:14:40 +08:00
flush = 1;
2004-03-23 14:01:52 +08:00
break;
}
2003-10-14 23:06:38 +08:00
2003-10-22 00:49:56 +08:00
p->out = p->out->next;
ngx_event_pipe_free_shadow_raw_buf(&p->free_raw_bufs,
cl->buf);
2003-10-14 13:26:00 +08:00
2003-10-22 00:49:56 +08:00
} else if (!p->cachable && p->in) {
2003-10-23 00:38:26 +08:00
cl = p->in;
2003-10-14 23:06:38 +08:00
if (bsize + ngx_buf_size(cl->buf) > p->busy_size) {
2004-04-02 13:14:40 +08:00
flush = 1;
2004-03-23 14:01:52 +08:00
break;
}
2003-10-22 00:49:56 +08:00
p->in = p->in->next;
} else {
2003-10-14 23:06:38 +08:00
break;
}
bsize += ngx_buf_size(cl->buf);
2003-10-23 00:38:26 +08:00
cl->next = NULL;
ngx_chain_add_link(out, ll, cl);
2003-10-22 00:49:56 +08:00
}
2003-10-14 13:26:00 +08:00
2004-04-02 13:14:40 +08:00
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, p->log, 0,
"pipe write: out:" PTR_FMT ", f:%d", out, flush);
2003-10-22 15:05:29 +08:00
2004-04-02 13:14:40 +08:00
if (out == NULL && !flush) {
break;
2003-10-14 13:26:00 +08:00
}
2003-10-22 00:49:56 +08:00
if (p->output_filter(p->output_ctx, out) == NGX_ERROR) {
2003-10-21 01:14:07 +08:00
p->downstream_error = 1;
2004-03-19 13:25:53 +08:00
return ngx_event_pipe_drain_chains(p);
2003-10-20 03:57:23 +08:00
}
2003-10-22 15:05:29 +08:00
ngx_chain_update_chains(&p->free, &p->busy, &out, p->tag);
2003-10-14 23:06:38 +08:00
2003-10-23 00:38:26 +08:00
for (cl = p->free; cl; cl = cl->next) {
2003-10-22 15:05:29 +08:00
if (cl->buf->temp_file) {
2003-11-06 01:03:41 +08:00
if (p->cachable || !p->cyclic_temp_file) {
continue;
}
/* reset p->temp_offset if all bufs had been sent */
2003-11-06 01:03:41 +08:00
if (cl->buf->file_last == p->temp_file->offset) {
2003-11-06 01:03:41 +08:00
p->temp_file->offset = 0;
}
}
/* TODO: free buf if p->free_bufs && upstream done */
/* add the free shadow raw buf to p->free_raw_bufs */
2003-10-22 15:05:29 +08:00
if (cl->buf->last_shadow) {
b = cl->buf->shadow;
b->pos = b->last = b->start;
b->shadow = NULL;
ngx_alloc_link_and_set_buf(tl, b, p->pool, NGX_ABORT);
ngx_event_pipe_add_free_buf(&p->free_raw_bufs, tl);
2003-10-14 13:26:00 +08:00
cl->buf->last_shadow = 0;
2003-10-14 13:26:00 +08:00
}
2003-10-22 15:05:29 +08:00
cl->buf->shadow = NULL;
2003-10-14 13:26:00 +08:00
}
}
return NGX_OK;
}
static ngx_int_t ngx_event_pipe_write_chain_to_temp_file(ngx_event_pipe_t *p)
2003-04-15 01:04:58 +08:00
{
2004-06-18 14:09:25 +08:00
ssize_t size, bsize;
ngx_buf_t *b;
2003-11-18 00:15:03 +08:00
ngx_chain_t *cl, *tl, *next, *out, **ll, **last_free, fl;
2003-04-15 01:04:58 +08:00
if (p->buf_to_file) {
fl.buf = p->buf_to_file;
2003-11-18 00:15:03 +08:00
fl.next = p->in;
out = &fl;
2003-04-15 01:04:58 +08:00
2003-11-03 06:56:18 +08:00
} else {
2003-11-18 00:15:03 +08:00
out = p->in;
2003-04-15 01:04:58 +08:00
}
2003-10-17 04:19:16 +08:00
if (!p->cachable) {
2003-04-15 01:04:58 +08:00
size = 0;
2003-11-18 00:15:03 +08:00
cl = out;
2003-10-23 00:38:26 +08:00
ll = NULL;
2003-10-21 15:47:21 +08:00
2004-02-12 01:08:49 +08:00
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
"pipe offset: %d", p->temp_file->offset);
2003-04-15 01:04:58 +08:00
do {
bsize = cl->buf->last - cl->buf->pos;
2003-10-21 15:47:21 +08:00
2004-04-02 13:14:40 +08:00
ngx_log_debug3(NGX_LOG_DEBUG_EVENT, p->log, 0,
"pipe buf " PTR_FMT ", pos " PTR_FMT ", size: %d",
cl->buf->start, cl->buf->pos, bsize);
2003-10-21 15:47:21 +08:00
if ((size + bsize > p->temp_file_write_size)
|| (p->temp_file->offset + size + bsize > p->max_temp_file_size))
2003-10-21 01:14:07 +08:00
{
2003-04-15 01:04:58 +08:00
break;
}
2003-10-21 15:47:21 +08:00
size += bsize;
2003-10-23 00:38:26 +08:00
ll = &cl->next;
cl = cl->next;
2003-04-15 01:04:58 +08:00
2003-10-23 00:38:26 +08:00
} while (cl);
2003-04-15 01:04:58 +08:00
2004-02-12 01:08:49 +08:00
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0, "size: %d", size);
2003-10-21 15:47:21 +08:00
2003-10-23 00:38:26 +08:00
if (cl) {
p->in = cl;
*ll = NULL;
2003-10-21 01:14:07 +08:00
} else {
2003-10-22 00:49:56 +08:00
p->in = NULL;
2003-10-21 15:47:21 +08:00
p->last_in = &p->in;
2003-10-21 01:14:07 +08:00
}
2003-04-15 01:04:58 +08:00
} else {
2003-10-22 00:49:56 +08:00
p->in = NULL;
2003-10-21 15:47:21 +08:00
p->last_in = &p->in;
2003-04-15 01:04:58 +08:00
}
2003-11-03 06:56:18 +08:00
if (ngx_write_chain_to_temp_file(p->temp_file, out) == NGX_ERROR) {
2003-10-10 23:10:50 +08:00
return NGX_ABORT;
2003-04-15 01:04:58 +08:00
}
for (last_free = &p->free_raw_bufs;
2003-10-20 03:57:23 +08:00
*last_free != NULL;
2003-10-21 15:47:21 +08:00
last_free = &(*last_free)->next)
2003-10-20 03:57:23 +08:00
{
/* void */
}
if (p->buf_to_file) {
p->temp_file->offset = p->buf_to_file->last - p->buf_to_file->pos;
p->buf_to_file = NULL;
2003-11-18 00:15:03 +08:00
out = out->next;
2003-11-03 06:56:18 +08:00
}
2003-10-23 00:38:26 +08:00
for (cl = out; cl; cl = next) {
next = cl->next;
cl->next = NULL;
2003-04-15 01:04:58 +08:00
b = cl->buf;
b->file = &p->temp_file->file;
b->file_pos = p->temp_file->offset;
p->temp_file->offset += b->last - b->pos;
b->file_last = p->temp_file->offset;
2003-11-06 01:03:41 +08:00
b->in_file = 1;
b->temp_file = 1;
2003-10-22 15:05:29 +08:00
2003-10-23 00:38:26 +08:00
ngx_chain_add_link(p->out, p->last_out, cl);
2003-10-20 03:57:23 +08:00
if (b->last_shadow) {
b->shadow->last = b->shadow->pos = b->shadow->start;
ngx_alloc_link_and_set_buf(tl, b->shadow, p->pool, NGX_ABORT);
2003-10-23 00:38:26 +08:00
*last_free = tl;
last_free = &tl->next;
2003-04-15 01:04:58 +08:00
}
}
return NGX_OK;
}
2003-04-15 23:06:52 +08:00
2003-04-21 22:55:47 +08:00
2003-04-15 23:06:52 +08:00
/* the copy input filter */
ngx_int_t ngx_event_pipe_copy_input_filter(ngx_event_pipe_t *p, ngx_buf_t *buf)
2003-04-15 23:06:52 +08:00
{
ngx_buf_t *b;
2003-10-23 00:38:26 +08:00
ngx_chain_t *cl;
2003-04-15 23:06:52 +08:00
if (buf->pos == buf->last) {
2003-10-14 23:06:38 +08:00
return NGX_OK;
}
2003-04-15 23:06:52 +08:00
2003-10-14 23:06:38 +08:00
if (p->free) {
b = p->free->buf;
2003-10-14 23:06:38 +08:00
p->free = p->free->next;
2003-04-15 23:06:52 +08:00
2003-10-14 23:06:38 +08:00
} else {
if (!(b = ngx_alloc_buf(p->pool))) {
return NGX_ERROR;
}
2003-10-14 23:06:38 +08:00
}
2003-04-15 23:06:52 +08:00
ngx_memcpy(b, buf, sizeof(ngx_buf_t));
b->shadow = buf;
b->tag = p->tag;
b->last_shadow = 1;
b->recycled = 1;
buf->shadow = b;
2003-04-15 23:06:52 +08:00
ngx_alloc_link_and_set_buf(cl, b, p->pool, NGX_ERROR);
2004-02-12 01:08:49 +08:00
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0, "buf #%d", b->num);
2004-02-12 01:08:49 +08:00
2003-10-23 00:38:26 +08:00
ngx_chain_add_link(p->in, p->last_in, cl);
2003-04-15 23:06:52 +08:00
2003-10-14 23:06:38 +08:00
return NGX_OK;
}
ngx_inline static void ngx_event_pipe_remove_shadow_links(ngx_buf_t *buf)
2003-10-14 23:06:38 +08:00
{
ngx_buf_t *b, *next;
2003-10-17 04:19:16 +08:00
if (buf->shadow == NULL) {
2003-10-17 04:19:16 +08:00
return;
}
b = buf->shadow;
2003-10-17 04:19:16 +08:00
while (!b->last_shadow) {
next = b->shadow;
#if 0
b->type &= ~(NGX_HUNK_TEMP|NGX_HUNK_IN_MEMORY|NGX_HUNK_RECYCLED);
#endif
b->temporary = 0;
b->recycled = 0;
b->shadow = NULL;
b = next;
2003-10-17 04:19:16 +08:00
}
#if 0
b->type &= ~(NGX_HUNK_TEMP
2003-10-17 04:19:16 +08:00
|NGX_HUNK_IN_MEMORY
|NGX_HUNK_RECYCLED
|NGX_HUNK_LAST_SHADOW);
#endif
b->temporary = 0;
b->recycled = 0;
b->last_shadow = 0;
2003-10-17 04:19:16 +08:00
b->shadow = NULL;
buf->shadow = NULL;
2003-10-14 23:06:38 +08:00
}
2003-04-15 23:06:52 +08:00
ngx_inline static void ngx_event_pipe_free_shadow_raw_buf(ngx_chain_t **free,
ngx_buf_t *buf)
2003-10-14 23:06:38 +08:00
{
ngx_buf_t *s;
2003-10-23 00:38:26 +08:00
ngx_chain_t *cl, **ll;
2003-04-15 23:06:52 +08:00
if (buf->shadow == NULL) {
2003-10-14 23:06:38 +08:00
return;
2003-04-15 23:06:52 +08:00
}
for (s = buf->shadow; !s->last_shadow; s = s->shadow) { /* void */ }
2003-10-14 23:06:38 +08:00
2003-10-23 00:38:26 +08:00
ll = free;
2003-10-14 23:06:38 +08:00
2003-10-23 00:38:26 +08:00
for (cl = *free ; cl; cl = cl->next) {
if (cl->buf == s) {
2003-10-23 00:38:26 +08:00
*ll = cl->next;
2003-10-14 23:06:38 +08:00
break;
}
if (cl->buf->shadow) {
2003-10-14 23:06:38 +08:00
break;
}
2003-10-23 00:38:26 +08:00
ll = &cl->next;
2003-10-14 23:06:38 +08:00
}
2003-04-15 23:06:52 +08:00
}
2003-10-14 23:06:38 +08:00
ngx_inline static void ngx_event_pipe_add_free_buf(ngx_chain_t **chain,
ngx_chain_t *cl)
2003-10-14 23:06:38 +08:00
{
2003-10-17 04:19:16 +08:00
if (*chain == NULL) {
2003-10-23 00:38:26 +08:00
*chain = cl;
2003-10-17 04:19:16 +08:00
return;
}
if ((*chain)->buf->pos != (*chain)->buf->last) {
2003-10-23 00:38:26 +08:00
cl->next = (*chain)->next;
(*chain)->next = cl;
2003-10-14 23:06:38 +08:00
} else {
2003-10-23 00:38:26 +08:00
cl->next = (*chain);
(*chain) = cl;
2003-10-14 23:06:38 +08:00
}
}
2003-10-21 15:47:21 +08:00
static ngx_int_t ngx_event_pipe_drain_chains(ngx_event_pipe_t *p)
2003-10-21 15:47:21 +08:00
{
ngx_buf_t *b;
2003-10-23 00:38:26 +08:00
ngx_chain_t *cl, *tl;
2003-10-21 15:47:21 +08:00
for ( ;; ) {
if (p->busy) {
2003-10-23 00:38:26 +08:00
cl = p->busy;
2003-11-18 16:04:34 +08:00
p->busy = NULL;
2003-10-21 15:47:21 +08:00
} else if (p->out) {
2003-10-23 00:38:26 +08:00
cl = p->out;
2003-11-18 16:04:34 +08:00
p->out = NULL;
2003-10-21 15:47:21 +08:00
} else if (p->in) {
2003-10-23 00:38:26 +08:00
cl = p->in;
2003-11-18 16:04:34 +08:00
p->in = NULL;
2003-10-21 15:47:21 +08:00
} else {
return NGX_OK;
}
2003-10-23 00:38:26 +08:00
while (cl) {
if (cl->buf->last_shadow) {
b = cl->buf->shadow;
b->pos = b->last = b->start;
b->shadow = NULL;
ngx_alloc_link_and_set_buf(tl, b, p->pool, NGX_ABORT);
ngx_event_pipe_add_free_buf(&p->free_raw_bufs, tl);
cl->buf->last_shadow = 0;
2003-10-21 15:47:21 +08:00
}
cl->buf->shadow = NULL;
2003-10-23 00:38:26 +08:00
tl = cl->next;
cl->next = p->free;
p->free = cl;
cl = tl;
2003-10-21 15:47:21 +08:00
}
}
}