nginx-0.3.15-RELEASE import

    *) Feature: the new 444 code of the "return" directive to close
       connection.

    *) Feature: the "so_keepalive" directive in IMAP/POP3 proxy.

    *) Bugfix: if there are unclosed connection nginx now calls abort()
       only on gracefull quit and active "debug_points" directive.
diff --git a/src/imap/ngx_imap.h b/src/imap/ngx_imap.h
index 1f3c12e..90375bf 100644
--- a/src/imap/ngx_imap.h
+++ b/src/imap/ngx_imap.h
@@ -40,6 +40,8 @@
 
     ngx_uint_t              protocol;
 
+    ngx_flag_t              so_keepalive;
+
     ngx_str_t               pop3_capability;
     ngx_str_t               pop3_starttls_capability;
     ngx_str_t               imap_capability;
diff --git a/src/imap/ngx_imap_core_module.c b/src/imap/ngx_imap_core_module.c
index e805a81..44484ac 100644
--- a/src/imap/ngx_imap_core_module.c
+++ b/src/imap/ngx_imap_core_module.c
@@ -75,6 +75,13 @@
       offsetof(ngx_imap_core_srv_conf_t, imap_client_buffer_size),
       NULL },
 
+    { ngx_string("so_keepalive"),
+      NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_FLAG,
+      ngx_conf_set_flag_slot,
+      NGX_IMAP_SRV_CONF_OFFSET,
+      offsetof(ngx_imap_core_srv_conf_t, so_keepalive),
+      NULL },
+
     { ngx_string("timeout"),
       NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1,
       ngx_conf_set_msec_slot,
@@ -156,8 +163,9 @@
     }
 
     cscf->imap_client_buffer_size = NGX_CONF_UNSET_SIZE;
-    cscf->timeout = NGX_CONF_UNSET_MSEC;
     cscf->protocol = NGX_CONF_UNSET_UINT;
+    cscf->timeout = NGX_CONF_UNSET_MSEC;
+    cscf->so_keepalive = NGX_CONF_UNSET;
 
     if (ngx_array_init(&cscf->pop3_capabilities, cf->pool, 4, sizeof(ngx_str_t))
         != NGX_OK)
@@ -192,6 +200,7 @@
     ngx_conf_merge_msec_value(conf->timeout, prev->timeout, 60000);
     ngx_conf_merge_unsigned_value(conf->protocol, prev->protocol,
                               NGX_IMAP_IMAP_PROTOCOL);
+    ngx_conf_merge_value(conf->so_keepalive, prev->so_keepalive, 0);
 
 
     if (conf->pop3_capabilities.nelts == 0) {
diff --git a/src/imap/ngx_imap_proxy_module.c b/src/imap/ngx_imap_proxy_module.c
index bdce364..7eb1b94 100644
--- a/src/imap/ngx_imap_proxy_module.c
+++ b/src/imap/ngx_imap_proxy_module.c
@@ -91,10 +91,27 @@
 void
 ngx_imap_proxy_init(ngx_imap_session_t *s, ngx_peers_t *peers)
 {
+    int                        keepalive;
     ngx_int_t                  rc;
     ngx_imap_proxy_ctx_t      *p;
     ngx_imap_core_srv_conf_t  *cscf;
 
+    s->connection->log->action = "connecting to upstream";
+
+    cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
+
+    if (cscf->so_keepalive) {
+        keepalive = 1;
+
+        if (setsockopt(s->connection->fd, SOL_SOCKET, SO_KEEPALIVE,
+                       (const void *) &keepalive, sizeof(int))
+                == -1)
+        {
+            ngx_log_error(NGX_LOG_ALERT, s->connection->log, ngx_socket_errno,
+                          "setsockopt(SO_KEEPALIVE) failed");
+        }
+    }
+
     p = ngx_pcalloc(s->connection->pool, sizeof(ngx_imap_proxy_ctx_t));
     if (p == NULL) {
         ngx_imap_session_internal_server_error(s);
@@ -107,8 +124,6 @@
     p->upstream.log = s->connection->log;
     p->upstream.log_error = NGX_ERROR_ERR;
 
-    s->connection->log->action = "in upstream auth state";
-
     rc = ngx_event_connect_peer(&p->upstream);
 
     if (rc == NGX_ERROR || rc == NGX_BUSY || rc == NGX_DECLINED) {
@@ -116,7 +131,6 @@
         return;
     }
 
-    cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
     ngx_add_timer(p->upstream.connection->read, cscf->timeout);
 
     p->upstream.connection->data = s;
@@ -205,6 +219,8 @@
         ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0,
                        "imap proxy send login");
 
+        s->connection->log->action = "sending LOGIN command to upstream";
+
         line.len = s->tag.len + sizeof("LOGIN ") - 1
                    + 1 + NGX_SIZE_T_LEN + 1 + 2;
         line.data = ngx_palloc(c->pool, line.len);
@@ -223,6 +239,8 @@
     case ngx_imap_login:
         ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy send user");
 
+        s->connection->log->action = "sending user name to upstream";
+
         line.len = s->login.len + 1 + 1 + NGX_SIZE_T_LEN + 1 + 2;
         line.data = ngx_palloc(c->pool, line.len);
         if (line.data == NULL) {
@@ -241,6 +259,8 @@
         ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0,
                        "imap proxy send passwd");
 
+        s->connection->log->action = "sending password to upstream";
+
         line.len = s->passwd.len + 2;
         line.data = ngx_palloc(c->pool, line.len);
         if (line.data == NULL) {
@@ -340,6 +360,8 @@
     case ngx_pop3_start:
         ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy send user");
 
+        s->connection->log->action = "sending user name to upstream";
+
         line.len = sizeof("USER ")  - 1 + s->login.len + 2;
         line.data = ngx_palloc(c->pool, line.len);
         if (line.data == NULL) {
@@ -357,6 +379,8 @@
     case ngx_pop3_user:
         ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy send pass");
 
+        s->connection->log->action = "sending password to upstream";
+
         line.len = sizeof("PASS ")  - 1 + s->passwd.len + 2;
         line.data = ngx_palloc(c->pool, line.len);
         if (line.data == NULL) {
@@ -431,6 +455,8 @@
     ssize_t     n;
     ngx_buf_t  *b;
 
+    s->connection->log->action = "reading response from upstream";
+
     b = s->proxy->buffer;
 
     n = s->proxy->upstream.connection->recv(s->proxy->upstream.connection,