2003-10-27 16:53:49 +08:00
|
|
|
|
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
2003-10-28 05:01:00 +08:00
|
|
|
#include <ngx_event.h>
|
2003-10-27 16:53:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
#define NGX_NONE 1
|
|
|
|
|
|
|
|
|
|
|
|
ngx_inline static int ngx_output_chain_need_to_copy(ngx_output_chain_ctx_t *ctx,
|
|
|
|
ngx_hunk_t *hunk);
|
|
|
|
static int ngx_output_chain_copy_hunk(ngx_hunk_t *dst, ngx_hunk_t *src,
|
2003-11-21 14:30:49 +08:00
|
|
|
u_int sendfile);
|
2003-10-27 16:53:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
int ngx_output_chain(ngx_output_chain_ctx_t *ctx, ngx_chain_t *in)
|
|
|
|
{
|
|
|
|
int rc, last;
|
2003-11-21 14:30:49 +08:00
|
|
|
size_t size, hsize;
|
2003-10-27 16:53:49 +08:00
|
|
|
ngx_chain_t *cl, *out, **last_out;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* the short path for the case when the chain ctx->in is empty
|
|
|
|
* and the incoming chain is empty too or it has the single hunk
|
|
|
|
* that does not require the copy
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ctx->in == NULL) {
|
|
|
|
|
|
|
|
if (in == NULL) {
|
2004-03-23 14:01:52 +08:00
|
|
|
return ctx->output_filter(ctx->filter_ctx, in);
|
2003-10-27 16:53:49 +08:00
|
|
|
}
|
|
|
|
|
2003-10-28 05:01:00 +08:00
|
|
|
if (in->next == NULL
|
2003-10-27 16:53:49 +08:00
|
|
|
&& (!ngx_output_chain_need_to_copy(ctx, in->hunk)))
|
|
|
|
{
|
2004-03-23 14:01:52 +08:00
|
|
|
return ctx->output_filter(ctx->filter_ctx, in);
|
2003-10-27 16:53:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add the incoming hunk to the chain ctx->in */
|
|
|
|
|
|
|
|
if (in) {
|
|
|
|
if (ngx_chain_add_copy(ctx->pool, &ctx->in, in) == NGX_ERROR) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
last = NGX_NONE;
|
2003-10-29 16:30:44 +08:00
|
|
|
out = NULL;
|
2003-10-27 16:53:49 +08:00
|
|
|
last_out = &out;
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
|
|
|
|
while (ctx->in) {
|
|
|
|
|
|
|
|
if (!ngx_output_chain_need_to_copy(ctx, ctx->in->hunk)) {
|
|
|
|
|
|
|
|
/* move the chain link to the chain out */
|
|
|
|
|
|
|
|
cl = ctx->in;
|
|
|
|
ctx->in = cl->next;
|
|
|
|
|
|
|
|
*last_out = cl;
|
|
|
|
last_out = &cl->next;
|
|
|
|
cl->next = NULL;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->hunk == NULL) {
|
|
|
|
|
|
|
|
/* get the free hunk */
|
|
|
|
|
|
|
|
if (ctx->free) {
|
|
|
|
ctx->hunk = ctx->free->hunk;
|
|
|
|
ctx->free = ctx->free->next;
|
|
|
|
|
|
|
|
} else if (ctx->hunks < ctx->bufs.num) {
|
|
|
|
|
|
|
|
size = ctx->bufs.size;
|
|
|
|
|
|
|
|
if (ctx->in->hunk->type & NGX_HUNK_LAST) {
|
|
|
|
|
|
|
|
hsize = ngx_hunk_size(ctx->in->hunk);
|
|
|
|
|
|
|
|
if (hsize < ctx->bufs.size) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* allocate small temp hunk for the small last hunk
|
|
|
|
* or its small last part
|
|
|
|
*/
|
|
|
|
|
|
|
|
size = hsize;
|
|
|
|
|
|
|
|
} else if (ctx->bufs.num == 1
|
|
|
|
&& (hsize < ctx->bufs.size
|
|
|
|
+ (ctx->bufs.size >> 2)))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* allocate a temp hunk that equals
|
|
|
|
* to the last hunk if the last hunk size is lesser
|
|
|
|
* than 1.25 of bufs.size and a temp hunk is single
|
|
|
|
*/
|
|
|
|
|
|
|
|
size = hsize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_test_null(ctx->hunk,
|
2003-10-30 16:51:06 +08:00
|
|
|
ngx_create_temp_hunk(ctx->pool, size),
|
2003-10-27 16:53:49 +08:00
|
|
|
NGX_ERROR);
|
|
|
|
ctx->hunk->tag = ctx->tag;
|
|
|
|
ctx->hunk->type |= NGX_HUNK_RECYCLED;
|
|
|
|
ctx->hunks++;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ngx_output_chain_copy_hunk(ctx->hunk, ctx->in->hunk,
|
|
|
|
ctx->sendfile);
|
|
|
|
|
|
|
|
if (rc == NGX_ERROR) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc == NGX_AGAIN) {
|
|
|
|
if (out) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* delete the completed hunk from the chain ctx->in */
|
|
|
|
|
|
|
|
if (ngx_hunk_size(ctx->in->hunk) == 0) {
|
|
|
|
ctx->in = ctx->in->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_alloc_link_and_set_hunk(cl, ctx->hunk, ctx->pool, NGX_ERROR);
|
|
|
|
*last_out = cl;
|
|
|
|
last_out = &cl->next;
|
|
|
|
ctx->hunk = NULL;
|
|
|
|
|
|
|
|
if (ctx->free == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out == NULL && last != NGX_NONE) {
|
|
|
|
return last;
|
|
|
|
}
|
|
|
|
|
2004-03-23 14:01:52 +08:00
|
|
|
last = ctx->output_filter(ctx->filter_ctx, out);
|
2003-10-27 16:53:49 +08:00
|
|
|
|
|
|
|
ngx_chain_update_chains(&ctx->free, &ctx->busy, &out, ctx->tag);
|
|
|
|
last_out = &out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ngx_inline static int ngx_output_chain_need_to_copy(ngx_output_chain_ctx_t *ctx,
|
|
|
|
ngx_hunk_t *hunk)
|
|
|
|
{
|
|
|
|
if (ngx_hunk_special(hunk)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-11-03 06:56:18 +08:00
|
|
|
if (!ctx->sendfile) {
|
|
|
|
if (!(hunk->type & NGX_HUNK_IN_MEMORY)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
hunk->type &= ~NGX_HUNK_FILE;
|
2003-10-27 16:53:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->need_in_memory && (!(hunk->type & NGX_HUNK_IN_MEMORY))) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (ctx->need_in_temp && (hunk->type & (NGX_HUNK_MEMORY|NGX_HUNK_MMAP))) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int ngx_output_chain_copy_hunk(ngx_hunk_t *dst, ngx_hunk_t *src,
|
2003-11-21 14:30:49 +08:00
|
|
|
u_int sendfile)
|
2003-10-27 16:53:49 +08:00
|
|
|
{
|
2003-11-21 14:30:49 +08:00
|
|
|
size_t size;
|
|
|
|
ssize_t n;
|
2003-10-27 16:53:49 +08:00
|
|
|
|
|
|
|
size = ngx_hunk_size(src);
|
|
|
|
|
2003-11-21 14:30:49 +08:00
|
|
|
if (size > (size_t) (dst->end - dst->pos)) {
|
2003-10-27 16:53:49 +08:00
|
|
|
size = dst->end - dst->pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (src->type & NGX_HUNK_IN_MEMORY) {
|
|
|
|
ngx_memcpy(dst->pos, src->pos, size);
|
|
|
|
src->pos += size;
|
|
|
|
dst->last += size;
|
|
|
|
|
|
|
|
if (src->type & NGX_HUNK_FILE) {
|
|
|
|
src->file_pos += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((src->type & NGX_HUNK_LAST) && src->pos == src->last) {
|
|
|
|
dst->type |= NGX_HUNK_LAST;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
n = ngx_read_file(src->file, dst->pos, size, src->file_pos);
|
|
|
|
|
|
|
|
if (n == NGX_ERROR) {
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if (NGX_FILE_AIO_READ)
|
|
|
|
if (n == NGX_AGAIN) {
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-11-21 14:30:49 +08:00
|
|
|
if ((size_t) n != size) {
|
2003-10-27 16:53:49 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, src->file->log, 0,
|
|
|
|
ngx_read_file_n " reads only %d of %d from file",
|
|
|
|
n, size);
|
|
|
|
if (n == 0) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
src->file_pos += n;
|
|
|
|
dst->last += n;
|
|
|
|
|
|
|
|
if (!sendfile) {
|
|
|
|
dst->type &= ~NGX_HUNK_FILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((src->type & NGX_HUNK_LAST) && src->file_pos == src->file_last) {
|
|
|
|
dst->type |= NGX_HUNK_LAST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
2003-10-28 05:01:00 +08:00
|
|
|
|
|
|
|
|
2003-10-30 16:51:06 +08:00
|
|
|
int ngx_chain_writer(void *data, ngx_chain_t *in)
|
2003-10-28 05:01:00 +08:00
|
|
|
{
|
2003-10-30 16:51:06 +08:00
|
|
|
ngx_chain_writer_ctx_t *ctx = data;
|
2003-10-28 05:01:00 +08:00
|
|
|
|
|
|
|
ngx_chain_t *cl;
|
|
|
|
|
|
|
|
|
|
|
|
for (/* void */; in; in = in->next) {
|
|
|
|
ngx_alloc_link_and_set_hunk(cl, in->hunk, ctx->pool, NGX_ERROR);
|
|
|
|
*ctx->last = cl;
|
|
|
|
ctx->last = &cl->next;
|
|
|
|
}
|
|
|
|
|
2004-03-16 21:35:20 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_CORE, ctx->connection->log, 0,
|
|
|
|
"WRITER0: %X", ctx->out);
|
|
|
|
|
2003-10-28 05:01:00 +08:00
|
|
|
ctx->out = ngx_write_chain(ctx->connection, ctx->out);
|
|
|
|
|
2004-03-16 21:35:20 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_CORE, ctx->connection->log, 0,
|
|
|
|
"WRITER1: %X", ctx->out);
|
|
|
|
|
2003-10-28 05:01:00 +08:00
|
|
|
if (ctx->out == NGX_CHAIN_ERROR) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->out == NULL) {
|
|
|
|
ctx->last = &ctx->out;
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_AGAIN;
|
|
|
|
}
|