nginx-0.0.1-2003-12-14-23:10:27 import
diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c
index 2f5c923..6d20fdf 100644
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -407,14 +407,17 @@
     clcfp = cscf->locations.elts;
     for (i = 0; i < cscf->locations.nelts; i++) {
 
-#if 1
-ngx_log_debug(r->connection->log, "trans: %s: %d" _
-              clcfp[i]->name.data _ clcfp[i]->exact_match);
-#endif
+#if (HAVE_PCRE)
 
         if (clcfp[i]->regex) {
             break;
         }
+#endif
+
+        ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                       "find location: %s\"%s\"",
+                       clcfp[i]->exact_match ? "= " : "",
+                       clcfp[i]->name.data);
 
         if (clcfp[i]->auto_redirect
             && r->uri.len == clcfp[i]->name.len - 1
@@ -449,20 +452,23 @@
         }
     }
 
+#if (HAVE_PCRE)
+
     if (!exact && !auto_redirect) {
         /* regex matches */
 
         for (/* void */; i < cscf->locations.nelts; i++) {
 
-#if 1
-ngx_log_debug(r->connection->log, "trans: %s: %d" _
-              clcfp[i]->name.data _ clcfp[i]->exact_match);
-#endif
-
             if (!clcfp[i]->regex) {
                 continue;
             }
 
+            ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                           "find location: %s\"%s\"",
+                           clcfp[i]->exact_match ? "= " :
+                                 clcfp[i]->regex ? "~ " : "",
+                           clcfp[i]->name.data);
+
             rc = ngx_regex_exec(clcfp[i]->regex, &r->uri);
 
             if (rc == NGX_DECLINED) {
@@ -488,6 +494,8 @@
         }
     }
 
+#endif /* HAVE_PCRE */
+
     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
 
     if (!(ngx_io.flags & NGX_IO_SENDFILE) || !clcf->sendfile) {
@@ -504,10 +512,6 @@
             return NGX_HTTP_INTERNAL_SERVER_ERROR;
         }
 
-#if 0
-        r->headers_out.location->key.len = 8;
-        r->headers_out.location->key.data = "Location";
-#endif
         r->headers_out.location->value = *auto_redirect;
 
         return NGX_HTTP_MOVED_PERMANENTLY;
@@ -614,7 +618,8 @@
 {
     int  i;
 
-    ngx_log_debug(r->connection->log, "internal redirect: '%s'" _ uri->data);
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                   "internal redirect: \"%s\"", uri->data);
 
     r->uri.len = uri->len;
     r->uri.data = uri->data;
@@ -798,6 +803,8 @@
 
     ngx_int_t  rc;
 
+#if (HAVE_PCRE)
+
     if (first->regex && !second->regex) {
         /* shift regex matches to the end */
         return 1;
@@ -808,6 +815,8 @@
         return 0;
     }
 
+#endif
+
     rc = ngx_strcmp(first->name.data, second->name.data);
 
     if (rc == 0 && second->exact_match) {
@@ -876,6 +885,7 @@
                        && value[1].data[0] == '~'
                        && value[1].data[1] == '*'))
         {
+#if (HAVE_PCRE)
             err.len = NGX_MAX_CONF_ERRSTR;
             err.data = errstr;
 
@@ -890,6 +900,13 @@
 
             clcf->name.len = value[2].len;
             clcf->name.data = value[2].data;
+#else
+            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                               "the using of the regex \"%s\" "
+                               "requires PCRE library",
+                               value[2].data);
+            return NGX_CONF_ERROR;
+#endif
 
         } else {
             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
diff --git a/src/http/ngx_http_core_module.h b/src/http/ngx_http_core_module.h
index ed228d7..17ee46f 100644
--- a/src/http/ngx_http_core_module.h
+++ b/src/http/ngx_http_core_module.h
@@ -134,7 +134,9 @@
 
     ngx_http_cache_hash_t  *open_files;
 
+#if (HAVE_PCRE)
     ngx_regex_t  *regex;
+#endif
 
     unsigned      exact_match:1;
     unsigned      auto_redirect:1;
diff --git a/src/http/ngx_http_header_filter.c b/src/http/ngx_http_header_filter.c
index 49f1dba..e3731d7 100644
--- a/src/http/ngx_http_header_filter.c
+++ b/src/http/ngx_http_header_filter.c
@@ -340,6 +340,8 @@
     /* the end of HTTP header */
     *(h->last++) = CR; *(h->last++) = LF;
 
+    r->header_size = h->last - h->pos;
+
     if (r->header_only) {
         h->type |= NGX_HUNK_LAST;
     }
diff --git a/src/http/ngx_http_log_handler.c b/src/http/ngx_http_log_handler.c
index ff94395..caef967 100644
--- a/src/http/ngx_http_log_handler.c
+++ b/src/http/ngx_http_log_handler.c
@@ -19,6 +19,8 @@
                                  uintptr_t data);
 static char *ngx_http_log_length(ngx_http_request_t *r, char *buf,
                                  uintptr_t data);
