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 int ngx_event_pipe_read_upstream(ngx_event_pipe_t *p);
|
|
|
|
static int ngx_event_pipe_write_to_downstream(ngx_event_pipe_t *p);
|
|
|
|
|
|
|
|
static int ngx_event_pipe_write_chain_to_temp_file(ngx_event_pipe_t *p);
|
2003-10-23 00:38:26 +08:00
|
|
|
ngx_inline static void ngx_event_pipe_remove_shadow_links(ngx_hunk_t *hunk);
|
|
|
|
ngx_inline static void ngx_event_pipe_free_shadow_raw_hunk(ngx_chain_t **free,
|
|
|
|
ngx_hunk_t *h);
|
|
|
|
ngx_inline static void ngx_event_pipe_add_free_hunk(ngx_chain_t **chain,
|
|
|
|
ngx_chain_t *cl);
|
|
|
|
static int 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
|
|
|
|
2003-10-22 00:49:56 +08:00
|
|
|
int ngx_event_pipe(ngx_event_pipe_t *p, int do_write)
|
2003-10-20 03:57:23 +08:00
|
|
|
{
|
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-03 06:56:18 +08:00
|
|
|
if (ngx_handle_read_event(rev, (rev->eof || rev->error)) == NGX_ERROR) {
|
|
|
|
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-10-29 16:30:44 +08:00
|
|
|
|
2003-11-03 06:56:18 +08:00
|
|
|
if (ngx_handle_write_event(wev, p->send_lowat) == NGX_ERROR) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-22 00:49:56 +08:00
|
|
|
int ngx_event_pipe_read_upstream(ngx_event_pipe_t *p)
|
2003-04-15 01:04:58 +08:00
|
|
|
{
|
|
|
|
int n, rc, size;
|
2003-10-17 04:19:16 +08:00
|
|
|
ngx_hunk_t *h;
|
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;
|
|
|
|
}
|
|
|
|
|
2003-10-17 04:19:16 +08:00
|
|
|
ngx_log_debug(p->log, "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_hunks == NULL && !p->upstream->read->ready) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-04-18 01:59:35 +08:00
|
|
|
if (p->preread_hunks) {
|
2003-10-14 00:32:29 +08:00
|
|
|
|
|
|
|
/* use the pre-read hunks if they exist */
|
|
|
|
|
2003-10-20 03:57:23 +08:00
|
|
|
p->read = 1;
|
2003-04-18 01:59:35 +08:00
|
|
|
chain = p->preread_hunks;
|
|
|
|
p->preread_hunks = NULL;
|
|
|
|
n = p->preread_size;
|
|
|
|
|
2003-10-17 04:19:16 +08:00
|
|
|
ngx_log_debug(p->log, "preread: %d" _ n);
|
2003-10-14 00:32:29 +08:00
|
|
|
|
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 hunk on these conditions
|
|
|
|
* 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
|
|
|
|
2003-10-14 23:06:38 +08:00
|
|
|
if (p->free_raw_hunks) {
|
2003-10-14 00:32:29 +08:00
|
|
|
|
|
|
|
/* use the free hunks if they exist */
|
|
|
|
|
2003-10-14 23:06:38 +08:00
|
|
|
chain = p->free_raw_hunks;
|
2003-10-30 16:51:06 +08:00
|
|
|
if (p->single_buf) {
|
|
|
|
p->free_raw_hunks = p->free_raw_hunks->next;
|
|
|
|
chain->next = NULL;
|
|
|
|
} else {
|
|
|
|
p->free_raw_hunks = NULL;
|
|
|
|
}
|
2003-04-15 01:04:58 +08:00
|
|
|
|
2003-10-14 00:32:29 +08:00
|
|
|
} else if (p->hunks < p->bufs.num) {
|
2003-04-15 01:04:58 +08:00
|
|
|
|
2003-10-14 00:32:29 +08:00
|
|
|
/* allocate a new hunk if it's still allowed */
|
2003-04-15 01:04:58 +08:00
|
|
|
|
2003-10-30 16:51:06 +08:00
|
|
|
ngx_test_null(h, ngx_create_temp_hunk(p->pool, p->bufs.size),
|
2003-10-14 00:32:29 +08:00
|
|
|
NGX_ABORT);
|
|
|
|
p->hunks++;
|
2003-04-21 22:55:47 +08:00
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
ngx_alloc_link_and_set_hunk(tl, h, p->pool, NGX_ABORT);
|
|
|
|
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
|
|
|
/*
|
2003-10-22 00:49:56 +08:00
|
|
|
* if the hunks are not needed to be saved in a cache and
|
|
|
|
* a downstream is ready then write the hunks to a downstream
|
2003-10-14 00:32:29 +08:00
|
|
|
*/
|
|
|
|
|
2003-10-22 15:05:29 +08:00
|
|
|
p->upstream_blocked = 1;
|
|
|
|
|
2003-10-17 04:19:16 +08:00
|
|
|
ngx_log_debug(p->log, "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
|
|
|
|
|
|
|
/*
|
2003-10-22 00:49:56 +08:00
|
|
|
* if it's allowed then save some hunks from r->in
|
|
|
|
* 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
|
|
|
|
2003-11-03 06:56:18 +08:00
|
|
|
ngx_log_debug(p->log, "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
|
|
|
|
2003-10-14 23:06:38 +08:00
|
|
|
chain = p->free_raw_hunks;
|
2003-10-30 16:51:06 +08:00
|
|
|
if (p->single_buf) {
|
|
|
|
p->free_raw_hunks = p->free_raw_hunks->next;
|
|
|
|
chain->next = NULL;
|
|
|
|
} else {
|
|
|
|
p->free_raw_hunks = NULL;
|
|
|
|
}
|
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 hunks to read in then disable a level event */
|
2003-04-21 22:55:47 +08:00
|
|
|
|
2003-10-17 04:19:16 +08:00
|
|
|
ngx_log_debug(p->log, "no hunks 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
|
|
|
|
2003-10-17 04:19:16 +08:00
|
|
|
ngx_log_debug(p->log, "recv_chain: %d" _ n);
|
2003-04-15 01:04:58 +08:00
|
|
|
|
2003-10-30 16:51:06 +08:00
|
|
|
if (p->free_raw_hunks) {
|
|
|
|
chain->next = p->free_raw_hunks;
|
|
|
|
}
|
2003-10-21 01:14:07 +08:00
|
|
|
p->free_raw_hunks = chain;
|
|
|
|
|
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->hunk);
|
|
|
|
}
|
|
|
|
|
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-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
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
ngx_event_pipe_remove_shadow_links(cl->hunk);
|
2003-10-17 04:19:16 +08:00
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
size = cl->hunk->end - cl->hunk->last;
|
2003-10-14 23:06:38 +08:00
|
|
|
|
|
|
|
if (n >= size) {
|
2003-10-23 00:38:26 +08:00
|
|
|
cl->hunk->last = cl->hunk->end;
|
2003-10-14 23:06:38 +08:00
|
|
|
|
2003-10-31 15:10:36 +08:00
|
|
|
/* STUB */ cl->hunk->num = p->num++;
|
2003-10-22 15:05:29 +08:00
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
if (p->input_filter(p, cl->hunk) == 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 {
|
2003-10-23 00:38:26 +08:00
|
|
|
cl->hunk->last += n;
|
2003-04-15 01:04:58 +08:00
|
|
|
n = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
p->free_raw_hunks = cl;
|
2003-04-15 01:04:58 +08:00
|
|
|
}
|
|
|
|
|
2003-10-17 04:19:16 +08:00
|
|
|
if ((p->upstream_eof || p->upstream_error) && p->free_raw_hunks) {
|
2003-10-31 15:10:36 +08:00
|
|
|
/* STUB */ p->free_raw_hunks->hunk->num = p->num++;
|
2003-10-17 04:19:16 +08:00
|
|
|
if (p->input_filter(p, p->free_raw_hunks->hunk) == NGX_ERROR) {
|
|
|
|
return NGX_ABORT;
|
2003-04-15 01:04:58 +08:00
|
|
|
}
|
|
|
|
|
2003-10-17 04:19:16 +08:00
|
|
|
p->free_raw_hunks = p->free_raw_hunks->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_hunks; cl; cl = cl->next) {
|
|
|
|
ngx_pfree(p->pool, cl->hunk->start);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-22 00:49:56 +08:00
|
|
|
int ngx_event_pipe_write_to_downstream(ngx_event_pipe_t *p)
|
2003-10-14 13:26:00 +08:00
|
|
|
{
|
2003-10-23 00:38:26 +08:00
|
|
|
size_t bsize, to_write;
|
2003-10-17 04:19:16 +08:00
|
|
|
ngx_hunk_t *h;
|
2003-10-23 00:38:26 +08:00
|
|
|
ngx_chain_t *out, **ll, *cl, *tl;
|
2003-10-14 13:26:00 +08:00
|
|
|
|
2003-10-20 03:57:23 +08:00
|
|
|
ngx_log_debug(p->log, "write downstream: %d" _ p->downstream->write->ready);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
if ((p->upstream_eof || p->upstream_error || p->upstream_done)
|
|
|
|
&& p->out == NULL && p->in == NULL)
|
|
|
|
{
|
|
|
|
p->downstream_done = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!p->downstream->write->ready) {
|
|
|
|
break;
|
|
|
|
}
|
2003-10-14 13:26:00 +08:00
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
/*
|
|
|
|
* bsize is the busy hunks size
|
|
|
|
* to_write is the size of data that to be written
|
|
|
|
*/
|
|
|
|
|
|
|
|
bsize = 0;
|
|
|
|
to_write = 0;
|
2003-10-21 15:47:21 +08:00
|
|
|
|
|
|
|
if (!(p->upstream_eof || p->upstream_error || p->upstream_done)) {
|
2003-10-23 00:38:26 +08:00
|
|
|
for (cl = p->busy; cl; cl = cl->next) {
|
|
|
|
bsize += cl->hunk->end - cl->hunk->start;
|
|
|
|
to_write += ngx_hunk_size(cl->hunk);
|
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;
|
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
|
|
|
|
2003-10-22 00:49:56 +08:00
|
|
|
if (!(p->upstream_eof || p->upstream_error || p->upstream_done)
|
2003-10-23 00:38:26 +08:00
|
|
|
&& (bsize + ngx_hunk_size(cl->hunk) > p->busy_size))
|
2003-10-22 00:49:56 +08:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2003-10-14 23:06:38 +08:00
|
|
|
|
2003-10-22 00:49:56 +08:00
|
|
|
p->out = p->out->next;
|
2003-10-23 00:38:26 +08:00
|
|
|
ngx_event_pipe_free_shadow_raw_hunk(&p->free_raw_hunks,
|
|
|
|
cl->hunk);
|
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
|
|
|
|
2003-10-22 00:49:56 +08:00
|
|
|
if (!(p->upstream_eof || p->upstream_error || p->upstream_done)
|
2003-10-23 00:38:26 +08:00
|
|
|
&& (bsize + ngx_hunk_size(cl->hunk) > p->busy_size))
|
2003-10-22 00:49:56 +08:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
p->in = p->in->next;
|
|
|
|
|
|
|
|
} else {
|
2003-10-14 23:06:38 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
bsize += ngx_hunk_size(cl->hunk);
|
|
|
|
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
|
|
|
|
2003-10-22 00:49:56 +08:00
|
|
|
if (out == NULL) {
|
2003-10-23 00:38:26 +08:00
|
|
|
ngx_log_debug(p->log, "no hunks to write BUSY: %d" _ to_write);
|
2003-10-22 15:05:29 +08:00
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
if (!(p->upstream_blocked && to_write)) {
|
2003-10-22 15:05:29 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
/*
|
|
|
|
* if the upstream is blocked and there are the busy hunks
|
|
|
|
* to write then write these hunks
|
|
|
|
*/
|
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;
|
2003-10-22 15:05:29 +08:00
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
/* handle the downstream error at the begin of a cycle */
|
2003-10-22 15:05:29 +08:00
|
|
|
|
2003-10-21 15:47:21 +08:00
|
|
|
continue;
|
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
|
|
|
|
2003-10-31 15:10:36 +08:00
|
|
|
/* TODO: free hunk if p->free_bufs && upstream done */
|
2003-10-22 15:05:29 +08:00
|
|
|
/* add the free shadow raw hunk to p->free_raw_hunks */
|
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
if (cl->hunk->type & NGX_HUNK_LAST_SHADOW) {
|
|
|
|
h = cl->hunk->shadow;
|
2003-10-22 15:05:29 +08:00
|
|
|
h->pos = h->last = h->start;
|
2003-10-14 13:26:00 +08:00
|
|
|
h->shadow = NULL;
|
2003-10-23 00:38:26 +08:00
|
|
|
ngx_alloc_link_and_set_hunk(tl, h, p->pool, NGX_ABORT);
|
|
|
|
ngx_event_pipe_add_free_hunk(&p->free_raw_hunks, tl);
|
2003-10-14 13:26:00 +08:00
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
cl->hunk->type &= ~NGX_HUNK_LAST_SHADOW;
|
2003-10-14 13:26:00 +08:00
|
|
|
}
|
2003-10-23 00:38:26 +08:00
|
|
|
cl->hunk->shadow = NULL;
|
2003-10-22 15:05:29 +08:00
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
if (p->cyclic_temp_file && (cl->hunk->type & NGX_HUNK_TEMP_FILE)) {
|
2003-10-22 15:05:29 +08:00
|
|
|
|
|
|
|
/* reset p->temp_offset if all hunks had been sent */
|
|
|
|
|
2003-11-03 06:56:18 +08:00
|
|
|
if (cl->hunk->file_last == p->temp_file->offset) {
|
|
|
|
p->temp_file->offset = 0;
|
2003-10-22 15:05:29 +08:00
|
|
|
}
|
|
|
|
}
|
2003-10-14 13:26:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-22 00:49:56 +08:00
|
|
|
static int ngx_event_pipe_write_chain_to_temp_file(ngx_event_pipe_t *p)
|
2003-04-15 01:04:58 +08:00
|
|
|
{
|
2003-11-04 06:20:44 +08:00
|
|
|
int size, hsize;
|
2003-11-03 06:56:18 +08:00
|
|
|
char *save_pos;
|
2003-04-15 01:04:58 +08:00
|
|
|
ngx_hunk_t *h;
|
2003-10-23 00:38:26 +08:00
|
|
|
ngx_chain_t *cl, *tl, *next, *out, **ll, **last_free;
|
2003-04-15 01:04:58 +08:00
|
|
|
|
2003-10-17 04:19:16 +08:00
|
|
|
ngx_log_debug(p->log, "write to file");
|
2003-04-15 01:04:58 +08:00
|
|
|
|
2003-11-03 06:56:18 +08:00
|
|
|
out = p->in;
|
2003-04-21 22:55:47 +08:00
|
|
|
|
2003-11-03 06:56:18 +08:00
|
|
|
if (out->hunk->type & NGX_HUNK_PREREAD) {
|
|
|
|
save_pos = out->hunk->pos;
|
|
|
|
out->hunk->pos = out->hunk->start;
|
2003-04-15 01:04:58 +08:00
|
|
|
|
2003-11-03 06:56:18 +08:00
|
|
|
} else {
|
|
|
|
save_pos = NULL;
|
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-10-23 00:38:26 +08:00
|
|
|
cl = p->in;
|
|
|
|
ll = NULL;
|
2003-10-21 15:47:21 +08:00
|
|
|
|
2003-11-03 06:56:18 +08:00
|
|
|
ngx_log_debug(p->log, "offset: %d" _ p->temp_file->offset);
|
2003-04-15 01:04:58 +08:00
|
|
|
|
|
|
|
do {
|
2003-10-23 00:38:26 +08:00
|
|
|
hsize = cl->hunk->last - cl->hunk->pos;
|
2003-10-21 15:47:21 +08:00
|
|
|
|
2003-10-22 15:05:29 +08:00
|
|
|
ngx_log_debug(p->log, "hunk size: %d" _ hsize);
|
2003-10-21 15:47:21 +08:00
|
|
|
|
2003-10-22 15:05:29 +08:00
|
|
|
if ((size + hsize > p->temp_file_write_size)
|
2003-11-03 06:56:18 +08:00
|
|
|
|| (p->temp_file->offset + size + hsize > 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
|
|
|
|
2003-10-22 15:05:29 +08:00
|
|
|
size += hsize;
|
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
|
|
|
|
2003-10-21 15:47:21 +08:00
|
|
|
ngx_log_debug(p->log, "size: %d" _ size);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2003-10-20 03:57:23 +08:00
|
|
|
for (last_free = &p->free_raw_hunks;
|
|
|
|
*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 */
|
|
|
|
}
|
|
|
|
|
2003-11-03 06:56:18 +08:00
|
|
|
if (out->hunk->type & NGX_HUNK_PREREAD) {
|
|
|
|
p->temp_file->offset += save_pos - out->hunk->pos;
|
|
|
|
out->hunk->pos = save_pos;
|
|
|
|
out->hunk->type &= ~NGX_HUNK_PREREAD;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
h = cl->hunk;
|
2003-11-03 06:56:18 +08:00
|
|
|
h->file = &p->temp_file->file;
|
|
|
|
h->file_pos = p->temp_file->offset;
|
|
|
|
p->temp_file->offset += h->last - h->pos;
|
|
|
|
h->file_last = p->temp_file->offset;
|
2003-04-15 01:04:58 +08:00
|
|
|
|
2003-10-22 15:05:29 +08:00
|
|
|
if (p->cachable) {
|
|
|
|
h->type |= NGX_HUNK_FILE;
|
|
|
|
} else {
|
|
|
|
h->type |= NGX_HUNK_FILE|NGX_HUNK_TEMP_FILE;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2003-04-15 23:06:52 +08:00
|
|
|
if (h->type & NGX_HUNK_LAST_SHADOW) {
|
2003-04-18 01:59:35 +08:00
|
|
|
h->shadow->last = h->shadow->pos = h->shadow->start;
|
2003-10-23 00:38:26 +08:00
|
|
|
ngx_alloc_link_and_set_hunk(tl, h->shadow, p->pool, NGX_ABORT);
|
|
|
|
*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 */
|
|
|
|
|
2003-10-22 00:49:56 +08:00
|
|
|
int ngx_event_pipe_copy_input_filter(ngx_event_pipe_t *p, ngx_hunk_t *hunk)
|
2003-04-15 23:06:52 +08:00
|
|
|
{
|
|
|
|
ngx_hunk_t *h;
|
2003-10-23 00:38:26 +08:00
|
|
|
ngx_chain_t *cl;
|
2003-04-15 23:06:52 +08:00
|
|
|
|
2003-10-14 23:06:38 +08:00
|
|
|
if (hunk->pos == hunk->last) {
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
2003-04-15 23:06:52 +08:00
|
|
|
|
2003-10-14 23:06:38 +08:00
|
|
|
if (p->free) {
|
|
|
|
h = p->free->hunk;
|
|
|
|
p->free = p->free->next;
|
2003-04-15 23:06:52 +08:00
|
|
|
|
2003-10-14 23:06:38 +08:00
|
|
|
} else {
|
|
|
|
ngx_test_null(h, ngx_alloc_hunk(p->pool), NGX_ERROR);
|
|
|
|
}
|
2003-04-15 23:06:52 +08:00
|
|
|
|
2003-10-14 23:06:38 +08:00
|
|
|
ngx_memcpy(h, hunk, sizeof(ngx_hunk_t));
|
|
|
|
h->shadow = hunk;
|
2003-10-22 15:05:29 +08:00
|
|
|
h->tag = p->tag;
|
2003-10-14 23:06:38 +08:00
|
|
|
h->type |= NGX_HUNK_LAST_SHADOW|NGX_HUNK_RECYCLED;
|
|
|
|
hunk->shadow = h;
|
2003-04-15 23:06:52 +08:00
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
ngx_alloc_link_and_set_hunk(cl, h, p->pool, NGX_ERROR);
|
2003-10-22 15:05:29 +08:00
|
|
|
ngx_log_debug(p->log, "HUNK %d" _ h->num);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
ngx_inline static void ngx_event_pipe_remove_shadow_links(ngx_hunk_t *hunk)
|
2003-10-14 23:06:38 +08:00
|
|
|
{
|
2003-10-17 04:19:16 +08:00
|
|
|
ngx_hunk_t *h, *next;
|
|
|
|
|
|
|
|
if (hunk->shadow == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
h = hunk->shadow;
|
|
|
|
|
|
|
|
while (!(h->type & NGX_HUNK_LAST_SHADOW)) {
|
|
|
|
next = h->shadow;
|
|
|
|
h->type &= ~(NGX_HUNK_TEMP|NGX_HUNK_IN_MEMORY|NGX_HUNK_RECYCLED);
|
|
|
|
h->shadow = NULL;
|
|
|
|
h = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
h->type &= ~(NGX_HUNK_TEMP
|
|
|
|
|NGX_HUNK_IN_MEMORY
|
|
|
|
|NGX_HUNK_RECYCLED
|
|
|
|
|NGX_HUNK_LAST_SHADOW);
|
|
|
|
h->shadow = NULL;
|
|
|
|
|
|
|
|
hunk->shadow = NULL;
|
2003-10-14 23:06:38 +08:00
|
|
|
}
|
2003-04-15 23:06:52 +08:00
|
|
|
|
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
ngx_inline static void ngx_event_pipe_free_shadow_raw_hunk(ngx_chain_t **free,
|
|
|
|
ngx_hunk_t *h)
|
2003-10-14 23:06:38 +08:00
|
|
|
{
|
2003-10-17 04:19:16 +08:00
|
|
|
ngx_hunk_t *s;
|
2003-10-23 00:38:26 +08:00
|
|
|
ngx_chain_t *cl, **ll;
|
2003-04-15 23:06:52 +08:00
|
|
|
|
2003-10-14 23:06:38 +08:00
|
|
|
if (h->shadow == NULL) {
|
|
|
|
return;
|
2003-04-15 23:06:52 +08:00
|
|
|
}
|
|
|
|
|
2003-10-14 23:06:38 +08:00
|
|
|
for (s = h->shadow; !(s->type & NGX_HUNK_LAST_SHADOW); s = s->shadow) {
|
|
|
|
/* void */
|
|
|
|
}
|
|
|
|
|
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->hunk == s) {
|
|
|
|
*ll = cl->next;
|
2003-10-14 23:06:38 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
if (cl->hunk->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
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
ngx_inline static void ngx_event_pipe_add_free_hunk(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)->hunk->pos != (*chain)->hunk->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
|
|
|
|
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
static int ngx_event_pipe_drain_chains(ngx_event_pipe_t *p)
|
2003-10-21 15:47:21 +08:00
|
|
|
{
|
|
|
|
ngx_hunk_t *h;
|
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-10-21 15:47:21 +08:00
|
|
|
|
|
|
|
} else if (p->out) {
|
2003-10-23 00:38:26 +08:00
|
|
|
cl = p->out;
|
2003-10-21 15:47:21 +08:00
|
|
|
|
|
|
|
} else if (p->in) {
|
2003-10-23 00:38:26 +08:00
|
|
|
cl = p->in;
|
2003-10-21 15:47:21 +08:00
|
|
|
|
|
|
|
} else {
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
while (cl) {
|
|
|
|
if (cl->hunk->type & NGX_HUNK_LAST_SHADOW) {
|
|
|
|
h = cl->hunk->shadow;
|
|
|
|
h->pos = h->last = h->start;
|
2003-10-21 15:47:21 +08:00
|
|
|
h->shadow = NULL;
|
2003-10-23 00:38:26 +08:00
|
|
|
ngx_alloc_link_and_set_hunk(tl, h, p->pool, NGX_ABORT);
|
|
|
|
ngx_event_pipe_add_free_hunk(&p->free_raw_hunks, tl);
|
2003-10-21 15:47:21 +08:00
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
cl->hunk->type &= ~NGX_HUNK_LAST_SHADOW;
|
2003-10-21 15:47:21 +08:00
|
|
|
}
|
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
cl->hunk->shadow = NULL;
|
|
|
|
tl = cl->next;
|
|
|
|
cl->next = p->free;
|
|
|
|
p->free = cl;
|
|
|
|
cl = tl;
|
2003-10-21 15:47:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|