Accessing original object in varnish background Fetch

Guillaume Quintard guillaume at varnish-software.com
Thu Mar 4 04:42:33 UTC 2021


Hi,

This expectation is wrong:
             # Here beresp.http.myval (on RHS of assignment expression).
             # should be the original hdr value stored with the object

beresp is the new backend response, and VCL doesn't make the old one
available.

There are two ways to deal with this.

The first one is vmod_stale (
https://docs.varnish-software.com/varnish-cache-plus/vmods/stale/#get-header)
in Varnish Enterprise that will allow you to get direct access to that
header.

The second one, if you want to stick to open source, uses vcl_hit to stash
the old headers in req so that they are available in bereq. That would look
something like this (untested code, use with caution):

sub vcl_recv {
    # avoid injections from the clients
    unset req.http.myval;
}

sub vcl_hit {
    # store the header
    set req.http.myval = obj.http.myval;
}

sub vcl_backend_response {
    # bereq is basically a req copy, so myval crossed over

    # note that I do that unconditionally here, but you can indeed check
bereq.is_bg_fetch

    set beresp.http.myval = bereq.http.myval + " extra string";
}


that should do the trick, however, be careful, the code above will add more
text to the header at every background fetch, so you are more than likely
to hit the header length limit if you are not careful.

Does that make sense?

-- 
Guillaume Quintard


On Wed, Mar 3, 2021 at 8:23 PM Arunabha Saha <arunabha.saha at gmail.com>
wrote:

> Hello,
>       Something I am trying to do is update an existing value in a
> cached object after every background fetch.    I can't seem to figure
> out how to access the existing object parameters during the background
> fetch.
>
> Example
>
> vcl_backend_response {
>       if (!bereq.is_bgfetch) {
>             set beresp.http.myval = beresp.http.val_from_backend;
>       } else {
>              #
>              #Here beresp.http.myval (on RHS of assignment expression).
>              # should be the original hdr value stored with the object
> but i can't seem to access it this way
>              # or any other way.
>              #
>              set beresp.http.myval = beresp.http.myval +
> beresp.http.val_from_backend;
>       }
> }
>
> --
> regards,
> Arun
> _______________________________________________
> varnish-misc mailing list
> varnish-misc at varnish-cache.org
> https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.varnish-cache.org/lists/pipermail/varnish-misc/attachments/20210303/1398b8b6/attachment.html>


More information about the varnish-misc mailing list