[4.1] 7782833 Use a weak comparison function for If-None-Match

Lasse Karstensen lkarsten at varnish-software.com
Thu Jan 14 15:15:09 CET 2016


commit 778283389bae7809dd8fc40565ae64ba85fbfa4e
Author: Federico G. Schwindt <fgsch at lodoss.net>
Date:   Fri Dec 11 15:26:03 2015 +0000

    Use a weak comparison function for If-None-Match
    
    As per RFC 7232.
    
    Fixes #1816.

diff --git a/bin/varnishd/cache/cache_rfc2616.c b/bin/varnishd/cache/cache_rfc2616.c
index 7608755..d3fa8a2 100644
--- a/bin/varnishd/cache/cache_rfc2616.c
+++ b/bin/varnishd/cache/cache_rfc2616.c
@@ -232,6 +232,16 @@ RFC2616_Req_Gzip(const struct http *hp)
 
 /*--------------------------------------------------------------------*/
 
+static inline int
+rfc2616_weak_compare(const char *p, const char *e)
+{
+	if (p[0] == 'W' && p[1] == '/')
+		p += 2;
+	if (e[0] == 'W' && e[1] == '/')
+		e += 2;
+	return (strcmp(p, e) != 0);
+}
+
 int
 RFC2616_Do_Cond(const struct req *req)
 {
@@ -239,12 +249,20 @@ RFC2616_Do_Cond(const struct req *req)
 	double ims, lm;
 	int do_cond = 0;
 
-	/* RFC 2616 13.3.4 states we need to match both ETag
-	   and If-Modified-Since if present*/
+	/*
+	 * RFC 2616 13.3.4 states we need to match both ETag and
+	 * If-Modified-Since if present.
+	 */
+	if (http_GetHdr(req->http, H_If_None_Match, &p) &&
+	    http_GetHdr(req->resp, H_ETag, &e)) {
+		if (rfc2616_weak_compare(p, e))
+			return (0);
+		do_cond = 1;
+	}
 
-	if (http_GetHdr(req->http, H_If_Modified_Since, &p) ) {
+	if (http_GetHdr(req->http, H_If_Modified_Since, &p)) {
 		ims = VTIM_parse(p);
-		if (ims > req->t_req)	/* [RFC2616 14.25] */
+		if (!ims || ims > req->t_req)	/* [RFC2616 14.25] */
 			return (0);
 		AZ(ObjGetDouble(req->wrk, req->objcore, OA_LASTMODIFIED, &lm));
 		if (lm > ims)
@@ -252,13 +270,6 @@ RFC2616_Do_Cond(const struct req *req)
 		do_cond = 1;
 	}
 
-	if (http_GetHdr(req->http, H_If_None_Match, &p) &&
-	    http_GetHdr(req->resp, H_ETag, &e)) {
-		if (strcmp(p,e) != 0)
-			return (0);
-		do_cond = 1;
-	}
-
 	return (do_cond);
 }
 
diff --git a/bin/varnishtest/tests/c00025.vtc b/bin/varnishtest/tests/c00025.vtc
index bf519f2..9b7fc95 100644
--- a/bin/varnishtest/tests/c00025.vtc
+++ b/bin/varnishtest/tests/c00025.vtc
@@ -4,6 +4,10 @@ server s1 {
 	rxreq
 	expect req.url == /
 	txresp -hdr {ETag: "123456789"} -bodylen 10
+
+	rxreq
+	expect req.url == /other
+	txresp -hdr {ETag: W/"123456789"} -bodylen 10
 } -start
 
 varnish v1 -vcl+backend { } -start
@@ -22,4 +26,36 @@ client c1 {
 	txreq -hdr {If-None-Match: "123456789"}
 	rxresp -no_obj
 	expect resp.status == 304
+
+	txreq -hdr {If-None-Match: W/"12345678"}
+	rxresp
+	expect resp.status == 200
+
+	txreq -hdr {If-None-Match: W/"123456789"}
+	rxresp -no_obj
+	expect resp.status == 304
+} -run
+
+client c2 {
+	txreq -url /other
+	rxresp
+	expect resp.status == 200
+	expect resp.bodylen == 10
+	expect resp.http.etag == {W/"123456789"}
+
+	txreq -url /other -hdr {If-None-Match: "12345678"}
+	rxresp
+	expect resp.status == 200
+
+	txreq -url /other -hdr {If-None-Match: "123456789"}
+	rxresp -no_obj
+	expect resp.status == 304
+
+	txreq -url /other -hdr {If-None-Match: W/"12345678"}
+	rxresp
+	expect resp.status == 200
+
+	txreq -url /other -hdr {If-None-Match: W/"123456789"}
+	rxresp -no_obj
+	expect resp.status == 304
 } -run



More information about the varnish-commit mailing list