nginx-0.0.1-2003-04-14-21:04:58 import
diff --git a/src/http/modules/proxy/ngx_http_event_proxy_handler.c b/src/http/modules/proxy/ngx_http_event_proxy_handler.c
index 6977503..3d76668 100644
--- a/src/http/modules/proxy/ngx_http_event_proxy_handler.c
+++ b/src/http/modules/proxy/ngx_http_event_proxy_handler.c
@@ -7,6 +7,7 @@
 #include <ngx_inet.h>
 #include <ngx_conf_file.h>
 #include <ngx_event_write.h>
+#include <ngx_event_proxy.h>
 
 #include <ngx_http.h>
 #include <ngx_http_config.h>
@@ -15,6 +16,11 @@
 #include <ngx_http_event_proxy_handler.h>
 
 
+/* STUB */
+typedef struct {
+    int   dummy;
+} ngx_cache_header_t;
+
 static int ngx_http_proxy_handler(ngx_http_request_t *r);
 
 static ngx_chain_t *ngx_http_proxy_create_request(ngx_http_proxy_ctx_t *p);
@@ -30,8 +36,8 @@
 static int ngx_http_proxy_process_upstream_headers(ngx_http_proxy_ctx_t *p);
 static int ngx_http_proxy_process_upstream_header_line(ngx_http_proxy_ctx_t *p);
 
-
-static int ngx_http_proxy_write_upstream_body(ngx_event_t *wev);
+static int ngx_http_proxy_read_upstream_body(ngx_http_proxy_ctx_t *p);
+static int ngx_http_proxy_write_upstream_body(ngx_http_proxy_ctx_t *p);
 
 
 static int ngx_http_proxy_read_response_body(ngx_event_t *ev);
@@ -715,7 +721,11 @@
     r = p->request;
 
     ngx_test_null(p->header_in,
-                  ngx_create_temp_hunk(r->pool, p->lcf->header_size, 0, 0),
+                  ngx_create_temp_hunk(r->pool,
+                                       p->lcf->header_size
+                                                  - sizeof(ngx_cache_header_t),
+                                       sizeof(ngx_cache_header_t),
+                                       0),
                   NGX_ERROR);
 
     p->header_in->type = NGX_HUNK_MEMORY|NGX_HUNK_IN_MEMORY;
@@ -748,7 +758,9 @@
     int                  i, n, rc;
     ngx_event_t         *rev;
     ngx_table_elt_t     *ch, *ph;
+    ngx_event_proxy_t   *ep;
     ngx_http_request_t  *r;
+    ngx_http_proxy_log_ctx_t   *lcx;
 
     rev = p->connection->read;
 
@@ -833,10 +845,53 @@
 
         rc = ngx_http_send_header(r);
 
+#if 1
+        rc = ngx_http_output_filter(r, p->header_in);
+
+        ngx_test_null(ep, ngx_pcalloc(r->pool, sizeof(ngx_event_proxy_t)),
+                      NGX_ERROR);
+
+        ep->output_filter = (ngx_event_proxy_output_filter_pt)
+                                                        ngx_http_output_filter;
+        ep->output_data = r;
+        ep->block_size = p->lcf->block_size;
+        ep->max_block_size = p->lcf->max_block_size;
+        ep->file_block_size = p->lcf->file_block_size;
+        ep->upstream = p->connection;
+        ep->client = r->connection;
+        ep->pool = r->pool;
+        ep->log = p->log;
+        ep->temp_path = p->lcf->temp_path;
+
+        ngx_test_null(ep->temp_file, ngx_palloc(r->pool, sizeof(ngx_file_t)),
+                      NGX_ERROR);
+        ep->temp_file->fd = NGX_INVALID_FILE;
+        ep->temp_file->log = p->log;
+
+        ep->number = 10;
+        ep->random = 5;
+
+        ep->max_temp_size = 6000;
+        ep->temp_file_warn = "an upstream response is buffered "
+                             "to a temporary file";
+
+        p->event_proxy = ep;
+
+        lcx = p->log->data;
+        lcx->action = "reading an upstream";
+
+        p->state_read_upstream_handler = ngx_http_proxy_read_upstream_body;
+        p->state_write_upstream_handler = ngx_http_proxy_write_upstream_body;
+
+        ngx_http_proxy_read_upstream_body(p);
+#endif
+
+#if 0
         /* STUB */
         p->header_in->type |= NGX_HUNK_LAST;
         rc = ngx_http_output_filter(r, p->header_in);
         ngx_http_proxy_finalize_request(p, NGX_OK);
+#endif
 
         /* STUB */ return NGX_DONE;
     }
@@ -1030,11 +1085,32 @@
 }
 
 
