Subroutines
VCL services do not have a single entry point or main function, but rather a number of predefined subroutines that are called at various stages of the request lifecycle. Seeusing VCL to learn more about how and when each subroutine is called. It is also possible to define your own custom subroutines and call them from the built-in ones.
Built-in (lifecycle) subroutines
The following subroutines are part of theVCL lifecycle.Each subroutine represents a different stage of the Varnish state machine.
The prefixvcl_ is reserved and cannot be used in the names of custom subroutines.
Custom subroutines
Custom subroutines are defined using thesub keyword followed by a name,an optional parameter list, and an optional return type.Subroutine parameter and return types many be any of the following:ACL, BACKEND, BOOL, INTEGER, FLOAT, TIME, REGEX, RTIME, STRING, or IP.The code for a subroutine is contained within brackets{}.
Defining aVOID-typed subroutine:
sub compression_check {if (req.http.Accept-Encoding~"gzip|br") {setreq.http.Compression-Accepted-By-Client="yes"; }}Subroutines may optionally have atype,and return a value in accordance with their type:
sub compression_matchesBOOL {if (req.http.Accept-Encoding~"gzip|br") {returntrue; }returnfalse;}If no type is supplied, the type is implicitlyVOID.
Defining aVOID-typed subroutine with a parameter list:
sub url_path_check(STRING var.path) {if (req.url.path== var.path) {setreq.http.X-Is-Valid-Path="yes"; }}sub url_path_check_either(STRING var.path1,STRING var.path2) {if (req.url.path== var.path1||req.url.path== var.path2) {setreq.http.X-Is-Valid-Path="yes"; }}Defining subroutine with a parameter list that returns a value:
sub url_path_is(STRING var.path)BOOL {if (req.url.path== var.path) {returntrue; }returnfalse;}sub url_path_is_either(STRING var.path1,STRING var.path2)BOOL {if (req.url.path== var.path1||req.url.path== var.path2) {returntrue; }returnfalse;}Calling a subroutine
VOID-typed custom subroutines (those that do not return a value) are called using thecall statement.Calling aVOID-typed subroutine:
sub vcl_recv {# set the compression-accepted-by-client header if applicable call compression_check; call compression_check();# Equivalent to the call above.# Check if the request URL path matches a target path. call url_path_check("index.html"); call url_path_check_either("index.html","blog.html");}Typed subroutines other thanVOID are called by placing them to the right hand side of an assignment followed by parentheses().Calling subroutines that return a value:
sub vcl_recv {declare local var.compressedBOOL;set var.compressed= compression_matches();declare local var.is_home_pageBOOL;set var.is_home_page= url_path_is("index.html");declare local var.is_valid_pageBOOL;set var.is_valid_page= url_path_is_either("index.html","blog.html");}Thecall keyword is only for VOID typed subroutines that contain isolated logicyou wish to run.Typed subroutines other than VOID may not be called with thecall keyword.
Returning a value
VOID subroutines may blanketreturn; with no value.These return directly from the subroutine to its caller.
Non-VOID subroutines must return a value in accordancewith their type.These return directly from the subroutine to its caller,and the return value is made available to the caller.
It is permitted to "fall off" the end of a subroutineby not having a return statement, even for subroutines definedto return a non-VOID value.In this case, the value returned to the caller will be a default value.These default values correspond to the default values foruninitialised local variablesof the same type:
sub fINTEGER {}sub g {declare local var.iINTEGER;set var.i= f();# f() returns 0}Returning a state
In theVCL lifecycle, built-in subroutines return a state indicating what Fastly should do next.
HINT: Returning a state is different from returning avalue. All subroutines can return a state. It's also possible for a typed custom subroutine to return a state instead of its declared type. Returning a state from a typed subroutine will still terminate the parent subroutine. For example, this custom subroutine is typed as a BOOL but returns a state which ends the processing ofvcl_recv:
sub compression_matchesBOOL {if (req.http.Accept-Encoding~"gzip|br") {return(lookup);# Ends the parent sub and proceeds to 'lookup' (assignment is cancelled) }returnfalse;# Passes control back to parent sub and assigns the value false}Custom subroutines may also return a state as if they were the built-in subroutine that called them. For instance,vcl_recv might call a custom subroutine that includesreturn (lookup); because "lookup" is avalid return state of recv. This type of return acts as if it had been placed within the calling subroutine, and terminates processing of both the custom subroutine and the parent built-in subroutine.
Returning a state makes custom subroutines sensitive to where they are called from. If a custom subroutine contains areturn (fetch); statement, it can no longer be called from any context other thanvcl_miss (the only built-in subroutine to have a valid return state offetch). The same applies to usingvariables scoped to specific subroutines within custom subroutines.
Concatenation
Built-in subroutines with the same name are automatically concatenated in the order the compiler encounters them. Note that any statement that terminates processing of a subroutine (such asreturn,restart orerror) will make any subsequently defined subroutines with the same name unreachable.
sub vcl_recv {return (lookup);}sub vcl_recv {# this code won't run due to the return statement in the prior vcl_recv# if the order of these two subs were reversed, the origin would be set before returningsetreq.backend= origin;}Custom subroutines may not be concatenated. Multiple declarations of the same (non-built-in) name are not permitted.
Recursion
Subroutines may not call themselves, either directly or via another subroutine. Even if the loop is not infinite, the compiler will still disallow it.
The following code causes an infinite loop and will be disallowed by the compiler:
sub foo { call bar;}sub bar { call foo;}The following code cannot cause an infinite loop, but is still disallowed:
sub foo { call bar;}sub bar {if (false) { call foo; }}