[master] 99d13d215 Add STRING.upper and STRING.lower
Poul-Henning Kamp
phk at FreeBSD.org
Mon Aug 5 08:38:11 UTC 2019
commit 99d13d2152f8b7e35b61e61850096fe8d7e3f725
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date: Mon Aug 5 08:30:30 2019 +0000
Add STRING.upper and STRING.lower
Relevant for #3023
diff --git a/bin/varnishd/cache/cache_vrt.c b/bin/varnishd/cache/cache_vrt.c
index 5e68b07d8..d943fe205 100644
--- a/bin/varnishd/cache/cache_vrt.c
+++ b/bin/varnishd/cache/cache_vrt.c
@@ -36,6 +36,7 @@
#include "cache_objhead.h"
#include "vav.h"
#include "vcl.h"
+#include "vct.h"
#include "vrt_obj.h"
#include "vsa.h"
#include "vtcp.h"
@@ -183,6 +184,7 @@ VPI_BundleStrands(int n, struct strands *s, char const **d, const char *f, ...)
{
va_list ap;
+ assert(n > 0);
s->n = n;
s->p = d;
*d++ = f;
@@ -480,6 +482,57 @@ VRT_CollectStrands(VRT_CTX, VCL_STRANDS s)
return (b);
}
+/*--------------------------------------------------------------------
+ * upper/lower-case STRANDS (onto workspace)
+ */
+
+#include <stdio.h>
+
+VCL_STRING
+VRT_UpperLowerStrands(VRT_CTX, VCL_STRANDS s, int up)
+{
+ unsigned u;
+ char *b, *e, *r;
+ const char *p, *q = NULL;
+ int i, copy = 0;
+
+ CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+ CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC);
+ AN(s);
+ u = WS_ReserveAll(ctx->ws);
+ r = b = WS_Front(ctx->ws);
+ e = b + u;
+ for (i = 0; i < s->n; i++) {
+ if (s->p[i] == NULL || s->p[i][0] == '\0')
+ continue;
+ if (q != NULL)
+ copy = 1;
+ p = q = s->p[i];
+ for(p = q = s->p[i]; *p != '\0'; p++) {
+ if ((up && vct_islower(*p)) ||
+ (!up && vct_isupper(*p))) {
+ *b++ = *p ^ 0x20;
+ copy = 1;
+ } else {
+ *b++ = *p;
+ }
+ if (b == e) {
+ WS_Release(ctx->ws, 0);
+ VRT_fail(ctx, "Workspace overflow");
+ return (NULL);
+ }
+ }
+ }
+ if (!copy) {
+ WS_Release(ctx->ws, 0);
+ return (q);
+ }
+ *b++ = '\0';
+ assert(b <= e);
+ WS_ReleaseP(ctx->ws, b);
+ return (r);
+}
+
/*--------------------------------------------------------------------*/
VCL_VOID
diff --git a/bin/varnishtest/tests/v00058.vtc b/bin/varnishtest/tests/v00058.vtc
index cef91e8b7..c7f39e628 100644
--- a/bin/varnishtest/tests/v00058.vtc
+++ b/bin/varnishtest/tests/v00058.vtc
@@ -174,3 +174,36 @@ client c1 {
varnish v1 -expect client_resp_500 == 1
varnish v1 -expect ws_client_overflow == 2
+
+# Test $STRINGS.{upper|lower}
+
+server s1 {
+ rxreq
+ txresp
+} -start
+
+varnish v1 -vcl+backend {
+ sub vcl_deliver {
+ set resp.http.l-proto = resp.proto.lower;
+ set resp.http.u-proto = resp.proto.upper;
+ set resp.http.l-req = req.url.lower;
+ set resp.http.u-req = req.url.upper;
+ set resp.http.l-mod = (req.url + "bar").lower;
+ set resp.http.u-mod = (req.url + "bar").upper;
+ set resp.http.uu-mod = (req.url + "bar").upper.upper;
+ set resp.http.ul-mod = (req.url + "bar").upper.lower;
+ }
+}
+
+client c1 {
+ txreq -url /foo
+ rxresp
+ expect resp.http.l-proto == "http/1.1"
+ expect resp.http.u-proto == "HTTP/1.1"
+ expect resp.http.l-req == "/foo"
+ expect resp.http.u-req == "/FOO"
+ expect resp.http.l-mod == "/foobar"
+ expect resp.http.u-mod == "/FOOBAR"
+ expect resp.http.uu-mod == "/FOOBAR"
+ expect resp.http.ul-mod == "/foobar"
+} -run
diff --git a/include/vrt.h b/include/vrt.h
index a288cb2fd..2a741c125 100644
--- a/include/vrt.h
+++ b/include/vrt.h
@@ -52,6 +52,7 @@
* binary/load-time compatible, increment MAJOR version
*
* unreleased (planned for 2019-09-15)
+ * VRT_UpperLowerStrands added.
* VRT_synth_page now takes STRANDS argument
* VRT_hashdata() now takes STRANDS argument
* VCL_BOOL VRT_Strands2Bool(VCL_STRANDS) added.
@@ -538,6 +539,7 @@ VCL_BOOL VRT_Strands2Bool(VCL_STRANDS);
char *VRT_Strands(char *, size_t, VCL_STRANDS);
VCL_STRING VRT_StrandsWS(struct ws *, const char *, VCL_STRANDS);
VCL_STRING VRT_CollectStrands(VRT_CTX, VCL_STRANDS);
+VCL_STRING VRT_UpperLowerStrands(VRT_CTX, VCL_STRANDS s, int up);
VCL_STRING VRT_BACKEND_string(VCL_BACKEND);
VCL_STRING VRT_BOOL_string(VCL_BOOL);
diff --git a/lib/libvcc/vcc_expr.c b/lib/libvcc/vcc_expr.c
index 6776a3f86..b1ed9b64e 100644
--- a/lib/libvcc/vcc_expr.c
+++ b/lib/libvcc/vcc_expr.c
@@ -803,7 +803,7 @@ vcc_expr5(struct vcc *tl, struct expr **e, vcc_type_t fmt)
/*--------------------------------------------------------------------
* SYNTAX:
* Expr4:
- * Expr5
+ * Expr5 [ '.' type_method() ]*
*/
static const struct vcc_methods {
@@ -818,6 +818,9 @@ static const struct vcc_methods {
{ STEVEDORE, vtype, #nm, "VRT_stevedore_" #nm "(\v1)"},
#include "tbl/vrt_stv_var.h"
+ { STRINGS, STRING, "upper", "VRT_UpperLowerStrands(ctx, \vT, 1)" },
+ { STRINGS, STRING, "lower", "VRT_UpperLowerStrands(ctx, \vT, 0)" },
+
{ NULL, NULL, NULL, NULL},
};
@@ -851,6 +854,11 @@ vcc_expr4(struct vcc *tl, struct expr **e, vcc_type_t fmt)
}
vcc_NextToken(tl);
*e = vcc_expr_edit(tl, vm->type_to, vm->impl, *e, NULL);
+ ERRCHK(tl);
+ if ((*e)->fmt == STRING) {
+ (*e)->fmt = STRINGS;
+ (*e)->nstr = 1;
+ }
}
}
More information about the varnish-commit
mailing list