Proxy: always emit "Host" header first.

Change-Id: Ib757bc625b61bb2d36960fc672a2d1fdfc60b6de
Signed-off-by: Piotr Sikora <piotrsikora@google.com>
Reviewed-on: https://nginx-review.googlesource.com/2831
Reviewed-by: Lizan Zhou <zlizan@google.com>
diff --git a/src/http/modules/ngx_http_proxy_module.c b/src/http/modules/ngx_http_proxy_module.c
index e1ac1ef..58c4a93 100644
--- a/src/http/modules/ngx_http_proxy_module.c
+++ b/src/http/modules/ngx_http_proxy_module.c
@@ -3421,7 +3421,7 @@
     uintptr_t                    *code;
     ngx_uint_t                    i;
     ngx_array_t                   headers_names, headers_merged;
-    ngx_keyval_t                 *src, *s, *h;
+    ngx_keyval_t                 *host, *src, *s, *h;
     ngx_hash_key_t               *hk;
     ngx_hash_init_t               hash;
     ngx_http_script_compile_t     sc;
@@ -3453,11 +3453,33 @@
         return NGX_ERROR;
     }
 
+    h = default_headers;
+
+    if (h->key.len != sizeof("Host") - 1
+        || ngx_strcasecmp(h->key.data, (u_char *) "Host") != 0)
+    {
+        return NGX_ERROR;
+    }
+
+    host = ngx_array_push(&headers_merged);
+    if (host == NULL) {
+        return NGX_ERROR;
+    }
+
+    *host = *h++;
+
     if (conf->headers_source) {
 
         src = conf->headers_source->elts;
         for (i = 0; i < conf->headers_source->nelts; i++) {
 
+            if (src[i].key.len == sizeof("Host") - 1
+                && ngx_strcasecmp(src[i].key.data, (u_char *) "Host") == 0)
+            {
+                *host = src[i];
+                continue;
+            }
+
             s = ngx_array_push(&headers_merged);
             if (s == NULL) {
                 return NGX_ERROR;
@@ -3467,8 +3489,6 @@
         }
     }
 
-    h = default_headers;
-
     while (h->key.len) {
 
         src = headers_merged.elts;