[4.0] 89870e0 Be more RFC2616 compliant, by keeping Date header as given from the backend. Set it on the object if missing.

Martin Blix Grydeland martin at varnish-software.com
Tue Jun 24 11:31:37 CEST 2014


commit 89870e0bbd785964c322e1e453f492d747731c88
Author: Martin Blix Grydeland <martin at varnish-software.com>
Date:   Mon Apr 14 14:40:11 2014 +0200

    Be more RFC2616 compliant, by keeping Date header as given from the
    backend. Set it on the object if missing.
    
    Previously we would add an explicit Date header on each response with
    the time and date of the response, overwriting any Date header on the
    object that came from the backend.
    
    Now we will add a Date header if the backend didn't supply one just
    before vcl_backend_response() is run, and not add any Date headers for
    the replies.
    
    This change means Date headers sent by Varnish is approximately the
    time and date this object was generated by the backend, and not the
    current date and time.

diff --git a/bin/varnishd/cache/cache_fetch.c b/bin/varnishd/cache/cache_fetch.c
index 19b8fb8..3091d29 100644
--- a/bin/varnishd/cache/cache_fetch.c
+++ b/bin/varnishd/cache/cache_fetch.c
@@ -249,6 +249,8 @@ static enum fetch_step
 vbf_stp_startfetch(struct worker *wrk, struct busyobj *bo)
 {
 	int i, do_ims;
+	double now;
+	char time_str[VTIM_FORMAT_SIZE];
 
 	CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
 	CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
@@ -292,7 +294,8 @@ vbf_stp_startfetch(struct worker *wrk, struct busyobj *bo)
 		VSC_C_main->backend_retry++;
 		i = V1F_fetch_hdr(wrk, bo, bo->req);
 	}
-	VSLb_ts_busyobj(bo, "Beresp", W_TIM_real(wrk));
+	now = W_TIM_real(wrk);
+	VSLb_ts_busyobj(bo, "Beresp", now);
 
 	if (i) {
 		AZ(bo->vbc);
@@ -302,6 +305,22 @@ vbf_stp_startfetch(struct worker *wrk, struct busyobj *bo)
 	AN(bo->vbc);
 	http_VSL_log(bo->beresp);
 
+	if (!http_GetHdr(bo->beresp, H_Date, NULL)) {
+		/*
+		 * RFC 2616 14.18 Date: The Date general-header field
+		 * represents the date and time at which the message was
+		 * originated, having the same semantics as orig-date in
+		 * RFC 822. ... A received message that does not have a
+		 * Date header field MUST be assigned one by the recipient
+		 * if the message will be cached by that recipient or
+		 * gatewayed via a protocol which requires a Date.
+		 *
+		 * If we didn't get a Date header, we assign one here.
+		 */
+		VTIM_format(now, time_str);
+		http_PrintfHeader(bo->beresp, "Date: %s", time_str);
+	}
+
 	/*
 	 * These two headers can be spread over multiple actual headers
 	 * and we rely on their content outside of VCL, so collect them
diff --git a/bin/varnishd/cache/cache_req_fsm.c b/bin/varnishd/cache/cache_req_fsm.c
index 1e9ecd5..c782d44 100644
--- a/bin/varnishd/cache/cache_req_fsm.c
+++ b/bin/varnishd/cache/cache_req_fsm.c
@@ -87,7 +87,6 @@ DOT deliver:deliver:s -> DONE [style=bold,color=blue]
 static enum req_fsm_nxt
 cnt_deliver(struct worker *wrk, struct req *req)
 {
-	char time_str[30];
 	double now;
 
 	CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
@@ -110,10 +109,6 @@ cnt_deliver(struct worker *wrk, struct req *req)
 	http_ClrHeader(req->resp);
 	http_FilterResp(req->obj->http, req->resp, 0);
 
-	http_Unset(req->resp, H_Date);
-	VTIM_format(now, time_str);
-	http_PrintfHeader(req->resp, "Date: %s", time_str);
-
 	if (req->wrk->stats.cache_hit)
 		http_PrintfHeader(req->resp,
 		    "X-Varnish: %u %u", req->vsl->wid & VSL_IDENTMASK,
diff --git a/bin/varnishtest/tests/c00044.vtc b/bin/varnishtest/tests/c00044.vtc
index 2d1d3cc..091e8a8 100644
--- a/bin/varnishtest/tests/c00044.vtc
+++ b/bin/varnishtest/tests/c00044.vtc
@@ -25,6 +25,8 @@ varnish v1 \
 	sub vcl_backend_response {
 		set beresp.do_stream = false;
 		set beresp.storage_hint = "invalid";
+		# Unset Date header to not change the object sizes
+		unset beresp.http.Date;
 	}
 } -start
 
diff --git a/bin/varnishtest/tests/c00045.vtc b/bin/varnishtest/tests/c00045.vtc
index 126f1a9..33b7b9e 100644
--- a/bin/varnishtest/tests/c00045.vtc
+++ b/bin/varnishtest/tests/c00045.vtc
@@ -17,6 +17,8 @@ varnish v1 \
 	sub vcl_backend_response {
 		set beresp.do_stream = false;
 		set beresp.storage_hint = "s0";
+		# Unset Date header to not change the object sizes
+		unset beresp.http.Date;
 	}
 } -start
 
diff --git a/bin/varnishtest/tests/r01140.vtc b/bin/varnishtest/tests/r01140.vtc
index e38e304..11e2a72 100644
--- a/bin/varnishtest/tests/r01140.vtc
+++ b/bin/varnishtest/tests/r01140.vtc
@@ -22,6 +22,8 @@ varnish v1 -arg "-p nuke_limit=0 -p shortlived=0" \
 	-arg "-smalloc,1m" -vcl+backend {
 	sub vcl_backend_response {
 		set beresp.do_stream = false;
+		# Unset Date header to not change the object sizes
+		unset beresp.http.Date;
 	}
 } -start
 
diff --git a/bin/varnishtest/tests/r01284.vtc b/bin/varnishtest/tests/r01284.vtc
index f89b579..dc6336f 100644
--- a/bin/varnishtest/tests/r01284.vtc
+++ b/bin/varnishtest/tests/r01284.vtc
@@ -16,6 +16,8 @@ varnish v1 \
 	sub vcl_backend_response {
 		set beresp.do_stream = false;
 		set beresp.storage_hint = "Transient";
+		# Unset Date header to not change the object sizes
+		unset beresp.http.Date;
 	}
 } -start
 



More information about the varnish-commit mailing list