diff src/http/ngx_http_upstream.c @ 294:27d9d1f26b38 NGINX_0_5_17

nginx 0.5.17 *) Change: now nginx always returns the 405 status for the TRACE method. *) Feature: now nginx supports the "include" directive inside the "types" block. *) Bugfix: the $document_root variable usage in the "root" and "alias" directives is disabled: this caused recursive stack overflow. *) Bugfix: in the HTTPS protocol in the "proxy_pass" directive. *) Bugfix: in some cases non-cachable variables (such as $uri variable) returned old cached value.
author Igor Sysoev <http://sysoev.ru>
date Mon, 02 Apr 2007 00:00:00 +0400
parents 92402f034b28
children 2ceaee987f37
line wrap: on
line diff
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -21,6 +21,7 @@ static void ngx_http_upstream_send_reque
     ngx_http_upstream_t *u);
 static void ngx_http_upstream_send_request_handler(ngx_event_t *wev);
 static void ngx_http_upstream_process_header(ngx_event_t *rev);
+static ngx_int_t ngx_http_upstream_test_connect(ngx_connection_t *c);
 static void ngx_http_upstream_process_body_in_memory(ngx_event_t *rev);
 static void ngx_http_upstream_send_response(ngx_http_request_t *r,
     ngx_http_upstream_t *u);
@@ -757,8 +758,7 @@ ngx_http_upstream_reinit(ngx_http_reques
 static void
 ngx_http_upstream_send_request(ngx_http_request_t *r, ngx_http_upstream_t *u)
 {
-    int                rc, err;
-    socklen_t          len;
+    ngx_int_t          rc;
     ngx_connection_t  *c;
 
     c = u->peer.connection;
@@ -766,41 +766,9 @@ ngx_http_upstream_send_request(ngx_http_
     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
                    "http upstream send request");
 
-    if (!u->request_sent) {
-
-#if (NGX_HAVE_KQUEUE)
-
-        if (ngx_event_flags & NGX_USE_KQUEUE_EVENT)  {
-            if (c->write->pending_eof) {
-                (void) ngx_connection_error(c, c->write->kq_errno,
-                                    "kevent() reported that connect() failed");
-                ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
-                return;
-            }
-
-        } else
-#endif
-        {
-            err = 0;
-            len = sizeof(int);
-
-            /*
-             * BSDs and Linux return 0 and set a pending error in err
-             * Solaris returns -1 and sets errno
-             */
-
-            if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, (void *) &err, &len)
-                == -1)
-            {
-                err = ngx_errno;
-            }
-
-            if (err) {
-                (void) ngx_connection_error(c, err, "connect() failed");
-                ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
-                return;
-            }
-        }
+    if (!u->request_sent && ngx_http_upstream_test_connect(c) != NGX_OK) {
+	ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
+        return;
     }
 
     c->log->action = "sending request to upstream";
@@ -945,6 +913,11 @@ ngx_http_upstream_process_header(ngx_eve
         return;
     }
 
+    if (!u->request_sent && ngx_http_upstream_test_connect(c) != NGX_OK) {
+	ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
+        return;
+    }
+
     if (u->buffer.start == NULL) {
         u->buffer.start = ngx_palloc(r->pool, u->conf->buffer_size);
         if (u->buffer.start == NULL) {
@@ -1277,6 +1250,48 @@ ngx_http_upstream_process_header(ngx_eve
 }
 
 
+static ngx_int_t
+ngx_http_upstream_test_connect(ngx_connection_t *c)
+{
+    int        err;
+    socklen_t  len;
+
+#if (NGX_HAVE_KQUEUE)
+
+    if (ngx_event_flags & NGX_USE_KQUEUE_EVENT)  {
+	if (c->write->pending_eof) {
+	    (void) ngx_connection_error(c, c->write->kq_errno,
+			            "kevent() reported that connect() failed");
+	    return NGX_ERROR;
+	}
+
+    } else
+#endif
+    {
+	err = 0;
+	len = sizeof(int);
+
+	/*
+	 * BSDs and Linux return 0 and set a pending error in err
+	 * Solaris returns -1 and sets errno
+	 */
+
+	if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, (void *) &err, &len)
+	    == -1)
+	{
+	    err = ngx_errno;
+	}
+
+	if (err) {
+	    (void) ngx_connection_error(c, err, "connect() failed");
+	    return NGX_ERROR;
+	}
+    }
+
+    return NGX_OK;
+}
+
+
 static void
 ngx_http_upstream_process_body_in_memory(ngx_event_t *rev)
 {