nginx-0.3.28-RELEASE import

    *) Feature: the "restrict_host_names" directive was canceled.

    *) Feature: the --with-cpu-opt=ppc64 configuration parameter.

    *) Bugfix: on some condition the proxied connection with a client was
       terminated prematurely.
       Thanks to Vladimir Shutoff.

    *) Bugfix: the "X-Accel-Limit-Rate" header line was not taken into
       account if the request was redirected using the "X-Accel-Redirect"
       header line.

    *) Bugfix: the "post_action" directive ran only after a successful
       completion of a request.

    *) Bugfix: the proxied response body generated by the "post_action"
       directive was transferred to a client.
diff --git a/src/http/modules/ngx_http_empty_gif_module.c b/src/http/modules/ngx_http_empty_gif_module.c
index c7af121..d72983a 100644
--- a/src/http/modules/ngx_http_empty_gif_module.c
+++ b/src/http/modules/ngx_http_empty_gif_module.c
@@ -148,7 +148,7 @@
     b->last_buf = 1;
 
     r->headers_out.status = NGX_HTTP_OK;
-    r->headers_out.content_length_n = b->last - b->pos;
+    r->headers_out.content_length_n = sizeof(ngx_empty_gif);
     r->headers_out.last_modified_time = 23349600;
 
     rc = ngx_http_send_header(r);
diff --git a/src/http/modules/ngx_http_referer_module.c b/src/http/modules/ngx_http_referer_module.c
index 4eac263..3ad419b 100644
--- a/src/http/modules/ngx_http_referer_module.c
+++ b/src/http/modules/ngx_http_referer_module.c
@@ -334,6 +334,7 @@
         }
 
         uri.len = 0;
+        uri.data = NULL;
 
         if (ngx_strcmp(value[i].data, "server_names") == 0) {
 
diff --git a/src/http/modules/perl/nginx.xs b/src/http/modules/perl/nginx.xs
index c24788c..2d7ede0 100644
--- a/src/http/modules/perl/nginx.xs
+++ b/src/http/modules/perl/nginx.xs
@@ -415,9 +415,11 @@
 
 
 int
-sendfile(r, filename)
+sendfile(r, filename, offset = -1, bytes = 0)
     nginx                     r
     char                     *filename
+    int                       offset;
+    size_t                    bytes;
 
     PREINIT:
 
@@ -460,17 +462,26 @@
         goto done;
     }
 
-    if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) {
-        ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
-                      ngx_fd_info_n " \"%s\" failed", filename);
+    if (offset == -1) {
+        offset = 0;
+    }
 
-        if (ngx_close_file(fd) == NGX_FILE_ERROR) {
-            ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
-                          ngx_close_file_n " \"%s\" failed", filename);
+    if (bytes == 0) {
+        if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) {
+            ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
+                          ngx_fd_info_n " \"%s\" failed", filename);
+
+            if (ngx_close_file(fd) == NGX_FILE_ERROR) {
+                ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
+                              ngx_close_file_n " \"%s\" failed", filename);
+            }
+
+            RETVAL = NGX_ERROR;
+            goto done;
+
         }
 
-        RETVAL = NGX_ERROR;
-        goto done;
+        bytes = ngx_file_size(&fi) - offset;
     }
 
     cln->handler = ngx_pool_cleanup_file;
@@ -481,8 +492,9 @@
     clnf->log = r->pool->log;
 
     b->in_file = 1;
-    b->file_pos = 0;
-    b->file_last = ngx_file_size(&fi);
+
+    b->file_pos = offset;
+    b->file_last = offset + bytes;
 
     b->file->fd = fd;
     b->file->log = r->connection->log;
diff --git a/src/http/ngx_http.c b/src/http/ngx_http.c
index 6211f0e..7f19a43 100644
--- a/src/http/ngx_http.c
+++ b/src/http/ngx_http.c
@@ -534,7 +534,6 @@
         /*
          * check whether all name-based servers have the same configuraiton
          *     as the default server,
-         * or some servers restrict the host names,
          * or some servers disable optimizing the server names
          */
 
