changeset 136:3656228c0b56 NGINX_0_3_15

nginx 0.3.15 *) 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.
author Igor Sysoev <http://sysoev.ru>
date Wed, 07 Dec 2005 00:00:00 +0300
parents c1ac76c0e9df
children 768f51dd150b
files CHANGES CHANGES.ru src/core/nginx.h src/core/ngx_connection.c src/event/ngx_event_pipe.c src/http/modules/ngx_http_static_module.c src/http/ngx_http_request.c src/http/ngx_http_request.h src/http/ngx_http_special_response.c src/imap/ngx_imap.h src/imap/ngx_imap_core_module.c src/imap/ngx_imap_proxy_module.c src/os/unix/ngx_freebsd_sendfile_chain.c src/os/unix/ngx_process_cycle.c src/os/unix/ngx_readv_chain.c src/os/unix/ngx_recv.c src/os/unix/ngx_send.c src/os/unix/ngx_writev_chain.c
diffstat 18 files changed, 120 insertions(+), 70 deletions(-) [+]
line wrap: on
line diff
--- a/CHANGES
+++ b/CHANGES
@@ -1,3 +1,14 @@
+
+Changes with nginx 0.3.15                                        07 Dec 2005
+
+    *) 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.
+
 
 Changes with nginx 0.3.14                                        05 Dec 2005
 
--- a/CHANGES.ru
+++ b/CHANGES.ru
@@ -1,3 +1,14 @@
+
+Изменения в nginx 0.3.15                                          07.12.2005
+
+    *) Добавление: новой код 444 в директиве return для закрытия соединения.
+
+    *) Добавление: директива so_keepalive в IMAP/POP3 прокси.
+
+    *) Исправление: nginx теперь вызывает abort() при обнаружении 
+       незакрытых соединений только при планом выходе и включённой 
+       директиве debug_points.
+
 
 Изменения в nginx 0.3.14                                          05.12.2005
 
--- a/src/core/nginx.h
+++ b/src/core/nginx.h
@@ -8,7 +8,7 @@
 #define _NGINX_H_INCLUDED_
 
 
-#define NGINX_VER          "nginx/0.3.14"
+#define NGINX_VER          "nginx/0.3.15"
 
 #define NGINX_VAR          "NGINX"
 #define NGX_OLDPID_EXT     ".oldbin"
