Commit 9524c583 authored by Geoff Simmons's avatar Geoff Simmons

Add the .string() methods to both the label and sub objects.

parent 382c9bcf
......@@ -70,6 +70,15 @@ label.go
VOID label.go(INT)
.. _func_label.string:
label.string
------------
::
STRING label.string(INT)
.. _obj_sub:
sub
......@@ -97,6 +106,15 @@ sub.call
VOID sub.call(INT n)
.. _func_sub.string:
sub.string
----------
::
STRING sub.string(INT)
.. _func_version:
version
......@@ -148,7 +166,7 @@ COPYRIGHT
::
Copyright (c) 2017 UPLEX Nils Goroll Systemoptimierung
Copyright (c) 2017-2018 UPLEX Nils Goroll Systemoptimierung
All rights reserved
Author: Geoffrey Simmons <geoffrey.simmons@uplex.de>
......
......@@ -44,12 +44,21 @@ varnish v1 -vcl {
}
sub vcl_recv {
set req.http.Label0 = l.string(0);
set req.http.Label1 = l.string(1);
if (req.url == "/0") {
l.go(0);
}
if (req.url == "/1") {
l.go(1);
}
return(synth(200));
}
sub vcl_synth {
set resp.http.Label0 = req.http.Label0;
set resp.http.Label1 = req.http.Label1;
return(deliver);
}
}
......@@ -62,6 +71,11 @@ client c1 {
rxresp
expect resp.status == 200
expect resp.http.VCL == "2"
txreq
rxresp
expect resp.status == 200
expect resp.http.Label0 == "VCL1"
expect resp.http.Label1 == "VCL2"
} -run
varnish v1 -cliok "vcl.list"
......@@ -155,3 +169,62 @@ logexpect l1 -v v1 -d 1 -g vxid -q "VCL_Error" {
} -start
logexpect l1 -wait
server s1 -repeat 3 {
rxreq
txresp
} -start
varnish v1 -vcl+backend {
import ${vmod_dispatch};
sub vcl_init {
new l = dispatch.label();
l.add(0, "VCL1");
l.add(2, "VCL2");
}
sub vcl_deliver {
if (req.url == "/negative") {
set resp.http.Label = l.string(-1);
}
elsif (req.url == "/notset") {
set resp.http.Label = l.string(1);
}
elsif (req.url == "/outofrange") {
set resp.http.Label = l.string(3);
}
}
}
logexpect l1 -v v1 -d 0 -g vxid -q "VCL_Error" {
expect * * VCL_Error "^vmod dispatch error: l.string.-1.: n must be >= 0$"
expect * * VCL_Error "^vmod dispatch error: l.string.1.: label 1 was not added$"
expect * * VCL_Error "^vmod dispatch error: l.string.3.: highest label number is 2"
} -start
client c1 {
txreq -url "/negative"
rxresp
expect resp.status == 503
expect resp.reason == "VCL failed"
expect resp.http.Label == <undef>
} -run
client c1 {
txreq -url "/notset"
rxresp
expect resp.status == 503
expect resp.reason == "VCL failed"
expect resp.http.Label == <undef>
} -run
client c1 {
txreq -url "/outofrange"
rxresp
expect resp.status == 503
expect resp.reason == "VCL failed"
expect resp.http.Label == <undef>
} -run
logexpect l1 -wait
......@@ -51,6 +51,10 @@ varnish v1 -arg "-p vcc_err_unref=off" -vcl {
sub vcl_synth {
set resp.http.Sub = req.http.Sub;
set resp.http.Sub-0 = s.string(0);
set resp.http.Sub-1 = s.string(1);
set resp.http.Sub-2 = s.string(2);
set resp.http.Sub-3 = s.string(3);
}
} -start
......@@ -59,18 +63,34 @@ client c1 {
rxresp
expect resp.status == 200
expect resp.http.Sub == "sub1"
expect resp.http.Sub-0 == "sub1"
expect resp.http.Sub-1 == "sub2"
expect resp.http.Sub-2 == "sub_underscore"
expect resp.http.Sub-3 == "sub-hyphen"
txreq -url "/1"
rxresp
expect resp.status == 200
expect resp.http.Sub == "sub2"
expect resp.http.Sub-0 == "sub1"
expect resp.http.Sub-1 == "sub2"
expect resp.http.Sub-2 == "sub_underscore"
expect resp.http.Sub-3 == "sub-hyphen"
txreq -url "/2"
rxresp
expect resp.status == 200
expect resp.http.Sub == "sub_underscore"
expect resp.http.Sub-0 == "sub1"
expect resp.http.Sub-1 == "sub2"
expect resp.http.Sub-2 == "sub_underscore"
expect resp.http.Sub-3 == "sub-hyphen"
txreq -url "/3"
rxresp
expect resp.status == 200
expect resp.http.Sub == "sub-hyphen"
expect resp.http.Sub-0 == "sub1"
expect resp.http.Sub-1 == "sub2"
expect resp.http.Sub-2 == "sub_underscore"
expect resp.http.Sub-3 == "sub-hyphen"
} -run
varnish v1 -errvcl {vmod dispatch error: s.add(0): subroutine name is empty} {
......@@ -182,3 +202,74 @@ logexpect l1 -v v1 -d 1 -g vxid -q "VCL_Error" {
} -start
logexpect l1 -wait
server s1 -repeat 3 {
rxreq
txresp
} -start
varnish v1 -vcl+backend {
import ${vmod_dispatch};
sub vcl_init {
new s = dispatch.sub();
s.add(0, "sub1");
s.add(2, "sub2");
}
sub vcl_deliver {
set resp.http.Sub = req.http.Sub;
if (req.url == "/negative") {
set resp.http.SubN = s.string(-1);
}
elsif (req.url == "/notset") {
set resp.http.SubN = s.string(1);
}
elsif (req.url == "/outofrange") {
set resp.http.SubN = s.string(3);
}
}
sub sub1 {
set req.http.Sub = "sub1";
}
sub sub2 {
set req.http.Sub = "sub2";
}
}
logexpect l1 -v v1 -d 0 -g vxid -q "VCL_Error" {
expect * * VCL_Error "^vmod dispatch error: s.string.-1.: n must be >= 0$"
expect * * VCL_Error "^vmod dispatch error: s.string.1.: sub 1 was not added$"
expect * * VCL_Error "^vmod dispatch error: s.string.3.: highest sub number is 2"
} -start
client c1 {
txreq -url "/negative"
rxresp
expect resp.status == 503
expect resp.reason == "VCL failed"
expect resp.http.Sub == <undef>
expect resp.http.SubN == <undef>
} -run
client c1 {
txreq -url "/notset"
rxresp
expect resp.status == 503
expect resp.reason == "VCL failed"
expect resp.http.Sub == <undef>
expect resp.http.SubN == <undef>
} -run
client c1 {
txreq -url "/outofrange"
rxresp
expect resp.status == 503
expect resp.reason == "VCL failed"
expect resp.http.Sub == <undef>
expect resp.http.SubN == <undef>
} -run
logexpect l1 -wait
......@@ -57,6 +57,7 @@
struct vmod_dispatch_label {
unsigned magic;
#define VMOD_DISPATCH_LABEL_MAGIC 0x44515cd0
char **string;
VCL_VCL *vcl;
struct vbitmap *bitmap;
char *vcl_name;
......@@ -67,6 +68,7 @@ struct vmod_dispatch_sub {
unsigned magic;
#define VMOD_DISPATCH_SUB_MAGIC 0x24a617ec
vcl_func_f **func;
char **string;
struct vbitmap *bitmap;
char *vcl_name;
unsigned nsubs;
......@@ -100,6 +102,7 @@ vmod_label__init(VRT_CTX, struct vmod_dispatch_label **labelp,
label->vcl_name = strdup(vcl_name);
AN(label->vcl_name);
AZ(label->vcl);
AZ(label->string);
AZ(label->nvcls);
}
......@@ -115,9 +118,12 @@ vmod_label__fini(struct vmod_dispatch_label **labelp)
*labelp = NULL;
if (label->vcl != NULL) {
for (unsigned i = 0; i < label->nvcls; i++)
if (vbit_test(label->bitmap, i)
&& label->vcl[i] != NULL)
VRT_vcl_rel(&dummy_ctx, label->vcl[i]);
if (vbit_test(label->bitmap, i)) {
if (label->vcl[i] != NULL)
VRT_vcl_rel(&dummy_ctx, label->vcl[i]);
if (label->string[i] != NULL)
free(label->string[i]);
}
free(label->vcl);
}
if (label->bitmap != NULL)
......@@ -159,11 +165,19 @@ vmod_label_add(VRT_CTX, struct vmod_dispatch_label *label, VCL_INT n,
label->nvcls = n + 1;
label->vcl = realloc(label->vcl, label->nvcls * sizeof(vcl));
if (label->vcl == NULL) {
VERR(ctx, "%s.add(%ld, %s): out of memory", label->vcl_name, n,
label_name);
VERR(ctx, "%s.add(%ld, %s): out of memory for label",
label->vcl_name, n, label_name);
return;
}
label->vcl[n] = vcl;
label->string = realloc(label->string,
label->nvcls * sizeof(label_name));
if (label->string == NULL) {
VERR(ctx, "%s.add(%ld, %s): out of memory for string",
label->vcl_name, n, label_name);
return;
}
label->string[n] = strdup(label_name);
vbit_set(label->bitmap, n);
}
......@@ -197,6 +211,30 @@ vmod_label_go(VRT_CTX, struct vmod_dispatch_label *label, VCL_INT n)
VRT_handling(ctx, VCL_RET_VCL);
}
VCL_STRING
vmod_label_string(VRT_CTX, struct vmod_dispatch_label *label, VCL_INT n)
{
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(label, VMOD_DISPATCH_LABEL_MAGIC);
if (n < 0) {
VERR(ctx, "%s.string(%ld): n must be >= 0", label->vcl_name, n);
return NULL;
}
if (n >= label->nvcls) {
VERR(ctx, "%s.string(%ld): highest label number is %d",
label->vcl_name, n, label->nvcls - 1);
return NULL;
}
if (!vbit_test(label->bitmap, n)) {
VERR(ctx, "%s.string(%ld): label %ld was not added",
label->vcl_name, n, n);
return NULL;
}
AN(label->string[n]);
return label->string[n];
}
VCL_VOID
vmod_sub__init(VRT_CTX, struct vmod_dispatch_sub **subp, const char *vcl_name)
{
......@@ -215,6 +253,7 @@ vmod_sub__init(VRT_CTX, struct vmod_dispatch_sub **subp, const char *vcl_name)
sub->vcl_name = strdup(vcl_name);
AN(sub->vcl_name);
AZ(sub->func);
AZ(sub->string);
AZ(sub->nsubs);
}
......@@ -230,8 +269,12 @@ vmod_sub__fini(struct vmod_dispatch_sub **subp)
*subp = NULL;
if (sub->func != NULL)
free(sub->func);
if (sub->bitmap != NULL)
if (sub->bitmap != NULL) {
for (unsigned i = 0; i < sub->nsubs; i++)
if (vbit_test(sub->bitmap, i) && sub->string[i] != NULL)
free(sub->string[i]);
vbit_destroy(sub->bitmap);
}
if (sub->vcl_name != NULL)
free(sub->vcl_name);
FREE_OBJ(sub);
......@@ -315,11 +358,18 @@ vmod_sub_add(VRT_CTX, struct vmod_dispatch_sub *sub, VCL_INT n,
sub->nsubs = n + 1;
if ((sub->func = realloc(sub->func, sub->nsubs * sizeof(vcl_func)))
== NULL) {
VERR(ctx, "%s.add(%ld, %s): out of memory", sub->vcl_name, n,
subname);
VERR(ctx, "%s.add(%ld, %s): out of memory for sub",
sub->vcl_name, n, subname);
return;
}
sub->func[n] = vcl_func;
sub->string = realloc(sub->string, sub->nsubs * sizeof(subname));
if (sub->string == NULL) {
VERR(ctx, "%s.add(%ld, %s): out of memory for string",
sub->vcl_name, n, subname);
return;
}
sub->string[n] = strdup(subname);
vbit_set(sub->bitmap, n);
}
......@@ -347,6 +397,30 @@ vmod_sub_call(VRT_CTX, struct vmod_dispatch_sub *sub, VCL_INT n)
sub->func[n](ctx);
}
VCL_STRING
vmod_sub_string(VRT_CTX, struct vmod_dispatch_sub *sub, VCL_INT n)
{
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(sub, VMOD_DISPATCH_SUB_MAGIC);
if (n < 0) {
VERR(ctx, "%s.string(%ld): n must be >= 0", sub->vcl_name, n);
return NULL;
}
if (n >= sub->nsubs) {
VERR(ctx, "%s.string(%ld): highest sub number is %d",
sub->vcl_name, n, sub->nsubs - 1);
return NULL;
}
if (!vbit_test(sub->bitmap, n)) {
VERR(ctx, "%s.string(%ld): sub %ld was not added",
sub->vcl_name, n, n);
return NULL;
}
AN(sub->string[n]);
return sub->string[n];
}
VCL_STRING
vmod_version(VRT_CTX)
{
......
#-
# Copyright (c) 2017 UPLEX Nils Goroll Systemoptimierung
# Copyright (c) 2017-2018 UPLEX Nils Goroll Systemoptimierung
# All rights reserved
#
# Author: Geoffrey Simmons <geoffrey.simmons@uplex.de>
......@@ -27,12 +27,16 @@ $Method VOID .add(INT n, STRING label)
$Method VOID .go(INT)
$Method STRING .string(INT)
$Object sub()
$Method VOID .add(INT n, STRING sub)
$Method VOID .call(INT n)
$Method STRING .string(INT)
$Function STRING version()
Return the version string for this VMOD.
......
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