Hi,

I had some time on the plane today so here's a quick stab at getting
Surrogate-Control support as discussed during the vdd. Disabled by default.
Only max-age is supported with this patch.

f.-
diff --git a/bin/varnishd/cache/cache_rfc2616.c b/bin/varnishd/cache/cache_rfc2616.c
index 99de23f..c4c2cf8 100644
--- a/bin/varnishd/cache/cache_rfc2616.c
+++ b/bin/varnishd/cache/cache_rfc2616.c
@@ -67,14 +67,15 @@ RFC2616_Ttl(struct busyobj *bo, double now)
 {
 	unsigned max_age, age;
 	double h_date, h_expires;
-	char *p;
-	const struct http *hp;
+	char *p, *r;
+	struct http *hp;
 	struct exp *expp;
 
 	CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
 	expp = &bo->fetch_objcore->exp;
 
 	hp = bo->beresp;
+	r = NULL;
 
 	assert(now != 0.0 && !isnan(now));
 	expp->t_origin = now;
@@ -121,6 +122,30 @@ RFC2616_Ttl(struct busyobj *bo, double now)
 	case 307: /* Temporary Redirect */
 	case 410: /* Gone */
 	case 404: /* Not Found */
+
+		/*
+		 * If Surrogate support is enabled check the Surrogate-Control
+		 * header before Cache-Control.
+		 */
+
+		if (cache_param->surrogate_support &&
+		    http_GetHdrField(hp, H_Surrogate_Control, "max-age", &p) &&
+		    p != NULL) {
+
+			if (*p == '-')
+				max_age = 0;
+			else
+				max_age = strtoul(p, &r, 0);
+
+			if (*r == '+')
+				expp->grace = strtoul(r, NULL, 0);
+
+			expp->ttl = max_age;
+
+			http_Unset(hp, H_Surrogate_Control);
+			break;
+		}
+
 		/*
 		 * First find any relative specification from the backend
 		 * These take precedence according to RFC2616, 13.2.4
diff --git a/bin/varnishd/common/params.h b/bin/varnishd/common/params.h
index 2ce95c6..d8cd3a1 100644
--- a/bin/varnishd/common/params.h
+++ b/bin/varnishd/common/params.h
@@ -194,6 +194,8 @@ struct params {
 	unsigned		gzip_level;
 	unsigned		gzip_memlevel;
 
+	unsigned		surrogate_support;
+
 	unsigned		obj_readonly;
 
 	double			critbit_cooloff;
diff --git a/bin/varnishd/mgt/mgt_param_tbl.c b/bin/varnishd/mgt/mgt_param_tbl.c
index 52d25dc..11f1e16 100644
--- a/bin/varnishd/mgt/mgt_param_tbl.c
+++ b/bin/varnishd/mgt/mgt_param_tbl.c
@@ -568,6 +568,12 @@ struct parspec mgt_parspec[] = {
 		0,
 		"on", "bool" },
 
+	{ "surrogate_support", tweak_bool, &mgt_param.surrogate_support,
+		NULL, NULL,
+		"Enable Surrogate-Control support.",
+		0,
+		"off", "bool" },
+
 	{ "pcre_match_limit", tweak_uint,
 		&mgt_param.vre_limits.match,
 		"1", NULL,
diff --git a/bin/varnishtest/tests/b00043.vtc b/bin/varnishtest/tests/b00043.vtc
new file mode 100644
index 0000000..84ce977
--- /dev/null
+++ b/bin/varnishtest/tests/b00043.vtc
@@ -0,0 +1,30 @@
+varnishtest "Test Surrogate-Control support"
+
+server s1 {
+	rxreq
+	txresp -hdr "Surrogate-Control: max-age=30" -hdr "Cache-Control: max-age=90"
+	rxreq
+	txresp -hdr "Surrogate-Control: max-age=30+60" -hdr "Cache-Control: max-age=90"
+} -start
+
+varnish v1 -vcl+backend {
+	sub vcl_backend_response {
+		set beresp.http.x-ttl = beresp.ttl;
+		set beresp.http.x-grace = beresp.grace;
+	}
+} -start
+
+varnish v1 -cliok "param.set surrogate_support true"
+
+client c1 {
+	txreq -url /foo
+	rxresp
+	expect resp.http.x-ttl == 30.000
+	expect resp.http.x-grace == 10.000
+	expect resp.http.Surrogate-Control == <undef>
+	txreq -url /bar
+	rxresp
+	expect resp.http.x-ttl == 30.000
+	expect resp.http.x-grace == 60.000
+	expect resp.http.Surrogate-Control == <undef>
+} -run
diff --git a/include/tbl/http_headers.h b/include/tbl/http_headers.h
index 26443ca..052fe29 100644
--- a/include/tbl/http_headers.h
+++ b/include/tbl/http_headers.h
@@ -86,6 +86,7 @@ HTTPH("Referer",		H_Referer,		0					  )	/* RFC2616 14.36 */
 HTTPH("Retry-After",		H_Retry_After,		0					  )	/* RFC2616 14.37 */
 HTTPH("Server",			H_Server,		0					  )	/* RFC2616 14.38 */
 HTTPH("Set-Cookie",		H_Set_Cookie,		0					  )	/* RFC6265 4.1 */
+HTTPH("Surrogate-Control",	H_Surrogate_Control,	0					  )	/* Not RFC */
 HTTPH("TE",			H_TE,			HTTPH_R_PASS | HTTPH_R_FETCH | HTTPH_A_INS)	/* RFC2616 14.39 */
 HTTPH("Trailer",		H_Trailer,		HTTPH_R_PASS | HTTPH_R_FETCH | HTTPH_A_INS)	/* RFC2616 14.40 */
 HTTPH("Transfer-Encoding",	H_Transfer_Encoding,	HTTPH_R_PASS | HTTPH_R_FETCH | HTTPH_A_INS)	/* RFC2616 14.41 */
_______________________________________________
varnish-dev mailing list
[email protected]
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-dev

Reply via email to