+static char *ngx_http_log_apache_length(ngx_http_request_t *r, char *buf,
+                                        uintptr_t data);
 static char *ngx_http_log_header_in(ngx_http_request_t *r, char *buf,
                                     uintptr_t data);
 static char *ngx_http_log_connection_header_out(ngx_http_request_t *r,
@@ -97,7 +99,7 @@
                           "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
 
 static ngx_str_t ngx_http_combined_fmt =
-    ngx_string("%addr - - [%time] \"%request\" %status %length "
+    ngx_string("%addr - - [%time] \"%request\" %status %apache_length "
                "\"%{Referer}i\" %{User-Agent}i\"");
 
 
@@ -110,6 +112,7 @@
     { ngx_string("request"), 0, ngx_http_log_request },
     { ngx_string("status"), 3, ngx_http_log_status },
     { ngx_string("length"), NGX_OFF_T_LEN, ngx_http_log_length },
+    { ngx_string("apache_length"), NGX_OFF_T_LEN, ngx_http_log_apache_length },
     { ngx_string("i"), NGX_HTTP_LOG_ARG, ngx_http_log_header_in },
     { ngx_string("o"), NGX_HTTP_LOG_ARG, ngx_http_log_header_out },
     { ngx_null_string, 0, NULL }
@@ -129,7 +132,8 @@
     u_int                     written;
 #endif
 
-    ngx_log_debug(r->connection->log, "log handler");
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                   "http log handler");
 
     lcf = ngx_http_get_module_loc_conf(r, ngx_http_log_module);
 
@@ -248,6 +252,14 @@
 }
 
 
+static char *ngx_http_log_apache_length(ngx_http_request_t *r, char *buf,
+                                        uintptr_t data)
+{
+    return buf + ngx_snprintf(buf, NGX_OFF_T_LEN + 1, OFF_T_FMT,
+                              r->connection->sent - r->header_size);
+}
+
+
 static char *ngx_http_log_header_in(ngx_http_request_t *r, char *buf,
                                     uintptr_t data)
 {
diff --git a/src/http/ngx_http_parse.c b/src/http/ngx_http_parse.c
index 5dc0bc3..599114c 100644
--- a/src/http/ngx_http_parse.c
+++ b/src/http/ngx_http_parse.c
@@ -3,7 +3,7 @@
 #include <ngx_core.h>
 #include <ngx_http.h>
 
-int ngx_http_parse_request_line(ngx_http_request_t *r)
+ngx_int_t ngx_http_parse_request_line(ngx_http_request_t *r)
 {
     char   ch, *p;
     enum {
@@ -419,7 +419,7 @@
 }
 
 
-int ngx_http_parse_header_line(ngx_http_request_t *r, ngx_hunk_t *h)
+ngx_int_t ngx_http_parse_header_line(ngx_http_request_t *r, ngx_hunk_t *h)
 {
     char   c, ch, *p;
     enum {
@@ -621,7 +621,7 @@
 }
 
 
-int ngx_http_parse_complex_uri(ngx_http_request_t *r)
+ngx_int_t ngx_http_parse_complex_uri(ngx_http_request_t *r)
 {
     char  c, ch, decoded, *p, *u;
     enum {
diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c
index 51478c6..66bf9be 100644
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -42,10 +42,12 @@
 };
 
 
+#if 0
 static void ngx_http_dummy(ngx_event_t *wev)
 {
     return;
 }
+#endif
 
 
 void ngx_http_init_connection(ngx_connection_t *c)
@@ -110,7 +112,7 @@
 
 static void ngx_http_init_request(ngx_event_t *rev)
 {
-    int                        i;
+    ngx_int_t                  i;
     socklen_t                  len;
     struct sockaddr_in         addr_in;
     ngx_connection_t          *c;
@@ -147,8 +149,6 @@
     in_port = c->servers;
     in_addr = in_port->addrs.elts;
 
-ngx_log_debug(rev->log, "IN: %08x" _ in_port);
-
     r->port = in_port->port;
     r->port_name = &in_port->port_name;
 
@@ -274,8 +274,8 @@
 
 static void ngx_http_process_request_line(ngx_event_t *rev)
 {
-    int                        rc, offset;
     ssize_t                    n;
+    ngx_int_t                  rc, offset;
     ngx_connection_t          *c;
     ngx_http_request_t        *r;
     ngx_http_log_ctx_t        *ctx;
@@ -439,15 +439,18 @@
             ngx_cpystrn(r->args.data, r->args_start, r->args.len + 1);
         }
 
-#if 1 /* DEBUG */
-        if (r->exten.data == NULL) { r->exten.data = ""; }
-        if (r->args.data == NULL) { r->args.data = ""; }
-        ngx_log_debug(c->log, "HTTP: %d, %d, '%s', '%s', '%s'" _
-                      r->method _ r->http_version _
-                      r->uri.data _ r->exten.data _ r->args.data);
-        if (r->exten.data[0] == '\0') { r->exten.data = NULL; }
-        if (r->args.data[0] == '\0') { r->args.data = NULL; }
-#endif
+        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
+                       "http request line: \"%s\"", r->request_line.data);
+
+        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
+                       "http uri: \"%s\"", r->uri.data);
+
+        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
+                       "http args: \"%s\"", r->args.data ? r->args.data : "");
+
+        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
+                       "http exten: \"%s\"",
+                       r->exten.data ? r->exten.data : "");
 
         if (r->http_version < NGX_HTTP_VERSION_10) {
             rev->event_handler = ngx_http_block_read;
@@ -532,8 +535,8 @@
 
 static void ngx_http_process_request_headers(ngx_event_t *rev)
 {
-    int                        rc, i, offset;
     ssize_t                    n;
+    ngx_int_t                  rc, i, offset;
     ngx_table_elt_t           *h;
     ngx_connection_t          *c;
     ngx_http_request_t        *r;
@@ -616,8 +619,9 @@
                 }
             }
 
-            ngx_log_debug(r->connection->log, "HTTP header: '%s: %s'" _
-                          h->key.data _ h->value.data);
+            ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                           "http header: \"%s: %s\"'",
+                           h->key.data, h->value.data);
 
             if (cscf->large_client_header
                 && r->header_in->pos == r->header_in->last)
