nginx-0.1.37-RELEASE import

    *) Change: now the "\n" is added to the end of the "nginx.pid" file.

    *) Bugfix: the responses may be transferred not completely, if many
       parts or the big parts were included by SSI.

    *) Bugfix: if all backends had returned the 404 reponse and the
       "http_404" parameter of the "proxy_next_upstream" or
       "fastcgi_next_upstream" directives was used, then nginx started to
       request all backends again.
diff --git a/src/imap/ngx_imap.h b/src/imap/ngx_imap.h
index 38f4498..5ba3900 100644
--- a/src/imap/ngx_imap.h
+++ b/src/imap/ngx_imap.h
@@ -136,6 +136,7 @@
 
 void ngx_imap_init_connection(ngx_connection_t *c);
 void ngx_imap_close_connection(ngx_connection_t *c);
+void ngx_imap_session_internal_server_error(ngx_imap_session_t *s);
 
 ngx_int_t ngx_pop3_parse_command(ngx_imap_session_t *s);
 
diff --git a/src/imap/ngx_imap_auth_http_module.c b/src/imap/ngx_imap_auth_http_module.c
index 7e6c44b..fa984b8 100644
--- a/src/imap/ngx_imap_auth_http_module.c
+++ b/src/imap/ngx_imap_auth_http_module.c
@@ -23,6 +23,7 @@
 
 typedef struct {
     ngx_buf_t              *request;
+    ngx_buf_t              *response;
     ngx_peer_connection_t   peer;
 } ngx_imap_auth_http_ctx_t;
 
@@ -91,7 +92,7 @@
 
     ctx = ngx_pcalloc(s->connection->pool, sizeof(ngx_imap_auth_http_ctx_t));
     if (ctx == NULL) {
-        ngx_imap_close_connection(s->connection);
+        ngx_imap_session_internal_server_error(s);
         return;
     }
 
@@ -99,7 +100,7 @@
 
     ctx->request = ngx_imap_auth_http_create_request(s, ahcf);
     if (ctx->request == NULL) {
-        ngx_imap_close_connection(s->connection);
+        ngx_imap_session_internal_server_error(s);
         return;
     }
 
@@ -112,7 +113,7 @@
     rc = ngx_event_connect_peer(&ctx->peer);
 
     if (rc == NGX_ERROR) {
-        ngx_imap_close_connection(s->connection);
+        ngx_imap_session_internal_server_error(s);
         return;
     }
 
@@ -153,8 +154,8 @@
     if (wev->timedout) {  
         ngx_log_error(NGX_LOG_ERR, wev->log, NGX_ETIMEDOUT,
                       "auth http server timed out");
-        ngx_imap_close_connection(ctx->peer.connection);
-        ngx_imap_close_connection(s->connection);
+        ngx_close_connection(ctx->peer.connection);
+        ngx_imap_session_internal_server_error(s);
         return;
     }
 
@@ -163,8 +164,8 @@
     n = ngx_send(c, ctx->request->pos, size);
 
     if (n == NGX_ERROR) {
-        ngx_imap_close_connection(ctx->peer.connection);
-        ngx_imap_close_connection(s->connection);
+        ngx_close_connection(ctx->peer.connection);
+        ngx_imap_session_internal_server_error(s);
         return;
     }
 
@@ -192,23 +193,52 @@
 static void
 ngx_imap_auth_http_read_handler(ngx_event_t *rev)
 {
+    ssize_t                     n, size;
     ngx_peers_t                *peers;
     ngx_connection_t          *c;
     ngx_imap_session_t        *s;
-#if 0
     ngx_imap_auth_http_ctx_t  *ctx;
-#endif
 
     c = rev->data;
     s = c->data;
 
-#if 0
-    ctx = ngx_imap_get_module_ctx(s, ngx_imap_auth_http_module);
-#endif
-
     ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0,
                    "imap auth http read handler");
 
+    ctx = ngx_imap_get_module_ctx(s, ngx_imap_auth_http_module);
+
+    if (rev->timedout) {  
+        ngx_log_error(NGX_LOG_ERR, rev->log, NGX_ETIMEDOUT,
+                      "auth http server timed out");
+        ngx_close_connection(ctx->peer.connection);
+        ngx_imap_session_internal_server_error(s);
+        return;
+    }
+
+    if (ctx->response == NULL) {
+        ctx->response = ngx_create_temp_buf(s->connection->pool, 1024);
+        if (ctx->response == NULL) {
+            ngx_close_connection(ctx->peer.connection);
+            ngx_imap_session_internal_server_error(s);
+            return;
+        }
+    }
+
+    size = ctx->response->last - ctx->response->pos;
+
+    n = ngx_recv(c, ctx->response->pos, size);
+
+    if (n == NGX_ERROR || n == 0) {
+        ngx_close_connection(ctx->peer.connection);
+        ngx_imap_session_internal_server_error(s);
+        return;
+    }
+
+
+
+
+
+
     peers = NULL;
 
     ngx_imap_proxy_init(s, peers);
@@ -231,8 +261,8 @@
 
         ctx = ngx_imap_get_module_ctx(s, ngx_imap_auth_http_module);
 
-        ngx_imap_close_connection(ctx->peer.connection);
-        ngx_imap_close_connection(s->connection);
+        ngx_close_connection(ctx->peer.connection);
+        ngx_imap_session_internal_server_error(s);
     }
 }
 
diff --git a/src/imap/ngx_imap_handler.c b/src/imap/ngx_imap_handler.c
index be2c9d1..680bf20 100644
--- a/src/imap/ngx_imap_handler.c
+++ b/src/imap/ngx_imap_handler.c
@@ -24,6 +24,11 @@
    ngx_string("* OK " NGINX_VER " ready" CRLF)
 };
 
+static ngx_str_t  internal_server_errors[] = {
+   ngx_string("-ERR internal server error" CRLF),
+   ngx_string("* BAD internal server error" CRLF),
+};
+
 static u_char  pop3_ok[] = "+OK" CRLF;
 static u_char  pop3_invalid_command[] = "-ERR invalid command" CRLF;
 
@@ -342,6 +347,16 @@
 
 
 void
+ngx_imap_session_internal_server_error(ngx_imap_session_t *s)
+{
+    (void) ngx_send(s->connection, internal_server_errors[s->protocol].data,
+                    internal_server_errors[s->protocol].len);
+
+    ngx_imap_close_connection(s->connection);
+}
+
+
+void
 ngx_imap_close_connection(ngx_connection_t *c)
 {
     ngx_pool_t  *pool;