Search code examples
varnishvarnish-vclfastlyfastly-vcl

How to extract first n characters of a string in varnish-vcl?


I'm looking for a way to extract the first 'n' characters from a string in VCL. I couldn't find any function like trim(str,starting_pos) or substring(str,len) in the VCL documentation. I've tried searching for this on google and stackoverflow and nothing came up, so I'm asking here. I appreciate your help.


Solution

  • I'm not aware of any such string functions being available in the Fastly Varnish environment.

    However, I think you could accomplish the same using regular expression capture groups.

    set req.http.Foo = "foobar";
    
    if (req.http.Foo ~ "^(.{0,3})") {
      set resp.http.match0 = re.group.0; # this should now equal 'foo'
    }