Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
L
libvmod-selector
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
uplex-varnish
libvmod-selector
Commits
ad22becd
Commit
ad22becd
authored
Oct 16, 2020
by
Geoff Simmons
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add the element parameter to .which().
parent
8d41e185
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
101 additions
and
34 deletions
+101
-34
README.rst
README.rst
+26
-17
match.vtc
src/tests/match.vtc
+38
-1
vmod_selector.c
src/vmod_selector.c
+14
-1
vmod_selector.vcc
src/vmod_selector.vcc
+23
-15
No files found.
README.rst
View file @
ad22becd
...
...
@@ -39,9 +39,9 @@ SYNOPSIS
# Match properties
INT <obj>.nmatches()
BOOL <obj>.matched([INT n] [, STRING element] [, ENUM select])
INT <obj>.which([ENUM select])
INT <obj>.which([ENUM select]
[, STRING element]
)
# Retrieving objects after match
# Retrieving objects
by index, by string, or
after match
STRING <obj>.element([INT n] [, ENUM select])
STRING <obj>.string([INT n] [, STRING element] [, ENUM select])
INT <obj>.integer([INT n] [, STRING element] [, ENUM select])
...
...
@@ -714,32 +714,37 @@ Example::
.. _xset.which():
INT xset.which(ENUM select)
---------------------------
INT xset.which(ENUM select
, STRING element
)
---------------------------
----------------
::
INT xset.which(
ENUM {UNIQUE, EXACT, FIRST, LAST, SHORTEST, LONGEST} select=UNIQUE
ENUM {UNIQUE, EXACT, FIRST, LAST, SHORTEST, LONGEST} select=UNIQUE,
STRING element=0
)
After a successful ``.match()`` or ``.hasprefix()`` call for the same
set object in the same task scope, return the index of the matching
element indicated by ``select``. The numbering corresponds to the
order of ``.add()`` calls in ``vcl_init``.
Return the index of the element indicated by ``element`` or
``select``. The numbering corresponds to the order of ``.add()`` calls
in ``vcl_init``, starting from 1.
Return 0 if the most recent ``.match()`` or ``.hasprefix()`` call
returned ``false``
.
If the ``element`` parameter is set, then return the numeric index for
that string in the set
.
If more than one element matched after calling ``.hasprefix()``, the
index is chosen with the ``select`` parameter, according to the rules
given above. By default, ``select`` is ``UNIQUE``.
If ``element`` is unset, then the index is chosen with the ``select``
parameter, and refers to the previous ``.match()`` or ``.hasprefix()``
call for the same set object in the same task scope, according to the
rules given above. By default, ``select`` is ``UNIQUE``.
If ``element`` is unset, and the most recent ``.match()`` or
``.hasprefix()`` call returned ``false``, return 0.
``.which()`` invokes VCL failure if:
* The choice of ``select`` indicates failure, as documented above; that
is, if ``select`` is ``UNIQUE`` or ``EXACT``, but there was no unique
or exact match, respectively.
* The choice of ``element`` or ``select`` indicates failure, as
documented above; that is, if ``element`` is a string that is not in
the set, or ``select`` is ``UNIQUE`` or ``EXACT``, but there was no
unique or exact match, respectively.
* There was no prior invocation of ``.match()`` or ``.hasprefix()`` in
the same task scope.
...
...
@@ -752,6 +757,10 @@ Example::
}
}
if (myset.which(element=bereq.url) == 1) {
call do_if_the_url_was_the_first_element;
}
.. _xset.element():
STRING xset.element(INT n, ENUM select)
...
...
src/tests/match.vtc
View file @
ad22becd
...
...
@@ -31,6 +31,11 @@ varnish v1 -vcl {
set resp.http.Match-Zab = t.match("zab");
set resp.http.Match-Xuuq = t.match("xuuq");
set resp.http.Match-Raboof = t.match("raboof");
set resp.http.Which-Foo = t.which(element="foo");
set resp.http.Which-Bar = t.which(element="bar");
set resp.http.Which-Baz = t.which(element="baz");
set resp.http.Which-Quux = t.which(element="quux");
set resp.http.Which-Foobar = t.which(element="foobar");
return (deliver);
}
} -start
...
...
@@ -48,6 +53,11 @@ client c1 {
expect resp.http.Match-Zab == "false"
expect resp.http.Match-Xuuq == "false"
expect resp.http.Match-Raboof == "false"
expect resp.http.Which-Foo == 1
expect resp.http.Which-Bar == 2
expect resp.http.Which-Baz == 3
expect resp.http.Which-Quux == 4
expect resp.http.Which-Foobar == 5
} -run
varnish v1 -vcl {
...
...
@@ -877,9 +887,36 @@ client c1 {
expect resp.reason == "VCL failed"
} -run
varnish v1 -vcl {
import ${vmod_selector};
backend b None;
sub vcl_init {
new s = selector.set();
s.add("foo");
s.compile();
}
sub vcl_recv {
set req.http.Which = s.which(element="bar");
}
}
client c1 {
txreq
rxresp
expect resp.status == 503
expect resp.reason == "VCL failed"
} -run
logexpect l1 -v v1 -d 1 -g vxid -q "VCL_Error" {
expect 0 * Begin req
expect * = VCL_Error {^vmod selector failure: s\.matched\(2\) out of range \(1 members\)$}
expect * = VCL_Error {^vmod selector failure: s\.matched\(2\) out of range \(1 members\)$}
expect * = VCL_return fail
expect * = End
expect 0 * Begin req
expect * = VCL_Error {^vmod selector failure: s\.which\(element="bar"\): no such element$}
expect * = VCL_return fail
expect * = End
} -run
...
...
src/vmod_selector.c
View file @
ad22becd
...
...
@@ -689,14 +689,27 @@ select(VRT_CTX, const struct match_data * const restrict match,
}
VCL_INT
vmod_set_which
(
VRT_CTX
,
struct
vmod_selector_set
*
set
,
VCL_ENUM
selects
)
vmod_set_which
(
VRT_CTX
,
struct
vmod_selector_set
*
set
,
VCL_ENUM
selects
,
VCL_STRING
element
)
{
struct
match_data
*
match
;
CHECK_OBJ_NOTNULL
(
ctx
,
VRT_CTX_MAGIC
);
CHECK_OBJ_NOTNULL
(
set
,
VMOD_SELECTOR_SET_MAGIC
);
if
(
element
!=
NULL
)
if
(
!
vmod_set_match
(
ctx
,
TRUST_ME
(
set
),
element
))
{
VFAIL
(
ctx
,
"%s.which(element=
\"
%s
\"
): no such element"
,
set
->
vcl_name
,
element
);
return
(
UINT_MAX
);
}
match
=
get_existing_match_data
(
ctx
,
set
,
"which"
);
if
(
element
!=
NULL
)
{
CHECK_OBJ_NOTNULL
(
match
,
MATCH_DATA_MAGIC
);
assert
(
match
->
n
==
1
);
return
(
match
->
indices
[
0
]
+
1
);
}
if
(
match
==
NULL
||
match
->
n
==
0
)
return
(
0
);
return
(
select
(
ctx
,
match
,
set
->
vcl_name
,
selects
,
"which"
)
+
1
);
...
...
src/vmod_selector.vcc
View file @
ad22becd
...
...
@@ -35,9 +35,9 @@ SYNOPSIS
# Match properties
INT <obj>.nmatches()
BOOL <obj>.matched([INT n] [, STRING element] [, ENUM select])
INT <obj>.which([ENUM select])
INT <obj>.which([ENUM select]
[, STRING element]
)
# Retrieving objects after match
# Retrieving objects
by index, by string, or
after match
STRING <obj>.element([INT n] [, ENUM select])
STRING <obj>.string([INT n] [, STRING element] [, ENUM select])
INT <obj>.integer([INT n] [, STRING element] [, ENUM select])
...
...
@@ -663,25 +663,29 @@ Example::
}
$Method INT .which(ENUM {UNIQUE, EXACT, FIRST, LAST, SHORTEST, LONGEST}
select=UNIQUE)
select=UNIQUE
, STRING element=0
)
After a successful ``.match()`` or ``.hasprefix()`` call for the same
set object in the same task scope, return the index of the matching
element indicated by ``select``. The numbering corresponds to the
order of ``.add()`` calls in ``vcl_init``.
Return the index of the element indicated by ``element`` or
``select``. The numbering corresponds to the order of ``.add()`` calls
in ``vcl_init``, starting from 1.
If the ``element`` parameter is set, then return the numeric index for
that string in the set.
Return 0 if the most recent ``.match()`` or ``.hasprefix()`` call
returned ``false``.
If ``element`` is unset, then the index is chosen with the ``select``
parameter, and refers to the previous ``.match()`` or ``.hasprefix()``
call for the same set object in the same task scope, according to the
rules given above. By default, ``select`` is ``UNIQUE``.
If more than one element matched after calling ``.hasprefix()``, the
index is chosen with the ``select`` parameter, according to the rules
given above. By default, ``select`` is ``UNIQUE``.
If ``element`` is unset, and the most recent ``.match()`` or
``.hasprefix()`` call returned ``false``, return 0.
``.which()`` invokes VCL failure if:
* The choice of ``select`` indicates failure, as documented above; that
is, if ``select`` is ``UNIQUE`` or ``EXACT``, but there was no unique
or exact match, respectively.
* The choice of ``element`` or ``select`` indicates failure, as
documented above; that is, if ``element`` is a string that is not in
the set, or ``select`` is ``UNIQUE`` or ``EXACT``, but there was no
unique or exact match, respectively.
* There was no prior invocation of ``.match()`` or ``.hasprefix()`` in
the same task scope.
...
...
@@ -694,6 +698,10 @@ Example::
}
}
if (myset.which(element=bereq.url) == 1) {
call do_if_the_url_was_the_first_element;
}
$Method STRING .element(INT n=0,
ENUM {UNIQUE, EXACT, FIRST, LAST, SHORTEST, LONGEST}
select=UNIQUE)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment