Commit 79de4cf9 authored by Nils Goroll's avatar Nils Goroll

support use of decode/encode/transcode for varnish >= 5.0

We _could_ selectively keep the tests for these functions failing in <5.0,
but looking forward this does seem like wasted effort.
parent 7cb06c72
Pipeline #78 skipped
......@@ -4,53 +4,13 @@ varnishtest "test permitted use of functions in vcl_subs"
varnish v1 -vcl { backend b { .host="${bad_ip}"; } } -start
varnish v1 -errvcl {vmod blobcode error: decode() is illegal in vcl_init() and vcl_fini().vmod blobcode error: encode() is illegal in vcl_init() and vcl_fini().} {
import blobcode from "${vmod_topbuild}/src/.libs/libvmod_blobcode.so";
backend b { .host="${bad_ip}"; }
sub vcl_init {
new err = blobcode.blob(IDENTITY, blobcode.encode(IDENTITY,
blobcode.decode(IDENTITY, "x")));
}
}
varnish v1 -errvcl {vmod blobcode error: transcode() is illegal in vcl_init() and vcl_fini().} {
import blobcode from "${vmod_topbuild}/src/.libs/libvmod_blobcode.so";
backend b { .host="${bad_ip}"; }
sub vcl_init {
new err = blobcode.blob(IDENTITY,
blobcode.transcode(IDENTITY, IDENTITY, "x"));
}
}
varnish v1 -errvcl {vmod blobcode error: encode() is illegal in vcl_init() and vcl_fini().} {
import blobcode from "${vmod_topbuild}/src/.libs/libvmod_blobcode.so";
backend b { .host="${bad_ip}"; }
sub vcl_init {
new b1 = blobcode.blob(IDENTITY, "foo");
new b2 = blobcode.blob(IDENTITY, blobcode.encode(IDENTITY, b1.get()));
}
}
varnish v1 -errvcl {vmod blobcode error: decode() is illegal in vcl_init() and vcl_fini().vmod blobcode error: encode() is illegal in vcl_init() and vcl_fini().} {
import blobcode from "${vmod_topbuild}/src/.libs/libvmod_blobcode.so";
backend b { .host="${bad_ip}"; }
sub vcl_init {
new err = blobcode.blob(IDENTITY, blobcode.encode(IDENTITY,
blobcode.decode_n(1, IDENTITY, "x")));
}
}
varnish v1 -errvcl {vmod blobcode error: transcode() is illegal in vcl_init() and vcl_fini().} {
import blobcode from "${vmod_topbuild}/src/.libs/libvmod_blobcode.so";
backend b { .host="${bad_ip}"; }
sub vcl_init {
new err = blobcode.blob(IDENTITY,
blobcode.transcode_n(1, IDENTITY, IDENTITY,
"x"));
}
}
# for varnish < 5.0, we have no workspace in init/fini, so we cannot
# use the *code() functions there
# We could have separate tests for those versions, but looking forward,
# this appears to be wasted effort.
# The *code() functions are legal in every other VCL sub for all supported
# varnish versions
# The *code() functions are legal in every other VCL sub
varnish v1 -vcl {
import blobcode from "${vmod_topbuild}/src/.libs/libvmod_blobcode.so";
backend b { .host="${bad_ip}"; }
......
......@@ -136,9 +136,6 @@ static const struct vmod_blobcode_fptr {
#define INIT_FINI(ctx) (((ctx)->method & (VCL_MET_INIT | VCL_MET_FINI)) != 0)
#define ILLEGAL(ctx, m) \
ERR((ctx), m " is illegal in vcl_init() and vcl_fini().")
static const struct vmod_priv null_blob[1] =
{
{
......@@ -398,6 +395,17 @@ decode_id_inplace(struct vmod_priv *b, VCL_INT n, const char *restrict p,
return (b);
}
static int
has_ws(VRT_CTX, const char * const msg)
{
if (VRT_MAJOR_VERSION < 5 && INIT_FINI(ctx)) {
VERR(ctx, "%s", msg);
return 0;
}
return 1;
}
static VCL_BLOB
decode(VRT_CTX, VCL_INT n, VCL_ENUM decs,
const char *restrict const p, va_list ap) {
......@@ -410,10 +418,9 @@ decode(VRT_CTX, VCL_INT n, VCL_ENUM decs,
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
AENC(dec);
if (INIT_FINI(ctx)) {
ILLEGAL(ctx, "decode()");
if (! has_ws(ctx, __func__))
return NULL;
}
CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC);
snap = WS_Snapshot(ctx->ws);
......@@ -494,14 +501,14 @@ encode(VRT_CTX, enum encoding enc, VCL_BLOB b) {
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
AENC(enc);
if (INIT_FINI(ctx)) {
ILLEGAL(ctx, "encode()");
if (! has_ws(ctx, __func__))
return NULL;
}
CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC);
if (b == NULL)
return NULL;
CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC);
if (wb_create(ctx->ws, &wb) == NULL) {
ERRNOMEM(ctx, "cannot encode");
return NULL;
......@@ -539,10 +546,10 @@ transcode(VRT_CTX, VCL_INT n, VCL_ENUM decs, VCL_ENUM encs,
VCL_STRING r;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
if (INIT_FINI(ctx)) {
ILLEGAL(ctx, "transcode()");
if (! has_ws(ctx, __func__))
return NULL;
}
CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC);
AENC(dec);
......
......@@ -338,10 +338,11 @@ string is an illegal format for the decoding scheme.
If the ``blob`` object constructor fails, then the VCL program will
fail to load, and the VCC compiler will emit an error message.
The VMOD functions -- ``encode()``, ``decode()`` and ``transcode()``
-- may not be called in ``vcl_init()`` or ``vcl_fini()`` (since a
workspace must be available). Use in ``vcl_init()`` will also cause
the VCL program to fail with a VCC compiler error.
For varnish versions before 5.0, the VMOD functions -- ``encode()``,
``decode()`` and ``transcode()`` -- may not be called in
``vcl_init()`` or ``vcl_fini()`` (since a workspace must be
available). Use in ``vcl_init()`` will also cause the VCL program to
fail with a VCC compiler error.
If any of the VMOD's methods or functions fail at runtime, then an
error message will be written to the Varnish log using the tag
......
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