-gzipbody "The gzip response takes more space than the plain one"
rxreq
expect req.url == "/brfirst"
expect req.http.Accept-Encoding == "gzip"
txresp -hdr "Vary: Accept-Encoding" \
-gziplevel 9 \
-gzipbody "The gzip response takes more space than the plain one"
# no CE br first
rxreq
expect req.url == "/nocebr"
expect req.http.Accept-Encoding == "gzip"
txresp -body "Plain as flour"
# no CE gzip first
rxreq
expect req.url == "/nocegzip"
expect req.http.Accept-Encoding == "gzip"
txresp -body "Plain as flour"
# compressible br first
rxreq
expect req.url == "/compbr"
expect req.http.Accept-Encoding == "gzip"
txresp -hdr "Content-Type: text/plain" \
-body {Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.}
# compressible gzip first
rxreq
expect req.url == "/compgzip"
expect req.http.Accept-Encoding == "gzip"
txresp -hdr "Content-Type: text/plain" \
-body {Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.}
} -start
# vary: our three vairants are:
#
# Accept-Encoding V-A-E
# plain undef undef
# gzip gzip undef
# bt gzip br
#
# we prefer br if the client supports it
#
# the primary cache object is always the gzip variant, we add the br
# variant through a recursive request back to ourself
varnish v1 \
-arg "-a vtc=${tmpdir}/vtc.sock" \
-arg "-a self_plain=${tmpdir}/self_plain.sock" \
-arg "-p vsl_mask=+VfpAcct" \
-vcl+backend {
import std;
import ${vmod_brotli};
backend self_plain {
.path = "${tmpdir}/self_plain.sock";
}
sub br_recv {
unset req.http.V-A-E;
if (local.socket == "self_plain") {
set req.hash_ignore_busy = true;
if (req.http.Is-Bgfetch == "true") {
set req.grace = 0s;
}
return (hash);
}
if (req.restarts == 0) {
if (req.http.Accept-Encoding) {
set req.http.O-A-E = req.http.Accept-Encoding;
}
if (req.http.Accept-Encoding ~ "\bbr\b") {
set req.http.V-A-E = "br";
}
}
}
sub br_backend_fetch {
# for uncacheable requests, varnish has not fiddled
# A-E, so we see the original
if (! bereq.uncacheable && bereq.http.V-A-E) {
if (bereq.http.V-A-E != "br") {
return (abandon);
}
unset bereq.http.O-A-E;
unset bereq.http.Accept-Encoding;
set bereq.http.Is-Bgfetch = bereq.is_bgfetch;
set bereq.backend = self_plain;
return (fetch);
}
}
sub br_backend_response {
if (bereq.backend == self_plain) {
if (beresp.http.Vary !~ "(?i)\bAccept-Encoding\b") {
# the other cache copy is it
set beresp.ttl = 0s;
set beresp.grace = 0s;
set beresp.keep = 0s;
return (deliver);
}
if (beresp.http.Content-Encoding ||
beresp.http.Vary !~ "\bV-A-E\b") {
# assertion failure
return (abandon);
}
set beresp.filters = "br";
if (beresp.http.X-TTL) {
set beresp.ttl =
std.duration(beresp.http.X-TTL + "s", 1s);
unset beresp.http.X-TTL;
} else {
set beresp.uncacheable = true;
}
return (deliver);
}
if (beresp.http.Content-Type ~ "^text/|^application/(?:(?:x-)?javascript|json|x?html|xml|rss)|^image/(?:svg|x-icon)") {
set beresp.do_gzip = true;
if (! beresp.http.Vary) {
set beresp.http.Vary = "Accept-Encoding";
} else if (beresp.http.Vary !~ "(?i)\bAccept-Encoding\b") {
set beresp.http.Vary = beresp.http.Vary +
", Accept-Encoding";
}
}
if (beresp.http.Vary ~ "(?i)\bAccept-Encoding\b") {