+static int ngx_http_proxy_read_upstream_body(ngx_http_proxy_ctx_t *p)
+{
+    int  rc;
+
+    rc = ngx_event_proxy_read_upstream(p->event_proxy);
+    if (rc == NGX_OK) {
+        rc = ngx_event_close_connection(p->connection->read);
+    }
+
+    return rc;
+}
+
+
+static int ngx_http_proxy_write_upstream_body(ngx_http_proxy_ctx_t *p)
+{
+    return ngx_event_proxy_write_to_client(p->event_proxy);
+}
+
+
+#if 0
 static int ngx_http_proxy_read_upstream_body(ngx_event_t *rev)
 {
-    int                    rc, n, size;
+    int                    rc, n, size, eof;
     ngx_hunk_t            *h;
-    ngx_chain_t           *chain, chain_entry, *ce, *te;
+    ngx_chain_t           *chain, *ce, *tce;
+    ngx_event_t           *wev;
     ngx_connection_t      *c;
     ngx_http_request_t    *r;
     ngx_http_proxy_ctx_t  *p;
@@ -1044,13 +1120,14 @@
     p = (ngx_http_proxy_ctx_t *)
                          ngx_http_get_module_ctx(r, ngx_http_proxy_module_ctx);
 
-    chain_entry.next = NULL;
+    eof = 0;
 
     for ( ;; ) {
 
 #if (USE_KQUEUE)
 
         if (ev->eof && ev->available == 0) {
+            eof = 1;
             break;
         }
 
@@ -1059,82 +1136,59 @@
         if (ngx_event_type == NGX_HAVE_KQUEUE_EVENT
             && ev->eof && ev->available == 0)
         {
+            eof = 1;
             break;
         }
 
 #endif
 
+        /* use the free hunks if they exist */
+
         if (p->free_hunks) {
             chain = p->free_hunks;
             p->free_hunks = NULL;
 
+        /* allocate a new hunk if it's still allowed */
+
         } else if (p->allocated < p->lcf->max_block_size) {
             ngx_test_null(h,
                           ngx_create_temp_hunk(r->pool, p->block_size, 50, 50),
                           NGX_ERROR);
 
             p->allocated += p->block_size;
-            chain_entry.hunk = h;
-            chain = &chain_entry;
+
+            ngx_test_null(tce, ngx_create_chain_entry(r->pool), NGX_ERROR);
+            tce->hunk = h;
+            tce->next = NULL;
+            chain = tce;
+
+        /* use the shadow hunks if they exist */
+
+        } else if (p->shadow_hunks) {
+            chain = p->shadow_hunks;
+            p->shadow_hunks = NULL;
+
+        /* write all the incoming hunks or the first hunk only
+           to a temporary file and convert them to the shadow hunks */
 
         } else {
-            if (p->temp_file->fd == NGX_INVALID_FILE) {
-                rc = ngx_create_temp_file(p->temp_file, p->lcf->temp_path,
-                                          r->pool, 0, 2, r->cachable);
-
+            if (r->cachable) {
+                rc = ngx_http_proxy_write_chain_to_temp_file(p);
                 if (rc != NGX_OK) {
                     return rc;
                 }
 
-                if (p->lcf->temp_file_warn) {
-                    ngx_log_error(NGX_LOG_WARN, p->log, 0,
-                                  "an upstream response is buffered "
-                                  "to a temporary file");
+            } else {
+                tce = p->in_hunks->next;
+                p->in_hunks->next = NULL;
+
+                rc = ngx_http_proxy_write_chain_to_temp_file(p);
+                if (rc != NGX_OK) {
+                    p->in_hunks = tce;
+                    return rc;
                 }
-            }
 
-            n = ngx_write_chain_to_file(p->temp_file, p->in_hunks,
-                                        p->temp_offset, r->pool);
-
-            if (n == NGX_ERROR) {
-                return NGX_ERROR;
-            }
-
-            for (ce = p->in_hunks; ce; ce = ce->next) {
-                ngx_test_null(h, ngx_pcalloc(r->pool, sizeof(ngx_hunk_t)),
-                              NGX_ERROR);
-
-                h->type = NGX_HUNK_FILE
-                          |NGX_HUNK_TEMP|NGX_HUNK_IN_MEMORY|NGX_HUNK_RECYCLED;
-
-                ce->hunk->shadow = h;
-                h->shadow = ce->hunk;
-
-                h->file_pos = p->temp_offset;
-                p->temp_offset += ce->hunk->last - ce->hunk->pos;
-                h->file_last = p->temp_offset;
-
-                h->file->fd = p->temp_file->fd;
-                h->file->log = p->log;
-
-                h->pos = ce->hunk->pos;
-                h->last = ce->hunk->last;
-                h->start = ce->hunk->start;
-                h->end = ce->hunk->end;
-                h->pre_start = ce->hunk->pre_start;
-                h->post_end = ce->hunk->post_end;
-
-                ngx_test_null(te, ngx_create_chain_entry(r->pool), NGX_ERROR);
-                te->hunk = h;
-                te->next = NULL;
-
-                if (p->last_out_hunk) {
-                    p->last_out_hunk->next = te;
-                    p->last_out_hunk = te;
-
-                } else {
-                    p->last_out_hunk = te;
-                }
+                p->in_hunks = tce;
             }
         }
 
@@ -1149,20 +1203,21 @@
         }
 
         if (n == 0) {
+            eof = 1;
             break;
         }
 
         for (ce = chain; ce && n > 0; ce = ce->next) {
-            ngx_test_null(te, ngx_create_chain_entry(r->pool), NGX_ERROR);
-            te->hunk = ce->hunk;
-            te->next = NULL;
+            ngx_test_null(tce, ngx_create_chain_entry(r->pool), NGX_ERROR);
+            tce->hunk = ce->hunk;
+            tce->next = NULL;
 
             if (p->last_in_hunk) {
-                p->last_in_hunk->next = te;
-                p->last_in_hunk = te;
+                p->last_in_hunk->next = tce;
+                p->last_in_hunk = tce;
 
             } else {
-                p->last_in_hunk = te;
+                p->last_in_hunk = tce;
             }
 
             size = ce->hunk->end - ce->hunk->last;
@@ -1196,20 +1251,101 @@
             ce->next = p->free_hunks;
             p->free_hunks = ce;
             break;
-
-            return NGX_OK;
         }
     }
 
-    if (p->out_hunks && p->request->connection->write->ready) {
-        return
-             ngx_http_proxy_write_upstream_body(p->request->connection->write);
+    wev = p->request->connection->write;
+
+    if (r->cachable) {
+        if (p->in_hunks) {
+            rc = ngx_http_proxy_write_chain_to_temp_file(p);
+            if (rc != NGX_OK) {
+                return rc;
+            }
+        }
+
+        if (p->out_hunks && wev->ready) {
+            return ngx_http_proxy_write_upstream_body(wev);
+        }
+
+    } else {
+        if ((p->out_hunks || p->in_hunks) && wev->ready) {
+            return ngx_http_proxy_write_upstream_body(wev);
+        }
     }
 
     return NGX_OK;
 }
 
 
+static int ngx_http_proxy_write_chain_to_temp_file(ngx_http_proxy_ctx_t *p)
+{
+    int           i, rc;
+    ngx_hunk_t   *h;
+    ngx_chain_t  *ce, *tce;
+
+    if (p->temp_file->fd == NGX_INVALID_FILE) {
+        rc = ngx_create_temp_file(p->temp_file, p->lcf->temp_path,
+                                  p->request->pool,
+                                  0, 2,
+                                  p->request->cachable);
+
+        if (rc != NGX_OK) {
+            return rc;
+        }
+
+        if (p->lcf->temp_file_warn) {
+            ngx_log_error(NGX_LOG_WARN, p->log, 0,
+                          "an upstream response is buffered "
+                          "to a temporary file");
+        }
+    }
+
+    if (ngx_write_chain_to_file(p->temp_file, p->in_hunks,
+                              p->temp_offset, p->request->pool) == NGX_ERROR) {
+        return NGX_ERROR;
+    }
+
+    for (ce = p->in_hunks; ce; ce = ce->next) {
+        ngx_test_null(h, ngx_pcalloc(p->request->pool, sizeof(ngx_hunk_t)),
+                      NGX_ERROR);
+
+        h->type = NGX_HUNK_FILE
+                  |NGX_HUNK_TEMP|NGX_HUNK_IN_MEMORY|NGX_HUNK_RECYCLED;
+
+        ce->hunk->shadow = h;
+        h->shadow = ce->hunk;
+
+        h->file_pos = p->temp_offset;
+        p->temp_offset += ce->hunk->last - ce->hunk->pos;
+        h->file_last = p->temp_offset;
+
+        h->file->fd = p->temp_file->fd;
+        h->file->log = p->log;
+
+        h->pos = ce->hunk->pos;
+        h->last = ce->hunk->last;
+        h->start = ce->hunk->start;
+        h->end = ce->hunk->end;
+        h->pre_start = ce->hunk->pre_start;
+        h->post_end = ce->hunk->post_end;
+
+        ngx_test_null(tce, ngx_create_chain_entry(p->request->pool), NGX_ERROR);
+        tce->hunk = h;
+        tce->next = NULL;
+
+        if (p->last_out_hunk) {
+            p->last_out_hunk->next = tce;
+            p->last_out_hunk = tce;
+
+        } else {
+            p->last_out_hunk = tce;
+        }
+    }
+
+    return NGX_OK;
+}
+
 static int ngx_http_proxy_write_upstream_body(ngx_event_t *wev)
 {
     int                    rc;
@@ -1256,7 +1392,7 @@
 }
 
 
-
+#endif
 
 
 
@@ -1628,7 +1764,8 @@
     conf->read_timeout = 10000;
     conf->header_size = 1024;
     conf->block_size = 4096;
-    conf->max_block_size = 32768;
+    conf->max_block_size = 4096 * 3;
+    conf->file_block_size = 4096;
 
     ngx_test_null(conf->temp_path, ngx_pcalloc(pool, sizeof(ngx_path_t)), NULL);
 
diff --git a/src/http/modules/proxy/ngx_http_event_proxy_handler.h b/src/http/modules/proxy/ngx_http_event_proxy_handler.h
index 92840d0..bf159d6 100644
--- a/src/http/modules/proxy/ngx_http_event_proxy_handler.h
+++ b/src/http/modules/proxy/ngx_http_event_proxy_handler.h
@@ -4,6 +4,7 @@
 
 #include <ngx_config.h>
 #include <ngx_array.h>
+#include <ngx_event_proxy.h>
 #include <ngx_http.h>
 
 
@@ -70,6 +71,7 @@
 
     int   block_size;
     int   max_block_size;
+    int   file_block_size;
 
     ngx_path_t  *temp_path;
     int   temp_file_warn;
@@ -94,9 +96,13 @@
 typedef struct ngx_http_proxy_ctx_s  ngx_http_proxy_ctx_t;
 
 struct ngx_http_proxy_ctx_s {
+    ngx_event_proxy_t  *event_proxy;
+
     ngx_chain_t   *in_hunks;
     ngx_chain_t   *last_in_hunk;
 
+    ngx_chain_t   *shadow_hunks;
+
     ngx_chain_t   *out_hunks;
     ngx_chain_t   *last_out_hunk;
 
diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c
index 4953971..310f940 100644
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -674,6 +674,8 @@
             }
         }
 
+ngx_log_debug(cf->pool->log, "main merge");
+
         if (module->merge_loc_conf) {
             if (module->merge_loc_conf(cf->pool,
                                        prev->loc_conf[module->index],
@@ -682,6 +684,8 @@
                 return NGX_CONF_ERROR;
             }
 
+ngx_log_debug(cf->pool->log, "server merge");
+
             for (j = 0; j < scf->locations.nelts; j++) {
                 if (module->merge_loc_conf(cf->pool,
                                       ctx->loc_conf[module->index],
@@ -690,6 +694,7 @@
                     return NGX_CONF_ERROR;
                 }
             }
+ngx_log_debug(cf->pool->log, "server merge done");
         }
     }
 
diff --git a/src/http/ngx_http_output_filter.c b/src/http/ngx_http_output_filter.c
index dc5bbb1..426e921 100644
--- a/src/http/ngx_http_output_filter.c
+++ b/src/http/ngx_http_output_filter.c
@@ -265,7 +265,7 @@
             src->file_pos += n;
         }
 
-        if (src->type & NGX_HUNK_LAST && src->pos == src->last) {
+        if ((src->type & NGX_HUNK_LAST) && src->pos == src->last) {
             dst->type |= NGX_HUNK_LAST;
         }
 
@@ -294,7 +294,7 @@
         src->file_pos += n;
         dst->last += n;
 
-        if (src->type & NGX_HUNK_LAST && src->file_pos == src->file_last) {
+        if ((src->type & NGX_HUNK_LAST) && src->file_pos == src->file_last) {
             dst->type |= NGX_HUNK_LAST;
         }
     }
diff --git a/src/http/ngx_http_write_filter.c b/src/http/ngx_http_write_filter.c
index 151c12e..26d38d7 100644
--- a/src/http/ngx_http_write_filter.c
+++ b/src/http/ngx_http_write_filter.c
@@ -122,8 +122,9 @@
                                              ngx_http_write_filter_module_ctx);
 
 #if (NGX_DEBUG_WRITE_FILTER)
-    ngx_log_debug(r->connection->log, "write filter: last:%d flush:%d" _
-                  last _ flush);
+    ngx_log_debug(r->connection->log,
+                  "write filter: last:%d flush:%qd size:%qd" _
+                  last _ flush _ size);
 #endif
 
     /* avoid the output if there is no last hunk, no flush point and
@@ -164,6 +165,8 @@
 
     conf->buffer_output = NGX_CONF_UNSET;
 
+ngx_log_debug(pool->log, "write conf %08X %08X" _ conf _ conf->buffer_output);
+
     return conf;
 }
 
@@ -178,6 +181,8 @@
 
     ngx_conf_size_merge(conf->buffer_output, prev->buffer_output, 1460);
 
+ngx_log_debug(pool->log, "write merge %08X %08X %08X" _ prev _ conf _ conf->buffer_output);
+
     return NULL;
 }