[master] 9219b0dcd ban: Migrate to VRE

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Mon Jul 5 15:49:06 UTC 2021


commit 9219b0dcd9d3787fa85aa1482e2a685939530034
Author: Dridi Boukelmoune <dridi.boukelmoune at gmail.com>
Date:   Fri Jun 18 14:54:05 2021 +0200

    ban: Migrate to VRE
    
    Bans using regular expressions will consume slightly more space, but
    more importantly that breaks persistence binary compatibility. That's
    not a concern because we are both planning for a major release where
    that kind of breakage is acceptable, and in the context of a pcre2
    migration we would also break ban persistence.
    
    And now, VRE is the sole pcre consumer.

diff --git a/bin/varnishd/Makefile.am b/bin/varnishd/Makefile.am
index ce7145efd..90e8e2b05 100644
--- a/bin/varnishd/Makefile.am
+++ b/bin/varnishd/Makefile.am
@@ -159,7 +159,6 @@ nobase_pkginclude_HEADERS = \
 vcldir=$(datarootdir)/$(PACKAGE)/vcl
 
 varnishd_CFLAGS = \
-	@PCRE_CFLAGS@ \
 	@SAN_CFLAGS@ \
 	-DNOT_IN_A_VMOD \
 	-DVARNISH_STATE_DIR='"${VARNISH_STATE_DIR}"' \
@@ -174,7 +173,6 @@ varnishd_LDADD = \
 	$(top_builddir)/lib/libvgz/libvgz.a \
 	@SAN_LDFLAGS@ \
 	@JEMALLOC_LDADD@ \
-	@PCRE_LIBS@ \
 	${DL_LIBS} ${PTHREAD_LIBS} ${NET_LIBS} ${RT_LIBS} ${LIBM}
 
 if WITH_UNWIND
diff --git a/bin/varnishd/cache/cache_ban.c b/bin/varnishd/cache/cache_ban.c
index 03a912d9e..bd02f3001 100644
--- a/bin/varnishd/cache/cache_ban.c
+++ b/bin/varnishd/cache/cache_ban.c
@@ -32,7 +32,7 @@
 
 #include "config.h"
 
-#include <pcre.h>
+#include <stdlib.h>
 #include <stdio.h>
 
 #include "cache_varnishd.h"
@@ -495,6 +495,7 @@ ban_evaluate(struct worker *wrk, const uint8_t *bsarg, struct objcore *oc,
 	const char *p;
 	const char *arg1;
 	double darg1, darg2;
+	int rv;
 
 	/*
 	 * for ttl and age, fix the point in time such that banning refers to
@@ -567,15 +568,15 @@ ban_evaluate(struct worker *wrk, const uint8_t *bsarg, struct objcore *oc,
 			}
 			break;
 		case BANS_OPER_MATCH:
-			if (arg1 == NULL ||
-			    pcre_exec(bt.arg2_spec, NULL, arg1, strlen(arg1),
-			    0, 0, NULL, 0) < 0)
+			rv = VRE_match(bt.arg2_spec, arg1, 0, 0, NULL);
+			xxxassert(rv >= -1);
+			if (arg1 == NULL || rv < 0)
 				return (0);
 			break;
 		case BANS_OPER_NMATCH:
-			if (arg1 != NULL &&
-			    pcre_exec(bt.arg2_spec, NULL, arg1, strlen(arg1),
-			    0, 0, NULL, 0) >= 0)
+			rv = VRE_match(bt.arg2_spec, arg1, 0, 0, NULL);
+			xxxassert(rv >= -1);
+			if (arg1 == NULL || rv >= 0)
 				return (0);
 			break;
 		case BANS_OPER_GT:
diff --git a/bin/varnishd/cache/cache_ban.h b/bin/varnishd/cache/cache_ban.h
index 1302c17be..6512e20f6 100644
--- a/bin/varnishd/cache/cache_ban.h
+++ b/bin/varnishd/cache/cache_ban.h
@@ -55,11 +55,6 @@
  *	4 bytes - be32: length
  *	n bytes - content
  *
- * In a perfect world, we should vector through VRE to get to PCRE,
- * but since we rely on PCRE's ability to encode the regexp into a
- * byte string, that would be a little bit artificial, so this is
- * the exception that confirms the rule.
- *
  */
 
 /*--------------------------------------------------------------------
diff --git a/bin/varnishd/cache/cache_ban_build.c b/bin/varnishd/cache/cache_ban_build.c
index 1d6e3d06a..95327406c 100644
--- a/bin/varnishd/cache/cache_ban_build.c
+++ b/bin/varnishd/cache/cache_ban_build.c
@@ -32,7 +32,7 @@
 
 #include "config.h"
 
-#include <pcre.h>
+#include <stdlib.h>
 
 #include "cache_varnishd.h"
 #include "cache_ban.h"
@@ -40,6 +40,7 @@
 #include "vend.h"
 #include "vtim.h"
 #include "vnum.h"
+#include "vre.h"
 
 void BAN_Build_Init(void);
 void BAN_Build_Fini(void);
@@ -187,18 +188,26 @@ ban_parse_http(const struct ban_proto *bp, const char *a1)
 static const char *
 ban_parse_regexp(struct ban_proto *bp, const char *a3)
 {
-	const char *error;
-	int erroroffset, rc;
+	struct vsb vsb[1];
+	char errbuf[VRE_ERROR_LEN];
+	int errorcode, erroroffset;
 	size_t sz;
-	pcre *re;
-
-	re = pcre_compile(a3, 0, &error, &erroroffset, NULL);
-	if (re == NULL)
-		return (ban_error(bp, "Regex compile error: %s", error));
-	rc = pcre_fullinfo(re, NULL, PCRE_INFO_SIZE, &sz);
-	AZ(rc);
-	ban_add_lump(bp, re, sz);
-	pcre_free(re);
+	vre_t *re, *rex;
+
+	re = VRE_compile(a3, 0, &errorcode, &erroroffset, 0);
+	if (re == NULL) {
+		AN(VSB_init(vsb, errbuf, sizeof errbuf));
+		AZ(VRE_error(vsb, errorcode));
+		AZ(VSB_finish(vsb));
+		VSB_fini(vsb);
+		return (ban_error(bp, "Regex compile error: %s", errbuf));
+	}
+
+	rex = VRE_export(re, &sz);
+	AN(rex);
+	ban_add_lump(bp, rex, sz);
+	VRE_free(&rex);
+	VRE_free(&re);
 	return (0);
 }
 


More information about the varnish-commit mailing list