Commit 99d13d21 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Add STRING.upper and STRING.lower

Relevant for #3023
parent 1e4b687b
......@@ -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
......
......@@ -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
......@@ -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);
......
......@@ -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;
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment