Commit 4a899580 authored by Geoff Simmons's avatar Geoff Simmons

Add the .string() method.

parent 9eeb5ebe
......@@ -32,6 +32,7 @@ CONTENTS
* :ref:`func_set.match`
* :ref:`func_set.matched`
* :ref:`func_set.nmatches`
* :ref:`func_set.string`
* :ref:`func_set.which`
* :ref:`func_version`
......@@ -217,6 +218,27 @@ Example::
}
.. _func_set.string:
STRING xset.string(INT n, ENUM select)
--------------------------------------
::
STRING xset.string(
INT n=0,
ENUM {UNIQUE, EXACT, FIRST, LAST, SHORTEST, LONGEST} select=UNIQUE
)
Returns ...
Example::
if (myset.hasprefix(req.url)) {
# ...
}
.. _func_set.debug:
STRING xset.debug()
......
# looks like -*- vcl -*-
varnishtest "string() method"
varnish v1 -vcl {
import ${vmod_selector};
backend b { .host = "${bad_ip}"; }
sub vcl_init {
new s = selector.set();
s.add("foo", string="oof");
s.add("bar", string="rab");
s.add("baz", string="zab");
s.add("quux", string="xuuq");
s.add("foobar", string="raboof");
}
sub vcl_recv {
return (synth(200));
}
sub vcl_synth {
set resp.http.N-1 = s.string(1);
set resp.http.N-2 = s.string(2);
set resp.http.N-3 = s.string(3);
set resp.http.N-4 = s.string(4);
set resp.http.N-5 = s.string(5);
set resp.http.N--1 = s.string(-1);
set resp.http.N-0 = s.string(0);
set resp.http.N-6 = s.string(6);
if (s.match(req.http.Word)) {
set resp.http.String = s.string();
set resp.http.String-Unique = s.string(select=UNIQUE);
set resp.http.String-Exact = s.string(select=EXACT);
set resp.http.String-First = s.string(select=FIRST);
set resp.http.String-Last = s.string(select=LAST);
set resp.http.String-Shortest
= s.string(select=SHORTEST);
set resp.http.String-Longest
= s.string(select=LONGEST);
}
return (deliver);
}
} -start
client c1 {
txreq
rxresp
expect resp.status == 200
expect resp.http.N-1 == "oof"
expect resp.http.N-2 == "rab"
expect resp.http.N-3 == "zab"
expect resp.http.N-4 == "xuuq"
expect resp.http.N-5 == "raboof"
expect resp.http.N--1 == ""
expect resp.http.N-0 == ""
expect resp.http.N-6 == ""
txreq -hdr "Word: foo"
rxresp
expect resp.status == 200
expect resp.http.String == "oof"
expect resp.http.String-Unique == resp.http.String
expect resp.http.String-Exact == resp.http.String
expect resp.http.String-First == "oof"
expect resp.http.String-Last == "oof"
expect resp.http.String-Shortest == "oof"
expect resp.http.String-Longest == "oof"
txreq -hdr "Word: bar"
rxresp
expect resp.status == 200
expect resp.http.String == "rab"
expect resp.http.String-Unique == resp.http.String
expect resp.http.String-Exact == resp.http.String
expect resp.http.String-First == "rab"
expect resp.http.String-Last == "rab"
expect resp.http.String-Shortest == "rab"
expect resp.http.String-Longest == "rab"
txreq -hdr "Word: baz"
rxresp
expect resp.status == 200
expect resp.http.String == "zab"
expect resp.http.String-Unique == resp.http.String
expect resp.http.String-Exact == resp.http.String
expect resp.http.String-First == "zab"
expect resp.http.String-Last == "zab"
expect resp.http.String-Shortest == "zab"
expect resp.http.String-Longest == "zab"
txreq -hdr "Word: quux"
rxresp
expect resp.status == 200
expect resp.http.String == "xuuq"
expect resp.http.String-Unique == resp.http.String
expect resp.http.String-Exact == resp.http.String
expect resp.http.String-First == "xuuq"
expect resp.http.String-Last == "xuuq"
expect resp.http.String-Shortest == "xuuq"
expect resp.http.String-Longest == "xuuq"
txreq -hdr "Word: foobar"
rxresp
expect resp.status == 200
expect resp.http.String == "raboof"
expect resp.http.String-Unique == resp.http.String
expect resp.http.String-Exact == resp.http.String
expect resp.http.String-First == "raboof"
expect resp.http.String-Last == "raboof"
expect resp.http.String-Shortest == "raboof"
expect resp.http.String-Longest == "raboof"
} -run
logexpect l1 -v v1 -d 1 -g vxid -q "VCL_Error" {
expect 0 * Begin req
expect * = VCL_Error {^vmod selector error: s\.string\(\) called without prior match$}
expect * = VCL_Error {^vmod selector error: s\.string\(\) called without prior match$}
expect * = VCL_Error {^vmod selector error: s\.string\(6\): set has 5 elements$}
expect * = End
} -run
varnish v1 -vcl {
import ${vmod_selector};
backend b { .host = "${bad_ip}"; }
sub vcl_init {
new s = selector.set();
s.add("foobarbazquux", string="1");
s.add("foobarbaz", string="2");
s.add("foobar", string="3");
s.add("foo", string="4");
}
sub vcl_recv {
return (synth(200));
}
sub vcl_synth {
if (s.hasprefix(req.http.Word)) {
set resp.http.String = s.string();
set resp.http.String-Unique = s.string(select=UNIQUE);
set resp.http.String-Exact = s.string(select=EXACT);
set resp.http.String-First = s.string(select=FIRST);
set resp.http.String-Last = s.string(select=LAST);
set resp.http.String-Shortest
= s.string(select=SHORTEST);
set resp.http.String-Longest
= s.string(select=LONGEST);
}
return (deliver);
}
}
logexpect l1 -v v1 -d 0 -g vxid -q "VCL_Error" {
expect * 1009 Begin req
expect * = VCL_Error {^vmod selector error: s\.string\(select=UNIQUE\): 2 elements were matched$}
expect * = VCL_Error {^vmod selector error: s\.string\(select=UNIQUE\): 2 elements were matched$}
expect * = End
expect 0 * Begin req
expect * = VCL_Error {^vmod selector error: s\.string\(select=UNIQUE\): 3 elements were matched$}
expect * = VCL_Error {^vmod selector error: s\.string\(select=UNIQUE\): 3 elements were matched$}
expect * = End
expect 0 * Begin req
expect * = VCL_Error {^vmod selector error: s\.string\(select=UNIQUE\): 4 elements were matched$}
expect * = VCL_Error {^vmod selector error: s\.string\(select=UNIQUE\): 4 elements were matched$}
expect * = End
expect 0 * Begin req
expect * = VCL_Error {^vmod selector error: s\.string\(select=UNIQUE\): 2 elements were matched$}
expect * = VCL_Error {^vmod selector error: s\.string\(select=UNIQUE\): 2 elements were matched$}
expect * = VCL_Error {^vmod selector error: s\.string\(select=EXACT\): no element matched exactly$}
expect * = End
} -start
client c1 {
txreq -hdr "Word: foo"
rxresp
expect resp.status == 200
expect resp.http.String == "4"
expect resp.http.String-Unique == "4"
expect resp.http.String-Exact == "4"
expect resp.http.String-First == "4"
expect resp.http.String-Last == "4"
expect resp.http.String-Shortest == "4"
expect resp.http.String-Longest == "4"
txreq -hdr "Word: foobar"
rxresp
expect resp.status == 200
expect resp.http.String == ""
expect resp.http.String-Unique == ""
expect resp.http.String-Exact == "3"
expect resp.http.String-First == "3"
expect resp.http.String-Last == "4"
expect resp.http.String-Shortest == "4"
expect resp.http.String-Longest == "3"
txreq -hdr "Word: foobarbaz"
rxresp
expect resp.status == 200
expect resp.http.String == ""
expect resp.http.String-Unique == ""
expect resp.http.String-Exact == "2"
expect resp.http.String-First == "2"
expect resp.http.String-Last == "4"
expect resp.http.String-Shortest == "4"
expect resp.http.String-Longest == "2"
txreq -hdr "Word: foobarbazquux"
rxresp
expect resp.status == 200
expect resp.http.String == ""
expect resp.http.String-Unique == ""
expect resp.http.String-Exact == "1"
expect resp.http.String-First == "1"
expect resp.http.String-Last == "4"
expect resp.http.String-Shortest == "4"
expect resp.http.String-Longest == "1"
txreq -hdr "Word: foobarb"
rxresp
expect resp.status == 200
expect resp.http.String == ""
expect resp.http.String-Unique == ""
expect resp.http.String-Exact == ""
expect resp.http.String-First == "3"
expect resp.http.String-Last == "4"
expect resp.http.String-Shortest == "4"
expect resp.http.String-Longest == "3"
} -run
logexpect l1 -wait
varnish v1 -vcl {
import ${vmod_selector};
backend b { .host = "${bad_ip}"; }
sub vcl_init {
new s = selector.set();
s.add("foo", string="foo");
s.add("bar");
}
sub vcl_recv {
return (synth(200));
}
sub vcl_synth {
if (s.match(req.http.Word)) {
set resp.http.String = s.string();
}
return (deliver);
}
}
logexpect l1 -v v1 -d 0 -g vxid -q "VCL_Error" {
expect 0 * Begin req
expect * = VCL_Error {^vmod selector error: s\.string\(\): string not added for element 2$}
expect * = End
} -start
client c1 {
txreq -hdr "Word: foo"
rxresp
expect resp.status == 200
expect resp.http.String == "foo"
txreq -hdr "Word: bar"
rxresp
expect resp.status == 200
expect resp.http.String == ""
} -run
logexpect l1 -wait
......@@ -595,6 +595,27 @@ vmod_set_backend(VRT_CTX, struct vmod_selector_set *set, VCL_INT n,
return (b);
}
VCL_STRING
vmod_set_string(VRT_CTX, struct vmod_selector_set * set, VCL_INT n,
VCL_ENUM selects)
{
unsigned idx;
VCL_STRING s;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(set, VMOD_SELECTOR_SET_MAGIC);
idx = get_idx(ctx, n, set, "string", selects);
if (idx == UINT_MAX)
return (NULL);
if (!check_added(ctx, set, idx, STRING, "string", "string"))
return (NULL);
s = set->table[idx]->string;
AN(s);
return (s);
}
VCL_STRING
vmod_set_debug(VRT_CTX, struct vmod_selector_set *set)
{
......
......@@ -123,6 +123,18 @@ $Method BACKEND .backend(INT n=0,
Returns ...
Example::
if (myset.hasprefix(req.url)) {
# ...
}
$Method STRING .string(INT n=0,
ENUM {UNIQUE, EXACT, FIRST, LAST, SHORTEST, LONGEST}
select=UNIQUE)
Returns ...
Example::
if (myset.hasprefix(req.url)) {
......
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