@@ -545,9 +544,7 @@
             for (s = 0; s < in_addr[a].names.nelts; s++) {
 
                 if (in_addr[a].core_srv_conf != name[s].core_srv_conf
-                    || name[s].core_srv_conf->optimize_server_names == 0
-                    || name[s].core_srv_conf->restrict_host_names
-                       != NGX_HTTP_RESTRICT_HOST_OFF)
+                    || name[s].core_srv_conf->optimize_server_names == 0)
                 {
                     goto virtual_names;
                 }
@@ -556,7 +553,6 @@
             /*
              * if all name-based servers have the same configuration
              *         as the default server,
-             *     and no servers restrict the host names,
              *     and no servers disable optimizing the server names
              * then we do not need to check them at run-time at all
              */
diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c
index 1bc43de..da142c3 100644
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -67,14 +67,6 @@
 };
 
 
-static ngx_conf_enum_t  ngx_http_restrict_host_names[] = {
-    { ngx_string("off"), NGX_HTTP_RESTRICT_HOST_OFF },
-    { ngx_string("on"), NGX_HTTP_RESTRICT_HOST_ON },
-    { ngx_string("close"), NGX_HTTP_RESTRICT_HOST_CLOSE },
-    { ngx_null_string, 0 }
-};
-
-
 static ngx_command_t  ngx_http_core_commands[] = {
 
     { ngx_string("variables_hash_max_size"),
@@ -147,13 +139,6 @@
       offsetof(ngx_http_core_srv_conf_t, large_client_header_buffers),
       NULL },
 
-    { ngx_string("restrict_host_names"),
-      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
-      ngx_conf_set_enum_slot,
-      NGX_HTTP_SRV_CONF_OFFSET,
-      offsetof(ngx_http_core_srv_conf_t, restrict_host_names),
-      &ngx_http_restrict_host_names },
-
     { ngx_string("optimize_server_names"),
       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
       ngx_conf_set_flag_slot,
@@ -747,7 +732,9 @@
         r->connection->tcp_nopush = NGX_TCP_NOPUSH_DISABLED;
     }
 
-    r->limit_rate = clcf->limit_rate;
+    if (r->limit_rate == 0) {
+        r->limit_rate = clcf->limit_rate;
+    }
 
     if (clcf->handler) {
         r->content_handler = clcf->handler;
@@ -1879,7 +1866,6 @@
     cscf->request_pool_size = NGX_CONF_UNSET_SIZE;
     cscf->client_header_timeout = NGX_CONF_UNSET_MSEC;
     cscf->client_header_buffer_size = NGX_CONF_UNSET_SIZE;
-    cscf->restrict_host_names = NGX_CONF_UNSET_UINT;
     cscf->optimize_server_names = NGX_CONF_UNSET;
     cscf->ignore_invalid_headers = NGX_CONF_UNSET;
 
@@ -1965,9 +1951,6 @@
         return NGX_CONF_ERROR;
     }
 
-    ngx_conf_merge_unsigned_value(conf->restrict_host_names,
-                              prev->restrict_host_names, 0);
-
     ngx_conf_merge_value(conf->optimize_server_names,
                               prev->optimize_server_names, 1);
 
diff --git a/src/http/ngx_http_core_module.h b/src/http/ngx_http_core_module.h
index 6d815a7..9f5b65c 100644
--- a/src/http/ngx_http_core_module.h
+++ b/src/http/ngx_http_core_module.h
@@ -112,8 +112,6 @@
 
     ngx_msec_t                 client_header_timeout;
 
-    ngx_uint_t                 restrict_host_names;
-
     ngx_flag_t                 optimize_server_names;
     ngx_flag_t                 ignore_invalid_headers;
 } ngx_http_core_srv_conf_t;
diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c
index aa2b85f..b97515c 100644
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -25,7 +25,7 @@
     ngx_table_elt_t *h, ngx_uint_t offset);
 
 static ngx_int_t ngx_http_process_request_header(ngx_http_request_t *r);
-static ngx_int_t ngx_http_find_virtual_server(ngx_http_request_t *r);
+static void ngx_http_find_virtual_server(ngx_http_request_t *r);
 
 static void ngx_http_request_handler(ngx_event_t *ev);
 static ngx_int_t ngx_http_set_write_handler(ngx_http_request_t *r);
@@ -39,6 +39,7 @@
 static void ngx_http_keepalive_handler(ngx_event_t *ev);
 static void ngx_http_set_lingering_close(ngx_http_request_t *r);
 static void ngx_http_lingering_close_handler(ngx_event_t *ev);
+static ngx_int_t ngx_http_post_action(ngx_http_request_t *r);
 static void ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error);
 static void ngx_http_request_done(ngx_http_request_t *r, ngx_int_t error);
 static void ngx_http_close_connection(ngx_connection_t *c);
@@ -1164,9 +1165,8 @@
 static ngx_int_t
 ngx_http_process_request_header(ngx_http_request_t *r)
 {
-    size_t                      len;
-    u_char                     *ua, *user_agent, ch;
-    ngx_http_core_srv_conf_t   *cscf;
+    size_t   len;
+    u_char  *ua, *user_agent, ch;
 
     if (r->headers_in.host) {
         for (len = 0; len < r->headers_in.host->value.len; len++) {
@@ -1185,20 +1185,7 @@
 
         r->headers_in.host_name_len = len;
 
-        if (ngx_http_find_virtual_server(r) != NGX_OK) {
-            ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
-                          "client sent invalid \"Host\" header");
-
-            cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
-
-            if (cscf->restrict_host_names == NGX_HTTP_RESTRICT_HOST_CLOSE) {
-                ngx_http_close_request(r, NGX_HTTP_BAD_REQUEST);
-                return NGX_ERROR;
-            }
-
-            ngx_http_finalize_request(r, NGX_HTTP_INVALID_HOST);
-            return NGX_ERROR;
-        }
+        ngx_http_find_virtual_server(r);
 
     } else {
         if (r->http_version > NGX_HTTP_VERSION_10) {
@@ -1316,7 +1303,7 @@
 }
 
 
-static ngx_int_t
+static void
 ngx_http_find_virtual_server(ngx_http_request_t *r)
 {
     size_t                     len;
@@ -1328,7 +1315,7 @@
     vn = r->virtual_names;
 
     if (vn == NULL) {
-        return NGX_OK;
+        return;
     }
 
     host = r->headers_in.host->value.data;
@@ -1351,13 +1338,7 @@
         }
     }
 
-    cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
-
-    if (cscf->restrict_host_names == NGX_HTTP_RESTRICT_HOST_OFF) {
-        return NGX_OK;
-    }
-
-    return NGX_ERROR;
+    return;
 
 found:
 
@@ -1374,7 +1355,7 @@
         r->connection->log->log_level = clcf->err_log->log_level;
     }
 
-    return NGX_OK;
+    return;
 }
 
 
@@ -1411,7 +1392,15 @@
                    "http finalize request: %d, \"%V?%V\"",
                    rc, &r->uri, &r->args);
 
-    if (rc == NGX_ERROR || r->connection->error) {
+    if (rc == NGX_ERROR
+        || rc == NGX_HTTP_REQUEST_TIME_OUT
+        || r->connection->error)
+    {
+
+        if (ngx_http_post_action(r) == NGX_OK) {
+            return;
+        }
+
         ngx_http_close_request(r, 0);
         return;
     }
@@ -1515,12 +1504,7 @@
         return;
     }
 
