nginx-0.0.1-2003-10-27-11:53:49 import
diff --git a/src/core/ngx_file.c b/src/core/ngx_file.c
index 2383e5c..84e86ff 100644
--- a/src/core/ngx_file.c
+++ b/src/core/ngx_file.c
@@ -7,6 +7,27 @@
 static int ngx_random;
 
 
+int ngx_write_chain_to_temp_file(ngx_temp_file_t *tf, ngx_chain_t *chain)
+{
+    int  rc;
+
+    if (tf->file.fd == NGX_INVALID_FILE) {
+        rc = ngx_create_temp_file(&tf->file, &tf->path, tf->pool,
+                                  tf->persistent);
+    
+        if (rc == NGX_ERROR || rc == NGX_AGAIN) {
+            return rc;
+        }
+
+        if (!tf->persistent && tf->warn) {
+            ngx_log_error(NGX_LOG_WARN, tf->file.log, 0, tf->warn);
+        }
+    }
+
+    return ngx_write_chain_to_file(&tf->file, chain, tf->file.offset, tf->pool);
+}
+
+
 int ngx_create_temp_file(ngx_file_t *file, ngx_path_t *path,
                          ngx_pool_t *pool, int persistent)
 {
diff --git a/src/core/ngx_file.h b/src/core/ngx_file.h
index ef3f783..660750a 100644
--- a/src/core/ngx_file.h
+++ b/src/core/ngx_file.h
@@ -26,6 +26,17 @@
 } ngx_path_t;
 
 
+typedef struct {
+    ngx_file_t   file;
+    ngx_path_t   path;
+    ngx_pool_t  *pool;
+    char        *warn;
+
+    unsigned     persistent:1;
+} ngx_temp_file_t;
+
+
+int ngx_write_chain_to_temp_file(ngx_temp_file_t *tf, ngx_chain_t *chain);
 int ngx_create_temp_file(ngx_file_t *file, ngx_path_t *path,
                          ngx_pool_t *pool, int persistent);
 void ngx_create_hashed_filename(ngx_file_t *file, ngx_path_t *path);
@@ -46,7 +57,7 @@
             conf->level[0] = l1;                                             \
             conf->level[1] = l2;                                             \
             conf->level[2] = l3;                                             \
-            conf->len = l1 + l2 + l3 + l1 ? 1:0 + l2 ? 1:0 + l3 ? 1:0;       \
+            conf->len = l1 + l2 + l3 + (l1 ? 1:0) + (l2 ? 1:0) + (l3 ? 1:0); \
         } else {                                                             \
             conf = prev;                                                     \
         }                                                                    \
diff --git a/src/core/ngx_hunk.c b/src/core/ngx_hunk.c
index 100bc62..d69c0df 100644
--- a/src/core/ngx_hunk.c
+++ b/src/core/ngx_hunk.c
@@ -26,6 +26,48 @@
     return h;
 }
 
+
+ngx_chain_t *ngx_create_chain_of_hunks(ngx_pool_t *pool, ngx_bufs_t *bufs)
+{
+    int           i;
+    char         *p;
+    ngx_hunk_t   *h;
+    ngx_chain_t  *chain, *cl, **ll;
+
+    ngx_test_null(p, ngx_palloc(pool, bufs->num * bufs->size), NULL);
+
+    ll = &chain;
+
+    for (i = 0; i < bufs->num; i++) {
+        ngx_test_null(h, ngx_alloc_hunk(pool), NULL);
+
+        h->pos = p;
+        h->last = p;
+        h->file_pos = 0;
+        h->file_last = 0;
+
+        h->type = NGX_HUNK_IN_MEMORY|NGX_HUNK_TEMP;
+
+        h->start = p;
+        p += bufs->size;
+        h->end = p;
+
+        h->file = NULL;
+        h->shadow = NULL;
+        h->tag = 0;
+
+        ngx_test_null(cl, ngx_alloc_chain_link(pool), NULL);
+        cl->hunk = h;
+        *ll = cl;
+        ll = &cl->next;
+    }
+
+    *ll = NULL;
+
+    return chain;
+}
+
+
 ngx_hunk_t *ngx_create_hunk_before(ngx_pool_t *pool, ngx_hunk_t *hunk, int size)
 {
     ngx_hunk_t *h;
@@ -61,6 +103,7 @@
     return h;
 }
 
