Commit 0e998508 authored by Lasse Karstensen's avatar Lasse Karstensen

Working example

parent 82f16357
varnishtest "Test varnish-devicedetect"
server s1 {
rxreq
txresp
} -start
# this example shows how to normalize the user agent string seen by the backend.
varnish v1 -vcl+backend {
include "${projectdir}/../devicedetect.vcl";
sub vcl_recv { call devicedetect; }
sub append_ua_device {
if (req.http.X-UA-Device) {
set bereq.http.X-UA-Device = req.http.X-UA-Device; }
}
# This must be done in vcl_miss and vcl_pass, before any backend request is
# actually sent. vcl_fetch runs after the request to the backend has
# completed.
sub vcl_miss { call append_ua_device; }
sub vcl_pass { call append_ua_device; }
# so, this is a bit conterintuitive. The backend creates content based on the normalized User-Agent,
# but we use Vary on X-UA-Device so Varnish will use the same cached object for all U-As that map to
# the same X-UA-Device.
# If the backend does not mention in Vary that it has crafted special
# content based on the User-Agent (==X-UA-Device), add it.
# If your backend does set Vary: User-Agent, you may have to remove that here.
sub vcl_fetch {
if (req.http.X-UA-Device) {
if (!beresp.http.Vary) { # no Vary at all
set beresp.http.Vary = "X-UA-Device";
} elseif (beresp.http.Vary !~ "X-UA-Device") { # add to existing Vary
set beresp.http.Vary = beresp.http.Vary + ", X-UA-Device";
}
}
# comment this out if you don't want the client to know your classification
set beresp.http.X-UA-Device = req.http.X-UA-Device;
}
# to keep any caches in the wild from serving wrong content to client #2 behind them, we need to
# transform the Vary on the way out.
sub vcl_deliver {
if ((req.http.X-UA-Device) && (resp.http.Vary)) {
set resp.http.Vary = regsub(resp.http.Vary, "X-UA-Device", "User-Agent");
}
}
} -start
client c1 {
txreq -hdr "User-Agent: Mozilla/5.0 (Linux; U; Android 2.2; nb-no; HTC Desire Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"
rxresp
#expect bereq.http.User-Agent == "mobile-android"
expect resp.http.X-UA-Device == "mobile-android"
expect resp.http.Vary == "User-Agent"
}
client c1 -run
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment