.\" Man page generated from reStructuredText. . . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .TH "VMOD_SELECTOR" 3 "" "" "" .SH NAME vmod_selector \- Varnish Module for matching fixed strings, and mapping strings to backends, regexen and other data .\" . .\" NB: This file is machine generated, DO NOT EDIT! . .\" . .\" Edit ./vmod_selector.vcc and run make instead . .\" . .SH SYNOPSIS .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C import selector; # Set creation new = selector.set([BOOL case_sensitive] [, BOOL allow_overlaps]) VOID .add(STRING [, STRING string] [, REGEX regex] [, BACKEND backend] [, INT integer] [, BOOL bool] [, SUB sub]) VOID .create_stats() # Matching BOOL .match(STRING) BOOL .hasprefix(STRING) # Match properties INT .nmatches() BOOL .matched([INT n] [, STRING element] [, ENUM select]) INT .which([ENUM select] [, STRING element]) BOOL .check_call([INT n] [, STRING element] [, ENUM select]) # Retrieving objects by index, by string, or after match STRING .element([INT n] [, ENUM select]) STRING .string([INT n] [, STRING element] [, ENUM select]) BACKEND .backend([INT n] [, STRING element] [, ENUM select]) INT .integer([INT n] [, STRING element] [, ENUM select]) BOOL .bool([INT n] [, STRING element] [, ENUM select]) BOOL .re_match(STRING [, INT n] [, STRING element] [, ENUM select]) STRING .sub(STRING text, STRING rewrite [, BOOL all] [, INT n] [, STRING element] [, ENUM select]) SUB .subroutine([INT n] [, STRING element] [, ENUM select]) # VMOD version STRING selector.version() .ft P .fi .UNINDENT .UNINDENT .SH DESCRIPTION .sp Varnish Module (VMOD) for matching strings against sets of fixed strings. A VMOD object may also function as an associative array, mapping the matched string to one or more of a backend, another string, an integer, or a regular expression. The string may also map to a subroutine that can be invoked with \fBcall\fP\&. .sp The VMOD is intended to support a variety of use cases that are typical for VCL deployments, such as: .INDENT 0.0 .IP \(bu 2 Determining the backend based on the Host header or the prefix of the URL. .IP \(bu 2 Rewriting the URL or a header. .IP \(bu 2 Generating redirect responses, based on a header or the URL. .IP \(bu 2 Permitting or rejecting request methods. .IP \(bu 2 Matching the Basic Authentication credentials in an Authorization request header. .IP \(bu 2 Matching media types in the Content\-Type header of a backend response to determine if the content is compressible. .IP \(bu 2 Accessing data by string match, as in an associative array, or by numeric index, as in a standard array. .IP \(bu 2 Dispatching subroutine calls based on string matches. .IP \(bu 2 Executing conditional logic that depends on features of the request or response that can be determined by matching headers or URLs. .UNINDENT .sp Operations such as these are commonly implemented in native VCL with an \fBif\-elsif\-elsif\fP sequence of string comparisons or regex matches. As the number of matches increases, such a sequence becomes cumbersome and scales poorly \-\- the time needed to execute the sequence increases with the number of matches to be performed. .sp With the VMOD, the strings to be matched are declared in a tabular form in \fBvcl_init\fP, and the operation is executed in a few lines. For example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C import selector; # Assume that you have defined these subroutines to execute logic # in vcl_recv for URLs beginning with /foo/, /bar/ or /baz/. sub foo { # ... } sub bar { # ... } sub baz { # ... } sub vcl_init { # Requests for URLs with these prefixes will be sent to the # associated backend. In vcl_recv, the associated subroutine # will be called. new url_prefix = selector.set(); url_prefix.add("/foo/", backend=foo_backend, sub=foo); url_prefix.add("/bar/", backend=bar_backend, sub=bar); url_prefix.add("/baz/", backend=baz_backend, sub=baz); # For requests with these Host headers, generate a redirect # response, using the associated string to construct the # Location header, and the integer to set the response code. new redirect = selector.set(); redirect.add("www.foo.com", string="/foo", integer=301); redirect.add("www.bar.com", string="/bar", integer=302); redirect.add("www.baz.com", string="/baz", integer=303); redirect.add("www.quux.com", string="/quux", integer=307); # Requests for these URLs are rewritten by altering the # query string, using the associated regex for a # substitution operation, each of which removes a # parameter. new rewrite = selector.set(); rewrite.add("/alpha/beta", regex="(\e?.*)\ebfoo=[^&]+&?(.*)$"); rewrite.add("/delta/gamma", regex="(\e?.*)\ebbar=[^&]+&?(.*)$"); rewrite.add("/epsilon/zeta", regex="(\e?.*)\ebbaz=[^&]+&?(.*)$"); } sub vcl_recv { # .match() returns true if the Host header exactly matches # one of the strings in the set. if (redirect.match(req.http.Host)) { # .string() returns the string added to the set above with # the \(aqstring\(aq parameter, for the string that was # matched. We use it to construct a Location header, which # will be retrieved in vcl_synth below to construct the # redirect response. # # .integer() returns the integer added to the set with the # \(aqinteger\(aq parameter, for the string that was matched. We # use it as the argument of synth() to set the response # status (one of the redirect status codes). set req.http.Location = "http://other.com" + redirect.string() + req.url; return (synth(redirect.integer())); } # If the URL matches the rewrite set, change the query string by # applying a substitution using the associated regex (removing a # query parameter). if (rewrite.match(req.url)) { set req.url = rewrite.sub(req.url, "\e1\e2"); } # If the URL has a prefix in the url_prefix set, call the # associated subroutine. if (url_prefix.hasprefix(req.url)) { call url_prefix.subroutine(); } } sub vcl_synth { # We come here when Host matched the redirect set in vcl_recv # above. Set the Location response header from the request header # set in vcl_recv. if (req.http.Location && resp.status >= 301 && resp.status <= 307) { set resp.http.Location = req.http.Location; return (deliver); } } sub vcl_backend_fetch { # The .hasprefix() method returns true if the URL has a prefix # in the set. if (url_prefix.hasprefix(bereq.url)) { # .backend() returns the backend associated with the # string in the set that was matched as a prefix. set bereq.backend = url_prefix.backend(); } } .ft P .fi .UNINDENT .UNINDENT .sp Matches with the \fB\&.match()\fP and \fB\&.hasprefix()\fP methods scale well as the number of strings in the set increases. Experience has shown that both operations are predictable and fast for large sets of strings. .sp When new strings are added to a set (with new \fB\&.add()\fP statements in \fBvcl_init\fP), the VCL code that executes the various operations (rewrites, backend assignment and so forth) can remain unchanged. So the VMOD can contribute to better code maintainability. .sp Matches with \fB\&.match()\fP and \fB\&.hasprefix()\fP are fixed string matches; characters such as wildcards and regex metacharacters are matched literally, and have no special meaning. Regex operations such as matching or substitution can be performed after set matches, using the regex saved with the \fBregex\fP parameter. But if you need to match against sets of patterns, consider using the set interface of \fI\%VMOD re2\fP, which provides techniques similar to the present VMOD. .sp The limited expressiveness of strings to be matched means that this VMOD can implement fast algorithms. While regexen and a VMOD like re2 can be used to match fixed strings and prefixes, the matching operations of VMOD selector are orders of magnitude faster. That in turn contributes to scalability by consuming less CPU time for matches. So if your use case allows matches against strings without patterns, prefer the use of this VMOD. .SS Selecting matched elements of a set .sp The \fB\&.match()\fP operation is an exact, fixed string match, and hence always matches exactly one string in the set if it succeeds. With \fB\&.hasprefix()\fP, more than one string in the set may be matched, if the set includes strings that are prefixes of other strings in the same set: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C sub vcl_init { new myset = selector.set(); myset.add("/foo/"); # element 1 myset.add("/foo/bar/"); # element 2 myset.add("/foo/bar/baz/"); # element 3 } sub vcl_recv { # With .hasprefix(), a URL such as /foo/bar/baz/quux matches all # 3 elements in the set. if (myset.hasprefix(req.url)) { # ... } } .ft P .fi .UNINDENT .UNINDENT .sp Just calling \fB\&.hasprefix()\fP may be sufficient if all that matters is whether a string has any prefix that appears in the set. But for some uses it may be necessary to identify one matching element of the set; this is done in particular for the methods that retrieve data associated with a specific set element. For such cases, the method parameters \fBINT n\fP, \fBSTRING element\fP and \fBENUM select\fP are used to choose a matched element. .sp As indicated in the example, elements of a set are implicitly numbered in the order in which they were added to the set using the \fB\&.add()\fP method, starting from 1. In all of the following, the \fBn\fP, \fBelement\fP and \fBselect\fP parameters for a method call are evaluated as follows: .INDENT 0.0 .IP \(bu 2 If \fBn\fP >= 1, then the \fBn\fP\-th element of the set is chosen, and the \fBelement\fP and \fBselect\fP parameters have no effect. A method with \fBn\fP >= 1 can be called in any context, and does not depend on prior match operations. This is essentially a lookup by index. .IP \(bu 2 If \fBn\fP is greater than the number of elements in the set, the method invokes VCL failure (see \fI\%ERRORS\fP). .IP \(bu 2 If \fBn\fP <= 0 and the \fBelement\fP parameter is set, then the VMOD searches for the string specified by \fBelement\fP, in the same way that the \fB\&.match()\fP method is executed. This is in essence a lookup in an associative array. .sp If \fBelement\fP is set but the lookup fails, that is if there is no such element in the set, then VCL failure is invoked, with the string "no such element" in the \fBVCL_Error\fP log message. .sp If the lookup for the \fBelement\fP succeeds, then the successful match establishes a match context for subsequent code. That means that the rules presently described can be applied again, as if \fB\&.match()\fP had returned \fBtrue\fP for the \fBelement\fP (internally, that is in fact what happens). .sp The internal match against \fBelement\fP is case sensitive if and only if the \fBcase_sensitive\fP flag was \fBtrue\fP in the set constructor (this is the default). .sp \fBn\fP is 0 by default, so it can be left out of the method call when \fBelement\fP is set. .IP \(bu 2 If \fBn\fP <= 0 and \fBelement\fP is unset, then the \fBselect\fP parameter is used to choose an element based on the most recent \fB\&.match()\fP or \fB\&.hasprefix()\fP call for the same set object in the same task scope; that is, the most recent call in the same client or backend context. Thus a method call in one of the \fBvcl_backend_*\fP subroutines refers back to the most recent \fB\&.match()\fP or \fB\&.hasprefix()\fP invocation in the same backend context. .sp By default, \fBn\fP is 0 and \fBelement\fP is unset, so both of them can be left out of the call to use \fBselect\fP\&. .IP \(bu 2 If \fBn\fP <= 0 and \fBelement\fP is unset, and neither of \fB\&.match()\fP or \fB\&.hasprefix()\fP has been called for the same set object in the same task scope, or if the most recent call resulted in a failed match, then the method invokes VCL failure. .IP \(bu 2 When \fBn\fP <= 0 and \fBelement\fP is unset after a successful \fB\&.match()\fP call, then for any value of \fBselect\fP, the element chosen is the one that matched. .IP \(bu 2 When \fBn\fP <= 0 and \fBelement\fP is unset after a successful \fB\&.hasprefix()\fP call, then the value of \fBselect\fP determines the element chosen, as follows: .INDENT 2.0 .IP \(bu 2 \fBUNIQUE\fP (default): if exactly one element of the set matched, choose that element. The method invokes VCL failure in this case if more than one element matched. .sp Since the defaults for \fBn\fP and \fBselect\fP are 0 and \fBUNIQUE\fP, and \fBelement\fP is unset by default, \fBselect=UNIQUE\fP is in effect if all three parameters are left out of the method call. .IP \(bu 2 \fBEXACT\fP: if one of the elements in the set matched exactly (even if other prefixes in the set matched as well), choose that element. VCL failure is invoked if there was no exact match. .sp Thus if a prefix match for \fB/foo/bar\fP is run against a set containing \fB/foo\fP and \fB/foo/bar\fP, the latter element is chosen with \fBselect=EXACT\fP\&. .IP \(bu 2 \fBFIRST\fP: choose the first element in the set that matched (in the order in which they were added with \fB\&.add()\fP). .IP \(bu 2 \fBLAST\fP: choose the last element in the set that matched. .IP \(bu 2 \fBSHORTEST\fP: choose the shortest element in the set that matched. .IP \(bu 2 \fBLONGEST\fP: choose the longest element in the set that matched. .UNINDENT .UNINDENT .sp So for sets of strings with common prefixes, a strategy for selecting the matched element after a prefix match can be implemented by ordering the strings added to the set, by choosing only an exact match or the longest match, and so on: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # In this example, we set the backend for a fetch based on the most # specific matching prefix of the URL, i.e. the longest prefix in # the URL that appears in the set. sub vcl_init { new myset = selector.set(); myset.add("/foo/", backend=foo_backend); myset.add("/foo/bar/", backend=bar_backend); myset.add("/foo/bar/baz/", backend=baz_backend); } sub vcl_backend_fetch { if (myset.hasprefix(bereq.url)) { set bereq.backend = myset.backend(select=LONGEST); } } # This sets baz_backend for /foo/bar/baz/quux # bar_backend for /foo/bar/quux # foo_backend for /foo/quux .ft P .fi .UNINDENT .UNINDENT .sp To re\-state the rules more informally: .INDENT 0.0 .IP \(bu 2 Use only one of \fBn\fP, \fBelement\fP or \fBselect\fP to select a string in the set. .INDENT 2.0 .IP \(bu 2 If \fBn\fP > 0, use \fBn\fP\&. \fBn\fP = 0 by default. .IP \(bu 2 Otherwise if \fBelement\fP is set, use \fBelement\fP\&. \fBelement\fP is unset by default. .IP \(bu 2 Otherwise use \fBselect\fP, default \fBUNIQUE\fP\&. .UNINDENT .IP \(bu 2 \fBn\fP is a lookup by numeric index, as implied by the order of \fB\&.add()\fP in \fBvcl_init\fP\&. .IP \(bu 2 \fBelement\fP is an associative array lookup by string. .IP \(bu 2 \fBselect\fP refers back to the previous invocation of \fB\&.match()\fP or \fB\&.hasprefix()\fP\&. .INDENT 2.0 .IP \(bu 2 The value of \fBselect\fP is irrelevant (and can just as well be left out) if the prior invocation was \fB\&.match()\fP, or if it was \fB\&.hasprefix()\fP and exactly one string was found (which is always the case if strings in the set have no common prefixes). \fBselect\fP is meant to pick an element when \fB\&.hasprefix()\fP finds more than one string. .UNINDENT .UNINDENT .SS new xset = selector.set(BOOL case_sensitive, BOOL allow_overlaps) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C new xset = selector.set( BOOL case_sensitive=1, BOOL allow_overlaps=1 ) .ft P .fi .UNINDENT .UNINDENT .sp Create a set object. .sp When \fBcase_sensitive\fP is \fBfalse\fP, matches using the \fB\&.match()\fP and \fB\&.hasprefix()\fP methods are case\-insensitive. By default, \fBcase_sensitive\fP is \fBtrue\fP\&. .sp When \fBallow_overlaps\fP is \fBfalse\fP, the VCL load fails if any string added to the set is a prefix of another string in the set. This can be used to ensure that methods using the \fBselect=UNIQUE\fP enum will always succeed after \fB\&.hasprefix()\fP matches (and to fail fast if the restriction is not met). By default, \fBallow_overlaps\fP is \fBtrue\fP\&. .sp The initialization of a set is completed when \fBvcl_init\fP finishes, or when the deprecated \fB\&.compile()\fP method is called. This prepares the set for use with the strings added with the \fB\&.add()\fP method described below. The VCL load fails if: .INDENT 0.0 .IP \(bu 2 The same string is added to the same set more than once (that string is included in the error message). .IP \(bu 2 The set contains a string that is a prefix of another string in the same set, but \fBallow_overlaps\fP was set to \fBfalse\fP in the constructor. .UNINDENT .sp Set initialization may also fail due to conditions such as out of memory. .sp If no strings were added to the set before \fBvcl_init\fP finishes or \fB\&.compile()\fP is invoked, the VCL load will not fail, but all match operations on the set will fail. In that case, a warning is emitted to the log with the \fBVCL_Error\fP tag. Since that happens outside of any request/response transaction, the error message can only be seen when a tool like \fBvarnishlog(1)\fP is used with raw grouping (\fB\-g raw\fP). .sp Examples: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C sub vcl_init { # By default, matches are case\-sensitive, and overlapping # prefixes are permitted. new myset = selector.set(); # ... # For case\-insensitive matching. new caseless = selector.set(case_sensitive=false); # ... # Forbid overlapping prefixes. new allunique = selector.set(allow_overlaps=false); # ... } .ft P .fi .UNINDENT .UNINDENT .SS VOID xset.add(STRING, [STRING string], [REGEX regex], [BACKEND backend], [INT integer], [BOOL bool], [SUB sub]) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C VOID xset.add( STRING, [STRING string], [REGEX regex], [BACKEND backend], [INT integer], [BOOL bool], [SUB sub] ) .ft P .fi .UNINDENT .UNINDENT .sp 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 with \fB\&.add()\fP, starting with 1. .sp If values are set for any of the following optional parameters, then those values are associated with this element, and can be retrieved with the method shown in the second column. The retrieval methods are documented below. .TS center; |l|l|. _ T{ \fB\&.add()\fP parameter T} T{ Retrieval methods T} _ T{ \fBstring\fP T} T{ \fB\&.string()\fP T} _ T{ \fBregex\fP T} T{ \fB\&.re_match()\fP, \fB\&.sub()\fP T} _ T{ \fBbackend\fP T} T{ \fB\&.backend()\fP T} _ T{ \fBinteger\fP T} T{ \fB\&.integer()\fP T} _ T{ \fBbool\fP T} T{ \fB\&.bool()\fP T} _ T{ \fBsub\fP T} T{ \fB\&.subroutine()\fP T} _ .TE .sp A regular expression in the \fBregex\fP parameter is compiled at VCL load time. If the compile fails, then the VCL load fails with an error message. Regular expressions are evaluated exactly as native regexen in VCL. .sp A VCL subroutine specified by the \fBsub\fP parameter MUST be defined \fIprior\fP to the definition of \fBvcl_init\fP in which \fB\&.add()\fP is invoked. The VCL compiler does not support forward definitions for this purpose. .sp \fB\&.add()\fP invokes VCL failure if it is called in any subroutine besides \fBvcl_init\fP\&. The VCL load fails if: .INDENT 0.0 .IP \(bu 2 The string to be added is NULL. .IP \(bu 2 A regular expression in the \fBregex\fP parameter fails to compile. .IP \(bu 2 A subroutine specified by the \fBsub\fP parameter was not defined previously in the VCL source. .IP \(bu 2 The deprecated \fB\&.compile()\fP method has already been called. .UNINDENT .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C sub my_quux_sub { set req.http.Quux = "xyzzy"; } sub vcl_init { new myset = selector.set(); myset.add("www.foo.com"); myset.add("www.bar.com", string="/bar"); myset.add("www.baz.com", string="/baz", backend=baz_backend); myset.add("www.quux.com", string="/quux", backend=quux_backend, regex="^/quux/([^/]+)/", sub=my_quux_sub); } .ft P .fi .UNINDENT .UNINDENT .SS VOID xset.compile() .sp \fBThis method is deprecated\fP, and will be removed in a future version. \fB\&.compile()\fP may be omitted, since compilation happens automatically when \fBvcl_init\fP finishes. .sp \fB\&.compile()\fP compiles the set. This is done after all of the strings have been added. .sp \fB\&.compile()\fP invokes VCL failure if it is called in any subroutine besides \fBvcl_init\fP\&. The VCL load may fail for the same reasons described for set initialization above, or if \fB\&.compile()\fP is invoked more than once. .SS VOID xset.create_stats() .sp Create statistics counters for this object that are displayed by tools such as \fBvarnishstat(1)\fP\&. See \fI\%STATISTICS\fP for details. It must be called in \fBvcl_init\fP\&. No statistics are created for a set object if \fB\&.create_stats()\fP is not invoked. .sp \fB\&.create_stats()\fP invokes VCL failure if it is called in any VCL subroutine besides \fBvcl_init\fP\&. .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C sub vcl_init { new myset = selector.set(); myset.add("foo"); myset.add("bar"); myset.add("baz"); myset.create_stats(); } .ft P .fi .UNINDENT .UNINDENT .SS BOOL xset.match(STRING) .sp Returns \fBtrue\fP if the given STRING exactly matches one of the strings in the set. The match is case insensitive if and only if the parameter \fBcase_sensitive\fP was set to \fBfalse\fP in the set constructor (matches are case sensitive by default). .sp \fB\&.match()\fP invokes VCL failure if: .INDENT 0.0 .IP \(bu 2 No strings were added to the set. .IP \(bu 2 There is insufficient workspace for internal operations. .UNINDENT .sp If the string to be matched is NULL, for example when an unset header is unspecified, then \fB\&.match()\fP returns \fBfalse\fP, and a warning is emitted to the log with the \fBNotice\fP header (see \fI\%LOGGING\fP). This is because a match against an unset header may or may not have been intentional. .sp If you need to distinguish whether or not the header exists when using \fB\&.match()\fP, you can evaluate the header in boolean context: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C if (!myset.match(req.http.Foo)) { # Either there is no such header in the client request, or # the header does not match the set. # ... } if (req.http.Foo && !myset.match(req.http.Foo)) { # The header exists, but does not match the set. # ... } .ft P .fi .UNINDENT .UNINDENT .SS BOOL xset.hasprefix(STRING) .sp Returns \fBtrue\fP if the STRING to be matched has a prefix that is in the set. The match is case insensitive if \fBcase_sensitive\fP was set to \fBfalse\fP in the constructor. .sp \fB\&.hasprefix()\fP invokes VCL failure under the same conditions given for \fB\&.match()\fP above. Like \fB\&.match()\fP, \fB\&.hasprefix()\fP returns \fBfalse\fP if the string to be matched is NULL, for example if it is an unset header, and a \fBNotice\fP message is emitted to the log (see \fI\%LOGGING\fP). .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C if (myset.hasprefix(req.url)) { call do_if_prefix_matched; } .ft P .fi .UNINDENT .UNINDENT .SS INT xset.nmatches() .sp Returns the number of elements that were matched by the most recent successful invocation of \fB\&.match()\fP or \fB\&.hasprefix()\fP for the same set object in the same task scope (that is, in the same client or backend context). .sp \fB\&.nmatches()\fP returns 0 after either of \fB\&.match()\fP or \fB\&.hasprefix()\fP returned \fBfalse\fP, and it returns 1 after \fB\&.match()\fP returned \fBtrue\fP\&. After a successful \fB\&.hasprefix()\fP call, it returns the number of strings in the set that are prefixes of the string that was matched. .sp \fB\&.nmatches()\fP invokes VCL failure if there was no prior invocation of \fB\&.match()\fP or \fB\&.hasprefix()\fP in the same task scope. .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # For a use case that requires a unique prefix match, use # .nmatches() to ensure that there was exactly one match, and fail # fast with VCL failure otherwise. if (myset.hasprefix(bereq.url)) { if (myset.nmatches() != 1) { std.log(bereq.url + " matched > 1 prefix in the set"); return (fail); } set bereq.backend = myset.backend(select=UNIQUE); } .ft P .fi .UNINDENT .UNINDENT .SS BOOL xset.matched(INT n, STRING element, ENUM select) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C BOOL xset.matched( INT n=0, STRING element=0, ENUM {UNIQUE, EXACT, FIRST, LAST, SHORTEST, LONGEST} select=UNIQUE ) .ft P .fi .UNINDENT .UNINDENT .sp After a successful \fB\&.match()\fP or \fB\&.hasprefix()\fP call for the same set object in the same task scope, return \fBtrue\fP if the element indicated by the \fBn\fP, \fBelement\fP and \fBselect\fP parameters was matched, according to the rules described above. .sp For example if \fBn\fP > 0, \fB\&.matched(n)\fP returns \fBtrue\fP if and only if the \fBn\fP\-th element matched. The numbering corresponds to the order of \fB\&.add()\fP invocations in \fBvcl_init\fP (starting from 1). The \fBselect\fP and \fBelement\fP parameters are ignored in this case. .sp If \fBn\fP <= 0 and \fBelement\fP is set, then \fB\&.matched()\fP returns \fBtrue\fP if and only if the string specified by \fBelement\fP was matched in the previous successful \fB\&.match()\fP or \fB\&.hasprefix()\fP call. If \fBelement\fP is not in the set, then \fB\&.matched()\fP does not invoke VCL failure (this is a deviation from the general rules for \fBelement\fP), but \fB\&.matched()\fP always returns \fBfalse\fP in that case. Thus \fB\&.matched()\fP can always used with \fBelement\fP to safely check if a string was previously matched, regardless of whether the string is in the set. .sp \fBn\fP defaults to 0, so the \fBn\fP parameter can be left out if \fBelement\fP is set. .sp If \fBn\fP <= 0 and \fBelement\fP is unset, the set element is determined by the \fBselect\fP enum. In that case, \fB\&.matched()\fP returns \fBtrue\fP if and only if the element indicated by the enum was matched by the previous successful match operation. These distinctions are only relevant if the previous operation was \fB\&.hasprefix()\fP, and more than one string was matched due to overlapping prefixes. \fB\&.matched()\fP returns \fBtrue\fP for all values of \fBselect\fP if the previous successful operation was \fB\&.match()\fP\&. .sp \fBn\fP defaults to 0 and \fBelement\fP is unset by default, so the \fBn\fP and \fBelement\fP parameters can be left out if the use of \fBselect\fP is intended. .sp If \fBn\fP <= 0, \fBelement\fP is unset, and \fBselect\fP is \fBUNIQUE\fP or \fBEXACT\fP, then \fB\&.matched()\fP returns \fBtrue\fP if the enum\(aqs criteria are met; otherwise it returns \fBfalse\fP, and does not fail. This can be used as a safeguard for the methods described below, which invoke VCL failure if either of these two enums are specified, but their criteria are not met. .sp The other enum values (\fBFIRST\fP, \fBLAST\fP, \fBSHORTEST\fP and \fBLONGEST\fP) are included for consistency with the other methods, but they don\(aqt make a relevant distinction. If the prior invocation of \fB\&.match()\fP or \fB\&.hasprefix()\fP was successful (returned \fBtrue\fP), then \fB\&.matched()\fP returns \fBtrue\fP for each of these, since there is always an element that meets the criteria. .sp \fB\&.matched()\fP always returns \fBfalse\fP if the most recent \fB\&.match()\fP or \fB\&.hasprefix()\fP call returned \fBfalse\fP\&. .sp \fB\&.matched()\fP invokes VCL failure if: .INDENT 0.0 .IP \(bu 2 The \fBn\fP parameter is out of range \-\- greater than the number of elements in the set. .IP \(bu 2 There was no prior invocation of \fB\&.match()\fP or \fB\&.hasprefix()\fP in the same task scope. .UNINDENT .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C if (hosts.match(req.http.Host)) { if (hosts.matched(1)) { call do_if_the_first_host_element_matched; } } if (url_prefixes.hasprefix(req.url)) { if (urls.matched(select=UNIQUE)) { call do_if_a_unique_url_prefix_was_matched; } } if (url_prefixes.hasprefix(bereq.url)) { if (urls.matched(element="/foo/")) { call do_if_foo_was_matched; } } .ft P .fi .UNINDENT .UNINDENT .SS INT xset.which(ENUM select, STRING element) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C INT xset.which( ENUM {UNIQUE, EXACT, FIRST, LAST, SHORTEST, LONGEST} select=UNIQUE, STRING element=0 ) .ft P .fi .UNINDENT .UNINDENT .sp Return the index of the element indicated by \fBelement\fP or \fBselect\fP\&. The numbering corresponds to the order of \fB\&.add()\fP calls in \fBvcl_init\fP, starting from 1. .sp If the \fBelement\fP parameter is set, then return the numeric index for that string in the set. .sp If \fBelement\fP is unset, then the index is chosen with the \fBselect\fP parameter, and refers to the previous \fB\&.match()\fP or \fB\&.hasprefix()\fP call for the same set object in the same task scope, according to the rules given above. By default, \fBselect\fP is \fBUNIQUE\fP\&. .sp If \fBelement\fP is unset, and the most recent \fB\&.match()\fP or \fB\&.hasprefix()\fP call returned \fBfalse\fP, return 0. .sp \fB\&.which()\fP invokes VCL failure if: .INDENT 0.0 .IP \(bu 2 The choice of \fBelement\fP or \fBselect\fP indicates failure, as documented above; that is, if \fBelement\fP is a string that is not in the set, or \fBselect\fP is \fBUNIQUE\fP or \fBEXACT\fP, but there was no unique or exact match, respectively. .IP \(bu 2 There was no prior invocation of \fB\&.match()\fP or \fB\&.hasprefix()\fP in the same task scope. .UNINDENT .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C if (myset.hasprefix(req.url)) { if (myset.which(select=SHORTEST) > 1) { call do_if_the_shortest_match_was_not_the_first_element; } } if (myset.which(element=bereq.url) == 1) { call do_if_the_url_was_the_first_element; } .ft P .fi .UNINDENT .UNINDENT .SS STRING xset.element(INT n, ENUM select) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C STRING xset.element( INT n=0, ENUM {UNIQUE, EXACT, FIRST, LAST, SHORTEST, LONGEST} select=UNIQUE ) .ft P .fi .UNINDENT .UNINDENT .sp Returns the element of the set indicated by the \fBn\fP and \fBselect\fP parameters as described above. Thus if \fBn\fP >= 1, the \fBn\fP\-th element of the set is returned; otherwise the matched element indicated by \fBselect\fP is returned after calling \fB\&.match()\fP or \fB\&.hasprefix()\fP\&. .sp The string returned is the same as it was added to the set; even if a prior match was case insensitive, and the matched string differs in case, the string with the case as added to the set is returned. .sp \fB\&.element()\fP invokes VCL failure if the rules for \fBn\fP and \fBselect\fP indicate failure; that is: .INDENT 0.0 .IP \(bu 2 \fBn\fP is out of range (greater than the number of elements in the set) .IP \(bu 2 \fBn\fP < 1 and \fBselect\fP fails for \fBUNIQUE\fP or \fBEXACT\fP .IP \(bu 2 \fBn\fP < 1 and there was no prior invocation of \fB\&.match()\fP or \fB\&.hasprefix()\fP\&. .UNINDENT .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C if (myset.hasprefix(req.url)) { # Construct a redirect response for another host, using the # matching prefix in the request URL as the new URL path. set resp.http.Location = "http://other.com" + myset.element(); } .ft P .fi .UNINDENT .UNINDENT .SS BACKEND xset.backend(INT n, STRING element, ENUM select) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C BACKEND xset.backend( INT n=0, STRING element=0, ENUM {UNIQUE, EXACT, FIRST, LAST, SHORTEST, LONGEST} select=UNIQUE ) .ft P .fi .UNINDENT .UNINDENT .sp Returns the backend associated with the element of the set indicated by \fBn\fP, \fBelement\fP and \fBselect\fP, according to the rules given above; that is, it returns the backend that was set via the \fBbackend\fP parameter in \fB\&.add()\fP\&. .sp \fB\&.backend()\fP invokes VCL failure if: .INDENT 0.0 .IP \(bu 2 The rules for \fBn\fP, \fBelement\fP and \fBselect\fP indicate failure. .IP \(bu 2 No backend was set with the \fBbackend\fP parameter in the \fB\&.add()\fP call corresponding to the selected element. .UNINDENT .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C if (myset.hasprefix(bereq.url)) { # Set the backend associated with the string in the set that # forms the longest prefix of the URL set bereq.backend = myset.backend(select=LONGEST); } .ft P .fi .UNINDENT .UNINDENT .SS STRING xset.string(INT n, STRING element, ENUM select) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C STRING xset.string( INT n=0, STRING element=0, ENUM {UNIQUE, EXACT, FIRST, LAST, SHORTEST, LONGEST} select=UNIQUE ) .ft P .fi .UNINDENT .UNINDENT .sp Returns the string set by the \fBstring\fP parameter for the element of the set indicated by \fBn\fP, \fBelement\fP and \fBselect\fP, according to the rules given above. .sp \fB\&.string()\fP invokes VCL failure if: .INDENT 0.0 .IP \(bu 2 The rules for \fBn\fP, \fBelement\fP and \fBselect\fP indicate failure. .IP \(bu 2 No string was set with the \fBstring\fP parameter in \fB\&.add()\fP\&. .UNINDENT .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # Rewrite the URL if it matches one of the strings in the set. if (myset.match(req.url)) { set req.url = myset.string(); } .ft P .fi .UNINDENT .UNINDENT .SS INT xset.integer(INT n, STRING element, ENUM select) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C INT xset.integer( INT n=0, STRING element=0, ENUM {UNIQUE, EXACT, FIRST, LAST, SHORTEST, LONGEST} select=UNIQUE ) .ft P .fi .UNINDENT .UNINDENT .sp Returns the integer set by the \fBinteger\fP parameter for the element of the set indicated by \fBn\fP, \fBelement\fP and \fBselect\fP, according to the rules given above. .sp \fB\&.integer()\fP invokes VCL failure if: .INDENT 0.0 .IP \(bu 2 The rules for \fBn\fP, \fBelement\fP and \fBselect\fP indicate failure. .IP \(bu 2 No integer was set with the \fBinteger\fP parameter in \fB\&.add()\fP\&. .UNINDENT .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # Send a synthetic response if the URL has a prefix in the set, # using the response code set in .add(). if (myset.hasprefix(req.url)) { # Check .nmatches() to ensure that select=UNIQUE can be used # without risk of VCL failure. if (myset.nmatches() == 1) { return( synth(myset.integer(select=UNIQUE)) ); } } .ft P .fi .UNINDENT .UNINDENT .SS BOOL xset.bool(INT n, STRING element, ENUM select) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C BOOL xset.bool( INT n=0, STRING element=0, ENUM {UNIQUE, EXACT, FIRST, LAST, SHORTEST, LONGEST} select=UNIQUE ) .ft P .fi .UNINDENT .UNINDENT .sp Returns the boolean value set by the \fBbool\fP parameter for the element of the set indicated by \fBn\fP, \fBelement\fP and \fBselect\fP, according to the rules given above. .sp \fB\&.bool()\fP invokes VCL failure if: .INDENT 0.0 .IP \(bu 2 The rules for \fBn\fP, \fBelement\fP and \fBselect\fP indicate failure. .IP \(bu 2 No boolean was set with the \fBbool\fP parameter in \fB\&.add()\fP\&. .UNINDENT .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # 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; } } } .ft P .fi .UNINDENT .UNINDENT .SS BOOL xset.re_match(STRING subject, INT n, STRING element, ENUM select) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C BOOL xset.re_match( STRING subject, INT n=0, STRING element=0, ENUM {UNIQUE, EXACT, FIRST, LAST, SHORTEST, LONGEST} select=UNIQUE ) .ft P .fi .UNINDENT .UNINDENT .sp Using the regular expression set by the \fBregex\fP parameter for the element of the set indicated by \fBn\fP, \fBelement\fP and \fBselect\fP, return the result of matching the regex against \fBsubject\fP\&. The regex match is the same operation performed for the native VCL \fB~\fP operator, see vcl(7). .sp In other words, this method can be used to perform a second match with the saved regular expression, after matching a fixed string against the set. .sp The regex match is subject to the same conditions imposed for matching in native VCL; in particular, it may be limited by the varnishd parameters \fBpcre_match_limit\fP and \fBpcre_match_limit_recursion\fP (see varnishd(1)). .sp \fB\&.re_match()\fP invokes VCL failure if: .INDENT 0.0 .IP \(bu 2 The rules for \fBn\fP, \fBelement\fP and \fBselect\fP indicate failure. .IP \(bu 2 No regular expression was set with the \fBregex\fP parameter in \fB\&.add()\fP\&. .UNINDENT .sp The regex match may fail for any of the reasons that cause a native match to fail. In that case, \fB\&.re_match()\fP returns \fBfalse\fP, and a log message with tag \fBVCL_Error\fP is emitted (as for native regeex match failures). .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # If the Host header exactly matches a string in the set, perform a # regex match against the URL. if (myset.match(req.http.Host)) { if (myset.re_match(req.url)) { call do_if_the_URL_matches_the_regex_for_Host; } } .ft P .fi .UNINDENT .UNINDENT .SS STRING xset.sub(STRING str, STRING sub, BOOL all, INT n, STRING element, ENUM select) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C STRING xset.sub( STRING str, STRING sub, BOOL all=0, INT n=0, STRING element=0, ENUM {UNIQUE, EXACT, FIRST, LAST, SHORTEST, LONGEST} select=UNIQUE ) .ft P .fi .UNINDENT .UNINDENT .sp Using the regular expression set by the \fBregex\fP parameter for the element of the set indicated by \fBn\fP, \fBelement\fP and \fBselect\fP, return the result of a substitution using \fBstr\fP and \fBsub\fP\&. .sp Note that the method name "sub" refers to string substitution. To retrieve the subroutine set with the \fBsub\fP parameter in \fB\&.add()\fP, use the \fB\&.subroutine()\fP method documented below. .sp If \fBall\fP is \fBfalse\fP, then return the result of replacing the first portion of \fBstr\fP that matches the regex with \fBsub\fP\&. \fBsub\fP may contain backreferences \fB\e0\fP through \fB\e9\fP, to include captured substrings from \fBstr\fP in the substitution. This is the same operation performed by the native VCL function \fBregsub(str, regex, sub)\fP (see vcl(7)). By default, \fBall\fP is false. .sp If \fBall\fP is \fBtrue\fP, return the result of replacing each non\-overlapping portion of \fBstr\fP that matches the regex with \fBsub\fP (possibly with backreferences). This is the same operation as native VCL\(aqs \fBregsuball(str, regex, sub)\fP\&. .sp \fB\&.sub()\fP invokes VCL failure if: .INDENT 0.0 .IP \(bu 2 The rules for \fBn\fP, \fBelement\fP and \fBselect\fP indicate failure. .IP \(bu 2 No regular expression was set with the \fBregex\fP parameter in \fB\&.add()\fP\&. .UNINDENT .sp The substitution may fail for any of the reasons that cause native \fBregsub()\fP or \fBregsuball()\fP to fail. In that case, \fB\&.sub()\fP returns \fBstr\fP, and a \fBVCL_Error\fP message is written to the log, as for failures of native match substitution functions. As with the native functions, \fBstr\fP is returned if \fBregex\fP does not match \fBstr\fP\&. .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # In this example we match the URL prefix, and if a match is found, # rewrite the URL by exchanging path components as indicated. sub vcl_init { new rewrite = selector.set(); rewrite.add("/foo/", regex="^(/foo)/([^/]+)/([^/]+)/"); rewrite.add("/foo/bar/", regex="^(/foo/bar)/([^/]+)/([^/]+)/"); rewrite.add("/foo/bar/baz/", regex="^(/foo/bar/baz)/([^/]+)/([^/]+)/"); } if (rewrite.hasprefix(req.url)) { set req.url = rewrite.sub(req.url, "\e1/\e3/\e2/", select=LAST); } # /foo/1/2/* is rewritten as /foo/2/1/* # /foo/bar/1/2/* is rewritten as /foo/bar/2/1/* # /foo/bar/baz/1/2/* is rewritten as /foo/bar/baz/2/1/* .ft P .fi .UNINDENT .UNINDENT .SS SUB xset.subroutine(INT n, STRING element, ENUM select) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C SUB xset.subroutine( INT n=0, STRING element=0, ENUM {UNIQUE, EXACT, FIRST, LAST, SHORTEST, LONGEST} select=UNIQUE ) .ft P .fi .UNINDENT .UNINDENT .sp Returns the subroutine set by the \fBsub\fP parameter for the element of the set indicated by \fBn\fP, \fBelement\fP and \fBselect\fP, according to the rules given above. The subroutine may be invoked with VCL \fBcall\fP\&. .sp \fBNote\fP: you must ensure that the subroutine may invoked legally in the context in which it is called. This means that: .INDENT 0.0 .IP \(bu 2 The subroutine may only refer to VCL elements that are legal in the invocation context. For example, if the subroutine only refers to headers in \fBreq.http.*\fP, then it may be called in \fBvcl_recv\fP, but not if it refers to any header in \fBresp.http.*\fP\&. See \fBvcl\-var(7)\fP for the specification of which VCL variables may be used in which contexts. .IP \(bu 2 Recursive subroutine calls are not permitted in VCL. The subroutine invocation may not appear anywhere in its own call stack. .UNINDENT .sp For standard subroutine invocations with \fBcall\fP, the VCL compiler checks these conditions and issues a compile\-time error if either one is violated. This is not possible with invocations using \fB\&.subroutine()\fP; the error can only be determined at runtime. So it is advisable to test the use of \fB\&.subroutine()\fP carefully before using it in production. You can use the \fB\&.check_call()\fP method described below to determine if the subroutine call is legal. .sp \fB\&.subroutine()\fP invokes VCL failure if: .INDENT 0.0 .IP \(bu 2 The rules for \fBn\fP, \fBelement\fP and \fBselect\fP indicate failure. .IP \(bu 2 No subroutine was set with the \fBsub\fP parameter in \fB\&.add()\fP\&. .IP \(bu 2 The subroutine is invoked with \fBcall\fP, but the call is not legal in the invocation context, for the reasons given above. .UNINDENT .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # Due to the use of resp.http.*, this subroutine may only be invoked # in vcl_deliver or vcl_synth, as documented in vcl\-var(7). Note # that subroutine definitions must appear before vcl_init to # permitted for the sub parameter in .add(). sub resp_sub { set resp.http.Call\-Me = "but only in deliver or synth"; } sub vcl_init { new myset = selector.set(); myset.add("/foo", sub=resp_sub); myset.add("/foo/bar", sub=some_other_sub); # ... } sub vcl_deliver { if (resp_sub.hasprefix(req.url)) { call resp_sub.subroutine(select=LONGEST); } } .ft P .fi .UNINDENT .UNINDENT .SS BOOL xset.check_call(INT n, STRING element, ENUM select) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C BOOL xset.check_call( INT n=0, STRING element=0, ENUM {UNIQUE, EXACT, FIRST, LAST, SHORTEST, LONGEST} select=UNIQUE ) .ft P .fi .UNINDENT .UNINDENT .sp Returns \fBtrue\fP iff the subroutine returned by \fB\&.subroutine()\fP for the element of the set indicated by \fBn\fP, \fBelement\fP and \fBselect\fP may be invoked legally in the current context. The conditions for legal invocation are documented for \fB\&.subroutine()\fP above. .sp \fB\&.check_call()\fP never invokes VCL failure, but rather returns \fBfalse\fP under conditions for which the use of \fB\&.subroutine()\fP would invoke VCL failure, as described above. In that case, a message is emitted to the Vanrish log using the \fBNotice\fP tag (the same message that would appear with the \fBVCL_Error\fP tag if the subroutine were called). .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C sub vcl_deliver { if (resp_sub.hasprefix(req.url)) { if (resp_sub.check_call(select=LONGEST)) { call resp_sub.subroutine(select=LONGEST); } else { call do_if_resp_sub_is_illegal; } } } .ft P .fi .UNINDENT .UNINDENT .SS STRING version() .sp Return the version string for this VMOD. .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C std.log("Using VMOD selector version: " + selector.version()); .ft P .fi .UNINDENT .UNINDENT .SH STATISTICS .sp When \fB\&.create_stats()\fP is invoked for a set object, statistics are created that can be viewed with a tool like varnishstat(1). .sp The stats have the following naming schema: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C SELECTOR... .ft P .fi .UNINDENT .UNINDENT .sp \&... where \fB\fP is the VCL instance name, \fB\fP is the object name, and \fB\fP is the statistic. So the \fBelements\fP stat of the \fBmyset\fP object in the VCL instance \fBboot\fP is named: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C SELECTOR.boot.myset.elements .ft P .fi .UNINDENT .UNINDENT .sp The statistics describe properties of the set, and their values are constant, never changing during the lifetime of the VCL instance. .sp Statistics provided by the VMOD include: .INDENT 0.0 .IP \(bu 2 \fBelements\fP: the number of elements in the set (added via \fB\&.add()\fP) .IP \(bu 2 \fBsetsz\fP: the total size of the strings in the set \-\- the sum of the lengths of all of the strings, including their terminating null bytes .IP \(bu 2 \fBminlen\fP: the length of the shortest string in the set .IP \(bu 2 \fBmaxlen\fP: the length of the shortest string in the set .UNINDENT .sp The remaining stats refer to properties of a set object\(aqs internal data structures, and depend on the internal implementation. The implementation may be changed in any new version of the VMOD, and hence the stats may change. These are described further in an external document (see \fI\%STATISTICS\fP in the source repository). .sp The stats for a VCL instance are removed from view when the instance is set to the cold state, and become visible again when it set to the warm state. They are removed permanently when the VCL instance is discarded (see varnish\-cli(7)). .SH ERRORS .sp The method documentation above describes illegal uses for which VCL failure is invoked. VCL failure has the same results as if \fBreturn(fail)\fP is called from a VCL subroutine: .INDENT 0.0 .IP \(bu 2 If the failure occurs in \fBvcl_init\fP, then the VCL load fails with an error message. .IP \(bu 2 If the failure occurs in any other subroutine besides \fBvcl_synth\fP, then a \fBVCL_Error\fP message is written to the log, and control is directed immediately to \fBvcl_synth\fP, with \fBresp.status\fP set to 503 and \fBresp.reason\fP set to \fB"VCL failed"\fP\&. .IP \(bu 2 If the failure occurs in \fBvcl_synth\fP, then \fBvcl_synth\fP is aborted, and the response line "503 VCL failed" is sent. .UNINDENT .sp VCL failure is meant to "fail fast" on conditions that cannot be correct, or when resource limitations such as workspace exhaustion prevent further processing. Depending on your use case, you may be able to use the VMOD\(aqs methods without additional checking and with no risk of failure. For example, if it is known that none of the strings in a set have common prefixes, then methods with \fBselect=UNIQUE\fP can be used safely after calling \fB\&.hasprefix()\fP\&. .sp If you need to check against possible failure conditions: .INDENT 0.0 .IP \(bu 2 If \fB\&.nmatches() == 1\fP, then \fBselect=UNIQUE\fP can be used safely. .IP \(bu 2 The \fBUNIQUE\fP and \fBEXACT\fP conditions can also be checked with \fB\&.matched(select=UNIQUE)\fP and \fB\&.matched(select=EXACT)\fP\&. .IP \(bu 2 The \fBallow_overlaps\fP flag can be set in the constructor, to ensure that VCL load fails if a set unintentionally has strings with common prefixes. .IP \(bu 2 In most cases, a method invokes VCL failure if the value of the \fBelement\fP parameter is not in the set. But \fBelement\fP can be used safely with any string in \fB\&.matched()\fP to check if a string matched previously \-\- \fB\&.matched()\fP returns \fBfalse\fP if the \fBelement\fP is not in the set. .IP \(bu 2 The \fB\&.check_call()\fP method may be used to avoid VCL failure if a subroutine call using \fB\&.subroutine()\fP would be illegal. .UNINDENT .sp See \fI\%LIMITATIONS\fP for considerations if you encounter conditions such as workspace exhaustion. .SH LOGGING .sp Both of \fB\&.match()\fP and \fB\&.hasprefix()\fP return \fBfalse\fP when the string to be matched is NULL, typically because an unset header was specified. Such usage may be deliberate; you might intend VCL logic to depend on whether a header either doesn\(aqt match or does not exist. But it may be an error, for example due to misspelling the header name. .sp When the string to be matched is NULL, the VMOD emits a warning to the log with the tag \fBNotice\fP, in this format: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C vmod_selector: .(): subject string is NULL .ft P .fi .UNINDENT .UNINDENT .sp \&... where \fB\fP is the object name and \fB\fP is either \fBmatch\fP or \fBhasprefix\fP\&. .sp If \fB\&.check_call()\fP returns \fBfalse\fP, indicating that the use of \fB\&.subroutine()\fP would be illegal in that context, then the VMOD emits a log meesage using \fBNotice\fP in this format: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C vmod_selector: .check_call(): .ft P .fi .UNINDENT .UNINDENT .sp \&... where \fB\fP is the object name and \fB\fP is the message that would have been logged with \fBVCL_Error\fP if the subroutine were invoked. .sp As noted above, VCL failure during request/response transactions (after successful VCL load) is logged with an error message using the \fBVCL_Error\fP tag. These messages begin with the prefix \fBvmod selector failure\fP\&. .SH REQUIREMENTS .sp The VMOD requires Varnish since version 6.6.0. See the source repository for versions of the VMOD that are compatible with released versions of Varnish. .SH INSTALLATION .sp See \fI\%INSTALL.rst\fP in the source repository. .SH LIMITATIONS .sp The VMOD uses workspace for two purposes: .INDENT 0.0 .IP \(bu 2 Saving task\-scoped data about a match with \fB\&.match()\fP and \fB\&.hasprefix()\fP, for use by the methods that retrieve information about the prior match. This data is stored separately for each object for which a match is executed. .IP \(bu 2 A copy of the string to be matched for case insensitive matches (the copy is set to all one case). .UNINDENT .sp The default workspace sizes are usually more than large enough for typical usages, but that depends on workspace consumption for other purposes. .sp If you find that methods are failing with \fBVCL_Error\fP messages indicating "out of space", consider increasing the varnishd parameters \fBworkspace_client\fP and/or \fBworkspace_backend\fP (see varnishd(1)). .sp Set objects and their internal structures are allocated from the heap, and hence are only limited by available RAM. .sp The regex methods \fB\&.re_match()\fP and \fB\&.sub()\fP use the same internal mechanisms as native VCL\(aqs \fB~\fP operator and the \fBregsub/all()\fP functions, and are subject to the same limitations. In particular, they may be limited by the varnishd parameters \fBpcre_match_limit\fP and \fBpcre_match_limit_recursion\fP, in which case they emit the same \fBVCL_Error\fP messages as the native operations. If necessary, adjust these parameters as advised in varnishd(1). .SH SEE ALSO .INDENT 0.0 .IP \(bu 2 varnishd(1) .IP \(bu 2 vcl(7) .IP \(bu 2 vcl\-var(7) .IP \(bu 2 varnishstat(1) .IP \(bu 2 varnishlog(1) .IP \(bu 2 varnish\-cli(7) .IP \(bu 2 VMOD source repository: \fI\%https://code.uplex.de/uplex\-varnish/libvmod\-selector\fP .INDENT 2.0 .IP \(bu 2 Gitlab mirror: \fI\%https://gitlab.com/uplex/varnish/libvmod\-selector\fP .UNINDENT .IP \(bu 2 \fI\%VMOD re2\fP: \fI\%https://code.uplex.de/uplex\-varnish/libvmod\-re2\fP .UNINDENT .SH COPYRIGHT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Copyright (c) 2018 UPLEX Nils Goroll Systemoptimierung All rights reserved Author: Geoffrey Simmons See LICENSE .ft P .fi .UNINDENT .UNINDENT .\" Generated by docutils manpage writer. .