vmod_taskvar(3)

Name
vmod_taskvar - task variables as objects for VCL

SYNOPSIS

            import taskvar [as name] [from "path"]

            new xacl = taskvar.acl([ACL init])

                ACL xacl.get([ACL fallback])

                VOID xacl.set(ACL)

                VOID xacl.undefine()

                BOOL xacl.defined()

                VOID xacl.protect()

                BOOL xacl.protected()

            new xbackend = taskvar.backend([BACKEND init])

                BACKEND xbackend.get([BACKEND fallback])

                VOID xbackend.set(BACKEND)

                VOID xbackend.undefine()

                BOOL xbackend.defined()

                VOID xbackend.protect()

                BOOL xbackend.protected()

            new xblob = taskvar.blob([BLOB init])

                BLOB xblob.get([BLOB fallback])

                VOID xblob.set(BLOB)

                VOID xblob.undefine()

                BOOL xblob.defined()

                VOID xblob.protect()

                BOOL xblob.protected()

            new xbody = taskvar.body([BODY init])

                BODY xbody.get([BODY fallback])

                VOID xbody.set(BODY)

                VOID xbody.undefine()

                BOOL xbody.defined()

                VOID xbody.protect()

                BOOL xbody.protected()

            new xbool = taskvar.bool([BOOL init])

                BOOL xbool.get([BOOL fallback])

                VOID xbool.set(BOOL)

                VOID xbool.undefine()

                BOOL xbool.defined()

                VOID xbool.protect()

                BOOL xbool.protected()

            new xbytes = taskvar.bytes([BYTES init])

                BYTES xbytes.get([BYTES fallback])

                VOID xbytes.set(BYTES)

                VOID xbytes.undefine()

                BOOL xbytes.defined()

                VOID xbytes.protect()

                BOOL xbytes.protected()

            new xduration = taskvar.duration([DURATION init])

                DURATION xduration.get([DURATION fallback])

                VOID xduration.set(DURATION)

                VOID xduration.undefine()

                BOOL xduration.defined()

                VOID xduration.protect()

                BOOL xduration.protected()

            new xheader = taskvar.header([HEADER init])

                HEADER xheader.get([HEADER fallback])

                VOID xheader.set(HEADER)

                VOID xheader.undefine()

                BOOL xheader.defined()

                VOID xheader.protect()

                BOOL xheader.protected()

            new xint = taskvar.int([INT init])

                INT xint.get([INT fallback])

                VOID xint.set(INT)

                VOID xint.undefine()

                BOOL xint.defined()

                VOID xint.protect()

                BOOL xint.protected()

            new xip = taskvar.ip([IP init])

                IP xip.get([IP fallback])

                VOID xip.set(IP)

                VOID xip.undefine()

                BOOL xip.defined()

                VOID xip.protect()

                BOOL xip.protected()

            new xprobe = taskvar.probe([PROBE init])

                PROBE xprobe.get([PROBE fallback])

                VOID xprobe.set(PROBE)

                VOID xprobe.undefine()

                BOOL xprobe.defined()

                VOID xprobe.protect()

                BOOL xprobe.protected()

            new xreal = taskvar.real([REAL init])

                REAL xreal.get([REAL fallback])

                VOID xreal.set(REAL)

                VOID xreal.undefine()

                BOOL xreal.defined()

                VOID xreal.protect()

                BOOL xreal.protected()

            new xstevedore = taskvar.stevedore([STEVEDORE init])

                STEVEDORE xstevedore.get([STEVEDORE fallback])

                VOID xstevedore.set(STEVEDORE)

                VOID xstevedore.undefine()

                BOOL xstevedore.defined()

                VOID xstevedore.protect()

                BOOL xstevedore.protected()

            new xstring = taskvar.string([STRING init])

                STRING xstring.get([STRING fallback])

                VOID xstring.set(STRING)

                VOID xstring.undefine()

                BOOL xstring.defined()

                VOID xstring.protect()

                BOOL xstring.protected()

                VOID xstring.foreach(STRING string, SUB sub, STRING delim)

            new xtime = taskvar.time([TIME init])

                TIME xtime.get([TIME fallback])

                VOID xtime.set(TIME)

                VOID xtime.undefine()

                BOOL xtime.defined()

                VOID xtime.protect()

                BOOL xtime.protected()

            new xstrands = taskvar.strands(INT cap, [STRING joiner], [STRING prefix], [STRING postfix])

                VOID xstrands.add(STRING)

                VOID xstrands.clear()

                STRING xstrands.get()

