nginx-0.0.1-2003-11-10-20:17:31 import
diff --git a/src/http/modules/ngx_http_charset_filter.c b/src/http/modules/ngx_http_charset_filter.c
index dd48f0b..a545eaf 100644
--- a/src/http/modules/ngx_http_charset_filter.c
+++ b/src/http/modules/ngx_http_charset_filter.c
@@ -29,6 +29,8 @@
 
 
 static ngx_http_module_t  ngx_http_charset_filter_module_ctx = {
+    NULL,                                  /* pre conf */
+
     NULL,                                  /* create main configuration */
     NULL,                                  /* init main configuration */
 
diff --git a/src/http/modules/ngx_http_chunked_filter.c b/src/http/modules/ngx_http_chunked_filter.c
index 20a5d0d..5e7bd28 100644
--- a/src/http/modules/ngx_http_chunked_filter.c
+++ b/src/http/modules/ngx_http_chunked_filter.c
@@ -8,6 +8,8 @@
 
 
 static ngx_http_module_t  ngx_http_chunked_filter_module_ctx = {
+    NULL,                                  /* pre conf */
+
     NULL,                                  /* create main configuration */
     NULL,                                  /* init main configuration */
 
diff --git a/src/http/modules/ngx_http_gzip_filter.c b/src/http/modules/ngx_http_gzip_filter.c
index 5a091b4..09980ef 100644
--- a/src/http/modules/ngx_http_gzip_filter.c
+++ b/src/http/modules/ngx_http_gzip_filter.c
@@ -34,6 +34,10 @@
 
     unsigned             flush:4;
     unsigned             redo:1;
+    unsigned             done:1;
+
+    size_t               zin;
+    size_t               zout;
 
     u_int                crc32;
     z_stream             zstream;
@@ -46,6 +50,11 @@
 static void ngx_http_gzip_filter_free(void *opaque, void *address);
 
 ngx_inline static int ngx_http_gzip_error(ngx_http_gzip_ctx_t *ctx);
+
+static char *ngx_http_gzip_log_ratio(ngx_http_request_t *r, char *buf,
+                                     uintptr_t data);
+
+static int ngx_http_gzip_pre_conf(ngx_conf_t *cf);
 static int ngx_http_gzip_filter_init(ngx_cycle_t *cycle);
 static void *ngx_http_gzip_create_conf(ngx_conf_t *cf);
 static char *ngx_http_gzip_merge_conf(ngx_conf_t *cf,
@@ -114,6 +123,8 @@
 
 
 static ngx_http_module_t  ngx_http_gzip_filter_module_ctx = {
+    ngx_http_gzip_pre_conf,                /* pre conf */
+
     NULL,                                  /* create main configuration */
     NULL,                                  /* init main configuration */
 
@@ -135,6 +146,13 @@
 };
 
 
+static ngx_http_log_op_name_t ngx_http_gzip_log_fmt_ops[] = {
+    { ngx_string("gzip_ratio"), NGX_INT32_LEN + 3, ngx_http_gzip_log_ratio },
+    { ngx_null_string, 0, NULL }
+};
+
+
+
 static u_char  gzheader[10] = { 0x1f, 0x8b, Z_DEFLATED, 0, 0, 0, 0, 0, 0, 3 };
 
 #if (HAVE_LITTLE_ENDIAN)
@@ -215,7 +233,7 @@
 
 static int ngx_http_gzip_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
 {
-    int                    rc, wbits, memlevel, zin, zout, last;
+    int                    rc, wbits, memlevel, last;
     struct gztrailer      *trailer;
     ngx_hunk_t            *h;
     ngx_chain_t           *cl;
@@ -226,6 +244,10 @@
         return ngx_http_next_body_filter(r, in);
     }
 
+    if (ctx->done) {
+        return ngx_http_next_body_filter(r, in);
+    }
+
     conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
 
     if (ctx->preallocated == NULL) {
@@ -400,8 +422,8 @@
 
                     /* rc == Z_STREAM_END */
 
-                    zin = ctx->zstream.total_in;
-                    zout = 10 + ctx->zstream.total_out + 8;
+                    ctx->zin = ctx->zstream.total_in;
+                    ctx->zout = 10 + ctx->zstream.total_out + 8;
 
                     rc = deflateEnd(&ctx->zstream);
                     if (rc != Z_OK) {
@@ -441,7 +463,7 @@
 
 #if (HAVE_LITTLE_ENDIAN)
                     trailer->crc32 = ctx->crc32;
-                    trailer->zlen = zin;
+                    trailer->zlen = ctx->zin;
 #else
                     /* STUB */
 #endif
@@ -449,7 +471,11 @@
                     ctx->zstream.avail_in = 0;
                     ctx->zstream.avail_out = 0;
 
+                    ctx->done = 1;
+
+#if 0
                     ngx_http_delete_ctx(r, ngx_http_gzip_filter_module);
+#endif
 
                     break;
 
@@ -531,6 +557,40 @@
 }
 
 
+static char *ngx_http_gzip_log_ratio(ngx_http_request_t *r, char *buf,
+                                     uintptr_t data)
+{
+    int                   zint, zfrac;
+    ngx_http_gzip_ctx_t  *ctx;
+
+    ctx = ngx_http_get_module_ctx(r, ngx_http_gzip_filter_module);
+
+    if (ctx == NULL || ctx->zout == 0) {
+        *buf = '-';
+        return buf + 1;
+    }
+
+#if 0
+    return buf + ngx_snprintf(buf, NGX_INT32_LEN + 4, "%.2f",
+                              (float) ctx->zin / ctx->zout);
+#endif
+
+    /* we prefer do not use FPU */
+
+    zint = ctx->zin / ctx->zout;
+    zfrac = (ctx->zin * 100 / ctx->zout) % 100;
+
+    if ((ctx->zin * 1000 / ctx->zout) %10 > 4) {
+        if (++zfrac > 99) {
+            zint++;
+            zfrac = 0;
+        }
+    }
+
+    return buf + ngx_snprintf(buf, NGX_INT32_LEN + 4, "%d.%02d", zint, zfrac);
+}
+
+
 ngx_inline static int ngx_http_gzip_error(ngx_http_gzip_ctx_t *ctx)
 {
     deflateEnd(&ctx->zstream);
@@ -544,6 +604,27 @@
 }
 
 
+static int ngx_http_gzip_pre_conf(ngx_conf_t *cf)
+{
+    ngx_http_log_op_name_t  *op;
+
+    for (op = ngx_http_gzip_log_fmt_ops; op->name.len; op++) { /* void */ }
+    op->op = NULL;
+
+    op = ngx_http_log_fmt_ops;
+
+    for (op = ngx_http_log_fmt_ops; op->op; op++) {
+        if (op->name.len == 0) {
+            op = (ngx_http_log_op_name_t *) op->op;
+        }
+    }
+
+    op->op = (ngx_http_log_op_pt) ngx_http_gzip_log_fmt_ops;
+
+    return NGX_OK;
+}
+
+
 static int ngx_http_gzip_filter_init(ngx_cycle_t *cycle)
 {
     ngx_http_next_header_filter = ngx_http_top_header_filter;
diff --git a/src/http/modules/ngx_http_index_handler.c b/src/http/modules/ngx_http_index_handler.c
index 9ec9325..f4c7157 100644
--- a/src/http/modules/ngx_http_index_handler.c
+++ b/src/http/modules/ngx_http_index_handler.c
@@ -46,6 +46,8 @@
 
 
 ngx_http_module_t  ngx_http_index_module_ctx = {
+    NULL,                                  /* pre conf */
+
     NULL,                                  /* create main configuration */
     NULL,                                  /* init main configuration */
 
diff --git a/src/http/modules/ngx_http_not_modified_filter.c b/src/http/modules/ngx_http_not_modified_filter.c
index b00e2ff..c623752 100644
--- a/src/http/modules/ngx_http_not_modified_filter.c
+++ b/src/http/modules/ngx_http_not_modified_filter.c
@@ -9,6 +9,8 @@
 
 
 static ngx_http_module_t  ngx_http_not_modified_filter_module_ctx = {
+    NULL,                                  /* pre conf */
+
     NULL,                                  /* create main configuration */
     NULL,                                  /* init main configuration */
 
diff --git a/src/http/modules/ngx_http_range_filter.c b/src/http/modules/ngx_http_range_filter.c
index 850235a..11b7d0f 100644
--- a/src/http/modules/ngx_http_range_filter.c
+++ b/src/http/modules/ngx_http_range_filter.c
@@ -48,6 +48,8 @@
 
 
 static ngx_http_module_t  ngx_http_range_filter_module_ctx = {
+    NULL,                                  /* pre conf */
+
     NULL,                                  /* create main configuration */
     NULL,                                  /* init main configuration */
 
diff --git a/src/http/modules/ngx_http_static_handler.c b/src/http/modules/ngx_http_static_handler.c
index 9849dbf..33d293a 100644
--- a/src/http/modules/ngx_http_static_handler.c
+++ b/src/http/modules/ngx_http_static_handler.c
@@ -16,6 +16,8 @@
 
 
 ngx_http_module_t  ngx_http_static_module_ctx = {
+    NULL,                                  /* pre conf */
+
     NULL,                                  /* create main configuration */
     NULL,                                  /* init main configuration */
     
diff --git a/src/http/modules/proxy/ngx_http_proxy_cache.c b/src/http/modules/proxy/ngx_http_proxy_cache.c
index 0f56686..3c4f5ca 100644
--- a/src/http/modules/proxy/ngx_http_proxy_cache.c
+++ b/src/http/modules/proxy/ngx_http_proxy_cache.c
@@ -302,6 +302,7 @@
         return;
     }
     
+    p->state->status = NGX_HTTP_SERVICE_UNAVAILABLE;
     ngx_http_proxy_finalize_request(p, NGX_HTTP_SERVICE_UNAVAILABLE);
 }
 
@@ -468,7 +469,7 @@
 
 int ngx_http_proxy_is_cachable(ngx_http_proxy_ctx_t *p)
 {
-    time_t                        date, last_modified, expires;
+    time_t                        date, last_modified, expires, t;
     ngx_http_proxy_headers_in_t  *h;
 
     switch (p->upstream->status) {
@@ -509,6 +510,7 @@
                            h->x_accel_expires->value.len);
         if (expires != NGX_ERROR) {
             p->state->reason = NGX_HTTP_PROXY_CACHE_XAE;
+            p->state->expires = expires;
             p->cache->ctx.expires = date + expires;
             return (expires > 0);
         }
@@ -523,6 +525,7 @@
                                           h->expires->value.len);
             if (expires != NGX_ERROR) {
                 p->state->reason = NGX_HTTP_PROXY_CACHE_EXP;
+                p->state->expires = expires - date;
                 p->cache->ctx.expires = expires;
                 return (date < expires);
             }
@@ -531,6 +534,7 @@
 
     if (p->upstream->status == NGX_HTTP_MOVED_PERMANENTLY) {
         p->state->reason = NGX_HTTP_PROXY_CACHE_MVD;
+        p->state->expires = /* STUB: 1 hour */ 60 * 60;
         p->cache->ctx.expires = /* STUB: 1 hour */ 60 * 60;
         return 1;
     }
@@ -544,14 +548,17 @@
         /* FIXME: time_t == int_64_t, we can use fpu */ 
 
         p->state->reason = NGX_HTTP_PROXY_CACHE_LMF;
-        p->cache->ctx.expires = (time_t) (ngx_time()
-             + (((int64_t) (date - last_modified)) * p->lcf->lm_factor) / 100);
+        t = (time_t)
+              ((((int64_t) (date - last_modified)) * p->lcf->lm_factor) / 100);
+        p->state->expires = t;
+        p->cache->ctx.expires = ngx_time() + t;
         return 1;
     }
 
     if (p->lcf->default_expires > 0) {
         p->state->reason = NGX_HTTP_PROXY_CACHE_PDE;
-        p->cache->ctx.expires = p->lcf->default_expires;
+        p->state->expires = p->lcf->default_expires;
+        p->cache->ctx.expires = ngx_time() + p->lcf->default_expires;
         return 1;
     }
 
diff --git a/src/http/modules/proxy/ngx_http_proxy_handler.c b/src/http/modules/proxy/ngx_http_proxy_handler.c
index 4a48250..ad719ca 100644
--- a/src/http/modules/proxy/ngx_http_proxy_handler.c
+++ b/src/http/modules/proxy/ngx_http_proxy_handler.c
@@ -7,7 +7,14 @@
 
 static int ngx_http_proxy_handler(ngx_http_request_t *r);
 
-static int ngx_http_proxy_init(ngx_cycle_t *cycle);
+static char *ngx_http_proxy_log_proxy_state(ngx_http_request_t *r, char *buf,
+                                            uintptr_t data);
+static char *ngx_http_proxy_log_cache_state(ngx_http_request_t *r, char *buf,
+                                            uintptr_t data);
+static char *ngx_http_proxy_log_reason(ngx_http_request_t *r, char *buf,
+                                       uintptr_t data);
+
+static int ngx_http_proxy_pre_conf(ngx_conf_t *cf);
 static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf);
 static char *ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf,
                                            void *parent, void *child);
@@ -194,6 +201,8 @@
 
 
 ngx_http_module_t  ngx_http_proxy_module_ctx = {
+    ngx_http_proxy_pre_conf,               /* pre conf */
+
     NULL,                                  /* create main configuration */
     NULL,                                  /* init main configuration */
 
@@ -215,6 +224,19 @@
 };
 
 
+
+static ngx_http_log_op_name_t ngx_http_proxy_log_fmt_ops[] = {
+    { ngx_string("proxy"), /* STUB */ 100,
+                           ngx_http_proxy_log_proxy_state },
+    { ngx_string("proxy_cache_state"), sizeof("BYPASS") - 1,
+                                       ngx_http_proxy_log_cache_state },
+    { ngx_string("proxy_reason"), sizeof("BPS") - 1,
+                                  ngx_http_proxy_log_reason },
+    { ngx_null_string, 0, NULL }
+};
+
+
+
 ngx_http_header_t ngx_http_proxy_headers_in[] = {
     { ngx_string("Date"), offsetof(ngx_http_proxy_headers_in_t, date) },
     { ngx_string("Server"), offsetof(ngx_http_proxy_headers_in_t, server) },
@@ -252,7 +274,7 @@
 };
 
 
-static ngx_str_t cache_reason[] = {
+static ngx_str_t cache_reasons[] = {
     ngx_string("BPS"),
     ngx_string("XAE"),
     ngx_string("CTL"),
@@ -453,12 +475,6 @@
 }
 
 
-size_t ngx_http_proxy_log_state(void *data, char *buf, size_t len)
-{
-    return 0;
-}
-
-
 size_t ngx_http_proxy_log_error(void *data, char *buf, size_t len)
 {
     ngx_http_proxy_ctx_t *p = data;
@@ -482,6 +498,121 @@
 }
 
 
+static char *ngx_http_proxy_log_proxy_state(ngx_http_request_t *r, char *buf,
+                                            uintptr_t data)
+{
+    ngx_http_proxy_ctx_t  *p;
+
+    p = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
+
+    if (p == NULL) {
+        *buf = '-';
+        return buf + 1;
+    }
+
+    if (p->state->cache_state == 0) {
+        *buf++ = '-';
+
+    } else {
+        buf = ngx_cpymem(buf, cache_states[p->state->cache_state - 1].data,
+                         cache_states[p->state->cache_state - 1].len);
+    }
+
+    *buf++ = '/';
+
+    *buf++ = 'X';
+
+    *buf++ = '/';
+
+    *buf++ = 'X';
+
+    *buf++ = ' ';
+
+    if (p->state->status == 0) {
+        *buf++ = '-';
+
+    } else {
+        buf += ngx_snprintf(buf, 4, "%d", p->state->status);
+    }
+
+    *buf++ = '/';
+
+    if (p->state->reason == 0) {
+        *buf++ = '-';
+
+    } else {
+        buf = ngx_cpymem(buf, cache_reasons[p->state->reason - 1].data,
+                         cache_reasons[p->state->reason - 1].len);
+    }
+
+    *buf++ = '/';
+
+    if (p->state->reason >= NGX_HTTP_PROXY_CACHE_XAE) {
+        *buf++ = '-';
+
+    } else {
+        buf += ngx_snprintf(buf, NGX_TIME_LEN, TIME_FMT, p->state->expires);
+    }
+
+    return buf;
+}
+
+
+static char *ngx_http_proxy_log_cache_state(ngx_http_request_t *r, char *buf,
+                                            uintptr_t data)
+{
+    ngx_http_proxy_ctx_t  *p;
+
+    p = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
+
+    if (p == NULL || p->state->cache_state == 0) {
+        *buf = '-';
+        return buf + 1;
+    }
+
+    return ngx_cpymem(buf, cache_states[p->state->cache_state - 1].data,
+                      cache_states[p->state->cache_state - 1].len);
+}
+
+
+static char *ngx_http_proxy_log_reason(ngx_http_request_t *r, char *buf,
+                                       uintptr_t data)
+{
+    ngx_http_proxy_ctx_t  *p;
+
+    p = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
+
+    if (p == NULL || p->state->reason == 0) {
+        *buf = '-';
+        return buf + 1;
+    }
+
+    return ngx_cpymem(buf, cache_reasons[p->state->reason - 1].data,
+                      cache_reasons[p->state->reason - 1].len);
+}
+
+
+static int ngx_http_proxy_pre_conf(ngx_conf_t *cf)
+{
+    ngx_http_log_op_name_t  *op;
+
+    for (op = ngx_http_proxy_log_fmt_ops; op->name.len; op++) { /* void */ }
+    op->op = NULL;
+
+    op = ngx_http_log_fmt_ops;
+
+    for (op = ngx_http_log_fmt_ops; op->op; op++) {
+        if (op->name.len == 0) {
+            op = (ngx_http_log_op_name_t *) op->op;
+        }
+    }
+
+    op->op = (ngx_http_log_op_pt) ngx_http_proxy_log_fmt_ops;
+
+    return NGX_OK;
+}
+
+
 static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf)
 {
     ngx_http_proxy_loc_conf_t  *conf;
diff --git a/src/http/modules/proxy/ngx_http_proxy_upstream.c b/src/http/modules/proxy/ngx_http_proxy_upstream.c
index 0ec2fee..d404993 100644
--- a/src/http/modules/proxy/ngx_http_proxy_upstream.c
+++ b/src/http/modules/proxy/ngx_http_proxy_upstream.c
@@ -305,6 +305,13 @@
         }
     }
 
+    /* add one more state */
+
+    if (!(p->state = ngx_push_array(&p->states))) {
+        ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
+        return;
+    }
+
     p->status = 0;
     p->status_count = 0;
 }
@@ -344,6 +351,7 @@
         return;
     }
 
+    p->state->status = NGX_HTTP_SERVICE_UNAVAILABLE;
     ngx_http_proxy_finalize_request(p, NGX_HTTP_SERVICE_UNAVAILABLE);
 }
 
@@ -407,11 +415,6 @@
 
     p->request_sent = 0;
 
-    if (!(p->state = ngx_push_array(&p->states))) {
-        ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
-        return;
-    }
-
     if (rc == NGX_AGAIN) {
         ngx_add_timer(c->write, p->lcf->connect_timeout);
         return;
@@ -636,6 +639,7 @@
     /* rc == NGX_OK */
 
     p->upstream->status = p->status;
+    p->state->status = p->status;
 
     if (p->status == NGX_HTTP_INTERNAL_SERVER_ERROR) {