[master] 2b1320487 vmod: Teach VMODs to use REGEX expressions
Dridi Boukelmoune
dridi.boukelmoune at gmail.com
Fri Jan 15 16:17:07 UTC 2021
commit 2b13204874467d88e4638b6fcf2a452a34ad3a3f
Author: Dridi Boukelmoune <dridi.boukelmoune at gmail.com>
Date: Fri Jan 8 12:49:30 2021 +0100
vmod: Teach VMODs to use REGEX expressions
When they take a regular expression as an argument, they can benefit
from compile time checks and avoid PRIV_CALL or on-the-fly regex
compilation.
diff --git a/bin/varnishtest/tests/c00103.vtc b/bin/varnishtest/tests/c00103.vtc
new file mode 100644
index 000000000..11da0f0b9
--- /dev/null
+++ b/bin/varnishtest/tests/c00103.vtc
@@ -0,0 +1,32 @@
+varnishtest "REGEX expressions in VCL"
+
+varnish v1 -vcl {
+ import debug;
+ backend be none;
+ sub vcl_recv {
+ # NB: the REGEX expression below is needlessly complicated
+ # on purpose to ensure we don't treat REGEX expressions
+ # differently.
+ if (req.url ~ (debug.just_return_regex(
+ debug.just_return_regex("hello")))) {
+ return (synth(200));
+ }
+ return (synth(500));
+ }
+ sub vcl_synth {
+ set resp.reason = regsub(resp.reason,
+ debug.just_return_regex("OK"), "world");
+ }
+} -start
+
+client c1 {
+ txreq
+ rxresp
+ expect resp.status == 500
+ expect resp.reason == "Internal Server Error"
+
+ txreq -url "/hello"
+ rxresp
+ expect resp.status == 200
+ expect resp.reason == world
+} -run
diff --git a/doc/sphinx/reference/vmod.rst b/doc/sphinx/reference/vmod.rst
index 5e65edf16..08a791fd8 100644
--- a/doc/sphinx/reference/vmod.rst
+++ b/doc/sphinx/reference/vmod.rst
@@ -381,6 +381,11 @@ REAL
A floating point value.
+REGEX
+ C-type: ``const struct vre *``
+
+ This is an opaque type for regular expressions with a VCL scope.
+
STRING
C-type: ``const char *``
diff --git a/lib/libvcc/vmodtool.py b/lib/libvcc/vmodtool.py
index 25d3f977e..8bc43a807 100755
--- a/lib/libvcc/vmodtool.py
+++ b/lib/libvcc/vmodtool.py
@@ -110,6 +110,7 @@ CTYPES = {
'IP': "VCL_IP",
'PROBE': "VCL_PROBE",
'REAL': "VCL_REAL",
+ 'REGEX': "VCL_REGEX",
'STEVEDORE': "VCL_STEVEDORE",
'STRANDS': "VCL_STRANDS",
'STRING': "VCL_STRING",
diff --git a/vmod/vmod_debug.c b/vmod/vmod_debug.c
index 2be114a4f..997dfe33a 100644
--- a/vmod/vmod_debug.c
+++ b/vmod/vmod_debug.c
@@ -1278,3 +1278,12 @@ xyzzy_validhdr(VRT_CTX, VCL_STRANDS s)
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
return (VRT_ValidHdr(ctx, s));
}
+
+VCL_REGEX v_matchproto_(td_xyzzy_regex)
+xyzzy_just_return_regex(VRT_CTX, VCL_REGEX r)
+{
+
+ CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+ AN(r);
+ return (r);
+}
diff --git a/vmod/vmod_debug.vcc b/vmod/vmod_debug.vcc
index d085d797f..3f2e13bcc 100644
--- a/vmod/vmod_debug.vcc
+++ b/vmod/vmod_debug.vcc
@@ -319,3 +319,7 @@ A function mixing a named PRIV_TASK with optional parameters.
$Function BOOL validhdr(STRANDS)
Test if the argument is a valid header according to RFC7230 section 3.2.
+
+$Function REGEX just_return_regex(REGEX)
+
+Take a REGEX argument and return it.
More information about the varnish-commit
mailing list