@@ -631,7 +635,8 @@
 
             /* a whole header has been parsed successfully */
 
-            ngx_log_debug(r->connection->log, "HTTP header done");
+            ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                           "http header done");
 
             rc = ngx_http_process_request_header(r);
 
@@ -746,10 +751,10 @@
 }
 
 
-static int ngx_http_process_request_header(ngx_http_request_t *r)
+static ngx_int_t ngx_http_process_request_header(ngx_http_request_t *r)
 {
-    int                        i;
     size_t                     len;
+    ngx_int_t                  i;
     ngx_http_server_name_t    *name;
     ngx_http_core_loc_conf_t  *clcf;
 
@@ -828,13 +833,16 @@
 
 void ngx_http_finalize_request(ngx_http_request_t *r, int rc)
 {
+    ngx_http_core_loc_conf_t  *clcf;
+
     /* r can be already destroyed when rc == NGX_DONE */
 
     if (rc == NGX_DONE || r->main) {
         return;
     }
 
-    ngx_log_debug(r->connection->log, "finalize http request");
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                   "http finalize request");
 
     if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
 
@@ -868,10 +876,12 @@
         ngx_del_timer(r->connection->write);
     }
 
-    if (r->keepalive != 0) {
+    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
+
+    if (r->keepalive != 0 && clcf->keepalive_timeout > 0) {
         ngx_http_set_keepalive(r);
 
-    } else if (r->lingering_close) {
+    } else if (r->lingering_close && clcf->lingering_timeout > 0) {
         ngx_http_set_lingering_close(r);
 
     } else {
@@ -916,7 +926,7 @@
     ngx_http_request_t        *r;
     ngx_http_core_loc_conf_t  *clcf;
 
-    ngx_log_debug(wev->log, "http writer handler");
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, "http writer handler");
 
     c = wev->data;
     r = c->data;
@@ -934,7 +944,8 @@
 
     rc = ngx_http_output_filter(r, NULL);
 
-    ngx_log_debug(c->log, "writer output filter: %d" _ rc);
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
+                  "http writer output filter: %d", rc);
 
     if (rc == NGX_AGAIN) {
         if (!wev->ready) {
@@ -951,7 +962,7 @@
         return;
     }
 
-    ngx_log_debug(c->log, "http writer done");
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http writer done");
 
     ngx_http_finalize_request(r, rc);
 }
@@ -962,7 +973,7 @@
     ngx_connection_t          *c;
     ngx_http_request_t        *r;
 
-    ngx_log_debug(rev->log, "http read blocked");
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0, "http read blocked");
 
     /* aio does not call this handler */
 
@@ -986,7 +997,7 @@
 
     rev = r->connection->read;
 
-    ngx_log_debug(rev->log, "set discard body");
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0, "http set discard body");
 
     if (rev->timer_set) {
         ngx_del_timer(rev);
diff --git a/src/http/ngx_http_request.h b/src/http/ngx_http_request.h
index 82e8bf7..785c8e1 100644
--- a/src/http/ngx_http_request.h
+++ b/src/http/ngx_http_request.h
@@ -217,6 +217,8 @@
 
     ngx_array_t          cleanup;
 
+    size_t               header_size;
+
     char                *discarded_buffer;
     void               **err_ctx;
     int                  err_status;