.\" 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_RE2" 3 "" "" .SH NAME vmod_re2 \- Varnish Module for access to the Google RE2 regular expression engine .\" . .\" NB: This file is machine generated, DO NOT EDIT! . .\" . .\" Edit ./vmod_re2.vcc and run make instead . .\" . .SH SYNOPSIS .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C import re2; # regex object interface new OBJECT = re2.regex(STRING pattern [, ]) BOOL .match(STRING) STRING .backref(INT ref) STRING .namedref(STRING name) STRING .sub(STRING text, STRING rewrite) STRING .suball(STRING text, STRING rewrite) STRING .extract(STRING text, STRING rewrite) INT .cost() # regex function interface BOOL re2.match(STRING pattern, STRING subject [, ]) STRING re2.backref(INT ref) STRING re2.namedref(STRING name) STRING re2.sub(STRING pattern, STRING text, STRING rewrite [, ]) STRING re2.suball(STRING pattern, STRING text, STRING rewrite [, ]) STRING re2.extract(STRING pattern, STRING text, STRING rewrite [, ]) INT re2.cost(STRING pattern [, ]) # set object interface new OBJECT = re2.set([ENUM anchor] [, ]) VOID .add(STRING [, BOOL save] [, BOOL never_capture] [, STRING string] [, BACKEND backend] [, INT integer] [,SUB sub]) BOOL .match(STRING) INT .nmatches() BOOL .matched(INT) INT .which([ENUM select]) STRING .string([INT n,] [ENUM select]) BACKEND .backend([INT n,] [ENUM select]) INT .integer([INT n] [, ENUM select]) SUB .subroutine([INT n] [, ENUM select]) BOOL .check_call([INT n] [, ENUM select]) STRING .sub(STRING text, STRING rewrite [, INT n] [, ENUM select]) STRING .suball(STRING text, STRING rewrite [, INT n] [, ENUM select]) STRING .extract(STRING text, STRING rewrite [, INT n] [, ENUM select]) BOOL .saved([ENUM {REGEX, STR, BE, INT, SUB} which] [, INT n] [, ENUM select]) VOID .hdr_filter(HTTP [, BOOL]) # utility function STRING re2.quotemeta(STRING) # VMOD version STRING re2.version() .ft P .fi .UNINDENT .UNINDENT .SH DESCRIPTION .sp Varnish Module (VMOD) for access to the Google RE2 regular expression engine. .sp Varnish VCL uses the PCRE library (Perl Compatible Regular Expressions) for its native regular expressions, which runs very efficiently for many common uses of pattern matching in VCL, as attested by years of successful use of PCRE with Varnish. .sp But for certain kinds of patterns, the worst\-case running time of the PCRE matcher is exponential in the length of the string to be matched. The matcher uses backtracking, implemented with recursive calls to the internal \fBmatch()\fP function. In principle there is no upper bound to the possible depth of backtracking and recursion, except as imposed by the \fBvarnishd\fP runtime parameters \fBpcre_match_limit\fP and \fBpcre_match_limit_recursion\fP; matches fail if either of these limits are met. Stack overflow caused by deep backtracking has occasionally been the subject of \fBvarnishd\fP issues. .sp RE2 differs from PCRE in that it limits the syntax of patterns so that they always specify a regular language in the formally strict sense. Most notably, backreferences within a pattern are not permitted, for example \fB(foo|bar)\e1\fP to match \fBfoofoo\fP and \fBbarbar\fP, but not \fBfoobar\fP or \fBbarfoo\fP\&. See the link in \fBSEE ALSO\fP for the specification of RE2 syntax. .sp This means that an RE2 matcher runs as a finite automaton, which guarantees linear running time in the length of the matched string. There is no backtracking, and hence no risk of deep recursion or stack overflow. .sp The relative advantages and disadvantages of RE2 and PCRE is a broad subject, beyond the scope of this manual. See the references in \fBSEE ALSO\fP for more in\-depth discussion. .SS regex object and function interfaces .sp The VMOD provides regular expression operations by way of the \fBregex\fP object interface and a functional interface. For \fBregex\fP objects, the pattern is compiled at VCL initialization time, and the compiled pattern is re\-used for each invocation of its methods. Compilation failures (due to errors in the pattern) cause failure at initialization time, and the VCL fails to load. The \fB\&.backref()\fP and \fB\&.namedref()\fP methods refer back to the last invocation of the \fB\&.match()\fP method for the same object. .sp The functional interface provides the same set of operations, but the pattern is compiled at runtime on each invocation (and then discarded). Compilation failures are reported as errors in the Varnish log. The \fBbackref()\fP and \fBnamedref()\fP functions refer back to the last invocation of the \fBmatch()\fP function, for any pattern. .sp Compiling a pattern at runtime on each invocation is considerably more costly than re\-using a compiled pattern. So for patterns that are fixed and known at VCL initialization, the object interface should be used. The functional interface should only be used for patterns whose contents are not known until runtime. .SS set object interface .sp \fBset\fP objects provide a shorthand for constructing patterns that consist of an alternation \-\- a group of patterns combined with \fB|\fP for \(dqor\(dq. For example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C import re2; sub vcl_init { new myset = re2.set(); myset.add(\(dqfoo\(dq); # Pattern 1 myset.add(\(dqbar\(dq); # Pattern 2 myset.add(\(dqbaz\(dq); # Pattern 3 } .ft P .fi .UNINDENT .UNINDENT .sp \fBmyset.match()\fP can now be used to match a string against the pattern \fBfoo|bar|baz\fP\&. When a match is successful, the matcher has determined all of the patterns that matched. These can then be retrieved with the method \fB\&.nmatches()\fP for the number of matched patterns, and with \fB\&.matched(n)\fP, which returns \fBtrue\fP if the \fBnth\fP pattern matched, where the patterns are numbered in the order in which they were added: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C if (myset.match(\(dqfoobar\(dq)) { std.log(\(dqMatched \(dq + myset.nmatches() + \(dq patterns\(dq); if (myset.matched(1)) { # Pattern /foo/ matched call do_foo; } if (myset.matched(2)) { # Pattern /bar/ matched call do_bar; } if (myset.matched(3)) { # Pattern /baz/ matched call do_baz; } } .ft P .fi .UNINDENT .UNINDENT .sp An advantage of alternations and sets with RE2, as opposed to an alternation in PCRE or a series of separate matches in an if\-elsif\-elsif sequence, comes from the fact that the matcher is implemented as a state machine. That means that the matcher progresses through the string to be matched just once, following patterns in the set that match through the state machine, or determining that there is no match as soon as there are no more possible paths in the state machine. So a string can be matched against a large set of patterns in time that is proportional to the length of the string to be matched. In contrast, PCRE matches patterns in an alternation one after another, stopping after the first matching pattern, or attempting matches against all of them if there is no match. Thus a match against an alternation in PCRE is not unlike an if\-elsif\-elsif sequence of individual matches, and requires the time needed for each individual match, overall in proportion with the number of patterns to be matched. .sp Another advantage of the VMOD\(aqs set object is the ability to associate strings or backends with the patterns added to the set with the \fB\&.add()\fP method: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C sub vcl_init { new prefix = re2.set(anchor=start); prefix.add(\(dq/foo\(dq, string=\(dqwww.domain1.com\(dq); prefix.add(\(dq/bar\(dq, string=\(dqwww.domain2.com\(dq); prefix.add(\(dq/baz\(dq, string=\(dqwww.domain3.com\(dq); prefix.add(\(dq/quux\(dq, string=\(dqwww.domain4.com\(dq); new appmatcher = re2.set(anchor=start); appmatcher.add(\(dq/foo\(dq, backend=app1); appmatcher.add(\(dq/bar\(dq, backend=app2); appmatcher.add(\(dq/baz\(dq, backend=app3); appmatcher.add(\(dq/quux\(dq, backend=app4); } .ft P .fi .UNINDENT .UNINDENT .sp After a successful match, the string or backend associated with the matching pattern can be retrieved with the \fB\&.string()\fP and \fB\&.backend()\fP methods. This makes it possible, for example, to construct a redirect response or choose the backend with code that is both efficient and compact, even with a large set of patterns to be matched: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # Use the prefix object to construct a redirect response from # a matching request URL. sub vcl_recv { if (prefix.match(req.url)) { # Pass the string associated with the matching pattern # to vcl_synth. return(synth(1301, prefix.string())); } } sub vcl_synth { # The string associated with the matching pattern is in # resp.reason. if (resp.status == 1301) { set resp.http.Location = \(dqhttp://\(dq + resp.reason + req.url; set resp.status = 301; set resp.reason = \(dqMoved Permanently\(dq; } } # Use the appmatcher object to choose a backend based on the # request URL prefix. sub vcl_recv { if (appmatcher.match(req.url)) { set req.backend_hint = appmatcher.backend(); } } .ft P .fi .UNINDENT .UNINDENT .SS regex options .sp Where a pattern is compiled \-\- in the \fBregex\fP and \fBset\fP constructors, and in functions that require compilation \-\- options may be specified that can affect the interpretation of the pattern or the operation of the matcher. There are default values for each option, and it is only necessary to specify options in VCL that differ from the defaults. Options specified in a \fBset\fP constructor apply to all of the patterns in the resulting alternation. .INDENT 0.0 .TP .B \fButf8\fP If true, characters in a pattern match Unicode code points, and hence may match more than one byte. If false, the pattern and strings to be matched are interpreted as Latin\-1 (ISO 8859\-1), and a pattern character matches exactly one byte. Default is \fBfalse\fP\&. Note that this differs from the RE2 default. .TP .B \fBposix_syntax\fP If true, patterns are restricted to POSIX (egrep) syntax. Otherwise, the pattern syntax resembles that of PCRE, with some deviations. See the link in \fBSEE ALSO\fP for the syntax specification. Default is \fBfalse\fP\&. The options \fBperl_classes\fP, \fBword_boundary\fP and \fBone_line\fP are only consulted when this option is true. .TP .B \fBlongest_match\fP If true, the matcher searches for the longest possible match where alternatives are possible. Otherwise, search for the first match. For example with the pattern \fBa(b|bb)\fP and the string \fBabb\fP, \fBabb\fP matches when \fBlongest_match\fP is true, and backref 1 is \fBbb\fP\&. Otherwise, \fBab\fP matches, and backref 1 is \fBb\fP\&. Default is \fBfalse\fP\&. .TP .B \fBmax_mem\fP An upper bound (in bytes) for the size of the compiled pattern. If \fBmax_mem\fP is too small, the matcher may fall back to less efficient algorithms, or the pattern may fail to compile. Default is the RE2 default (8MB), which should suffice for typical patterns. .TP .B \fBliteral\fP If true, the pattern is interpreted as a literal string, and no regex metacharacters (such as \fB*\fP, \fB+\fP, \fB^\fP and so forth) have their special meaning. Default is \fBfalse\fP\&. .TP .B \fBnever_nl\fP If true, the newline character \fB\en\fP in a string is never matched, even if it appears in the pattern. Default is \fBfalse\fP\&. .TP .B \fBdot_nl\fP If true, then the dot character \fB\&.\fP in a pattern matches everything, including newline. Otherwise, \fB\&.\fP never matches newline. Default is \fBfalse\fP\&. .TP .B \fBnever_capture\fP If true, parentheses in a pattern are interpreted as non\-capturing, and all invocations of the \fBbackref\fP and \fBnamedref\fP methods or functions will lead to VCL faillure (see \fI\%ERRORS\fP), including \fBbackref(0)\fP after a successful match. Default is \fBfalse\fP, except for set objects, for which \fBnever_capture\fP is always true (and cannot be changed), since back references are not possible with sets. .TP .B \fBcase_sensitive\fP If true, matches are case\-sensitive. A pattern can override this option with the \fB(?i)\fP flag, unless \fBposix_syntax\fP is true. Default is \fBtrue\fP\&. .UNINDENT .sp The following options are only consulted when \fBposix_syntax\fP is true. If \fBposix_syntax\fP is false, then these features are always enabled and cannot be turned off. .INDENT 0.0 .TP .B \fBperl_classes\fP If true, then the perl character classes \fB\ed\fP, \fB\es\fP, \fB\ew\fP, \fB\eD\fP, \fB\eS\fP and \fB\eW\fP are permitted in a pattern. Default is \fBfalse\fP\&. .TP .B \fBword_boundary\fP If true, the perl assertions \fB\eb\fP and \fB\eB\fP (word boundary and not a word boundary) are permitted. Default is \fBfalse\fP\&. .TP .B \fBone_line\fP If true, then \fB^\fP and \fB$\fP only match at the beginning and end of the string to be matched, regardless of newlines. Otherwise, \fB^\fP also matches just after a newline, and \fB$\fP also matches just before a newline. Default is \fBfalse\fP\&. .UNINDENT .SS new xregex = re2.regex(STRING pattern, BOOL utf8, BOOL posix_syntax, BOOL longest_match, INT max_mem, BOOL literal, BOOL never_nl, BOOL dot_nl, BOOL never_capture, BOOL case_sensitive, BOOL perl_classes, BOOL word_boundary, BOOL one_line) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C new xregex = re2.regex( STRING pattern, BOOL utf8=0, BOOL posix_syntax=0, BOOL longest_match=0, INT max_mem=8388608, BOOL literal=0, BOOL never_nl=0, BOOL dot_nl=0, BOOL never_capture=0, BOOL case_sensitive=1, BOOL perl_classes=0, BOOL word_boundary=0, BOOL one_line=0 ) .ft P .fi .UNINDENT .UNINDENT .sp Create a regex object from \fBpattern\fP and the given options (or option defaults). If the pattern is invalid, then VCL will fail to load and the VCC compiler will emit an error message. .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C sub vcl_init { new domainmatcher = re2.regex(\(dq^www\e.([^.]+)\e.com$\(dq); new maxagematcher = re2.regex(\(dqmax\-age\es*=\es*(\ed+)\(dq); # Group possible subdomains without capturing new submatcher = re2.regex(\(dq^www\e.(domain1|domain2)\e.com$\(dq, never_capture=true); } .ft P .fi .UNINDENT .UNINDENT .SS BOOL xregex.match(STRING) .sp Returns \fBtrue\fP if and only if the compiled regex matches the given string; corresponds to VCL\(aqs infix operator \fB~\fP\&. .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C if (myregex.match(req.http.Host)) { call do_on_match; } .ft P .fi .UNINDENT .UNINDENT .SS STRING xregex.backref(INT ref, STRING fallback) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C STRING xregex.backref( INT ref, STRING fallback=\(dq**BACKREF METHOD FAILED**\(dq ) .ft P .fi .UNINDENT .UNINDENT .sp Returns the \fInth\fP captured subexpression from the most recent successful call of the \fB\&.match()\fP method for this object in the same client or backend context, or a fallback string in case the capture fails. Backref 0 indicates the entire matched string. Thus this function behaves like the \fB\en\fP notation in the native VCL functions \fBregsub\fP and \fBregsuball\fP, and the \fB$1\fP, \fB$2\fP ... variables in Perl. .sp Since Varnish client and backend operations run in different threads, \fB\&.backref()\fP can only refer back to a \fB\&.match()\fP call in the same thread. Thus a \fB\&.backref()\fP call in any of the \fBvcl_backend_*\fP subroutines \-\- the backend context \-\- refers back to a previous \fB\&.match()\fP in any of those same subroutines; and a call in any of the other VCL subroutines \-\- the client context \-\- refers back to a \fB\&.match()\fP in the same client context. .sp \fB\&.backref()\fP may return \fBfallback\fP after a successful match, if no captured group in the matching string corresponds to the backref number. For example, when the pattern \fB(a|(b))c\fP matches the string \fBac\fP, there is no backref 2, since nothing matches \fBb\fP in the string. The default value of \fBfallback\fP is \fB\(dq**BACKREF METHOD FAILED**\(dq\fP, but you may set another value (such as the empty string). .sp After unsuccessful matches, \fB\&.backref()\fP invokes VCL failure (see \fI\%ERRORS\fP). \fB\&.backref()\fP always fails after a failed match, even if \fB\&.match()\fP had been called successfully before the failure. .sp The VCL infix operators \fB~\fP and \fB!~\fP do not affect this method, nor do the functions \fBregsub\fP or \fBregsuball\fP\&. Nor is it affected by the matches performed by any other method or function in this VMOD (such as the \fBsub()\fP, \fBsuball()\fP or \fBextract()\fP methods or functions, or the \fBset\fP object\(aqs \fB\&.match()\fP method). .sp \fB\&.backref()\fP invokes VCL failure under the following conditions, even if a previous match was successful and a substring could have been captured (see \fI\%ERRORS\fP): .INDENT 0.0 .IP \(bu 2 The \fBfallback\fP string is undefined, for example if set from an unset header variable. .IP \(bu 2 The \fBnever_capture\fP option was set to \fBtrue\fP for this object. In this case, even \fB\&.backref(0)\fP fails after a successful match (otherwise, backref 0 always returns the full matched string). .IP \(bu 2 \fBref\fP (the backref number) is out of range, i.e. it is larger than the highest number for a capturing group in the pattern. .IP \(bu 2 \fB\&.match()\fP was never called for this object prior to calling \fB\&.backref()\fP\&. .IP \(bu 2 There is insufficient workspace for the string to be returned. .UNINDENT .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C if (domainmatcher.match(req.http.Host)) { set req.http.X\-Domain = domainmatcher.backref(1); } .ft P .fi .UNINDENT .UNINDENT .SS STRING xregex.namedref(STRING name, STRING fallback) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C STRING xregex.namedref( STRING name, STRING fallback=\(dq**NAMEDREF METHOD FAILED**\(dq ) .ft P .fi .UNINDENT .UNINDENT .sp Returns the captured subexpression designated by \fBname\fP from the most recent successful call to \fB\&.match()\fP in the current context (client or backend). .sp Named capturing groups are written in RE2 as: \fB(?Pre)\fP\&. (Note that this syntax with \fBP\fP, inspired by Python, differs from the notation for named capturing groups in PCRE.) Thus when \fB(?P.+)bar$\fP matches \fBbazbar\fP, then \fB\&.namedref(\(dqfoo\(dq)\fP returns \fBbaz\fP\&. .sp Note that a named capturing group can also be referenced as a numbered group. So in the previous example, \fB\&.backref(1)\fP also returns \fBbaz\fP\&. .sp \fBfallback\fP is returned when the named reference did not match. The default fallback is \fB\(dq**NAMEDREF METHOD FAILED**\(dq\fP\&. .sp Like \fB\&.backref()\fP, \fB\&.namedref()\fP is not affected by native VCL regex operations, nor by any other matches performed by methods or functions of the VMOD, except for a prior \fB\&.match()\fP for the same object. .sp \fB\&.namedref()\fP invokes VCL failure (see \fI\%ERRORS\fP) if: .INDENT 0.0 .IP \(bu 2 The \fBfallback\fP string is undefined. .IP \(bu 2 \fBname\fP is undefined or the empty string. .IP \(bu 2 The \fBnever_capture\fP option was set to \fBtrue\fP\&. .IP \(bu 2 There is no such named group. .IP \(bu 2 \fB\&.match()\fP was not called for this object. .IP \(bu 2 There is insufficient workspace for the string to be returned. .UNINDENT .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C sub vcl_init { new domainmatcher = re2.regex(\(dq^www\e.(?P[^.]+)\e.com$\(dq); } sub vcl_recv { if (domainmatcher.match(req.http.Host)) { set req.http.X\-Domain = domainmatcher.namedref(\(dqdomain\(dq); } } .ft P .fi .UNINDENT .UNINDENT .SS STRING xregex.sub(STRING text, STRING rewrite, STRING fallback) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C STRING xregex.sub( STRING text, STRING rewrite, STRING fallback=\(dq**SUB METHOD FAILED**\(dq ) .ft P .fi .UNINDENT .UNINDENT .sp If the compiled pattern for this regex object matches \fBtext\fP, then return the result of replacing the first match in \fBtext\fP with \fBrewrite\fP\&. Within \fBrewrite\fP, \fB\e1\fP through \fB\e9\fP can be used to insert the the numbered capturing group from the pattern, and \fB\e0\fP to insert the entire matching text. This method corresponds to the VCL native function \fBregsub()\fP\&. .sp \fBfallback\fP is returned if the pattern does not match \fBtext\fP\&. The default fallback is \fB\(dq**SUB METHOD FAILED**\(dq\fP\&. .sp \fB\&.sub()\fP invokes VCL failure (see \fI\%ERRORS\fP) if: .INDENT 0.0 .IP \(bu 2 Any of \fBtext\fP, \fBrewrite\fP or \fBfallback\fP are undefined. .IP \(bu 2 There is insufficient workspace for the rewritten string. .UNINDENT .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C sub vcl_init { new bmatcher = re2.regex(\(dqb+\(dq); } sub vcl_recv { # If Host contains \(dqwww.yabba.dabba.doo.com\(dq, then this will # set X\-Yada to \(dqwww.yada.dabba.doo.com\(dq. set req.http.X\-Yada = bmatcher.sub(req.http.Host, \(dqd\(dq); } .ft P .fi .UNINDENT .UNINDENT .SS STRING xregex.suball(STRING text, STRING rewrite, STRING fallback) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C STRING xregex.suball( STRING text, STRING rewrite, STRING fallback=\(dq**SUBALL METHOD FAILED**\(dq ) .ft P .fi .UNINDENT .UNINDENT .sp Like \fB\&.sub()\fP, except that all successive non\-overlapping matches in \fBtext\fP are replaced with \fBrewrite\fP\&. This method corresponds to VCL native \fBregsuball()\fP\&. .sp The default fallback is \fB\(dq**SUBALL METHOD FAILED**\(dq\fP\&. \fB\&.suball()\fP fails under the same conditions as \fB\&.sub()\fP\&. .sp Since only non\-overlapping matches are substituted, replacing \fB\(dqana\(dq\fP within \fB\(dqbanana\(dq\fP only results in one substitution, not two. .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C sub vcl_init { new bmatcher = re2.regex(\(dqb+\(dq); } sub vcl_recv { # If Host contains \(dqwww.yabba.dabba.doo.com\(dq, then set X\-Yada to # \(dqwww.yada.dada.doo.com\(dq. set req.http.X\-Yada = bmatcher.suball(req.http.Host, \(dqd\(dq); } .ft P .fi .UNINDENT .UNINDENT .SS STRING xregex.extract(STRING text, STRING rewrite, STRING fallback) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C STRING xregex.extract( STRING text, STRING rewrite, STRING fallback=\(dq**EXTRACT METHOD FAILED**\(dq ) .ft P .fi .UNINDENT .UNINDENT .sp If the compiled pattern for this regex object matches \fBtext\fP, then return \fBrewrite\fP with substitutions from the matching portions of \fBtext\fP\&. Non\-matching substrings of \fBtext\fP are ignored. .sp The default fallback is \fB\(dq**EXTRACT METHOD FAILED**\(dq\fP\&. Like \fB\&.sub()\fP and \fB\&.suball()\fP, \fB\&.extract()\fP fails if: .INDENT 0.0 .IP \(bu 2 Any of \fBtext\fP, \fBrewrite\fP or \fBfallback\fP are undefined. .IP \(bu 2 There is insufficient workspace for the rewritten string. .UNINDENT .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C sub vcl_init { new email = re2.regex(\(dq(.*)@([^.]*)\(dq); } sub vcl_deliver { # Sets X\-UUCP to \(dqkremvax!boris\(dq set resp.http.X\-UUCP = email.extract(\(dqboris@kremvax.ru\(dq, \(dq\e2!\e1\(dq); } .ft P .fi .UNINDENT .UNINDENT .SS INT xregex.cost() .sp Return a numeric measurement > 0 for this regex object from the RE2 library. According to the RE2 documentation: .INDENT 0.0 .INDENT 3.5 \&... a very approximate measure of a regexp\(aqs \(dqcost\(dq. Larger numbers are more expensive than smaller numbers. .UNINDENT .UNINDENT .sp The absolute numeric values are opaque and not relevant, but they are meaningful relative to one another \-\- more complex regexen have a higher cost than less complex regexen. This may be useful during development and optimization of regular expressions. .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C std.log(\(dqr1 cost=\(dq + r1.cost() + \(dq r_alt cost=\(dq + r_alt.cost()); .ft P .fi .UNINDENT .UNINDENT .SH REGEX FUNCTIONAL INTERFACE .SS BOOL match(STRING pattern, STRING subject, BOOL utf8, BOOL posix_syntax, BOOL longest_match, INT max_mem, BOOL literal, BOOL never_nl, BOOL dot_nl, BOOL never_capture, BOOL case_sensitive, BOOL perl_classes, BOOL word_boundary, BOOL one_line) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C BOOL match( STRING pattern, STRING subject, BOOL utf8=0, BOOL posix_syntax=0, BOOL longest_match=0, INT max_mem=8388608, BOOL literal=0, BOOL never_nl=0, BOOL dot_nl=0, BOOL never_capture=0, BOOL case_sensitive=1, BOOL perl_classes=0, BOOL word_boundary=0, BOOL one_line=0 ) .ft P .fi .UNINDENT .UNINDENT .sp Like the \fBregex.match()\fP method, return \fBtrue\fP if \fBpattern\fP matches \fBsubject\fP, where \fBpattern\fP is compiled with the given options (or default options) on each invocation. .sp If \fBpattern\fP fails to compile, then VCL failure is invoked (see \fI\%ERRORS\fP). .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # Match the bereq Host header against a backend response header if (re2.match(pattern=bereq.http.Host, subject=beresp.http.X\-Host)) { call do_on_match; } .ft P .fi .UNINDENT .UNINDENT .SS STRING backref(INT ref, STRING fallback) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C STRING backref( INT ref, STRING fallback=\(dq**BACKREF FUNCTION FAILED**\(dq ) .ft P .fi .UNINDENT .UNINDENT .sp Returns the \fInth\fP captured subexpression from the most recent successful call of the \fBmatch()\fP function in the current client or backend context, or a fallback string if the capture fails. The default \fBfallback\fP is \fB\(dq**BACKREF FUNCTION FAILED**\(dq\fP\&. .sp Similarly to the \fBregex.backref()\fP method, \fBfallback\fP is returned if there is no captured group corresponding to the backref number. The function is not affected by native VCL regex operations, or any other method or function of the VMOD except for the \fBmatch()\fP function. .sp The function invokes VCL failure under the same conditions as the corresponding method (see \fI\%ERRORS\fP): .INDENT 0.0 .IP \(bu 2 \fBfallback\fP is undefined. .IP \(bu 2 \fBnever_capture\fP was true in the previous invocation of the \fBmatch()\fP function. .IP \(bu 2 \fBref\fP is out of range. .IP \(bu 2 The \fBmatch()\fP function was never called in this context, or if the previous \fBmatch()\fP call failed (returned \fBfalse\fP). .IP \(bu 2 The pattern failed to compile for the previous \fBmatch()\fP call. .IP \(bu 2 There is insufficient workspace for the captured subexpression. .UNINDENT .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # Match against a pattern provided in a beresp header, and capture # subexpression 1. if (re2.match(pattern=beresp.http.X\-Pattern, bereq.http.X\-Foo)) { set beresp.http.X\-Capture = re2.backref(1); } .ft P .fi .UNINDENT .UNINDENT .SS STRING namedref(STRING name, STRING fallback) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C STRING namedref( STRING name, STRING fallback=\(dq**NAMEDREF FUNCTION FAILED**\(dq ) .ft P .fi .UNINDENT .UNINDENT .sp Returns the captured subexpression designated by \fBname\fP from the most recent successful call to the \fBmatch()\fP function in the current context, or \fBfallback\fP if the corresponding group did not match. The default fallback is \fB\(dq**NAMEDREF FUNCTION FAILED**\(dq\fP\&. .sp The function invokes VCL failure under the same conditions as the corresponding method (see \fI\%ERRORS\fP): .INDENT 0.0 .IP \(bu 2 \fBfallback\fP is undefined. .IP \(bu 2 \fBname\fP is undefined or the empty string. .IP \(bu 2 The \fBnever_capture\fP option was set to \fBtrue\fP\&. .IP \(bu 2 There is no such named group. .IP \(bu 2 \fBmatch()\fP was not called in this context, or the previous call failed. .IP \(bu 2 The pattern failed to compile for the previous \fBmatch()\fP call. .IP \(bu 2 There is insufficient workspace for the captured expression. .UNINDENT .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C if (re2.match(beresp.http.X\-Pattern\-With\-Names, bereq.http.X\-Foo)) { set beresp.http.X\-Capture = re2.namedref(\(dqfoo\(dq); } .ft P .fi .UNINDENT .UNINDENT .SS STRING sub(STRING pattern, STRING text, STRING rewrite, STRING fallback, BOOL utf8, BOOL posix_syntax, BOOL longest_match, INT max_mem, BOOL literal, BOOL never_nl, BOOL dot_nl, BOOL never_capture, BOOL case_sensitive, BOOL perl_classes, BOOL word_boundary, BOOL one_line) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C STRING sub( STRING pattern, STRING text, STRING rewrite, STRING fallback=\(dq**SUB FUNCTION FAILED**\(dq, BOOL utf8=0, BOOL posix_syntax=0, BOOL longest_match=0, INT max_mem=8388608, BOOL literal=0, BOOL never_nl=0, BOOL dot_nl=0, BOOL never_capture=0, BOOL case_sensitive=1, BOOL perl_classes=0, BOOL word_boundary=0, BOOL one_line=0 ) .ft P .fi .UNINDENT .UNINDENT .sp Compiles \fBpattern\fP with the given options, and if it matches \fBtext\fP, then return the result of replacing the first match in \fBtext\fP with \fBrewrite\fP\&. As with the \fBregex.sub()\fP method, \fB\e0\fP through \fB\e9\fP may be used in \fBrewrite\fP to substitute captured groups from the pattern. .sp \fBfallback\fP is returned if the pattern does not match \fBtext\fP\&. The default fallback is \fB\(dq**SUB FUNCTION FAILED**\(dq\fP\&. .sp \fBsub()\fP invokes VCL failure (see \fI\%ERRORS\fP) if: .INDENT 0.0 .IP \(bu 2 \fBpattern\fP cannot be compiled. .IP \(bu 2 Any of \fBtext\fP, \fBrewrite\fP or \fBfallback\fP are undefined. .IP \(bu 2 There is insufficient workspace for the rewritten string. .UNINDENT .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # If the beresp header X\-Sub\-Letters contains \(dqb+\(dq, and Host contains # \(dqwww.yabba.dabba.doo.com\(dq, then set X\-Yada to # \(dqwww.yada.dabba.doo.com\(dq. set beresp.http.X\-Yada = re2.sub(beresp.http.X\-Sub\-Letters, bereq.http.Host, \(dqd\(dq); .ft P .fi .UNINDENT .UNINDENT .SS STRING suball(STRING pattern, STRING text, STRING rewrite, STRING fallback, BOOL utf8, BOOL posix_syntax, BOOL longest_match, INT max_mem, BOOL literal, BOOL never_nl, BOOL dot_nl, BOOL never_capture, BOOL case_sensitive, BOOL perl_classes, BOOL word_boundary, BOOL one_line) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C STRING suball( STRING pattern, STRING text, STRING rewrite, STRING fallback=\(dq**SUBALL FUNCTION FAILED**\(dq, BOOL utf8=0, BOOL posix_syntax=0, BOOL longest_match=0, INT max_mem=8388608, BOOL literal=0, BOOL never_nl=0, BOOL dot_nl=0, BOOL never_capture=0, BOOL case_sensitive=1, BOOL perl_classes=0, BOOL word_boundary=0, BOOL one_line=0 ) .ft P .fi .UNINDENT .UNINDENT .sp Like the \fBsub()\fP function, except that all successive non\-overlapping matches in \fBtext\fP are replace with \fBrewrite\fP\&. .sp The default fallback is \fB\(dq**SUBALL FUNCTION FAILED**\(dq\fP\&. The \fBsuball()\fP function fails under the same conditions as \fBsub()\fP\&. .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # If the beresp header X\-Sub\-Letters contains \(dqb+\(dq, and Host contains # \(dqwww.yabba.dabba.doo.com\(dq, then set X\-Yada to # \(dqwww.yada.dada.doo.com\(dq. set beresp.http.X\-Yada = re2.suball(beresp.http.X\-Sub\-Letters, bereq.http.Host, \(dqd\(dq); .ft P .fi .UNINDENT .UNINDENT .SS STRING extract(STRING pattern, STRING text, STRING rewrite, STRING fallback, BOOL utf8, BOOL posix_syntax, BOOL longest_match, INT max_mem, BOOL literal, BOOL never_nl, BOOL dot_nl, BOOL never_capture, BOOL case_sensitive, BOOL perl_classes, BOOL word_boundary, BOOL one_line) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C STRING extract( STRING pattern, STRING text, STRING rewrite, STRING fallback=\(dq**EXTRACT FUNCTION FAILED**\(dq, BOOL utf8=0, BOOL posix_syntax=0, BOOL longest_match=0, INT max_mem=8388608, BOOL literal=0, BOOL never_nl=0, BOOL dot_nl=0, BOOL never_capture=0, BOOL case_sensitive=1, BOOL perl_classes=0, BOOL word_boundary=0, BOOL one_line=0 ) .ft P .fi .UNINDENT .UNINDENT .sp Compiles \fBpattern\fP with the given options, and if it matches \fBtext\fP, then return \fBrewrite\fP with substitutions from the matching portions of \fBtext\fP, ignoring the non\-matching portions. .sp The default fallback is \fB\(dq**EXTRACT FUNCTION FAILED**\(dq\fP\&. The \fBextract()\fP function fails under the same conditions as \fBsub()\fP and \fBsuball()\fP\&. .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # If beresp header X\-Params contains \(dq(foo|bar)=(baz|quux)\(dq, and the # URL contains \(dqbar=quux\(dq, then set X\-Query to \(dqbar:quux\(dq. set beresp.http.X\-Query = re2.extract(beresp.http.X\-Params, bereq.url, \(dq\e1:\e2\(dq); .ft P .fi .UNINDENT .UNINDENT .SS INT cost(STRING pattern, BOOL utf8, BOOL posix_syntax, BOOL longest_match, INT max_mem, BOOL literal, BOOL never_nl, BOOL dot_nl, BOOL never_capture, BOOL case_sensitive, BOOL perl_classes, BOOL word_boundary, BOOL one_line) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C INT cost( STRING pattern, BOOL utf8=0, BOOL posix_syntax=0, BOOL longest_match=0, INT max_mem=8388608, BOOL literal=0, BOOL never_nl=0, BOOL dot_nl=0, BOOL never_capture=0, BOOL case_sensitive=1, BOOL perl_classes=0, BOOL word_boundary=0, BOOL one_line=0 ) .ft P .fi .UNINDENT .UNINDENT .sp Like the \fB\&.cost()\fP method above, return a numeric measurement > 0 from the RE2 library for \fBpattern\fP with the given options. More complex regexen have a higher cost than less complex regexen. .sp Invokes VCL failure if \fBpattern\fP cannot be compiled (see \fI\%ERRORS\fP). .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C std.log(\(dqsimple cost=\(dq + re2.cost(\(dqsimple\(dq) + \(dq complex cost=\(dq + re2.cost(\(dqcomplex{1,128}\(dq)); .ft P .fi .UNINDENT .UNINDENT .SS new xset = re2.set(ENUM anchor, BOOL utf8, BOOL posix_syntax, BOOL longest_match, INT max_mem, BOOL literal, BOOL never_nl, BOOL dot_nl, BOOL case_sensitive, BOOL perl_classes, BOOL word_boundary, BOOL one_line) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C new xset = re2.set( ENUM {none, start, both} anchor=none, BOOL utf8=0, BOOL posix_syntax=0, BOOL longest_match=0, INT max_mem=8388608, BOOL literal=0, BOOL never_nl=0, BOOL dot_nl=0, BOOL case_sensitive=1, BOOL perl_classes=0, BOOL word_boundary=0, BOOL one_line=0 ) .ft P .fi .UNINDENT .UNINDENT .sp Initialize a set object that represents several patterns combined by alternation \-\- \fB|\fP for \(dqor\(dq. .sp Optional parameters control the interpretation of the resulting composed pattern. The \fBanchor\fP parameter is an enum that can have the values \fBnone\fP, \fBstart\fP or \fBboth\fP, where \fBnone\fP is the default. \fBstart\fP means that each pattern is matched as if it begins with \fB^\fP for start\-of\-text, and \fBboth\fP means that each pattern is anchored with both \fB^\fP at the beginning and \fB$\fP for end\-of\-text at the end. \fBnone\fP means that each pattern is interpreted as a partial match (although individual patterns within the set may have either of \fB^\fP of \fB$\fP). .sp For example, if a set is initialized with \fBanchor=both\fP, and the patterns \fBfoo\fP and \fBbar\fP are added, then matches against the set match a string against \fB^foo$|^bar$\fP, or equivalently \fB^(foo|bar)$\fP\&. .sp The usual regex options can be set, which then control matching against the resulting composed pattern. However, the \fBnever_capture\fP option cannot be set, and is always implicitly true, since backrefs and namedrefs are not possible with sets. .sp Sets are compiled automatically when \fBvcl_init\fP finishes (or when the deprecated \fB\&.compile()\fP method is called). Compilation fails if any of the added patterns cannot be compiled, or if no patterns were added to the set. It may also fail if the \fBmax_mem\fP setting is not large enough for the composed pattern. In that case, the VCL load will fail with an error message (then consider a larger value for \fBmax_mem\fP in the set constructor). .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C sub vcl_init { # Initialize a regex set for partial matches # with default options new foo = re2.set(); # Initialize a regex set for case insensitive matches # with anchors on both ends (^ and $). new bar = re2.set(anchor=both, case_sensitive=false); # Initialize a regex set using POSIX syntax, but allowing # Perl character classes, and anchoring at the left (^). new baz = re2.set(anchor=start, posix_syntax=true, perl_classes=true); } .ft P .fi .UNINDENT .UNINDENT .SS VOID xset.add(STRING, [STRING string], [BACKEND backend], [BOOL save], [BOOL never_capture], [INT integer], [SUB sub]) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C VOID xset.add( STRING, [STRING string], [BACKEND backend], [BOOL save], [BOOL never_capture], [INT integer], [SUB sub] ) .ft P .fi .UNINDENT .UNINDENT .sp Add the given pattern to the set. If the pattern is invalid, \fB\&.add()\fP fails, and the VCL will fail to load, with an error message describing the problem. .sp If values for the \fBstring\fP, \fBbackend\fP, \fBinteger\fP and/or \fBsub\fP parameters are provided, then these values can be retrieved with the \fB\&.string()\fP, \fB\&.backend()\fP, \fB\&.integer()\fP and \fB\&.subroutine()\fP methods, respectively, as described below. This makes it possible to associate data with the added pattern after it matches successfully. By default the pattern is not associated with any such value. .sp If \fBsave\fP is true, then the given pattern is compiled and saved as a \fBregex\fP object, just as if the \fBregex\fP constructor described above is invoked. This object is stored internally in the \fBset\fP object as an independent matcher, separate from \(dqcompound\(dq pattern formed by the set as an alternation of the patterns added to it. By default, \fBsave\fP is \fBfalse\fP\&. .sp When the \fB\&.match()\fP method on the set is successful, and one of the patterns that matched is associated with a saved internal \fBregex\fP object, then that object may be used for subsequent method invocations such as \fB\&.sub()\fP on the set object, whose meanings are the same as documented above for \fBregex\fP objects. Details are described below. .sp When an internal \fBregex\fP object is saved (i.e. when \fBsave\fP is true), it is compiled with the same options that were provided to the set object in the constructor. The \fBnever_capture\fP option can also be set to false for the individual regex, even though it is implicitly set to true for the full set object (default is false). .sp \fB\&.add()\fP MUST be called in \fBvcl_init\fP, and MAY NOT be called after \fB\&.compile()\fP\&. VCL failure is invoked if \fB\&.add()\fP is called in any other subroutine (see \fI\%ERRORS\fP). If it is called in \fBvcl_init\fP after \fB\&.compile()\fP, then the VCL load will fail with an error message. Note that \fB\&.compile()\fP is now unnecessary and deprecated. .sp When the \fB\&.matched(INT)\fP method is called after a successful match, the numbering corresponds to the order in which patterns were added. The same is true of the INT arguments that may be given for methods such as \fB\&.string()\fP, \fB\&.backend()\fP or \fB\&.sub()\fP, as described below. .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C sub vcl_init { # literal=true means that the dots are interpreted as literal # dots, not \(dqmatch any character\(dq. new hostmatcher = re2.set(anchor=both, case_sensitive=false, literal=true); hostmatcher.add(\(dqwww.domain1.com\(dq); hostmatcher.add(\(dqwww.domain2.com\(dq); hostmatcher.add(\(dqwww.domain3.com\(dq); } # See the documentation of the .string() and .backend() methods # below for uses of the parameters string and backend for .add(). .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 now happens automatically when \fBvcl_init\fP finishes. .sp Compile the compound pattern represented by the set \-\- an alternation of all patterns added by \fB\&.add()\fP\&. .sp Compilation may fail for any of the reasons described for automatic compilation of set objects as described above. .sp \fB\&.compile()\fP MUST be called in \fBvcl_init\fP, and MAY NOT be called more than once for a set object. VCL failure is invoked if it is called in any other subroutine. If it is called a second time in \fBvcl_init\fP, the VCL load will fail. .SS BOOL xset.match(STRING) .sp Returns \fBtrue\fP if the given string matches the compound pattern represented by the set, i.e. if it matches any of the patterns that were added to the set. .sp The matcher identifies all of the patterns that were added to the set and match the given string. These can be determined after a successful match using the \fB\&.matched(INT)\fP and \fB\&.nmatches()\fP methods described below. .sp A match may also fail (leading to VCL failure) if the internal memory limit imposed by the \fBmax_mem\fP parameter in the constructor is exceeded. (With the default value of \fBmax_mem\fP, this ordinarily requires very large patterns and/or a very large string to be matched.) Since about version 2017\-12\-01, the RE2 library reports this condition. If matches fail due to the out\-of\-memory condition, increase the \fBmax_mem\fP parameter in the constructor. .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C if (hostmatcher.match(req.http.Host)) { call do_when_a_host_matched; } .ft P .fi .UNINDENT .UNINDENT .SS BOOL xset.matched(INT) .sp Returns \fBtrue\fP after a successful match if the \fBnth\fP pattern that was added to the set is among the patterns that matched, \fBfalse\fP otherwise. The numbering of the patterns corresponds to the order in which patterns were added in \fBvcl_init\fP, counting from 1. .sp The method refers back to the most recent invocation of \fB\&.match()\fP for the same object in the same client or backend context. It always returns \fBfalse\fP, for every value of the parameter, if it is called after an unsuccessful match (\fB\&.match()\fP returned \fBfalse\fP). .sp \fB\&.matched()\fP invokes VCL failure (see \fI\%ERRORS\fP) if: .INDENT 0.0 .IP \(bu 2 The \fB\&.match()\fP method was not called for this object in the same client or backend scope. .IP \(bu 2 The integer parameter is out of range; that is, if it is less than 1 or greater than the number of patterns added to the set. .UNINDENT .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C if (hostmatcher.match(req.http.Host)) { if (hostmatcher.matched(1)) { call do_domain1; } if (hostmatcher.matched(2)) { call do_domain2; } if (hostmatcher.matched(3)) { call do_domain3; } } .ft P .fi .UNINDENT .UNINDENT .SS INT xset.nmatches() .sp Returns the number of patterns that were matched by the most recent invocation of \fB\&.match()\fP for the same object in the same client or backend context. The method always returns 0 after an unsuccessful match (\fB\&.match()\fP returned \fBfalse\fP). .sp If \fB\&.match()\fP was not called for this object in the same client or backend scope, \fB\&.nmatches()\fP invokes VCL failure (see \fI\%ERRORS\fP). .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C if (myset.match(req.url)) { std.log(\(dqURL matched \(dq + myset.nmatches() + \(dq patterns from the set\(dq); } .ft P .fi .UNINDENT .UNINDENT .SS INT xset.which(ENUM {FIRST, LAST, UNIQUE} select=UNIQUE) .sp Returns a number indicating which pattern in a set matched in the most recent invocation of \fB\&.match()\fP in the client or backend context. The number corresponds to the order in which patterns were added to the set in \fBvcl_init\fP, counting from 1. .sp If exactly one pattern matched in the most recent \fB\&.match()\fP call (so that \fB\&.nmatches()\fP returns 1), and the \fBselect\fP ENUM is set to \fBUNIQUE\fP, then the number for that pattern is returned. \fBselect\fP defaults to \fBUNIQUE\fP, so it can be left out in this case. .sp If more than one pattern matched in the most recent \fB\&.match()\fP call (\fB\&.nmatches()\fP > 1), then the \fBselect\fP ENUM determines the integer that is returned. The values \fBFIRST\fP and \fBLAST\fP specify that, of the patterns that matched, the first or last one added via the \fB\&.add()\fP method is chosen, and the number for that pattern is returned. .sp \fB\&.which()\fP invokes VCL failure (see \fI\%ERRORS\fP) if: .INDENT 0.0 .IP \(bu 2 \fB\&.match()\fP was not called for the set in the current client or backend transaction, or if the previous call returned \fBfalse\fP\&. .IP \(bu 2 More than one pattern in the set matched in the previous \fB\&.match()\fP call, but the \fBselect\fP parameter is set to \fBUNIQUE\fP (or left out, since \fBselect\fP defaults to \fBUNIQUE\fP). .UNINDENT .sp Examples: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C sub vcl_init { new myset = re2.set(); myset.add(\(dqfoo\(dq); # Pattern 1 myset.add(\(dqbar\(dq); # Pattern 2 myset.add(\(dqbaz\(dq); # Pattern 3 myset.compile(); } sub vcl_recv { if (myset.match(\(dqbar\(dq)) { # myset.which() returns 2. } if (myset.which(\(dqfoobaz\(dq)) { # myset.which() fails and returns 0, with a log # message indicating that 2 patterns # matched. # myset.which(FIRST) returns 1. # myset.which(LAST) returns 3. } if (myset.match(\(dqquux\(dq)) { # ... } else { # myset.which() fails and returns 0, with either or # no value for the select ENUM, with a log message # indicating that the previous .match() call was # unsuccessful. } .ft P .fi .UNINDENT .UNINDENT .SS STRING xset.string(INT n, ENUM select) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C STRING xset.string( INT n=0, ENUM {FIRST, LAST, UNIQUE} select=UNIQUE ) .ft P .fi .UNINDENT .UNINDENT .sp Returns the string associated with the \fInth\fP pattern added to the set, or with the pattern in the set that matched in the most recent call to \fB\&.match()\fP in the same task scope (client or backend context). The string set with the \fBstring\fP parameter of the \fB\&.add()\fP method during \fBvcl_init\fP is returned. .sp The pattern is identified with the parameters \fBn\fP and \fBselect\fP according to these rules, which also hold for all further \fBset\fP methods documented in the following. .INDENT 0.0 .IP \(bu 2 If \fBn\fP > 0, then select the \fInth\fP pattern added to the set with the \fB\&.add()\fP method, counting from 1. This identifies the \fInth\fP pattern in any context, regardless of whether \fB\&.match()\fP was called previously, or whether a previous call returned \fBtrue\fP or \fBfalse\fP\&. The \fBselect\fP parameter is ignored in this case. .IP \(bu 2 If \fBn\fP <= 0, then select a pattern in the set that matched successfully in the most recent call to \fB\&.match()\fP in the same task scope. Since \fBn\fP is 0 by default, \fBn\fP can be left out for this purpose. .IP \(bu 2 If \fBn\fP <= 0 and exactly one pattern in the set matched in the most recent invocation of \fB\&.match()\fP (and hence \fB\&.nmatches()\fP returns 1), and \fBselect\fP is set to \fBUNIQUE\fP, then select that pattern. \fBselect\fP defaults to \fBUNIQUE\fP, so when exactly one pattern in the set matched, both \fBn\fP and \fBselect\fP can be left out. .IP \(bu 2 If \fBn\fP <= 0 and more than one pattern matched in the most recent \fB\&.match()\fP call (\fB\&.nmatches()\fP > 1), then the selection of a pattern is determined by the \fBselect\fP parameter. As with \fB\&.which()\fP, \fBFIRST\fP and \fBLAST\fP specify the first or last matching pattern added via the \fB\&.add()\fP method. .UNINDENT .sp For the pattern selected by these rules, return the string that was set with the \fBstring\fP parameter in the \fB\&.add()\fP method that added the pattern to the set. .sp \fB\&.string()\fP invokes VCL failure (see \fI\%ERRORS\fP) if: .INDENT 0.0 .IP \(bu 2 The values of \fBn\fP and \fBselect\fP are invalid: .INDENT 2.0 .IP \(bu 2 \fBn\fP is greater than the number of patterns in the set. .IP \(bu 2 \fBn\fP <= 0 (or left to the default), but \fB\&.match()\fP was not called earlier in the same task scope (client or backend context). .IP \(bu 2 \fBn\fP <= 0, but the previous \fB\&.match()\fP call returned \fBfalse\fP\&. .IP \(bu 2 \fBn\fP <= 0 and the \fBselect\fP ENUM is \fBUNIQUE\fP (or default), but more than one pattern matched in the previous \fB\&.match()\fP call. This can be avoided by checking for \fB\&.nmatches() == 1\fP\&. .UNINDENT .IP \(bu 2 No string was associated with the pattern selected by \fBn\fP and \fBselect\fP; that is, the \fBstring\fP parameter was not set in the \fB\&.add()\fP call that added the pattern. This can be avoided by checking the \fB\&.saved()\fP method (see below). .UNINDENT .sp Examples: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # Match the request URL against a set of patterns, and generate # a synthetic redirect response with a Location header derived # from the string assoicated with the matching pattern. # In the first example, exactly one pattern in the set matches. sub vcl_init { # With anchor=both, we specify exact matches. new matcher = re2.set(anchor=both); matcher.add(\(dq/foo/bar\(dq, \(dq/baz/quux\(dq); matcher.add(\(dq/baz/bar/foo\(dq, \(dq/baz/quux/foo\(dq); matcher.add(\(dq/quux/bar/baz/foo\(dq, \(dq/baz/quux/foo/bar\(dq); matcher.compile(); } sub vcl_recv { if (matcher.match(req.url)) { # Confirm that there was exactly one match if (matcher.nmatches() != 1) { return(fail); } # Divert to vcl_synth, sending the string associated # with the matching pattern in the \(dqreason\(dq field. return(synth(1301, matcher.string())); } } sub vcl_synth { # Construct a redirect response, using the path set in # resp.reason. if (resp.status == 1301) { set resp.http.Location = \(dqhttp://otherdomain.org\(dq + resp.reason; set resp.status = 301; set resp.reason = \(dqMoved Permanently\(dq; return(deliver); } } # In the second example, the patterns that may match have # common prefixes, and more than one pattern may match. We # add patterns to the set in a \(dqmore specific\(dq to \(dqless # specific\(dq order, and we choose the most specific pattern # that matches, by specifying the first matching pattern in # the set. sub vcl_init { # With anchor=start, we specify matching prefixes. new matcher = re2.set(anchor=start); matcher.add(\(dq/foo/bar/baz/quux\(dq, \(dq/baz/quux\(dq); matcher.add(\(dq/foo/bar/baz\(dq, \(dq/baz/quux/foo\(dq); matcher.add(\(dq/foo/bar\(dq, \(dq/baz/quux/foo/bar\(dq); matcher.add(\(dq/foo\(dq, \(dq/baz\(dq); matcher.compile(); } sub vcl_recv { if (matcher.match(req.url)) { # Select the first matching pattern return(synth(1301, matcher.string(select=FIRST))); } } # vcl_synth is implemented as shown above .ft P .fi .UNINDENT .UNINDENT .SS BACKEND xset.backend(INT n, ENUM select) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C BACKEND xset.backend( INT n=0, ENUM {FIRST, LAST, UNIQUE} select=UNIQUE ) .ft P .fi .UNINDENT .UNINDENT .sp Returns the backend associated with the \fInth\fP pattern added to the set, or with the pattern in the set that matched in the most recent call to \fB\&.match()\fP in the same task scope (client or backend context). .sp The rules for selecting a pattern from the set and its associated backend based on \fBn\fP and \fBselect\fP are the same as described above for \fB\&.string()\fP\&. .sp \fB\&.backend()\fP invokes VCL failure under the same conditions described for \fB\&.string()\fP above \-\- \fBn\fP and \fBselect\fP are invalid, or no backend was associated with the selected pattern with the \fB\&.add()\fP method (see \fI\%ERRORS\fP). .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # Choose a backend based on the URL prefix. # In this example, assume that backends b1 through b4 # have been defined. sub vcl_init { # Use anchor=start to match prefixes. # The prefixes are unique, so exactly one will match. new matcher = re2.set(anchor=start); matcher.add(\(dq/foo\(dq, backend=b1); matcher.add(\(dq/bar\(dq, backend=b2); matcher.add(\(dq/baz\(dq, backend=b3); matcher.add(\(dq/quux\(dq, backend=b4); matcher.compile(); } sub vcl_recv { if (matcher.match(req.url)) { # Confirm that there was exactly one match if (matcher.nmatches() != 1) { return(fail); } # Set the backend hint to the backend associated # with the matching pattern. set req.backend_hint = matcher.backend(); } } .ft P .fi .UNINDENT .UNINDENT .SS INT xset.integer(INT n, ENUM select) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C INT xset.integer( INT n=0, ENUM {FIRST, LAST, UNIQUE} select=UNIQUE ) .ft P .fi .UNINDENT .UNINDENT .sp Returns the integer associated with the \fInth\fP pattern added to the set, or with the pattern in the set that matched in the most recent call to \fB\&.match()\fP in the same task scope. .sp The rules for selecting a pattern from the set and its associated integer based on \fBn\fP and \fBselect\fP are the same as described above for \fB\&.string()\fP\&. .sp \fB\&.integer()\fP invokes VCL failure under the same error conditions described for \fB\&.string()\fP above \-\- \fBn\fP and \fBselect\fP are invalid, or no integer was associated with the selected pattern with the \fB\&.add()\fP method (see \fI\%ERRORS\fP). .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # Generate redirect responses based on the Host header. In the # example, subdomains are removed in the new Location, and the # associated integer is used to set the redirect status code. sub vcl_init { # No more than one pattern can match the same string. So it # is safe to call .integer() with default select=UNIQUE in # vcl_recv below (no risk of VCL failure). new redir = re2.set(anchor=both); redir.add(\(dqwww\e.[^.]+\e.foo\e.com\(dq, integer=301, string=\(dqwww.foo.com\(dq); redir.add(\(dqwww\e.[^.]+\e.bar\e.com\(dq, integer=302, string=\(dqwww.bar.com\(dq); redir.add(\(dqwww\e.[^.]+\e.baz\e.com\(dq, integer=303, string=\(dqwww.baz.com\(dq); redir.add(\(dqwww\e.[^.]+\e.quux\e.com\(dq, integer=307, string=\(dqwww.quux.com\(dq); redir.compile(); } sub vcl_recv { if (redir.match(req.http.Host)) { # Construct a Location header that will be used in the # synthetic redirect response. set req.http.Location = \(dqhttp://\(dq + redir.string() + req.url; # Set the response status from the associated integer. return( synth(redir.integer()) ); } } sub vcl_synth { if (resp.status >= 301 && resp.status <= 307) { # We come here from the synth return for the redirect # response. The status code was set from .integer(). set resp.http.Location = req.http.Location; return(deliver); } } .ft P .fi .UNINDENT .UNINDENT .SS STRING xset.sub(STRING text, STRING rewrite, STRING fallback, INT n, ENUM select) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C STRING xset.sub( STRING text, STRING rewrite, STRING fallback=\(dq**SUB METHOD FAILED**\(dq, INT n=0, ENUM {FIRST, LAST, UNIQUE} select=UNIQUE ) .ft P .fi .UNINDENT .UNINDENT .sp Returns the result of the method call \fB\&.sub(text, rewrite, fallback)\fP, as documented above for the \fBregex\fP interface, invoked on the \fInth\fP pattern added to the set, or on the pattern in the set that matched in the most recent call to \fB\&.match()\fP in the same task scope. .sp \fB\&.sub()\fP requires that the pattern it identifies was saved as an internal \fBregex\fP object, by setting \fBsave\fP to true when it was added with the \fB\&.add()\fP method. .sp The associated pattern is determined by \fBn\fP and \fBselect\fP according to the rules given above. If an internal \fBregex\fP object was saved for that pattern, then the result of the \fB\&.sub()\fP method invoked on that object is returned. .sp \fB\&.sub()\fP invokes VCL failkure (see \fI\%ERRORS\fP) if: .INDENT 0.0 .IP \(bu 2 The values of \fBn\fP and \fBselect\fP are invalid, according to the rules given above. .IP \(bu 2 \fBsave\fP was false in the \fB\&.add()\fP method for the pattern identified by \fBn\fP and \fBselect\fP; that is, no internal \fBregex\fP object was saved on which the \fB\&.sub()\fP method could have been invoked. .IP \(bu 2 The \fB\&.sub()\fP method invoked on the \fBregex\fP object fails for any of the reasons described for \fBregex.sub()\fP\&. .UNINDENT .sp Examples: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # Generate synthethic redirect responses on URLs that match a set of # patterns, rewriting the URL according to the matched pattern. # In this example, we set the new URL in the redirect location to # the path that comes after the prefix of the original req.url. sub vcl_init { new matcher = re2.set(anchor=start); matcher.add(\(dq/foo/(.*)\(dq, save=true); matcher.add(\(dq/bar/(.*)\(dq, save=true); matcher.add(\(dq/baz/(.*)\(dq, save=true); matcher.compile(); } sub vcl_recv { if (matcher.match(req.url)) { if (matcher.nmatches() != 1) { return(fail); } return(synth(1301)); } } sub vcl_synth { if (resp.status == 1301) { # matcher.sub() rewrites the URL to the subpath after the # original prefix. set resp.http.Location = \(dqhttp://www.otherdomain.org\(dq + matcher.sub(req.url, \(dq/\e1\(dq); return(deliver); } } .ft P .fi .UNINDENT .UNINDENT .SS STRING xset.suball(STRING text, STRING rewrite, STRING fallback, INT n, ENUM select) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C STRING xset.suball( STRING text, STRING rewrite, STRING fallback=\(dq**SUBALL METHOD FAILED**\(dq, INT n=0, ENUM {FIRST, LAST, UNIQUE} select=UNIQUE ) .ft P .fi .UNINDENT .UNINDENT .sp Like the \fB\&.sub()\fP method, this returns the result of calling \fB\&.suball(text, rewrite, fallback)\fP from the regex interface on the \fInth\fP pattern added to the set, or the pattern that most recently matched in a \fB\&.match()\fP call. .sp \fB\&.suball()\fP is subject to the same conditions as the \fB\&.sub()\fP method: .INDENT 0.0 .IP \(bu 2 The pattern to which it is applied is identified by \fBn\fP and \fBselect\fP according to the rules given above. .IP \(bu 2 It fails if: .INDENT 2.0 .IP \(bu 2 The pattern that it identifies was not saved with \fB\&.add(save=true)\fP\&. .IP \(bu 2 The values of \fBn\fP or \fBselect\fP are invalid. .IP \(bu 2 The \fB\&.suball()\fP method invoked on the saved \fBregex\fP object fails. .UNINDENT .UNINDENT .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # In any URL that matches one of the words given below, replace all # occurrences of the matching word with \(dqquux\(dq (for example to # rewrite path components or elements of query strings). sub vcl_init { new matcher = re2.set(); matcher.add(\(dq\ebfoo\eb\(dq, save=true); matcher.add(\(dq\ebbar\eb\(dq, save=true); matcher.add(\(dq\ebbaz\eb\(dq, save=true); matcher.compile(); } sub vcl_recv { if (matcher.match(req.url)) { if (matcher.nmatches() != 1) { return(fail); } set req.url = matcher.suball(req.url, \(dqquux\(dq); } } .ft P .fi .UNINDENT .UNINDENT .SS STRING xset.extract(STRING text, STRING rewrite, STRING fallback, INT n, ENUM select) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C STRING xset.extract( STRING text, STRING rewrite, STRING fallback=\(dq**EXTRACT METHOD FAILED**\(dq, INT n=0, ENUM {FIRST, LAST, UNIQUE} select=UNIQUE ) .ft P .fi .UNINDENT .UNINDENT .sp Like the \fB\&.sub()\fP and \fB\&.suball()\fP methods, this method returns the result of calling \fB\&.extract(text, rewrite, fallback)\fP from the regex interface on the \fInth\fP pattern added to the set, or the pattern that most recently matched in a \fB\&.match()\fP call. .sp \fB\&.extract()\fP is subject to the same conditions as the other rewrite methods: .INDENT 0.0 .IP \(bu 2 The pattern to which it is applied is identified by \fBn\fP and \fBselect\fP according to the rules given above. .IP \(bu 2 It fails if: .INDENT 2.0 .IP \(bu 2 The pattern that it identifies was not saved with \fB\&.add(save=true)\fP\&. .IP \(bu 2 The values of \fBn\fP or \fBselect\fP are invalid. .IP \(bu 2 The \fB\&.extract()\fP method invoked on the saved \fBregex\fP object fails. .UNINDENT .UNINDENT .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # Rewrite any URL that matches one of the patterns in the set # by exchanging the path components. sub vcl_init { new matcher = re2.set(anchor=both); matcher.add(\(dq/(foo)/(bar)/\(dq, save=true); matcher.add(\(dq/(bar)/(baz)/\(dq, save=true); matcher.add(\(dq/(baz)/(quux)/\(dq, save=true); matcher.compile(); } sub vcl_recv { if (matcher.match(req.url)) { if (matcher.nmatches() != 1) { return(fail); } set req.url = matcher.extract(req.url, \(dq/\e2/\e1/\(dq); } } .ft P .fi .UNINDENT .UNINDENT .SS SUB xset.subroutine(INT n, ENUM select) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C SUB xset.subroutine( INT n=0, ENUM {FIRST, LAST, UNIQUE} 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 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 (See \fI\%ERRORS\fP) if: .INDENT 0.0 .IP \(bu 2 The rules for \fBn\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 = \(dqbut only in deliver or synth\(dq; } sub vcl_init { new myset = re2.set(); myset.add(\(dq/foo\(dq, sub=resp_sub); myset.add(\(dq/foo/bar\(dq, sub=some_other_sub); # ... } sub vcl_deliver { if (myset.match(req.url)) { call myset.subroutine(select=FIRST); } } .ft P .fi .UNINDENT .UNINDENT .SS BOOL xset.check_call(INT n, ENUM select) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C BOOL xset.check_call( INT n=0, ENUM {FIRST, LAST, UNIQUE} 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 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. 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 \fBNotice\fP messages in the log produced by this VMOD are always prefixed with the string .nf \(ga\(ga .fi vmod_re2: .nf \(ga\(ga .fi \&. .IP "System Message: WARNING/2 (vmod_re2.man.rst:, line 1748)" Inline literal start\-string without end\-string. .IP "System Message: WARNING/2 (vmod_re2.man.rst:, line 1748)" Inline literal start\-string without end\-string. .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # Assume that myset is declared as in the example above. sub vcl_deliver { if (myset.match(req.url)) { if (myset.check_call(select=FIRST)) { call myset.subroutine(select=FIRST); } else { call do_if_resp_sub_is_illegal; } } } .ft P .fi .UNINDENT .UNINDENT .SS BOOL xset.saved(ENUM which, INT n, ENUM select) .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C BOOL xset.saved( ENUM {REGEX, STR, BE, INT, SUB} which=REGEX, INT n=0, ENUM {FIRST, LAST, UNIQUE} select=UNIQUE ) .ft P .fi .UNINDENT .UNINDENT .sp Returns true if and only if an object of the type indicated by \fBwhich\fP was saved at initialization time for the \fBnth\fP pattern added to the set, or for the pattern indicated by \fBselect\fP after the most recent \fB\&.match()\fP call. .sp In other words, \fB\&.saved()\fP returns true: .INDENT 0.0 .IP \(bu 2 for \fBwhich=REGEX\fP if the individual regex was saved with \fB\&.add(save=true)\fP for the indicated pattern .IP \(bu 2 for \fBwhich=STR\fP if a string was stored with the \fBstring\fP parameter in \fB\&.add()\fP .IP \(bu 2 for \fBwhich=BE\fP if a backend was stored with the \fBbackend\fP attribute. .IP \(bu 2 for \fBwhich=INT\fP if an integer was stored with the \fBinteger\fP attribute. .IP \(bu 2 for \fBwhich=SUB\fP if an integer was stored with the \fBsub\fP attribute. .UNINDENT .sp The default value of \fBwhich\fP is \fBREGEX\fP\&. .sp The pattern in the set is identified by \fBn\fP and \fBselect\fP according to the rules given above. \fB\&.saved()\fP invokes VCL failure if the values of \fBn\fP or \fBselect\fP are invalid (see \fI\%ERRORS\fP). .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C sub vcl_init { new s = re2.set(); s.add(\(dq1\(dq, save=true, string=\(dq1\(dq, backend=b1); s.add(\(dq2\(dq, save=true, string=\(dq2\(dq); s.add(\(dq3\(dq, save=true, backend=b3); s.add(\(dq4\(dq, save=true); s.add(\(dq5\(dq, string=\(dq5\(dq, backend=b5); s.add(\(dq6\(dq, string=\(dq6\(dq); s.add(\(dq7\(dq, backend=b7); s.add(\(dq8\(dq); s.compile(); } # Then the following holds for this set: # s.saved(n=1) == true # for any value of which # s.saved(which=REGEX, n=2) == true # s.saved(which=STR, n=2) == true # s.saved(which=BE, n=2) == false # s.saved(which=REGEX, n=3) == true # s.saved(which=STR, n=3) == false # s.saved(which=BE, n=3) == true # s.saved(which=REGEX, n=4) == true # s.saved(which=STR, n=4) == false # s.saved(which=BE, n=4) == false # s.saved(which=REGEX, n=5) == false # s.saved(which=STR, n=5) == true # s.saved(which=BE, n=5) == true # s.saved(which=REGEX, n=6) == false # s.saved(which=STR, n=6) == true # s.saved(which=BE, n=6) == false # s.saved(which=REGEX, n=7) == false # s.saved(which=STR, n=7) == false # s.saved(which=BE, n=7) == true # s.saved(n=8) == false # for any value of which if (s.match(\(dq4\(dq)) { # The fourth pattern has been uniquely matched. # So in this context: s.saved() == true # Since save=true was used in .add() for the 4th pattern, # and which=REGEX by default. } .ft P .fi .UNINDENT .UNINDENT .SS VOID xset.hdr_filter(HTTP, BOOL whitelist=1) .sp Filters the headers in the HTTP object, which may be one of \fBreq\fP, \fBresp\fP, \fBbereq\fP, or \fBberesp\fP\&. In other words, filter the headers in the client or backend request or response. .sp If \fBwhitelist\fP is \fBtrue\fP, then headers that match one of the patterns in the set are retained, and all other headers are removed. Otherwise, headers that match a pattern in the set are removed, and all others are retained. By default, \fBwhitelist\fP is \fBtrue\fP\&. .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C sub vcl_init { # Header whitelist new white = re2.set(anchor=start); white.add(\(dqFoo:\(dq); white.add(\(dqBar:\(dq); white.add(\(dqBaz: baz$\(dq); white.compile(); # Header blacklist new black = re2.set(anchor=start); black.add(\(dqChaotic:\(dq); black.add(\(dqEvil:\(dq); black.add(\(dqWicked: wicked$\(dq); black.compile(); } sub vcl_recv { # Filter the client request header with the whitelist. # Headers that do not match any pattern in the set are removed. white.hdr_filter(req); } sub vcl_deliver { # Filter the client response header with the blacklist. # Headers that match any pattern in the set are removed. black.hdr_filter(resp, false); } .ft P .fi .UNINDENT .UNINDENT .SS STRING quotemeta(STRING) .sp Returns a copy of the argument string with all regex metacharacters escaped via backslash. When the returned string is used as a regular expression, it will exactly match the original string, regardless of any special characters. This function has a purpose similar to a \fB\eQ..\eE\fP sequence within a regex, or the \fBliteral=true\fP setting in a regex constructor. .sp The function invokes VCL failure if there is insufficient workspace for the return string (see \fI\%ERRORS\fP). .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # The following are always true: re2.quotemeta(\(dq1.5\-2.0?\(dq) == \(dq1\e.5\e\-2\e.0\e?\(dq re2.match(re2.quotemeta(\(dq1.5\-2.0?\(dq), \(dq1.5\-2.0?\(dq) .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(\(dqUsing VMOD re2 version: \(dq + re2.version()); .ft P .fi .UNINDENT .UNINDENT .SH ERRORS .sp Functions and methods of the VMOD may invoke VCL failure under unrecoverable error conditions. The effects of VCL failure depend on the VCL subroutine in which it takes place: .INDENT 0.0 .IP \(bu 2 If invoked during \fBvcl_init\fP, then the VCL load fails, and an error message is returned over the CLI (reported by \fBvarnishadm(1)\fP). .IP \(bu 2 If invoked during any other subroutine besides \fBvcl_synth\fP, then an error message is recorded in the log with the \fBVCL_Error\fP tag, further processing is aborted immediately, and a response with status 503 (Service Not Available) is returned with the reason string \(dqVCL failed\(dq. .IP \(bu 2 If invoked during \fBvcl_synth\fP, then further processing is aborted, the error message is logged with \fBVCL_Error\fP, and the client connection is immediately closed \-\- the client receives no response. .UNINDENT .sp Errors that lead to VCL failure include: .INDENT 0.0 .IP \(bu 2 Any regex compilation failure. .IP \(bu 2 Out of workspace errors (see \fI\%LIMITATIONS\fP). .IP \(bu 2 Failures reported by the RE2 library for: matches, backrefs, namedrefs, the rewrite operations (sub, suball and extract), the \fB\&.cost()\fP function or method, and the \fB\&.quotemeta()\fP function. The VMOD detects most common errors that would lead to library errors, and invokes VCL failure in such cases without calling the library. But library errors may happen under conditions such as out of memory. .IP \(bu 2 Functions and methods that require a previous successful match when there was no prior match, or when the previous match was unsuccessful. These include backrefs, namedrefs, and the data retrieval methods for set objects. .IP \(bu 2 Any of the following parameters are undefined, for example when set from an unset header: fallbacks; patterns for the regex functions (which are compiled at runtime); the text and rewrite parameters for rewrite operations; the name parameter for namedrefs. .IP \(bu 2 The name parameter for namedrefs is the empty string. .IP \(bu 2 Backref number is out of range (greater than the number of backrefs in the pattern). .IP \(bu 2 Backref or namedref attempted when the \fBnever_capture\fP option was set to \fBtrue\fP for the pattern. .IP \(bu 2 For set objects: .INDENT 2.0 .IP \(bu 2 Numeric index (parameter \fBn\fP) is out of range (greater than the number of patterns in the set). .IP \(bu 2 Use of \fBselect=UNIQUE\fP after more than one pattern was matched. The \fB\&.nmatches()\fP can be used to check for this condition, to avoid VCL failure \-\- \fBUNIQUE\fP will fail in \fB\&.namtches()\fP > 1. .IP \(bu 2 Retrieval of data from a set (such as a string, backend etc) by numeric index (\fBn\fP) or \(dqassociatively\(dq (after a match) when no such object was saved for the corresponding pattern. Use the \fB\&.saved()\fP and \fB\&.check_call()\fP methods to check for this. .IP \(bu 2 Calling the subroutine returned by \fB\&.subrooutine()\fP may be illegal, if it is not permitted in the subroutine from which it is called, or if it would lead to recursive calls. Use the \fB\&.check_call()\fP method to check for this. .UNINDENT .UNINDENT .SH REQUIREMENTS .sp The VMOD requires Varnish since version 6.6, or the master branch. See the source repository for versions of the VMOD that are compatible with other Varnish versions. .sp It requires the RE2 library, and has been tested against RE2 versions since 2015\-06\-01 (through 2021\-04\-01 at the time of writing). .sp If the VMOD is built against versions of RE2 since 2017\-12\-01, it uses a version of the set match operation that reports out\-of\-memory conditions during a match. (Versions of RE2 since June 2019 no longer have this error, but nevertheless the different internal call is used for set matches.) In that case, the VMOD is not compatible with earlier versions of RE2. This is only a problem if the runtime version of the library differs from the version against which the VMOD was built. If you encounter this error, consider re\-building the VMOD against the runtime version of RE2, or installing a newer version of RE2. .SH INSTALLATION .sp See \fI\%INSTALL.rst\fP in the source repository. .SH LIMITATIONS .sp The VMOD allocates Varnish workspace for captured groups and rewritten strings. If operations fail with \(dqinsufficient workspace\(dq error messages in the Varnish log (with the \fBVCL_Error\fP tag), increase the varnishd runtime parameters \fBworkspace_client\fP and/or \fBworkspace_backend\fP\&. .sp The RE2 documentation states that successful matches are slowed quite a bit when they also capture substrings. There is also additional overhead from the VMOD, unless the \fBnever_capture\fP flag is true, to manage data about captured groups in the workspace. This overhead is incurred even if there are no capturing expressions in a pattern, since it is always possible to call \fBbackref(0)\fP to obtain the matched portion of a string. .sp So if you are using a pattern only to match against strings, and never to capture subexpressions, consider setting the \fBnever_capture\fP option to true, to eliminate the extra work for both RE2 and the VMOD. .SH AUTHOR .INDENT 0.0 .IP \(bu 2 Geoffrey Simmons <\fI\%geoff@uplex.de\fP> .UNINDENT .sp UPLEX Nils Goroll Systemoptimierung .SH SEE ALSO .INDENT 0.0 .IP \(bu 2 varnishd(1) .IP \(bu 2 vcl(7) .IP \(bu 2 VMOD source repository: \fI\%https://code.uplex.de/uplex\-varnish/libvmod\-re2\fP .INDENT 2.0 .IP \(bu 2 Gitlab mirror: \fI\%https://gitlab.com/uplex/varnish/libvmod\-re2\fP .UNINDENT .IP \(bu 2 RE2 git repo: \fI\%https://github.com/google/re2\fP .IP \(bu 2 RE2 syntax: \fI\%https://github.com/google/re2/wiki/Syntax\fP .IP \(bu 2 \(dqImplementing Regular Expressions\(dq: \fI\%https://swtch.com/~rsc/regexp/\fP .INDENT 2.0 .IP \(bu 2 Series of articles motivating the design of RE2, with discussion of how RE2 compares with PCRE .UNINDENT .UNINDENT .SH COPYRIGHT .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C Copyright (c) 2016\-2018 UPLEX Nils Goroll Systemoptimierung All rights reserved Author: Geoffrey Simmons See LICENSE .ft P .fi .UNINDENT .UNINDENT .\" Generated by docutils manpage writer. .