Commit 5cea4e6d authored by Geoff Simmons's avatar Geoff Simmons

Add the .bool() method.

parent 43541478
...@@ -28,7 +28,7 @@ SYNOPSIS ...@@ -28,7 +28,7 @@ SYNOPSIS
new <obj> = selector.set([BOOL case_sensitive] new <obj> = selector.set([BOOL case_sensitive]
[, BOOL allow_overlaps]) [, BOOL allow_overlaps])
VOID <obj>.add(STRING [, STRING string] [, STRING regex] VOID <obj>.add(STRING [, STRING string] [, STRING regex]
[, BACKEND backend] [, INT integer]) [, BACKEND backend] [, INT integer] [, BOOL bool])
VOID <obj>.create_stats() VOID <obj>.create_stats()
# Matching # Matching
...@@ -43,8 +43,9 @@ SYNOPSIS ...@@ -43,8 +43,9 @@ SYNOPSIS
# Retrieving objects by index, by string, or after match # Retrieving objects by index, by string, or after match
STRING <obj>.element([INT n] [, ENUM select]) STRING <obj>.element([INT n] [, ENUM select])
STRING <obj>.string([INT n] [, STRING element] [, ENUM select]) STRING <obj>.string([INT n] [, STRING element] [, ENUM select])
INT <obj>.integer([INT n] [, STRING element] [, ENUM select])
BACKEND <obj>.backend([INT n] [, STRING element] [, ENUM select]) BACKEND <obj>.backend([INT n] [, STRING element] [, ENUM select])
INT <obj>.integer([INT n] [, STRING element] [, ENUM select])
BOOL <obj>.bool([INT n] [, STRING element] [, ENUM select])
BOOL <obj>.re_match(STRING [, INT n] [, STRING element] BOOL <obj>.re_match(STRING [, INT n] [, STRING element]
[, ENUM select]) [, ENUM select])
STRING <obj>.sub(STRING text, STRING rewrite [, BOOL all] [, INT n] STRING <obj>.sub(STRING text, STRING rewrite [, BOOL all] [, INT n]
...@@ -437,8 +438,8 @@ Examples:: ...@@ -437,8 +438,8 @@ Examples::
.. _xset.add(): .. _xset.add():
VOID xset.add(STRING, [STRING string], [STRING regex], [BACKEND backend], [INT integer]) VOID xset.add(STRING, [STRING string], [STRING regex], [BACKEND backend], [INT integer], [BOOL bool])
---------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------------
:: ::
...@@ -447,7 +448,8 @@ VOID xset.add(STRING, [STRING string], [STRING regex], [BACKEND backend], [INT i ...@@ -447,7 +448,8 @@ VOID xset.add(STRING, [STRING string], [STRING regex], [BACKEND backend], [INT i
[STRING string], [STRING string],
[STRING regex], [STRING regex],
[BACKEND backend], [BACKEND backend],
[INT integer] [INT integer],
[BOOL bool]
) )
Add the given string to the set. As indicated above, elements added to Add the given string to the set. As indicated above, elements added to
...@@ -455,10 +457,10 @@ the set are implicitly numbered in the order in which they are added ...@@ -455,10 +457,10 @@ the set are implicitly numbered in the order in which they are added
with ``.add()``, starting with 1. with ``.add()``, starting with 1.
If values are set for the optional parameters ``string``, ``regex``, If values are set for the optional parameters ``string``, ``regex``,
``backend`` or ``integer``, then those values are associated with this ``backend``, ``integer`` or ``bool``, then those values are associated
element, and can be retrieved with the ``.string()``, ``.backend()``, with this element, and can be retrieved with the ``.string()``,
``.integer()``, ``.re_match()`` or ``.sub()`` methods, as described ``.backend()``, ``.integer()``, ``.bool()``, ``.re_match()`` or
below. ``.sub()`` methods, as described below.
A regular expression in the ``regex`` parameter is compiled at VCL load A regular expression in the ``regex`` parameter is compiled at VCL load
time. If the compile fails, then the VCL load fails with an error message. time. If the compile fails, then the VCL load fails with an error message.
...@@ -895,6 +897,49 @@ Example:: ...@@ -895,6 +897,49 @@ Example::
} }
} }
.. _xset.bool():
BOOL xset.bool(INT n, STRING element, ENUM select)
--------------------------------------------------
::
BOOL xset.bool(
INT n=0,
STRING element=0,
ENUM {UNIQUE, EXACT, FIRST, LAST, SHORTEST, LONGEST} select=UNIQUE
)
Returns the boolean value set by the ``bool`` parameter for the
element of the set indicated by ``n``, ``element`` and ``select``,
according to the rules given above.
``.bool()`` invokes VCL failure if:
* The rules for ``n``, ``element`` and ``select`` indicate failure.
* No boolean was set with the ``bool`` parameter in ``.add()``.
Example::
# Match domains to the Host header, and append "www." where
# necessary.
sub vcl_init {
new domains = selector.set();
domains.add("example.com", bool=true);
domains.add("www.example.net", bool=false);
domains.add("example.org", bool=true);
domains.add("www.example.edu", bool=false)
}
sub vcl_recv {
if (domains.match(req.http.Host)) {
if (domains.bool()) {
set req.http.Host = "www." + req.http.Host;
}
}
}
.. _xset.re_match(): .. _xset.re_match():
BOOL xset.re_match(STRING subject, INT n, STRING element, ENUM select) BOOL xset.re_match(STRING subject, INT n, STRING element, ENUM select)
......
...@@ -255,3 +255,21 @@ vmod_set_sub(VRT_CTX, struct vmod_selector_set *set, VCL_STRING str, ...@@ -255,3 +255,21 @@ vmod_set_sub(VRT_CTX, struct vmod_selector_set *set, VCL_STRING str,
return (NULL); return (NULL);
return (VRT_regsub(ctx, all, str, re, sub)); return (VRT_regsub(ctx, all, str, re, sub));
} }
VCL_BOOL
vmod_set_bool(VRT_CTX, struct VPFX(selector_set) *set, VCL_INT n,
VCL_STRING element, VCL_ENUM selects)
{
unsigned idx;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(set, VMOD_SELECTOR_SET_MAGIC);
idx = get_idx(ctx, n, set, "bool", element, selects);
if (idx == UINT_MAX)
return (0);
if (!check_added(ctx, set, idx, BOOLEAN, "bool", "boolean"))
return (0);
return (set->table[idx]->bool);
}
This diff is collapsed.
...@@ -506,7 +506,7 @@ vmod_set_add(VRT_CTX, struct vmod_selector_set *set, ...@@ -506,7 +506,7 @@ vmod_set_add(VRT_CTX, struct vmod_selector_set *set,
} }
if (!args->valid_string && re == NULL && !args->valid_backend if (!args->valid_string && re == NULL && !args->valid_backend
&& !args->valid_integer) && !args->valid_integer && !args->valid_bool)
return; return;
set->table = realloc(set->table, n * sizeof(struct entry *)); set->table = realloc(set->table, n * sizeof(struct entry *));
...@@ -530,6 +530,10 @@ vmod_set_add(VRT_CTX, struct vmod_selector_set *set, ...@@ -530,6 +530,10 @@ vmod_set_add(VRT_CTX, struct vmod_selector_set *set,
entry->integer = args->integer; entry->integer = args->integer;
set_added(set, n - 1, INTEGER); set_added(set, n - 1, INTEGER);
} }
if (args->valid_bool) {
entry->bool = args->bool;
set_added(set, n - 1, BOOLEAN);
}
set->table[n - 1] = entry; set->table[n - 1] = entry;
} }
......
...@@ -52,6 +52,7 @@ struct entry { ...@@ -52,6 +52,7 @@ struct entry {
VCL_BACKEND backend; VCL_BACKEND backend;
vre_t *re; vre_t *re;
VCL_INT integer; VCL_INT integer;
VCL_BOOL bool;
}; };
enum bitmap_e { enum bitmap_e {
...@@ -59,6 +60,7 @@ enum bitmap_e { ...@@ -59,6 +60,7 @@ enum bitmap_e {
BACKEND, BACKEND,
REGEX, REGEX,
INTEGER, INTEGER,
BOOLEAN,
__MAX_BITMAP, __MAX_BITMAP,
}; };
......
...@@ -24,7 +24,7 @@ SYNOPSIS ...@@ -24,7 +24,7 @@ SYNOPSIS
new <obj> = selector.set([BOOL case_sensitive] new <obj> = selector.set([BOOL case_sensitive]
[, BOOL allow_overlaps]) [, BOOL allow_overlaps])
VOID <obj>.add(STRING [, STRING string] [, STRING regex] VOID <obj>.add(STRING [, STRING string] [, STRING regex]
[, BACKEND backend] [, INT integer]) [, BACKEND backend] [, INT integer] [, BOOL bool])
VOID <obj>.create_stats() VOID <obj>.create_stats()
# Matching # Matching
...@@ -39,8 +39,9 @@ SYNOPSIS ...@@ -39,8 +39,9 @@ SYNOPSIS
# Retrieving objects by index, by string, or after match # Retrieving objects by index, by string, or after match
STRING <obj>.element([INT n] [, ENUM select]) STRING <obj>.element([INT n] [, ENUM select])
STRING <obj>.string([INT n] [, STRING element] [, ENUM select]) STRING <obj>.string([INT n] [, STRING element] [, ENUM select])
INT <obj>.integer([INT n] [, STRING element] [, ENUM select])
BACKEND <obj>.backend([INT n] [, STRING element] [, ENUM select]) BACKEND <obj>.backend([INT n] [, STRING element] [, ENUM select])
INT <obj>.integer([INT n] [, STRING element] [, ENUM select])
BOOL <obj>.bool([INT n] [, STRING element] [, ENUM select])
BOOL <obj>.re_match(STRING [, INT n] [, STRING element] BOOL <obj>.re_match(STRING [, INT n] [, STRING element]
[, ENUM select]) [, ENUM select])
STRING <obj>.sub(STRING text, STRING rewrite [, BOOL all] [, INT n] STRING <obj>.sub(STRING text, STRING rewrite [, BOOL all] [, INT n]
...@@ -422,17 +423,17 @@ Examples:: ...@@ -422,17 +423,17 @@ Examples::
} }
$Method VOID .add(STRING, [STRING string], [STRING regex], [BACKEND backend], $Method VOID .add(STRING, [STRING string], [STRING regex], [BACKEND backend],
[INT integer]) [INT integer], [BOOL bool])
Add the given string to the set. As indicated above, elements added to Add the given string to the set. As indicated above, elements added to
the set are implicitly numbered in the order in which they are added the set are implicitly numbered in the order in which they are added
with ``.add()``, starting with 1. with ``.add()``, starting with 1.
If values are set for the optional parameters ``string``, ``regex``, If values are set for the optional parameters ``string``, ``regex``,
``backend`` or ``integer``, then those values are associated with this ``backend``, ``integer`` or ``bool``, then those values are associated
element, and can be retrieved with the ``.string()``, ``.backend()``, with this element, and can be retrieved with the ``.string()``,
``.integer()``, ``.re_match()`` or ``.sub()`` methods, as described ``.backend()``, ``.integer()``, ``.bool()``, ``.re_match()`` or
below. ``.sub()`` methods, as described below.
A regular expression in the ``regex`` parameter is compiled at VCL load A regular expression in the ``regex`` parameter is compiled at VCL load
time. If the compile fails, then the VCL load fails with an error message. time. If the compile fails, then the VCL load fails with an error message.
...@@ -801,6 +802,40 @@ Example:: ...@@ -801,6 +802,40 @@ Example::
} }
} }
$Method BOOL .bool(INT n=0, STRING element=0,
ENUM {UNIQUE, EXACT, FIRST, LAST, SHORTEST, LONGEST}
select=UNIQUE)
Returns the boolean value set by the ``bool`` parameter for the
element of the set indicated by ``n``, ``element`` and ``select``,
according to the rules given above.
``.bool()`` invokes VCL failure if:
* The rules for ``n``, ``element`` and ``select`` indicate failure.
* No boolean was set with the ``bool`` parameter in ``.add()``.
Example::
# Match domains to the Host header, and append "www." where
# necessary.
sub vcl_init {
new domains = selector.set();
domains.add("example.com", bool=true);
domains.add("www.example.net", bool=false);
domains.add("example.org", bool=true);
domains.add("www.example.edu", bool=false)
}
sub vcl_recv {
if (domains.match(req.http.Host)) {
if (domains.bool()) {
set req.http.Host = "www." + req.http.Host;
}
}
}
$Method BOOL .re_match(STRING subject, INT n=0, STRING element=0, $Method BOOL .re_match(STRING subject, INT n=0, STRING element=0,
ENUM {UNIQUE, EXACT, FIRST, LAST, SHORTEST, LONGEST} ENUM {UNIQUE, EXACT, FIRST, LAST, SHORTEST, LONGEST}
select=UNIQUE) select=UNIQUE)
......
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