DESCRIPTION

This module implements task scoped variables as objects: Each client or backend request (task) has their own view of taskvar variables.

A taskvar can be constructed with a default, which is used as the initial value at the beginning of each task.

The value of a taskvar is the either the default or the last value assigned to it within a task. If neither a default nor previous value was set, then the value is undefined.

A taskvar can also be undefined using the .undefine() method.

Attempts to .get() an undefined value will return the .get() method`s fallback argument. For most types, there is a default fallback; if there is none, using .get() on an undefined value triggers a VCL failure at runtime.

taskvar variables can be protected against write access, in which case any attempt to .set() them triggers a VCL failure at runtime.

Example with an initially undefined variable:

            import taskvar;

            sub vcl_init {
             new xint = taskvar.int();
            }

            sub vcl_recv {
             set req.http.def = xint.defined(); # false
             set req.http.fallback = xint.get(5); # 5
             xint.set(42);
            }

            sub vcl_deliver {
             set resp.http.the-answer = xint.get(); # 42
            }

Example with an initially defined variable:

            import taskvar;

            sub vcl_init {
             new xint = taskvar.int(17);
            }

            sub vcl_recv {
             set req.http.def = xint.defined(); # true
             set req.http.fallback = xint.get(5); # 17
             xint.set(42);
             xint.protect();
             if (false) {
              xint.set(3); # would fail
             }
            }

            sub vcl_deliver {
             set resp.http.the-answer = xint.get(); # 42
            }

The implementation of the various classes of this vmod is auto-generated, and so is the documentation. Thus, the documentation following this generic description is identical for all types except for the respective type names and the .get() fallback.

  new xacl = taskvar.acl([ACL init])

Construct a(n) ACL taskvar with the default value init, if provided. If no init value is provided, the variable will be initially undefined.

  ACL xacl.get([ACL fallback])

Return the value of the taskvar or the fallback argument if it is undefined. Using .get() on an undefined value without a fallback triggers a VCL error.

  VOID xacl.set(ACL)

Set the value of the taskvar.

Triggers a vcl failure for protected variables.

  VOID xacl.undefine()

Undefine the taskvar.

  BOOL xacl.defined()

Return whether the taskvar is defined.

  VOID xacl.protect()

Protect the taskvar, so any future .set() calls on it trigger a vcl failure.

  BOOL xacl.protected()

Return whether the taskvar is protected.

  new xbackend = taskvar.backend([BACKEND init])

Construct a(n) BACKEND taskvar with the default value init, if provided. If no init value is provided, the variable will be initially undefined.

  BACKEND xbackend.get([BACKEND fallback])

Return the value of the taskvar or the fallback argument if it is undefined. The default fallback is the None backend.

  VOID xbackend.set(BACKEND)

Set the value of the taskvar.

Triggers a vcl failure for protected variables.

  VOID xbackend.undefine()

Undefine the taskvar.

  BOOL xbackend.defined()

Return whether the taskvar is defined.

  VOID xbackend.protect()

Protect the taskvar, so any future .set() calls on it trigger a vcl failure.

  BOOL xbackend.protected()

Return whether the taskvar is protected.

  new xblob = taskvar.blob([BLOB init])

Construct a(n) BLOB taskvar with the default value init, if provided. If no init value is provided, the variable will be initially undefined.

  BLOB xblob.get([BLOB fallback])

Return the value of the taskvar or the fallback argument if it is undefined. The default fallback is the null blob.

  VOID xblob.set(BLOB)

Set the value of the taskvar.

Triggers a vcl failure for protected variables.

  VOID xblob.undefine()

Undefine the taskvar.

  BOOL xblob.defined()

Return whether the taskvar is defined.

  VOID xblob.protect()

Protect the taskvar, so any future .set() calls on it trigger a vcl failure.

  BOOL xblob.protected()

Return whether the taskvar is protected.

  new xbody = taskvar.body([BODY init])

Construct a(n) BODY taskvar with the default value init, if provided. If no init value is provided, the variable will be initially undefined.

  BODY xbody.get([BODY fallback])

Return the value of the taskvar or the fallback argument if it is undefined. Using .get() on an undefined value without a fallback triggers a VCL error.

  VOID xbody.set(BODY)

Set the value of the taskvar.

Triggers a vcl failure for protected variables.

  VOID xbody.undefine()

Undefine the taskvar.

  BOOL xbody.defined()

Return whether the taskvar is defined.

  VOID xbody.protect()

Protect the taskvar, so any future .set() calls on it trigger a vcl failure.

  BOOL xbody.protected()

Return whether the taskvar is protected.

  new xbool = taskvar.bool([BOOL init])

Construct a(n) BOOL taskvar with the default value init, if provided. If no init value is provided, the variable will be initially undefined.

  BOOL xbool.get([BOOL fallback])

Return the value of the taskvar or the fallback argument if it is undefined. The default fallback is false

  VOID xbool.set(BOOL)

Set the value of the taskvar.

Triggers a vcl failure for protected variables.

  VOID xbool.undefine()

Undefine the taskvar.

  BOOL xbool.defined()

Return whether the taskvar is defined.

  VOID xbool.protect()

Protect the taskvar, so any future .set() calls on it trigger a vcl failure.

  BOOL xbool.protected()

Return whether the taskvar is protected.

  new xbytes = taskvar.bytes([BYTES init])

Construct a(n) BYTES taskvar with the default value init, if provided. If no init value is provided, the variable will be initially undefined.

  BYTES xbytes.get([BYTES fallback])

Return the value of the taskvar or the fallback argument if it is undefined. The default fallback is 0 bytes.

  VOID xbytes.set(BYTES)

Set the value of the taskvar.

Triggers a vcl failure for protected variables.

  VOID xbytes.undefine()

Undefine the taskvar.

  BOOL xbytes.defined()

Return whether the taskvar is defined.

  VOID xbytes.protect()

Protect the taskvar, so any future .set() calls on it trigger a vcl failure.

  BOOL xbytes.protected()

Return whether the taskvar is protected.

  new xduration = taskvar.duration([DURATION init])

Construct a(n) DURATION taskvar with the default value init, if provided. If no init value is provided, the variable will be initially undefined.

  DURATION xduration.get([DURATION fallback])

Return the value of the taskvar or the fallback argument if it is undefined. The default fallback is 0s.

  VOID xduration.set(DURATION)

Set the value of the taskvar.

Triggers a vcl failure for protected variables.

  VOID xduration.undefine()

Undefine the taskvar.

  BOOL xduration.defined()

Return whether the taskvar is defined.

  VOID xduration.protect()

Protect the taskvar, so any future .set() calls on it trigger a vcl failure.

  BOOL xduration.protected()

Return whether the taskvar is protected.

  new xheader = taskvar.header([HEADER init])

Construct a(n) HEADER taskvar with the default value init, if provided. If no init value is provided, the variable will be initially undefined.

  HEADER xheader.get([HEADER fallback])

Return the value of the taskvar or the fallback argument if it is undefined. Using .get() on an undefined value without a fallback triggers a VCL error.

  VOID xheader.set(HEADER)

Set the value of the taskvar.

Triggers a vcl failure for protected variables.

  VOID xheader.undefine()

Undefine the taskvar.

  BOOL xheader.defined()

Return whether the taskvar is defined.

  VOID xheader.protect()

Protect the taskvar, so any future .set() calls on it trigger a vcl failure.

  BOOL xheader.protected()

Return whether the taskvar is protected.

  new xint = taskvar.int([INT init])

Construct a(n) INT taskvar with the default value init, if provided. If no init value is provided, the variable will be initially undefined.

  INT xint.get([INT fallback])

Return the value of the taskvar or the fallback argument if it is undefined. The default fallback is 0.

  VOID xint.set(INT)

Set the value of the taskvar.

Triggers a vcl failure for protected variables.

  VOID xint.undefine()

Undefine the taskvar.

  BOOL xint.defined()

Return whether the taskvar is defined.

  VOID xint.protect()

Protect the taskvar, so any future .set() calls on it trigger a vcl failure.

  BOOL xint.protected()

Return whether the taskvar is protected.

  new xip = taskvar.ip([IP init])

Construct a(n) IP taskvar with the default value init, if provided. If no init value is provided, the variable will be initially undefined.

  IP xip.get([IP fallback])

Return the value of the taskvar or the fallback argument if it is undefined. The default fallback is IPv4 0.0.0.0.

  VOID xip.set(IP)

Set the value of the taskvar.

Triggers a vcl failure for protected variables.

  VOID xip.undefine()

Undefine the taskvar.

  BOOL xip.defined()

Return whether the taskvar is defined.

  VOID xip.protect()

Protect the taskvar, so any future .set() calls on it trigger a vcl failure.

  BOOL xip.protected()

Return whether the taskvar is protected.

  new xprobe = taskvar.probe([PROBE init])

Construct a(n) PROBE taskvar with the default value init, if provided. If no init value is provided, the variable will be initially undefined.

  PROBE xprobe.get([PROBE fallback])

Return the value of the taskvar or the fallback argument if it is undefined. The default fallback is no backend.

  VOID xprobe.set(PROBE)

Set the value of the taskvar.

Triggers a vcl failure for protected variables.

  VOID xprobe.undefine()

Undefine the taskvar.

  BOOL xprobe.defined()

Return whether the taskvar is defined.

  VOID xprobe.protect()

Protect the taskvar, so any future .set() calls on it trigger a vcl failure.

  BOOL xprobe.protected()

Return whether the taskvar is protected.

  new xreal = taskvar.real([REAL init])

Construct a(n) REAL taskvar with the default value init, if provided. If no init value is provided, the variable will be initially undefined.

  REAL xreal.get([REAL fallback])

Return the value of the taskvar or the fallback argument if it is undefined. The default fallback is 0.0.

  VOID xreal.set(REAL)

Set the value of the taskvar.

Triggers a vcl failure for protected variables.

  VOID xreal.undefine()

Undefine the taskvar.

  BOOL xreal.defined()

Return whether the taskvar is defined.

  VOID xreal.protect()

Protect the taskvar, so any future .set() calls on it trigger a vcl failure.

  BOOL xreal.protected()

Return whether the taskvar is protected.

  new xstevedore = taskvar.stevedore([STEVEDORE init])

Construct a(n) STEVEDORE taskvar with the default value init, if provided. If no init value is provided, the variable will be initially undefined.

  STEVEDORE xstevedore.get([STEVEDORE fallback])

Return the value of the taskvar or the fallback argument if it is undefined. The default fallback is the default storage.

  VOID xstevedore.set(STEVEDORE)

Set the value of the taskvar.

Triggers a vcl failure for protected variables.

  VOID xstevedore.undefine()

Undefine the taskvar.

  BOOL xstevedore.defined()

Return whether the taskvar is defined.

  VOID xstevedore.protect()

Protect the taskvar, so any future .set() calls on it trigger a vcl failure.

  BOOL xstevedore.protected()

Return whether the taskvar is protected.

  new xstring = taskvar.string([STRING init])

Construct a(n) STRING taskvar with the default value init, if provided. If no init value is provided, the variable will be initially undefined.

  STRING xstring.get([STRING fallback])

Return the value of the taskvar or the fallback argument if it is undefined. The default fallback is the empty string.

  VOID xstring.set(STRING)

Set the value of the taskvar.

Triggers a vcl failure for protected variables.

  VOID xstring.undefine()

Undefine the taskvar.

  BOOL xstring.defined()

Return whether the taskvar is defined.

  VOID xstring.protect()

Protect the taskvar, so any future .set() calls on it trigger a vcl failure.

  BOOL xstring.protected()

Return whether the taskvar is protected.

  VOID xstring.foreach(STRING string, SUB sub, STRING delim)

            VOID xstring.foreach(STRING string, SUB sub, STRING delim=",")

Breaks string into a sequence of zero or more nonempty tokens, seperated by delim, and successively calls sub with the object set to each token.

Processing ends for a nonempty return() from sub.

  new xtime = taskvar.time([TIME init])

Construct a(n) TIME taskvar with the default value init, if provided. If no init value is provided, the variable will be initially undefined.

  TIME xtime.get([TIME fallback])

Return the value of the taskvar or the fallback argument if it is undefined. The default fallback is the epoch (1970/1/1 0:00:00 GMT).

  VOID xtime.set(TIME)

Set the value of the taskvar.

Triggers a vcl failure for protected variables.

  VOID xtime.undefine()

Undefine the taskvar.

  BOOL xtime.defined()

Return whether the taskvar is defined.

  VOID xtime.protect()

Protect the taskvar, so any future .set() calls on it trigger a vcl failure.

  BOOL xtime.protected()

Return whether the taskvar is protected.

  new xstrands = taskvar.strands(INT cap, [STRING joiner], [STRING prefix], [STRING postfix])

            new xstrands = taskvar.strands(
               INT cap=0,
               [STRING joiner],
               [STRING prefix],
               [STRING postfix]
            )

Construct a STRANDS collector task variable, which provides a more efficient way of constructing long strings than using the built-in += operator or repeatedly calling .set(... + .get() + ...) on a taskvar.string() variable.

The cap argument is he maximum capacity in terms of xstrands.add() calls. If zero, the capacity is dynamic and the collector is allocated on the heap. Otherwise, memory for the taskvar.strands() collector is allocated on the workspace of the respective task. When planning for a good cap value, consider the fact that added elements can themselves be STRANDS which may use up more than one cap unit.

The optional joiner argument specifies a string which is automatically inserted between added elements. It is equivalent to a prefix only added for the second and all other elements.

The optional prefix argument specifies a string which is automatically prepended to each added element. Likewise, the optional postfix argument specifies a string which is automatically appended to each added element.

If joiner, prefix or postfix are used, the internal capacity is automatically adjusted.

  VOID xstrands.add(STRING)

Add the argument to the collector.

  VOID xstrands.clear()

Clear the collector.

  STRING xstrands.get()

Return the (current) value of the collector.

SEE ALSO

vcl(7),vinyld(1)

COPYRIGHT

            Copyright 2018-2022 UPLEX Nils Goroll Systemoptimierung

            Redistribution and use in source and binary forms, with or without
            modification, are permitted provided that the following conditions are
            met:

            1. Redistributions of source code must retain the above copyright
               notice, this list of conditions and the following disclaimer.

            2. Redistributions in binary form must reproduce the above copyright
               notice, this list of conditions and the following disclaimer in the
               documentation and/or other materials provided with the
               distribution.

            THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
            "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
            LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
            A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
            HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
            SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
            LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
            DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
            THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
            (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
            OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.