diff src/http/modules/proxy/ngx_http_proxy_upstream.c @ 287:35a6a9df2d25

nginx-0.0.2-2004-03-12-19:57:08 import
author Igor Sysoev <igor@sysoev.ru>
date Fri, 12 Mar 2004 16:57:08 +0000
parents 70e1c7d2b83d
children f81d075ad172
line wrap: on
line diff
--- a/src/http/modules/proxy/ngx_http_proxy_upstream.c
+++ b/src/http/modules/proxy/ngx_http_proxy_upstream.c
@@ -38,6 +38,8 @@ static char *upstream_header_errors[] = 
 
 static char  http_version[] = " HTTP/1.0" CRLF;
 static char  host_header[] = "Host: ";
+static char  x_real_ip_header[] = "X-Real-IP: ";
+static char  x_forwarded_for_header[] = "X-Forwarded-For: ";
 static char  connection_close_header[] = "Connection: close" CRLF;
 
 
@@ -116,6 +118,27 @@ static ngx_chain_t *ngx_http_proxy_creat
           + sizeof(connection_close_header) - 1
           + 2;                          /* 2 is for "\r\n" at the header end */
 
+    if (p->lcf->set_x_real_ip) {
+                                                          /* 2 is for "\r\n" */
+        len += sizeof(x_real_ip_header) - 1 + INET_ADDRSTRLEN - 1 + 2;
+    }
+
+
+    if (p->lcf->add_x_forwarded_for) {
+        if (r->headers_in.x_forwarded_for) {
+            len += r->headers_in.x_forwarded_for->key.len
+                   + 2                                      /* 2 is ofr ": " */
+                   + r->headers_in.x_forwarded_for->value.len
+                   + 2                                      /* 2 is ofr ", " */
+                   + INET_ADDRSTRLEN - 1
+                   + 2;                                   /* 2 is for "\r\n" */
+        } else {
+            len += sizeof(x_forwarded_for_header) - 1 + INET_ADDRSTRLEN - 1 + 2;
+                                                          /* 2 is for "\r\n" */
+        }
+    }
+
+
     header = (ngx_table_elt_t *) r->headers_in.headers->elts;
     for (i = 0; i < r->headers_in.headers->nelts; i++) {
 
@@ -156,19 +179,57 @@ static ngx_chain_t *ngx_http_proxy_creat
     h->last = ngx_cpymem(h->last, http_version, sizeof(http_version) - 1);
 
 
-    /* "Host" header */
+    /* the "Host" header */
 
     h->last = ngx_cpymem(h->last, host_header, sizeof(host_header) - 1);
     h->last = ngx_cpymem(h->last, uc->host_header.data, uc->host_header.len);
     *(h->last++) = CR; *(h->last++) = LF;
 
 
-    /* "Connection: close" header */
+    /* the "Connection: close" header */
 
     h->last = ngx_cpymem(h->last, connection_close_header,
                          sizeof(connection_close_header) - 1);
 
 
+    /* the "X-Real-IP" header */
+
+    if (p->lcf->set_x_real_ip) {
+        h->last = ngx_cpymem(h->last, x_real_ip_header,
+                             sizeof(x_real_ip_header) - 1);
+        h->last = ngx_cpymem(h->last, r->connection->addr_text.data,
+                             r->connection->addr_text.len);
+        *(h->last++) = CR; *(h->last++) = LF;
+    }
+
+
+    /* the "X-Forwarded-For" header */
+
+    if (p->lcf->add_x_forwarded_for) {
+        if (r->headers_in.x_forwarded_for) {
+            h->last = ngx_cpymem(h->last,
+                                 r->headers_in.x_forwarded_for->key.data,
+                                 r->headers_in.x_forwarded_for->key.len);
+
+            *(h->last++) = ':'; *(h->last++) = ' ';
+
+            h->last = ngx_cpymem(h->last,
+                                 r->headers_in.x_forwarded_for->value.data,
+                                 r->headers_in.x_forwarded_for->value.len);
+
+            *(h->last++) = ','; *(h->last++) = ' ';
+
+        } else {
+            h->last = ngx_cpymem(h->last, x_forwarded_for_header,
+                                 sizeof(x_forwarded_for_header) - 1);
+        }
+
+        h->last = ngx_cpymem(h->last, r->connection->addr_text.data,
+                             r->connection->addr_text.len);
+        *(h->last++) = CR; *(h->last++) = LF;
+    }
+
+
     for (i = 0; i < r->headers_in.headers->nelts; i++) {
 
         if (&header[i] == r->headers_in.host) {
@@ -183,6 +244,12 @@ static ngx_chain_t *ngx_http_proxy_creat
             continue;
         }
 
+        if (&header[i] == r->headers_in.x_forwarded_for
+            && p->lcf->add_x_forwarded_for)
+        {
+            continue;
+        }
+
         h->last = ngx_cpymem(h->last, header[i].key.data, header[i].key.len);
 
         *(h->last++) = ':'; *(h->last++) = ' ';
@@ -377,12 +444,16 @@ void ngx_http_proxy_upstream_busy_lock(n
         ft_type = NGX_HTTP_PROXY_FT_MAX_WAITING;
     }
 
+#if (NGX_HTTP_CACHE)
+
     if (p->stale && (p->lcf->use_stale & ft_type)) {
         ngx_http_proxy_finalize_request(p,
                                         ngx_http_proxy_send_cached_response(p));
         return;
     }
 
+#endif
+
     p->state->status = NGX_HTTP_SERVICE_UNAVAILABLE;
     ngx_http_proxy_finalize_request(p, NGX_HTTP_SERVICE_UNAVAILABLE);
 }
@@ -692,6 +763,8 @@ static void ngx_http_proxy_process_upstr
             return;
         }
 
