[master] 4e1bc12 Regular expression operators

Martin Blix Grydeland martin at varnish-cache.org
Tue Oct 1 14:48:18 CEST 2013


commit 4e1bc12c64f0d8fca08eb4ff510f6435f3964e01
Author: Martin Blix Grydeland <martin at varnish-software.com>
Date:   Fri Sep 20 14:56:12 2013 +0200

    Regular expression operators

diff --git a/bin/varnishtest/tests/l00001.vtc b/bin/varnishtest/tests/l00001.vtc
index 36609ad..839accf 100644
--- a/bin/varnishtest/tests/l00001.vtc
+++ b/bin/varnishtest/tests/l00001.vtc
@@ -113,3 +113,17 @@ logexpect l1 -d 1 -g vxid -q "RespStatus >= 200." {
 	expect * =	ReqEnd
 	expect * =	End
 } -run
+
+# Test '~' operator
+logexpect l1 -d 1 -g vxid -q "RespStatus ~ '^200$'" {
+	expect * *	Begin	req
+	expect * =	ReqEnd
+	expect * =	End
+} -run
+
+# Test '!~' operator
+logexpect l1 -d 1 -g vxid -q "RespStatus !~ '^404$'" {
+	expect * *	Begin	req
+	expect * =	ReqEnd
+	expect * =	End
+} -run
diff --git a/lib/libvarnishapi/vsl_query.c b/lib/libvarnishapi/vsl_query.c
index 43eb7ba..dbf539b 100644
--- a/lib/libvarnishapi/vsl_query.c
+++ b/lib/libvarnishapi/vsl_query.c
@@ -74,7 +74,9 @@ vslq_test_rec(const struct vex *vex, const struct VSLC_ptr *rec)
 	long long lhs_int;
 	double lhs_float;
 	const char *lhs_string;
+	size_t lhs_stringlen;
 	char *p;
+	int i;
 
 	AN(vex);
 	AN(rec);
@@ -82,6 +84,7 @@ vslq_test_rec(const struct vex *vex, const struct VSLC_ptr *rec)
 	AN(rhs);
 
 	lhs_string = VSL_CDATA(rec->ptr);
+	lhs_stringlen = VSL_LEN(rec->ptr) - 1;
 
 	/* Prepare */
 	switch (vex->tok) {
@@ -127,16 +130,32 @@ vslq_test_rec(const struct vex *vex, const struct VSLC_ptr *rec)
 		VSLQ_TEST_NUMOP(rhs->type, lhs, >=, rhs->val);
 	case T_SEQ:		/* eq */
 		assert(rhs->type == VEX_STRING);
-		if (!strcmp(lhs_string, rhs->val_string))
+		if (lhs_stringlen == rhs->val_stringlen &&
+		    !strncmp(lhs_string, rhs->val_string, lhs_stringlen))
 			return (1);
 		return (0);
 	case T_SNEQ:		/* ne */
 		assert(rhs->type == VEX_STRING);
-		if (strcmp(lhs_string, rhs->val_string))
+		if (lhs_stringlen != rhs->val_stringlen ||
+		    strncmp(lhs_string, rhs->val_string, lhs_stringlen))
+			return (1);
+		return (0);
+	case '~':		/* ~ */
+		assert(rhs->type == VEX_REGEX && rhs->val_regex != NULL);
+		i = VRE_exec(rhs->val_regex, lhs_string, lhs_stringlen, 0, 0,
+		    NULL, 0, NULL);
+		if (i != VRE_ERROR_NOMATCH)
+			return (1);
+		return (0);
+	case T_NOMATCH:		/* !~ */
+		assert(rhs->type == VEX_REGEX && rhs->val_regex != NULL);
+		i = VRE_exec(rhs->val_regex, lhs_string, lhs_stringlen, 0, 0,
+		    NULL, 0, NULL);
+		if (i == VRE_ERROR_NOMATCH)
 			return (1);
 		return (0);
 	default:
-		INCOMPL();
+		WRONG("Bad expression token");
 	}
 
 	return (0);



More information about the varnish-commit mailing list