[4.1] d828a04 Proper handling of duplicate headers on IMS headers merge

Martin Blix Grydeland martin at varnish-software.com
Tue Mar 29 14:28:04 CEST 2016


commit d828a042b3fc2c2b4f1fea83021f0d5508649e50
Author: Pål Hermunn Johansen <hermunn at varnish-software.com>
Date:   Tue Mar 15 14:53:16 2016 +0100

    Proper handling of duplicate headers on IMS headers merge
    
    This fixes a problem when a backend replies with 304 Not Modified
    (after a http conditional request from varnish) and does not supply a
    header that was duplicate in the cached object.
    
    Before this patch, varnish would only supply (by copying from the
    expired object) the first instance of a duplicate header.

diff --git a/bin/varnishd/cache/cache_http.c b/bin/varnishd/cache/cache_http.c
index 8e3fe0f..e06e49e 100644
--- a/bin/varnishd/cache/cache_http.c
+++ b/bin/varnishd/cache/cache_http.c
@@ -986,6 +986,7 @@ HTTP_Merge(struct worker *wrk, struct objcore *oc, struct http *to)
 	const char *ptr;
 	unsigned u;
 	const char *p;
+	unsigned nhd_before_merge;
 
 	CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
 	CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
@@ -1003,10 +1004,12 @@ HTTP_Merge(struct worker *wrk, struct objcore *oc, struct http *to)
 		http_SetH(to, u, ptr);
 		ptr = strchr(ptr, '\0') + 1;
 	}
+	nhd_before_merge = to->nhd;
 	while (*ptr != '\0') {
 		p = strchr(ptr, ':');
 		AN(p);
-		if (!http_findhdr(to, p - ptr, ptr))
+		u = http_findhdr(to, p - ptr, ptr);
+		if (u == 0 || u >= nhd_before_merge)
 			http_SetHeader(to, ptr);
 		ptr = strchr(ptr, '\0') + 1;
 	}
diff --git a/bin/varnishtest/tests/r01879.vtc b/bin/varnishtest/tests/r01879.vtc
new file mode 100644
index 0000000..d3e014d
--- /dev/null
+++ b/bin/varnishtest/tests/r01879.vtc
@@ -0,0 +1,48 @@
+varnishtest "r01879: Check duplicate headers handling on IMS header merge"
+
+server s1 {
+	rxreq
+	txresp -hdr {etag: "foo"} -hdr "foo: a" -hdr "foo: b" -body "bdy"
+	rxreq
+	expect req.http.if-none-match == {"foo"}
+	txresp -status 304 -hdr {etag: "foo"}  -hdr "foo: c" -hdr "foo: d"
+	rxreq
+	txresp -hdr {etag: "bar"} -hdr "foo: a" -hdr "foo: b" -body "bdy"
+	rxreq
+	expect req.http.if-none-match == {"bar"}
+	txresp -status 304 -hdr {etag: "bar"}
+} -start
+
+varnish v1 -vcl+backend {
+	import ${vmod_std};
+
+	sub vcl_backend_response {
+		set beresp.ttl = 0.00001s;
+		set beresp.grace = 0.1s;
+		set beresp.keep = 9999s;
+	}
+	sub vcl_deliver {
+		std.collect(resp.http.foo);
+	}
+} -start
+
+client c1 {
+	txreq
+	rxresp
+	expect resp.http.foo == "a, b"
+	delay .5
+	txreq
+	rxresp
+	expect resp.http.foo == "c, d"
+	delay .5
+} -run
+
+client c2 {
+	txreq
+	rxresp
+	expect resp.http.foo == "a, b"
+	delay .5
+	txreq
+	rxresp
+	expect resp.http.foo == "a, b"
+} -run



More information about the varnish-commit mailing list