From af7b3d91089334b2b30b1c50df3b617ec160dc64 Mon Sep 17 00:00:00 2001 From: Nils Goroll Date: Tue, 29 Oct 2024 14:06:12 +0100 Subject: [PATCH] varnishncsa: Add hitmiss hitpass indicators to Varnish:handling --- bin/varnishncsa/varnishncsa.c | 12 +++- bin/varnishtest/tests/u00002.vtc | 93 ++++++++++++++++++++++++++++ doc/changes.rst | 3 + doc/sphinx/reference/varnishncsa.rst | 6 +- 4 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 bin/varnishtest/tests/u00002.vtc diff --git a/bin/varnishncsa/varnishncsa.c b/bin/varnishncsa/varnishncsa.c index 94af8374855..2b1d8e94b56 100644 --- a/bin/varnishncsa/varnishncsa.c +++ b/bin/varnishncsa/varnishncsa.c @@ -1071,6 +1071,14 @@ dispatch_f(struct VSL_data *vsl, struct VSL_transaction * const pt[], case SLT_RespHeader: process_hdr(&CTX.watch_resphdr, b, e); break; + case SLT_HitMiss: + CTX.hitmiss = "miss"; + CTX.handling = "hitmiss"; + break; + case SLT_HitPass: + CTX.hitmiss = "miss"; + CTX.handling = "hitpass"; + break; case SLT_VCL_call: if (!strcasecmp(b, "recv")) { CTX.hitmiss = "-"; @@ -1078,10 +1086,10 @@ dispatch_f(struct VSL_data *vsl, struct VSL_transaction * const pt[], } else if (!strcasecmp(b, "hit")) { CTX.hitmiss = "hit"; CTX.handling = "hit"; - } else if (!strcasecmp(b, "miss")) { + } else if (!strcasecmp(b, "miss") && strcmp(CTX.handling, "hitmiss")) { CTX.hitmiss = "miss"; CTX.handling = "miss"; - } else if (!strcasecmp(b, "pass")) { + } else if (!strcasecmp(b, "pass") && strcmp(CTX.handling, "hitpass")) { CTX.hitmiss = "miss"; CTX.handling = "pass"; } else if (!strcasecmp(b, "synth")) { diff --git a/bin/varnishtest/tests/u00002.vtc b/bin/varnishtest/tests/u00002.vtc new file mode 100644 index 00000000000..f0e26c06ec8 --- /dev/null +++ b/bin/varnishtest/tests/u00002.vtc @@ -0,0 +1,93 @@ +varnishtest "varnishncsa handling" + +server s1 { + rxreq + txresp + rxreq + txresp +} -start + +varnish v1 -vcl+backend { + backend none none; + + sub vcl_backend_fetch { + # XXX would like to do everything in v_b_e, but + # return(pass(x)) is not supported + if (bereq.url == "/hitpass") { + set bereq.backend = s1; + } else { + set bereq.backend = none; + } + } + + sub vcl_backend_response { + set beresp.ttl = 1y; + return (pass(1y)); + } + + sub vcl_backend_error { + set beresp.status = 200; + set beresp.ttl = 1y; + set beresp.body = bereq.url; + if (bereq.url == "/hitmiss") { + set beresp.uncacheable = true; + } + return (deliver); + } + + sub vcl_recv { + if (req.url == "/pass") { + return (pass); + } + if (req.url == "/synth") { + return (synth(204)); + } + } +} -start + +client c1 { + # prime + txreq -url "/hitmiss" + rxresp + expect resp.status == 200 + txreq -url "/hitpass" + rxresp + expect resp.status == 200 + txreq -url "/hit" + rxresp + expect resp.status == 200 + + # re-check + txreq -url "/hitmiss" + rxresp + expect resp.status == 200 + txreq -url "/hitpass" + rxresp + expect resp.status == 200 + txreq -url "/hit" + rxresp + expect resp.status == 200 + + # others + txreq -url "/pass" + rxresp + expect resp.status == 200 + txreq -url "/synth" + rxresp + expect resp.status == 204 +} -run + +shell { + cat <expect +miss /hitmiss +miss /hitpass +miss /hit +hitmiss /hitmiss +hitpass /hitpass +hit /hit +pass /pass +synth /synth +EOF + varnishncsa -d -n ${v1_name} -F "%{Varnish:handling}x %U" >have + diff -u expect have +} diff --git a/doc/changes.rst b/doc/changes.rst index 5f920f8e658..3cf47356155 100644 --- a/doc/changes.rst +++ b/doc/changes.rst @@ -41,6 +41,9 @@ Varnish Cache NEXT (2025-03-15) .. PLEASE keep this roughly in commit order as shown by git-log / tig (new to old) +* The ``hitmiss`` and ``hitpass`` handling indicators have been added to the + ``Varnish:handling`` format of ``varnishncsa``. + * The scope of VCL variables `req.is_hitmiss` and `req.is_hitpass` is now restricted to `vcl_miss, vcl_deliver, vcl_pass, vcl_synth` and `vcl_pass, vcl_deliver, vcl_synth` respectively. diff --git a/doc/sphinx/reference/varnishncsa.rst b/doc/sphinx/reference/varnishncsa.rst index de5ea842801..69d3d2ebd7b 100644 --- a/doc/sphinx/reference/varnishncsa.rst +++ b/doc/sphinx/reference/varnishncsa.rst @@ -189,9 +189,9 @@ Supported formatters are: misses. In backend mode, this field is blank. Varnish:handling - In client mode, one of the 'hit', 'miss', 'pass', 'pipe' or 'synth' strings - indicating how the request was handled. In backend mode, this field is - blank. + In client mode, one of the 'hit', 'hitmiss', 'hitpass', 'miss', 'pass', + 'pipe' or 'synth' strings indicating how the request was handled. In + backend mode, this field is blank. Varnish:side Backend or client side. One of two values, 'b' or 'c', depending