[master] e8f4171 Implicit backend cast to boolean

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Wed Sep 7 10:49:11 CEST 2016


commit e8f41715b95d45c0b9088b64dd2d01d6e9b8b733
Author: Dridi Boukelmoune <dridi.boukelmoune at gmail.com>
Date:   Mon Aug 29 16:33:18 2016 +0200

    Implicit backend cast to boolean
    
    This introduces a way to check whether a director returned no backend
    and act upon it.
    
    Closes #2060

diff --git a/bin/varnishtest/tests/d00014.vtc b/bin/varnishtest/tests/d00014.vtc
new file mode 100644
index 0000000..3053db0
--- /dev/null
+++ b/bin/varnishtest/tests/d00014.vtc
@@ -0,0 +1,20 @@
+varnishtest "Backend as a boolean expression"
+
+server s1 -start
+
+varnish v1 -vcl+backend {
+	import debug;
+
+	sub vcl_recv {
+		set req.backend_hint = debug.no_backend();
+		if (!req.backend_hint) {
+			return (synth(404));
+		}
+	}
+} -start
+
+client c1 {
+	txreq
+	rxresp
+	expect resp.status == 404
+} -run
diff --git a/doc/sphinx/reference/vcl.rst b/doc/sphinx/reference/vcl.rst
index b68fdc6..789b20d 100644
--- a/doc/sphinx/reference/vcl.rst
+++ b/doc/sphinx/reference/vcl.rst
@@ -87,7 +87,11 @@ characters except for the NUL (0x00) character.
 Booleans
 ~~~~~~~~
 
-Booleans can be either *true* or *false*.
+Booleans can be either *true* or *false*. Some values may be inferred as
+truthy or falthy. When cast to booleans, strings will evaluate to *false*
+if they are empty or NULL. This allows broad checks on headers existence
+for instance. Backend expressions can also be tested as booleans and can
+help find when a director like the hash director returns a NULL backend.
 
 Time
 ----
diff --git a/lib/libvcc/vcc_expr.c b/lib/libvcc/vcc_expr.c
index 4838450..d91c8bd 100644
--- a/lib/libvcc/vcc_expr.c
+++ b/lib/libvcc/vcc_expr.c
@@ -1191,7 +1191,7 @@ vcc_expr_cmp(struct vcc *tl, struct expr **e, vcc_type_t fmt)
 	default:
 		break;
 	}
-	if (fmt == BOOL && (*e)->fmt == STRING) {
+	if (fmt == BOOL && ((*e)->fmt == STRING || (*e)->fmt == BACKEND)) {
 		*e = vcc_expr_edit(BOOL, "(\v1 != 0)", *e, NULL);
 		return;
 	}



More information about the varnish-commit mailing list