Skip to content

topvar string value not working as expected

I'm not seeing the expected results when using string value for topvar. For a simple test case (https://github.com/johnarne/varnishtest/) I just tried to log the esi requests, using the following VCL:

vcl 4.0;

import std;
import topvar;

sub vcl_init {
  new reqCount = topvar.int();
  new reqLog = topvar.string();
}

backend default {
  .host = "nginx";
}

sub vcl_backend_response {
  set beresp.do_esi = true;
}

sub vcl_recv {
  if (req.url == "/esi-log") {
    return(synth(200));
  }
}

sub vcl_deliver {
  reqLog.set(reqLog.get("") + "Level " + req.esi_level + "<br/>");
  reqCount.set(reqCount.get(0) + 1);
}

sub vcl_synth {
  if (req.url == "/esi-log") {
    synthetic("count: " + reqCount.get(0) + "<div>" + reqLog.get("empty") + "</div>");
    return (deliver);
  }
}

Running this with a simple hierarchy (see https://github.com/johnarne/varnishtest/tree/main/nginx) I was able to get the correct count from reqCount, but reqLog was empty. Or rather an empty string, since it did not write "empty". See https://github.com/johnarne/varnishtest for the full example.