+
 ngx_hunk_t *ngx_create_hunk_after(ngx_pool_t *pool, ngx_hunk_t *hunk, int size)
 {
     ngx_hunk_t *h;
diff --git a/src/core/ngx_hunk.h b/src/core/ngx_hunk.h
index abbdcfe..e9bb71c 100644
--- a/src/core/ngx_hunk.h
+++ b/src/core/ngx_hunk.h
@@ -73,6 +73,29 @@
 } ngx_bufs_t;
 
 
+typedef int  (*ngx_output_chain_filter_pt)(void *ctx, ngx_chain_t *out);
+
+typedef struct {
+    ngx_hunk_t                  *hunk;
+    ngx_chain_t                 *in;
+    ngx_chain_t                 *free;
+    ngx_chain_t                 *busy;
+
+    unsigned                     sendfile;
+    unsigned                     need_in_memory;
+    unsigned                     need_in_temp;
+    unsigned                     copy_chain;
+
+    ngx_pool_t                  *pool;
+    int                          hunks;
+    ngx_bufs_t                   bufs;
+    ngx_hunk_tag_t               tag;
+
+    ngx_output_chain_filter_pt   output_filter;
+    void                        *output_ctx;
+} ngx_output_chain_ctx_t;
+
+
 #define NGX_CHAIN_ERROR     (ngx_chain_t *) NGX_ERROR
 
 
@@ -120,6 +143,7 @@
             last = &cl->next
 
 
+int ngx_output_chain(ngx_output_chain_ctx_t *ctx, ngx_chain_t *in);
 int ngx_chain_add_copy(ngx_pool_t *pool, ngx_chain_t **chain, ngx_chain_t *in);
 void ngx_chain_update_chains(ngx_chain_t **free, ngx_chain_t **busy,
                              ngx_chain_t **out, ngx_hunk_tag_t tag);
diff --git a/src/core/ngx_log.h b/src/core/ngx_log.h
index dcbbef8..959c132 100644
--- a/src/core/ngx_log.h
+++ b/src/core/ngx_log.h
@@ -63,11 +63,14 @@
 */
 
 
+typedef size_t  (*ngx_log_handler_pt) (void *ctx, char *buf, size_t len);
+
+
 typedef struct {
-    int               log_level;
-    ngx_open_file_t  *file;
-    void             *data;
-    size_t           (*handler)(void *ctx, char *buf, size_t len);
+    int                  log_level;
+    ngx_open_file_t     *file;
+    void                *data;
+    ngx_log_handler_pt   handler;
 
 #if 0
 /* STUB */
diff --git a/src/core/ngx_output_chain.c b/src/core/ngx_output_chain.c
new file mode 100644
index 0000000..6ef592d
--- /dev/null
+++ b/src/core/ngx_output_chain.c
@@ -0,0 +1,252 @@
+
+#include <ngx_config.h>
+#include <ngx_core.h>
+
+
+#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,
+                                      int sendfile);
+
+
+int ngx_output_chain(ngx_output_chain_ctx_t *ctx, ngx_chain_t *in)
+{
+    int           rc, last;
+    ssize_t       size, hsize;
+    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) {
+            return ctx->output_filter(ctx->output_ctx, in);
+        }
+
+        if (!ctx->copy_chain
+            && in->next == NULL
+            && (!ngx_output_chain_need_to_copy(ctx, in->hunk)))
+        {
+            return ctx->output_filter(ctx->output_ctx, in);
+        }
+    }
+
+    /* 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;
+    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,
+                                  ngx_create_temp_hunk(ctx->pool, size, 0, 0),
+                                  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;
+        }
+
+        last = ctx->output_filter(ctx->output_ctx, out);
+
+        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;
+    }
+
+    if (!ctx->sendfile) {
+        return 1;
+    }
+
+    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,
+                                      int sendfile)
+{
+    ssize_t  n, size;
+
+    size = ngx_hunk_size(src);
+
+    if (size > (dst->end - dst->pos)) {
+        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 == 0) {
+ngx_log_debug(src->file->log, "READ: %qd:%qd %X:%X %X:%X" _
+              src->file_pos _ src->file_last _
+              dst->pos _ dst->last _ dst->start _ dst->end);
+}
+
+        if (n == NGX_ERROR) {
+            return n;
+        }
+
+#if (NGX_FILE_AIO_READ)
+        if (n == NGX_AGAIN) {
+            return n;
+        }
+#endif
+
+        if (n != size) {
+            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;
+}