-    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
-
-    if (clcf->post_action.data) {
-        r->http_version = NGX_HTTP_VERSION_9;
-        r->header_only = 1;
-        ngx_http_internal_redirect(r, &clcf->post_action, NULL);
+    if (ngx_http_post_action(r) == NGX_OK) {
         return;
     }
 
@@ -1537,17 +1521,7 @@
         return;
     }
 
-#if 0
-    if (r->connection->read->pending_eof) {
-#if (NGX_HAVE_KQUEUE)
-        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log,
-                       r->connection->read->kq_errno,
-                       "kevent() reported about an closed connection");
-#endif
-        ngx_http_close_request(r, 0);
-        return;
-    }
-#endif
+    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
 
     if (!ngx_terminate
          && !ngx_exiting
@@ -1616,7 +1590,7 @@
                           "client timed out");
             c->timedout = 1;
 
-            ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
+            ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT);
             return;
         }
 
@@ -2265,7 +2239,27 @@
 }
 
 
-void
+static ngx_int_t
+ngx_http_post_action(ngx_http_request_t *r)
+{
+    ngx_http_core_loc_conf_t  *clcf;
+
+    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
+
+    if (clcf->post_action.data == NULL) {
+        return NGX_DECLINED;
+    }
+
+    r->http_version = NGX_HTTP_VERSION_9;
+    r->header_only = 1;
+
+    ngx_http_internal_redirect(r, &clcf->post_action, NULL);
+
+    return NGX_OK;
+}
+
+
+static void
 ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error)
 {
     ngx_connection_t    *c;
@@ -2285,7 +2279,7 @@
 }
 
 
-void
+static void
 ngx_http_request_done(ngx_http_request_t *r, ngx_int_t error)
 {
     ngx_log_t                  *log;
diff --git a/src/http/ngx_http_request.h b/src/http/ngx_http_request.h
index 9f4ae21..facd58e 100644
--- a/src/http/ngx_http_request.h
+++ b/src/http/ngx_http_request.h
@@ -77,11 +77,7 @@
  */
 #define NGX_HTTP_TO_HTTPS                  497
 
-/*
- * We use the special code for the requests with invalid host name
- * to distinguish it from 4XX in an error page redirection
- */
-#define NGX_HTTP_INVALID_HOST              498
+/* 498 is the canceled code for the requests with invalid host name */
 
 /*
  * HTTP does not define the code for the case when a client closed
@@ -107,13 +103,6 @@
 
 
 typedef enum {
-    NGX_HTTP_RESTRICT_HOST_OFF = 0,
-    NGX_HTTP_RESTRICT_HOST_ON,
-    NGX_HTTP_RESTRICT_HOST_CLOSE
-} ngx_http_restrict_host_e;
-
-
-typedef enum {
     NGX_HTTP_INITING_REQUEST_STATE = 0,
     NGX_HTTP_READING_REQUEST_STATE,
     NGX_HTTP_PROCESS_REQUEST_STATE,
diff --git a/src/http/ngx_http_special_response.c b/src/http/ngx_http_special_response.c
index 63a3a34..4bb42cd 100644
--- a/src/http/ngx_http_special_response.c
+++ b/src/http/ngx_http_special_response.c
@@ -343,11 +343,6 @@
                 r->headers_out.status = NGX_HTTP_BAD_REQUEST;
                 error = NGX_HTTP_BAD_REQUEST;
                 break;
-
-            case NGX_HTTP_INVALID_HOST:
-                r->headers_out.status = NGX_HTTP_NOT_FOUND;
-                error = NGX_HTTP_NOT_FOUND;
-                break;
         }
     }
 
diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c
index 9afa4d5..cb55283 100644
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -1209,7 +1209,10 @@
 
     rc = ngx_http_send_header(r);
 
-    if (rc == NGX_ERROR || rc > NGX_OK) {
+    if (rc == NGX_ERROR
+        || rc > NGX_OK
+                          /* post_action */
+        || (r->http_version == NGX_HTTP_VERSION_9 && r->header_only)) {
         ngx_http_upstream_finalize_request(r, u, rc);
         return;
     }