[master] 28590eef0 vcc: Allow arbitrary subs in the built-in VCL

Nils Goroll nils.goroll at uplex.de
Wed Mar 3 10:15:04 UTC 2021


commit 28590eef0439b6df6e16846f3fe5210b7b38363f
Author: Dridi Boukelmoune <dridi.boukelmoune at gmail.com>
Date:   Mon Dec 14 06:39:57 2020 +0100

    vcc: Allow arbitrary subs in the built-in VCL
    
    Now that we know in advance the available suboutines in the built-in VCL
    we can use that as the condition to create an append-able subroutine
    when the vcl_ prefix is encountered.
    
    This work is currently incomplete, at least because non-state built-in
    subroutines are currently not listed when an unknwon subroutine is
    encountered.
    
    Also, instead of proper test coverage this currently uses b00000.vtc as
    a strawman to show how to skip a specific processing.

diff --git a/bin/varnishd/builtin.vcl b/bin/varnishd/builtin.vcl
index 3fee28f52..f975fb4d3 100644
--- a/bin/varnishd/builtin.vcl
+++ b/bin/varnishd/builtin.vcl
@@ -65,13 +65,21 @@ sub vcl_recv {
 		# We only deal with GET and HEAD by default
 		return (pass);
 	}
-	if (req.http.Authorization || req.http.Cookie) {
+	if (req.http.Authorization) {
 		# Not cacheable by default
 		return (pass);
 	}
+	call vcl_req_cookie;
 	return (hash);
 }
 
+sub vcl_req_cookie {
+	if (req.http.Cookie) {
+		# Risky to cache by default
+		return (pass);
+	}
+}
+
 sub vcl_pipe {
 	# By default "Connection: close" is set on all piped requests, to stop
 	# connection reuse from sending future requests directly to the
diff --git a/bin/varnishtest/tests/c00000.vtc b/bin/varnishtest/tests/c00000.vtc
new file mode 100644
index 000000000..9da814c44
--- /dev/null
+++ b/bin/varnishtest/tests/c00000.vtc
@@ -0,0 +1,32 @@
+varnishtest "Built-in split subroutine"
+
+server s1 {
+	rxreq
+	txresp -hdr "age: 12" \
+	    -hdr "cache-control: public, max-age=10, stale-while-revalidate=20"
+} -start
+
+varnish v1 -vcl+backend {
+	sub vcl_req_cookie {
+		return; # trust beresp headers
+	}
+
+	sub vcl_beresp_stale {
+		if (beresp.ttl + beresp.grace > 0s) {
+			return; # cache stale responses
+		}
+	}
+} -start
+
+client c1 {
+	txreq
+	rxresp
+	expect resp.status == 200
+
+	txreq -hdr "cookie: unrelated=analytics"
+	rxresp
+	expect resp.status == 200
+} -run
+
+varnish v1 -expect cache_hit == 1
+varnish v1 -expect cache_hit == cache_hit_grace
diff --git a/lib/libvcc/vcc_parse.c b/lib/libvcc/vcc_parse.c
index dc1d55c5b..41fe8dd03 100644
--- a/lib/libvcc/vcc_parse.c
+++ b/lib/libvcc/vcc_parse.c
@@ -219,7 +219,7 @@ vcc_Compound(struct vcc *tl)
 static void
 vcc_ParseFunction(struct vcc *tl)
 {
-	struct symbol *sym;
+	struct symbol *sym, *bsym;
 	struct token *t;
 	struct proc *p;
 
@@ -231,9 +231,20 @@ vcc_ParseFunction(struct vcc *tl)
 	sym = VCC_SymbolGet(tl, SYM_MAIN, SYM_SUB, SYMTAB_CREATE, XREF_DEF);
 	ERRCHK(tl);
 	AN(sym);
+
+	if (vcc_builtin != NULL) {
+		vcc_builtin->t = t;
+		bsym = VCC_SymbolGet(vcc_builtin, SYM_MAIN, SYM_SUB,
+		    SYMTAB_NOERR, XREF_NONE);
+		AZ(vcc_builtin->err);
+	}
+	else
+		bsym = NULL;
+
 	p = sym->proc;
 	if (p == NULL) {
-		if ((t->b[0] == 'v'|| t->b[0] == 'V') &&
+		if (vcc_builtin != NULL && bsym == NULL &&
+		    (t->b[0] == 'v'|| t->b[0] == 'V') &&
 		    (t->b[1] == 'c'|| t->b[1] == 'C') &&
 		    (t->b[2] == 'l'|| t->b[2] == 'L') &&
 		    (t->b[3] == '_')) {
@@ -241,10 +252,10 @@ vcc_ParseFunction(struct vcc *tl)
 			    " are reserved for subroutines.\n");
 			vcc_ErrWhere(tl, t);
 			VSB_printf(tl->sb, "Valid vcl_* subroutines are:\n");
-			VTAILQ_FOREACH(p, &tl->procs, list) {
-				if (p->method != NULL)
-					VSB_printf(tl->sb, "\t%s\n",
-					    p->method->name);
+			VTAILQ_FOREACH(p, &vcc_builtin->procs, list) {
+				t = p->name;
+				VSB_printf(tl->sb, "\t%.*s\n",
+				    (int)pdiff(t->b, t->e), t->b);
 			}
 			return;
 		}
@@ -252,7 +263,7 @@ vcc_ParseFunction(struct vcc *tl)
 		p = vcc_NewProc(tl, sym);
 		p->name = t;
 		VSB_printf(p->cname, "%s", sym->lname);
-	} else if (p->method == NULL) {
+	} else if (p->method == NULL && bsym == NULL) {
 		VSB_printf(tl->sb, "Subroutine '%s' redefined\n", sym->name);
 		vcc_ErrWhere(tl, t);
 		VSB_printf(tl->sb, "Previously defined here:\n");
@@ -260,7 +271,6 @@ vcc_ParseFunction(struct vcc *tl)
 		return;
 	} else {
 		/* Add to VCL sub */
-		AN(p->method);
 		if (p->name == NULL)
 			p->name = t;
 	}


More information about the varnish-commit mailing list