changeset 177:4db54fdbcbe7

nginx-0.0.1-2003-11-10-20:17:31 import
author Igor Sysoev <igor@sysoev.ru>
date Mon, 10 Nov 2003 17:17:31 +0000
parents c0552e5ab567
children a8ff48d26cca
files src/core/ngx_conf_file.c src/http/modules/ngx_http_charset_filter.c src/http/modules/ngx_http_chunked_filter.c src/http/modules/ngx_http_gzip_filter.c src/http/modules/ngx_http_index_handler.c src/http/modules/ngx_http_not_modified_filter.c src/http/modules/ngx_http_range_filter.c src/http/modules/ngx_http_static_handler.c src/http/modules/proxy/ngx_http_proxy_cache.c src/http/modules/proxy/ngx_http_proxy_handler.c src/http/modules/proxy/ngx_http_proxy_upstream.c src/http/ngx_http.c src/http/ngx_http_config.h src/http/ngx_http_core_module.c src/http/ngx_http_header_filter.c src/http/ngx_http_log_handler.c src/http/ngx_http_log_handler.h src/http/ngx_http_output_filter.c src/http/ngx_http_write_filter.c src/os/unix/ngx_freebsd_config.h
diffstat 20 files changed, 312 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/ngx_conf_file.c
+++ b/src/core/ngx_conf_file.c
@@ -368,6 +368,11 @@ ngx_log_debug(cf->log, "%d:%d:%d:%d:%d '
                 return NGX_OK;
             }
 
+            ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
+                          "unexpected '%c' in %s:%d",
+                          ch, cf->conf_file->file.name.data,
+                          cf->conf_file->line);
+
             return NGX_ERROR;
         }
 