+#if (NGX_HTTP_CACHE)
+
         if (p->upstream->peer.tries == 0
             && p->stale
             && (p->lcf->use_stale & NGX_HTTP_PROXY_FT_HTTP_500))
@@ -701,6 +774,8 @@ static void ngx_http_proxy_process_upstr
 
             return;
         }
+
+#endif
     }
 
     if (p->status == NGX_HTTP_NOT_FOUND
@@ -841,10 +916,14 @@ static void ngx_http_proxy_process_upstr
 
             /* TODO: hook to process the upstream header */
 
+#if (NGX_HTTP_CACHE)
+
             if (p->cachable) {
                 p->cachable = ngx_http_proxy_is_cachable(p);
             }
 
+#endif
+
             ngx_http_proxy_send_response(p);
             return;
 
@@ -1118,6 +1197,9 @@ static void ngx_http_proxy_process_body(
     }
 
     if (p->upstream->peer.connection) {
+
+#if (NGX_HTTP_FILE_CACHE)
+
         if (ep->upstream_done && p->cachable) {
             if (ngx_http_proxy_update_cache(p) == NGX_ERROR) {
                 ngx_http_busy_unlock(p->lcf->busy_lock, &p->busy_lock);
@@ -1136,6 +1218,8 @@ static void ngx_http_proxy_process_body(
             }
         }
 
+#endif
+
         if (ep->upstream_done || ep->upstream_eof || ep->upstream_error) {
             ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0,
                            "http proxy upstream exit");
@@ -1214,12 +1298,17 @@ static void ngx_http_proxy_next_upstream
 
         if (p->upstream->peer.tries == 0 || !(p->lcf->next_upstream & ft_type))
         {
+
+#if (NGX_HTTP_CACHE)
+
             if (p->stale && (p->lcf->use_stale & ft_type)) {
                 ngx_http_proxy_finalize_request(p,
                                        ngx_http_proxy_send_cached_response(p));
                 return;
             }
 
+#endif
+
             ngx_http_proxy_finalize_request(p, status);
             return;
         }