--- a/src/core/ngx_connection.c
+++ b/src/core/ngx_connection.c
@@ -719,6 +719,9 @@ ngx_connection_error(ngx_connection_t *c
         || err == NGX_EPIPE
 #endif
         || err == NGX_ENOTCONN
+#if !(NGX_CRIT_ETIMEDOUT)
+        || err == NGX_ETIMEDOUT
+#endif
         || err == NGX_ECONNREFUSED
         || err == NGX_EHOSTUNREACH)
     {
--- a/src/event/ngx_event_pipe.c
+++ b/src/event/ngx_event_pipe.c
@@ -131,9 +131,7 @@ ngx_event_pipe_read_upstream(ngx_event_p
              */
 
             if (p->upstream->read->available == 0
-                && p->upstream->read->pending_eof
-                /* FreeBSD 5.x-6.x may erroneously report ETIMEDOUT */
-                && p->upstream->read->kq_errno != NGX_ETIMEDOUT)
+                && p->upstream->read->pending_eof)
             {
                 p->upstream->read->ready = 0;
                 p->upstream->read->eof = 0;
--- a/src/http/modules/ngx_http_static_module.c
+++ b/src/http/modules/ngx_http_static_module.c
@@ -244,12 +244,12 @@ ngx_http_static_handler(ngx_http_request
         return NGX_HTTP_INTERNAL_SERVER_ERROR;
     }
 
-    r->allow_ranges = 1;
-
-    if (r->header_only || (r->main != r && ngx_file_size(&fi) == 0)) {
+    if (r->main != r && ngx_file_size(&fi) == 0) {
         return ngx_http_send_header(r);
     }
 
+    r->allow_ranges = 1;
+
     /* we need to allocate all before the header would be sent */
 
     b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -1460,6 +1460,11 @@ ngx_http_finalize_request(ngx_http_reque
 
     if (rc >= NGX_HTTP_SPECIAL_RESPONSE || rc == NGX_HTTP_NO_CONTENT) {
 
+        if (rc == NGX_HTTP_CLOSE) {
+            ngx_http_close_request(r, rc);
+            return;
+        }
+
         if (r->main == r) {
             if (r->connection->read->timer_set) {
                 ngx_del_timer(r->connection->read);
@@ -1556,6 +1561,7 @@ ngx_http_finalize_request(ngx_http_reque
         ngx_del_timer(r->connection->write);
     }
 
+#if 0
     if (r->connection->read->pending_eof) {
 #if (NGX_HAVE_KQUEUE)
         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log,
@@ -1565,6 +1571,7 @@ ngx_http_finalize_request(ngx_http_reque
         ngx_http_close_request(r, 0);
         return;
     }
+#endif
 
     if (!ngx_terminate
          && !ngx_exiting
@@ -2046,10 +2053,7 @@ ngx_http_keepalive_handler(ngx_event_t *
 #if (NGX_HAVE_KQUEUE)
 
     if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
-        if (rev->pending_eof
-            /* FreeBSD 5.x-6.x may erroneously report ETIMEDOUT */
-            && rev->kq_errno != NGX_ETIMEDOUT)
-        {
+        if (rev->pending_eof) {
             c->log->handler = NULL;
             ngx_log_error(NGX_LOG_INFO, c->log, rev->kq_errno,
                           "kevent() reported that client %V closed "
--- a/src/http/ngx_http_request.h
+++ b/src/http/ngx_http_request.h
@@ -66,7 +66,10 @@
 
 /* Our own HTTP codes */
 
-#define NGX_HTTP_NGX_CODES                 NGX_HTTP_TO_HTTPS
+/* The special code to close connection without any response */
+#define NGX_HTTP_CLOSE                     444
+
+#define NGX_HTTP_OWN_CODES                 NGX_HTTP_TO_HTTPS
 
 /*
  * We use the special code for the plain HTTP requests that are sent to
--- a/src/http/ngx_http_special_response.c
+++ b/src/http/ngx_http_special_response.c
@@ -328,14 +328,14 @@ ngx_http_special_response_handler(ngx_ht
         /* 3XX */
         err = error - NGX_HTTP_MOVED_PERMANENTLY + NGX_HTTP_LEVEL_200;
 
-    } else if (error < NGX_HTTP_NGX_CODES) {
+    } else if (error < NGX_HTTP_OWN_CODES) {
         /* 4XX */
         err = error - NGX_HTTP_BAD_REQUEST + NGX_HTTP_LEVEL_200
                                            + NGX_HTTP_LEVEL_300;
 
     } else {
         /* 49X, 5XX */
-        err = error - NGX_HTTP_NGX_CODES + NGX_HTTP_LEVEL_200
+        err = error - NGX_HTTP_OWN_CODES + NGX_HTTP_LEVEL_200
                                          + NGX_HTTP_LEVEL_300
                                          + NGX_HTTP_LEVEL_400;
         switch (error) {
--- a/src/imap/ngx_imap.h
+++ b/src/imap/ngx_imap.h
@@ -40,6 +40,8 @@ typedef struct {
 
     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;
--- a/src/imap/ngx_imap_core_module.c
+++ b/src/imap/ngx_imap_core_module.c
@@ -75,6 +75,13 @@ static ngx_command_t  ngx_imap_core_comm
       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 @@ ngx_imap_core_create_srv_conf(ngx_conf_t
     }
 
     cscf->imap_client_buffer_size = NGX_CONF_UNSET_SIZE;
+    cscf->protocol = NGX_CONF_UNSET_UINT;
     cscf->timeout = NGX_CONF_UNSET_MSEC;
-    cscf->protocol = NGX_CONF_UNSET_UINT;
+    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_imap_core_merge_srv_conf(ngx_conf_t 
     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) {
--- a/src/imap/ngx_imap_proxy_module.c
+++ b/src/imap/ngx_imap_proxy_module.c
@@ -91,10 +91,27 @@ ngx_module_t  ngx_imap_proxy_module = {
 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 @@ ngx_imap_proxy_init(ngx_imap_session_t *
     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 @@ ngx_imap_proxy_init(ngx_imap_session_t *
         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_imap_proxy_imap_handler(ngx_event_t 
         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 @@ ngx_imap_proxy_imap_handler(ngx_event_t 
     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_imap_proxy_imap_handler(ngx_event_t 
         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 @@ ngx_imap_proxy_pop3_handler(ngx_event_t 
     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 @@ ngx_imap_proxy_pop3_handler(ngx_event_t 
     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 @@ ngx_imap_proxy_read_response(ngx_imap_se
     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,
--- a/src/os/unix/ngx_freebsd_sendfile_chain.c
+++ b/src/os/unix/ngx_freebsd_sendfile_chain.c
@@ -56,11 +56,7 @@ ngx_freebsd_sendfile_chain(ngx_connectio
 
 #if (NGX_HAVE_KQUEUE)
 
-    if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT)
-        && wev->pending_eof
-           /* FreeBSD 5.x-6.x may erroneously report ETIMEDOUT */
-        && wev->kq_errno != NGX_ETIMEDOUT)
-    {
+    if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT) && wev->pending_eof) {
         (void) ngx_connection_error(c, wev->kq_errno,
                                "kevent() reported about an closed connection");
         wev->error = 1;
--- a/src/os/unix/ngx_process_cycle.c
+++ b/src/os/unix/ngx_process_cycle.c
@@ -938,23 +938,25 @@ ngx_worker_process_exit(ngx_cycle_t *cyc
         }
     }
 
-    c = cycle->connections;
-    for (i = 0; i < cycle->connection_n; i++) {
-        if (c[i].fd != -1
-            && c[i].read
-            && !c[i].read->accept
-            && !c[i].read->channel)
-        {
-            ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
-                          "open socket #%d left in %ui connection, "
-                          "aborting",
-                          c[i].fd, i);
-            ngx_abort();
+    if (ngx_quit) {
+        c = cycle->connections;
+        for (i = 0; i < cycle->connection_n; i++) {
+            if (c[i].fd != -1
+                && c[i].read
+                && !c[i].read->accept
+                && !c[i].read->channel)
+            {
+                ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
+                              "open socket #%d left in %ui connection, "
+                              "aborting",
+                              c[i].fd, i);
+                ngx_debug_point();
+            }
         }
-    }
 
-    if (ngx_debug_quit) {
-        ngx_debug_point();
+        if (ngx_debug_quit) {
+            ngx_debug_point();
+        }
     }
 
     /*
--- a/src/os/unix/ngx_readv_chain.c
+++ b/src/os/unix/ngx_readv_chain.c
@@ -32,26 +32,23 @@ ngx_readv_chain(ngx_connection_t *c, ngx
                        rev->pending_eof, rev->available, rev->kq_errno);
 
         if (rev->available == 0) {
-
-            if (!rev->pending_eof) {
-                return NGX_AGAIN;
-            }
-
-            /* FreeBSD 5.x-6.x may erroneously report ETIMEDOUT */
-            if (rev->kq_errno != NGX_ETIMEDOUT) {
-
+            if (rev->pending_eof) {
                 rev->ready = 0;
                 rev->eof = 1;
 
+                ngx_log_error(NGX_LOG_INFO, c->log, rev->kq_errno,
+                              "kevent() reported about an closed connection");
+
                 if (rev->kq_errno) {
                     rev->error = 1;
                     ngx_set_socket_errno(rev->kq_errno);
-
-                    return ngx_connection_error(c, rev->kq_errno,
-                               "kevent() reported about an closed connection");
+                    return NGX_ERROR;
                 }
 
                 return 0;
+
+            } else {
+                return NGX_AGAIN;
             }
         }
     }
@@ -100,8 +97,8 @@ ngx_readv_chain(ngx_connection_t *c, ngx
                 rev->available -= n;
 
                 /*
-                 * rev->available can be negative here because some additional
-                 * bytes can be received between kevent() and recv()
+                 * rev->available may be negative here because some additional
+                 * bytes may be received between kevent() and recv()
                  */
 
                 if (rev->available <= 0) {
--- a/src/os/unix/ngx_recv.c
+++ b/src/os/unix/ngx_recv.c
@@ -25,14 +25,7 @@ ssize_t ngx_unix_recv(ngx_connection_t *
                        rev->pending_eof, rev->available, rev->kq_errno);
 
         if (rev->available == 0) {
-
-            if (!rev->pending_eof) {
-                return NGX_AGAIN;
-            }
-
-            /* FreeBSD 5.x-6.x may erroneously report ETIMEDOUT */
-            if (rev->kq_errno != NGX_ETIMEDOUT) {
-
+            if (rev->pending_eof) {
                 rev->ready = 0;
                 rev->eof = 1;
 
@@ -45,6 +38,9 @@ ssize_t ngx_unix_recv(ngx_connection_t *
                 }
 
                 return 0;
+
+            } else {
+                return NGX_AGAIN;
             }
         }
     }
@@ -61,7 +57,7 @@ ssize_t ngx_unix_recv(ngx_connection_t *
 
                 /*
                  * rev->available may be negative here because some additional
-                 * bytes can be received between kevent() and recv()
+                 * bytes may be received between kevent() and recv()
                  */
 
                 if (rev->available <= 0) {
--- a/src/os/unix/ngx_send.c
+++ b/src/os/unix/ngx_send.c
@@ -19,11 +19,7 @@ ssize_t ngx_unix_send(ngx_connection_t *
 
 #if (NGX_HAVE_KQUEUE)
 
-    if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT)
-        && wev->pending_eof
-           /* FreeBSD 5.x-6.x may erroneously report ETIMEDOUT */
-        && wev->kq_errno != NGX_ETIMEDOUT)
-    {
+    if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT) && wev->pending_eof) {
         (void) ngx_connection_error(c, wev->kq_errno,
                                "kevent() reported about an closed connection");
         wev->error = 1;
--- a/src/os/unix/ngx_writev_chain.c
+++ b/src/os/unix/ngx_writev_chain.c
@@ -33,11 +33,7 @@ ngx_writev_chain(ngx_connection_t *c, ng
 
 #if (NGX_HAVE_KQUEUE)
 
-    if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT)
-        && wev->pending_eof
-           /* FreeBSD 5.x-6.x may erroneously report ETIMEDOUT */
-        && wev->kq_errno != NGX_ETIMEDOUT)
-    {
+    if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT) && wev->pending_eof) {
         (void) ngx_connection_error(c, wev->kq_errno,
                                "kevent() reported about an closed connection");
         wev->error = 1;