Upstream: pass "Trailer" header only when passing trailers.

Change-Id: I5b1ab1a6f233851339eb7931a3b6cb1b2ff8a454
Signed-off-by: Piotr Sikora <piotrsikora@google.com>
Reviewed-on: https://nginx-review.googlesource.com/2920
Reviewed-by: Feng Li <fengli@google.com>
diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c
index 778a16a..fbbc4b4 100644
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -151,6 +151,8 @@
     ngx_table_elt_t *h, ngx_uint_t offset);
 static ngx_int_t ngx_http_upstream_copy_allow_ranges(ngx_http_request_t *r,
     ngx_table_elt_t *h, ngx_uint_t offset);
+static ngx_int_t ngx_http_upstream_copy_trailer(ngx_http_request_t *r,
+    ngx_table_elt_t *h, ngx_uint_t offset);
 
 #if (NGX_HTTP_GZIP || NGX_COMPAT)
 static ngx_int_t ngx_http_upstream_copy_content_encoding(ngx_http_request_t *r,
@@ -311,6 +313,10 @@
                  ngx_http_upstream_process_charset, 0,
                  ngx_http_upstream_copy_header_line, 0, 0 },
 
+    { ngx_string("Trailer"),
+                 ngx_http_upstream_ignore_header_line, 0,
+                 ngx_http_upstream_copy_trailer, 0, 0 },
+
     { ngx_string("Transfer-Encoding"),
                  ngx_http_upstream_process_transfer_encoding, 0,
                  ngx_http_upstream_ignore_header_line, 0, 0 },
@@ -5233,6 +5239,29 @@
 }
 
 
+static ngx_int_t
+ngx_http_upstream_copy_trailer(ngx_http_request_t *r,
+    ngx_table_elt_t *h, ngx_uint_t offset)
+{
+    ngx_table_elt_t  *ho;
+
+    if (!r->upstream->conf->pass_trailers
+        || !r->allow_trailers || !r->expect_trailers)
+    {
+        return NGX_OK;
+    }
+
+    ho = ngx_list_push(&r->headers_out.headers);
+    if (ho == NULL) {
+        return NGX_ERROR;
+    }
+
+    *ho = *h;
+
+    return NGX_OK;
+}
+
+
 #if (NGX_HTTP_GZIP || NGX_COMPAT)
 
 static ngx_int_t