--- a/src/http/modules/ngx_http_charset_filter.c
+++ b/src/http/modules/ngx_http_charset_filter.c
@@ -29,6 +29,8 @@ static ngx_command_t  ngx_http_charset_f
 
 
 static ngx_http_module_t  ngx_http_charset_filter_module_ctx = {
+    NULL,                                  /* pre conf */
+
     NULL,                                  /* create main configuration */
     NULL,                                  /* init main configuration */
 
--- a/src/http/modules/ngx_http_chunked_filter.c
+++ b/src/http/modules/ngx_http_chunked_filter.c
@@ -8,6 +8,8 @@ static int ngx_http_chunked_filter_init(
 
 
 static ngx_http_module_t  ngx_http_chunked_filter_module_ctx = {
+    NULL,                                  /* pre conf */
+
     NULL,                                  /* create main configuration */
     NULL,                                  /* init main configuration */
 
--- a/src/http/modules/ngx_http_gzip_filter.c
+++ b/src/http/modules/ngx_http_gzip_filter.c
@@ -34,6 +34,10 @@ typedef struct {
 
     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_alloc(
 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_command_t  ngx_http_gzip_filt
 
 
 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 @@ ngx_module_t  ngx_http_gzip_filter_modul
 };
 
 
+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_header_filter(n
 
 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 @@ static int ngx_http_gzip_body_filter(ngx
         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 @@ ngx_log_debug(r->connection->log, "DEFLA
 
                     /* 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 @@ ngx_log_debug(r->connection->log, "DEFLA
 
 #if (HAVE_LITTLE_ENDIAN)
                     trailer->crc32 = ctx->crc32;
-                    trailer->zlen = zin;
+                    trailer->zlen = ctx->zin;
 #else
                     /* STUB */
 #endif
@@ -449,7 +471,11 @@ ngx_log_debug(r->connection->log, "DEFLA
                     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 void ngx_http_gzip_filter_free(vo
 }
 
 
+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 @@ ngx_inline static int ngx_http_gzip_erro
 }
 
 
+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;
--- a/src/http/modules/ngx_http_index_handler.c
+++ b/src/http/modules/ngx_http_index_handler.c
@@ -46,6 +46,8 @@ static ngx_command_t ngx_http_index_comm
 
 
 ngx_http_module_t  ngx_http_index_module_ctx = {
+    NULL,                                  /* pre conf */
+
     NULL,                                  /* create main configuration */
     NULL,                                  /* init main configuration */
 
--- 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 int ngx_http_not_modified_filter_
 
 
 static ngx_http_module_t  ngx_http_not_modified_filter_module_ctx = {
+    NULL,                                  /* pre conf */
+
     NULL,                                  /* create main configuration */
     NULL,                                  /* init main configuration */
 
--- a/src/http/modules/ngx_http_range_filter.c
+++ b/src/http/modules/ngx_http_range_filter.c
@@ -48,6 +48,8 @@ static int ngx_http_range_filter_init(ng
 
 
 static ngx_http_module_t  ngx_http_range_filter_module_ctx = {
+    NULL,                                  /* pre conf */
+
     NULL,                                  /* create main configuration */
     NULL,                                  /* init main configuration */
 
--- a/src/http/modules/ngx_http_static_handler.c
+++ b/src/http/modules/ngx_http_static_handler.c
@@ -16,6 +16,8 @@ static ngx_command_t  ngx_http_static_co
 
 
 ngx_http_module_t  ngx_http_static_module_ctx = {
+    NULL,                                  /* pre conf */
+
     NULL,                                  /* create main configuration */
     NULL,                                  /* init main configuration */
     
--- a/src/http/modules/proxy/ngx_http_proxy_cache.c
+++ b/src/http/modules/proxy/ngx_http_proxy_cache.c
@@ -302,6 +302,7 @@ void ngx_http_proxy_cache_busy_lock(ngx_
         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_send_cached_response(
 
 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 @@ int ngx_http_proxy_is_cachable(ngx_http_
                            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 @@ int ngx_http_proxy_is_cachable(ngx_http_
                                           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 @@ int ngx_http_proxy_is_cachable(ngx_http_
 
     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 @@ int ngx_http_proxy_is_cachable(ngx_http_
         /* 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;
     }
 
--- 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 @@ static ngx_command_t  ngx_http_proxy_com
 
 
 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 @@ ngx_module_t  ngx_http_proxy_module = {
 };
 
 
+
+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_states[] = {
 };
 
 
-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 @@ void ngx_http_proxy_close_connection(ngx
 }
 
 
-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 @@ size_t ngx_http_proxy_log_error(void *da
 }
 
 
+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;
--- a/src/http/modules/proxy/ngx_http_proxy_upstream.c
+++ b/src/http/modules/proxy/ngx_http_proxy_upstream.c
@@ -305,6 +305,13 @@ static void ngx_http_proxy_reinit_upstre
         }
     }
 
+    /* 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 @@ void ngx_http_proxy_upstream_busy_lock(n
         return;
     }
 
+    p->state->status = NGX_HTTP_SERVICE_UNAVAILABLE;
     ngx_http_proxy_finalize_request(p, NGX_HTTP_SERVICE_UNAVAILABLE);
 }
 
@@ -407,11 +415,6 @@ static void ngx_http_proxy_connect(ngx_h
 
     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 @@ static void ngx_http_proxy_process_upstr
     /* rc == NGX_OK */
 
     p->upstream->status = p->status;
+    p->state->status = p->status;
 
     if (p->status == NGX_HTTP_INTERNAL_SERVER_ERROR) {
 
--- a/src/http/ngx_http.c
+++ b/src/http/ngx_http.c
@@ -107,6 +107,12 @@ static char *ngx_http_block(ngx_conf_t *
         module = ngx_modules[m]->ctx;
         mi = ngx_modules[m]->ctx_index;
 
+        if (module->pre_conf) {
+            if (module->pre_conf(cf) != NGX_OK) {
+                return NGX_CONF_ERROR;
+            }
+        }
+
         if (module->create_main_conf) {
             ngx_test_null(ctx->main_conf[mi], module->create_main_conf(cf),
                           NGX_CONF_ERROR);
@@ -157,6 +163,7 @@ static char *ngx_http_block(ngx_conf_t *
         if (module->init_main_conf) {
             rv = module->init_main_conf(cf, ctx->main_conf[mi]);
             if (rv != NGX_CONF_OK) {
+                *cf = pcf;
                 return rv;
             }
         }
@@ -170,6 +177,7 @@ static char *ngx_http_block(ngx_conf_t *
                                             ctx->srv_conf[mi],
                                             cscfp[s]->ctx->srv_conf[mi]);
                 if (rv != NGX_CONF_OK) {
+                    *cf = pcf;
                     return rv;
                 }
             }
@@ -182,6 +190,7 @@ static char *ngx_http_block(ngx_conf_t *
                                             ctx->loc_conf[mi],
                                             cscfp[s]->ctx->loc_conf[mi]);
                 if (rv != NGX_CONF_OK) {
+                    *cf = pcf;
                     return rv;
                 }
 
@@ -194,6 +203,7 @@ static char *ngx_http_block(ngx_conf_t *
                                                 cscfp[s]->ctx->loc_conf[mi],
                                                 clcfp[l]->loc_conf[mi]);
                     if (rv != NGX_CONF_OK) {
+                        *cf = pcf;
                         return rv;
                     }
                 }
@@ -201,6 +211,8 @@ static char *ngx_http_block(ngx_conf_t *
         }
     }
 
+    /* we needed "http"'s cf->ctx while merging configuration */
+    *cf = pcf;
 
     /* init lists of the handlers */
 
@@ -557,6 +569,5 @@ ngx_log_debug(cf->log, "%s %08x" _ s_nam
     }
     /**/
 
-    *cf = pcf;
     return NGX_CONF_OK;
 }
--- a/src/http/ngx_http_config.h
+++ b/src/http/ngx_http_config.h
@@ -20,6 +20,8 @@ typedef struct {
 
 
 typedef struct {
+    int    (*pre_conf)(ngx_conf_t *cf);
+
     void  *(*create_main_conf)(ngx_conf_t *cf);
     char  *(*init_main_conf)(ngx_conf_t *cf, void *conf);
 
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -212,6 +212,8 @@ static ngx_command_t  ngx_http_core_comm
 
 
 ngx_http_module_t  ngx_http_core_module_ctx = {
+    NULL,                                  /* pre conf */
+
     ngx_http_core_create_main_conf,        /* create main configuration */
     ngx_http_core_init_main_conf,          /* init main configuration */
 
--- a/src/http/ngx_http_header_filter.c
+++ b/src/http/ngx_http_header_filter.c
@@ -10,6 +10,8 @@ static int ngx_http_header_filter(ngx_ht
 
 
 static ngx_http_module_t  ngx_http_header_filter_module_ctx = {
+    NULL,                                  /* pre conf */
+
     NULL,                                  /* create main configuration */
     NULL,                                  /* init main configuration */
 
--- a/src/http/ngx_http_log_handler.c
+++ b/src/http/ngx_http_log_handler.c
@@ -28,6 +28,7 @@ static char *ngx_http_log_header_out(ngx
 static char *ngx_http_log_unknown_header_out(ngx_http_request_t *r, char *buf,
                                              uintptr_t data);
 
+static int ngx_http_log_pre_conf(ngx_conf_t *cf);
 static void *ngx_http_log_create_main_conf(ngx_conf_t *cf);
 static void *ngx_http_log_create_loc_conf(ngx_conf_t *cf);
 static char *ngx_http_log_merge_loc_conf(ngx_conf_t *cf, void *parent,
@@ -61,6 +62,8 @@ static ngx_command_t  ngx_http_log_comma
 
 
 ngx_http_module_t  ngx_http_log_module_ctx = {
+    ngx_http_log_pre_conf,                 /* pre conf */
+
     ngx_http_log_create_main_conf,         /* create main configuration */
     NULL,                                  /* init main configuration */
 
@@ -93,7 +96,7 @@ static ngx_str_t ngx_http_combined_fmt =
                "\"%{Referer}i\" %{User-Agent}i\"");
 
 
-static ngx_http_log_op_name_t ngx_http_log_fmt_ops[] = {
+ngx_http_log_op_name_t ngx_http_log_fmt_ops[] = {
     { ngx_string("addr"), INET_ADDRSTRLEN - 1, ngx_http_log_addr },
     { ngx_string("conn"), NGX_INT32_LEN, ngx_http_log_connection },
     { ngx_string("pipe"), 1, ngx_http_log_pipe },
@@ -439,6 +442,17 @@ static char *ngx_http_log_unknown_header
 }
 
 
+static int ngx_http_log_pre_conf(ngx_conf_t *cf)
+{
+    ngx_http_log_op_name_t  *op;
+
+    for (op = ngx_http_log_fmt_ops; op->name.len; op++) { /* void */ }
+    op->op = NULL;
+
+    return NGX_OK;
+}
+
+
 static void *ngx_http_log_create_main_conf(ngx_conf_t *cf)
 {
     ngx_http_log_main_conf_t  *conf;
@@ -474,7 +488,7 @@ static void *ngx_http_log_create_main_co
 
     rc = ngx_http_log_set_format(cf, NULL, conf);
     if (rc != NGX_CONF_OK) {
-        return rc;
+        return NULL;
     }
 
     return conf;
@@ -597,9 +611,6 @@ static char *ngx_http_log_set_format(ngx
     ngx_http_log_op_name_t     *name;
 
     value = cf->args->elts;
-#if 0
-    lmcf = ngx_http_conf_module_main_conf(cf, ngx_http_log_module);
-#endif
 
     fmt = lmcf->formats.elts;
     for (f = 0; f < lmcf->formats.nelts; f++) {
@@ -669,8 +680,8 @@ static char *ngx_http_log_set_format(ngx
                 fname = &value[s].data[i];
 
                 while (i < value[s].len
-                       && value[s].data[i] >= 'a'
-                       && value[s].data[i] <= 'z')
+                       && ((value[s].data[i] >= 'a' && value[s].data[i] <= 'z')
+                           || value[s].data[i] == '_'))
                 {
                     i++;
                 }
@@ -682,7 +693,11 @@ static char *ngx_http_log_set_format(ngx
                     break;
                 }
 
-                for (name = ngx_http_log_fmt_ops; name->name.len; name++) {
+                for (name = ngx_http_log_fmt_ops; name->op; name++) {
+                    if (name->name.len == 0) {
+                        name = (ngx_http_log_op_name_t *) name->op;
+                    }
+
                     if (name->name.len == fname_len
                         && ngx_strncmp(name->name.data, fname, fname_len) == 0)
                     {
--- a/src/http/ngx_http_log_handler.h
+++ b/src/http/ngx_http_log_handler.h
@@ -17,6 +17,7 @@ typedef char *(*ngx_http_log_op_pt) (ngx
 
 /* STUB */
 #define NGX_INT32_LEN      sizeof("4294967296") - 1
+#define NGX_TIME_LEN       sizeof("4294967296") - 1
 #define NGX_OFF_LEN        sizeof("18446744073709551616") - 1
 
 
@@ -56,4 +57,7 @@ typedef struct {
 } ngx_http_log_loc_conf_t;
 
 
+extern ngx_http_log_op_name_t ngx_http_log_fmt_ops[];
+
+
 #endif /* _NGX_HTTP_LOG_HANDLER_H_INCLUDED_ */
--- a/src/http/ngx_http_output_filter.c
+++ b/src/http/ngx_http_output_filter.c
@@ -28,6 +28,8 @@ static ngx_command_t  ngx_http_output_fi
 
 
 static ngx_http_module_t  ngx_http_output_filter_module_ctx = {
+    NULL,                                  /* pre conf */
+
     NULL,                                  /* create main configuration */
     NULL,                                  /* init main configuration */
 
--- a/src/http/ngx_http_write_filter.c
+++ b/src/http/ngx_http_write_filter.c
@@ -35,6 +35,8 @@ static ngx_command_t  ngx_http_write_fil
 
 
 ngx_http_module_t  ngx_http_write_filter_module_ctx = {
+    NULL,                                  /* pre conf */
+
     NULL,                                  /* create main configuration */
     NULL,                                  /* init main configuration */
 
--- a/src/os/unix/ngx_freebsd_config.h
+++ b/src/os/unix/ngx_freebsd_config.h
@@ -29,12 +29,14 @@
 #define OFF_FMT    "%lld"
 #define SIZE_FMT   "%d"
 #define SIZEX_FMT  "%x"
+#define TIME_FMT   "%lu"
 
 #else  /* amd64, alpha, sparc64, ia64 */
 
 #define OFF_FMT    "%ld"
 #define SIZE_FMT   "%ld"
 #define SIZEX_FMT  "%lx"
+#define TIME_FMT   "%lu"
 
 #endif