Commit 43148113 authored by Lasse Karstensen's avatar Lasse Karstensen

Convert VCL to Varnish 4.0 format.

No changes to the ruleset itself.
parent 277c0161
......@@ -39,17 +39,16 @@ VCL::
# 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) {
sub vcl_backend_response {
if (bereq.http.X-UA-Device) {
if (!beresp.http.Vary) { # no Vary at all
set beresp.http.Vary = "X-UA-Device";
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";
}
set beresp.http.Vary = beresp.http.Vary + ", X-UA-Device";
}
}
# remove comment for testing, be careful to use this in prod
# Google might be worried about crafted content
# set beresp.http.X-UA-Device = req.http.X-UA-Device;
# comment this out if you don't want the client to know your classification
set beresp.http.X-UA-Device = bereq.http.X-UA-Device;
}
# to keep any caches in the wild from serving wrong content to client #2 behind them, we need to
......@@ -88,26 +87,24 @@ VCL::
sub vcl_recv { call devicedetect; }
# override the header before it is sent to the backend
sub vcl_miss { if (req.http.X-UA-Device) { set bereq.http.User-Agent = req.http.X-UA-Device; } }
sub vcl_pass { if (req.http.X-UA-Device) { set bereq.http.User-Agent = req.http.X-UA-Device; } }
sub vcl_backend_fetch { if (bereq.http.X-UA-Device) { set bereq.http.User-Agent = bereq.http.X-UA-Device; } }
# standard Vary handling code from previous examples.
sub vcl_fetch {
if (req.http.X-UA-Device) {
sub vcl_backend_response {
if (bereq.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";
}
}
set beresp.http.X-UA-Device = req.http.X-UA-Device;
set beresp.http.X-UA-Device = bereq.http.X-UA-Device;
}
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");
}
}
.. 072-example2-end
Example 3: Add the device class as a GET query parameter
......@@ -125,26 +122,23 @@ VCL::
include "devicedetect.vcl";
sub vcl_recv { call devicedetect; }
sub append_ua {
if ((req.http.X-UA-Device) && (req.request == "GET")) {
# do this after vcl_hash, so all Vary-ants can be purged in one go. (avoid ban()ing)
sub vcl_backend_fetch {
if ((bereq.http.X-UA-Device) && (bereq.method == "GET")) {
# if there are existing GET arguments;
if (req.url ~ "\?") {
set req.http.X-get-devicetype = "&devicetype=" + req.http.X-UA-Device;
} else {
set req.http.X-get-devicetype = "?devicetype=" + req.http.X-UA-Device;
if (bereq.url ~ "\?") {
set bereq.http.X-get-devicetype = "&devicetype=" + bereq.http.X-UA-Device;
} else {
set bereq.http.X-get-devicetype = "?devicetype=" + bereq.http.X-UA-Device;
}
set req.url = req.url + req.http.X-get-devicetype;
unset req.http.X-get-devicetype;
set bereq.url = bereq.url + bereq.http.X-get-devicetype;
unset bereq.http.X-get-devicetype;
}
}
# do this after vcl_hash, so all Vary-ants can be purged in one go. (avoid ban()ing)
sub vcl_miss { call append_ua; }
sub vcl_pass { call append_ua; }
# Handle redirects, otherwise standard Vary handling code from previous examples.
sub vcl_fetch {
if (req.http.X-UA-Device) {
sub vcl_backend_response {
if (bereq.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
......@@ -155,10 +149,10 @@ VCL::
# will potentially show the extra address to the client. we don't want that.
# if the backend reorders the get parameters, you may need to be smarter here. (? and & ordering)
if (beresp.status == 301 || beresp.status == 302 || beresp.status == 303) {
set beresp.http.location = regsub(beresp.http.location, "[?&]devicetype=.*$", "");
set beresp.http.Location = regsub(beresp.http.location, "[?&]devicetype=.*$", "");
}
}
set beresp.http.X-UA-Device = req.http.X-UA-Device;
set beresp.http.X-UA-Device = bereq.http.X-UA-Device;
}
sub vcl_deliver {
if ((req.http.X-UA-Device) && (resp.http.Vary)) {
......@@ -202,14 +196,14 @@ VCL::
call devicedetect;
if (req.http.X-UA-Device ~ "^mobile" || req.http.X-UA-device ~ "^tablet") {
error 750 "Moved Temporarily";
return(synth(750, "Moved Temporarily"));
}
}
sub vcl_error {
if (obj.status == 750) {
set obj.http.Location = "http://m.example.com" + req.url;
set obj.status = 302;
sub vcl_synth {
if (resp.status == 750) {
set resp.http.Location = "http://m.example.com" + req.url;
set resp.status = 302;
return(deliver);
}
}
......
......@@ -21,7 +21,7 @@ positives or missing strings, fork the git repository and send a pull request.
Requirements
------------
You need Varnish 3. It may function with some adjustments on previous versions,
You need Varnish 4. It may function with some adjustments on previous versions,
but you are on your own.
It is worth noting that there is no compilation/linking required. This is VCL
......@@ -82,4 +82,4 @@ This project lives on Github:
Feature requests, bug reports and such can be added to the Github issue tracker.
This shiny piece of code is authored by Lasse Karstensen <lasse@varnish-software.com>.
This shiny piece of code is authored by Lasse Karstensen <lkarsten@varnish-software.com>.
#
#
# This file contains some manually classified user-agents that can be used
# to test for false positives.
#
#
# Format: <classification>\t<UA-string>\n
#
......
......@@ -3,6 +3,7 @@
#
# Enable test URLs and cookie overrides.
#
vcl 4.0;
backend devicetest {
.host = "127.0.0.1";
......@@ -12,29 +13,29 @@ backend devicetest {
sub vcl_recv {
# 701/702 are arbitrary chosen return codes that is only used internally in varnish.
if (req.url ~ "^/set_ua_device/.+") {
error 701 regsub(req.url, "^/set_ua_device/", "");
return(synth(701, regsub(req.url, "^/set_ua_device/", ""));
}
# set expired cookie if nothing is specified
if (req.url ~ "^/set_ua_device/") {
error 702 "OK";
return(synth(702, "OK"));
}
if (req.url ~ "^/devicetest") {
set req.backend = devicetest;
}
}
sub vcl_error {
if (obj.status == 701 || obj.status == 702) {
if (obj.status == 702) {
set obj.status = 200;
set obj.http.Set-Cookie = "X-UA-Device-force=expiring; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;";
sub vcl_synth {
if (resp.status == 701 || resp.status == 702) {
if (resp.status == 702) {
set resp.status = 200;
set resp.http.Set-Cookie = "X-UA-Device-force=expiring; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;";
} else {
set obj.status = 200;
set obj.http.Set-Cookie = "X-UA-Device-force=" + obj.response + "; Path=/;";
set resp.status = 200;
set resp.http.Set-Cookie = "X-UA-Device-force=" + resp.response + "; Path=/;";
}
set obj.http.Content-Type = "text/html; charset=utf-8";
set resp.http.Content-Type = "text/html; charset=utf-8";
synthetic {"<html><body><h1>OK, Cookie updated</h1><a href='/devicetest/'>/devicetest/</a></body></html>"};
set obj.response = "OK";
set resp.response = "OK";
return(deliver);
}
}
......@@ -24,7 +24,7 @@
# detectdevice.vcl - regex based device detection for Varnish
# http://github.com/varnish/varnish-devicedetect/
#
# Author: Lasse Karstensen <lasse@varnish-software.com>
# Author: Lasse Karstensen <lkarsten@varnish-software.com>
sub devicedetect {
unset req.http.X-UA-Device;
......
......@@ -13,10 +13,9 @@ varnish v1 -vcl+backend {
} -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"
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 resp.status == 200
}
client c1 -run
......@@ -13,7 +13,7 @@ varnish v1 -vcl+backend {
} -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"
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 resp.http.X-UA-Device == "mobile-android"
......@@ -23,5 +23,4 @@ client c1 {
#expect resp.http.X-UA-Device == "mobile-android"
}
client c1 -run
......@@ -12,14 +12,14 @@ varnish v1 -vcl+backend {
call devicedetect;
if (req.http.X-UA-Device ~ "^mobile" || req.http.X-UA-device ~ "^tablet") {
error 750 "Moved Temporarily";
return(synth(750, "Moved Temporarily"));
}
}
sub vcl_error {
if (obj.status == 750) {
set obj.http.Location = "http://m.example.com" + req.url;
set obj.status = 302;
sub vcl_synth {
if (resp.status == 750) {
set resp.http.Location = "http://m.example.com" + req.url;
set resp.status = 302;
return(deliver);
}
}
......
......@@ -17,16 +17,16 @@ varnish v1 -vcl+backend {
# 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) {
sub vcl_backend_response {
if (bereq.http.X-UA-Device) {
if (!beresp.http.Vary) { # no Vary at all
set beresp.http.Vary = "X-UA-Device";
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";
}
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;
set beresp.http.X-UA-Device = bereq.http.X-UA-Device;
}
# to keep any caches in the wild from serving wrong content to client #2 behind them, we need to
......@@ -39,11 +39,8 @@ varnish v1 -vcl+backend {
} -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"
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
} -run
......@@ -11,34 +11,29 @@ varnish v1 -vcl+backend {
sub vcl_recv { call devicedetect; }
# override the header before it is sent to the backend
sub vcl_miss { if (req.http.X-UA-Device) { set bereq.http.User-Agent = req.http.X-UA-Device; } }
sub vcl_pass { if (req.http.X-UA-Device) { set bereq.http.User-Agent = req.http.X-UA-Device; } }
sub vcl_backend_fetch { if (bereq.http.X-UA-Device) { set bereq.http.User-Agent = bereq.http.X-UA-Device; } }
# standard Vary handling code from previous examples.
sub vcl_fetch {
if (req.http.X-UA-Device) {
sub vcl_backend_response {
if (bereq.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";
}
}
set beresp.http.X-UA-Device = req.http.X-UA-Device;
set beresp.http.X-UA-Device = bereq.http.X-UA-Device;
}
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"
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
} -run
......@@ -9,26 +9,23 @@ varnish v1 -vcl+backend {
include "${projectdir}/../devicedetect.vcl";
sub vcl_recv { call devicedetect; }
sub append_ua {
if ((req.http.X-UA-Device) && (req.request == "GET")) {
# do this after vcl_hash, so all Vary-ants can be purged in one go. (avoid ban()ing)
sub vcl_backend_fetch {
if ((bereq.http.X-UA-Device) && (bereq.method == "GET")) {
# if there are existing GET arguments;
if (req.url ~ "\?") {
set req.http.X-get-devicetype = "&devicetype=" + req.http.X-UA-Device;
} else {
set req.http.X-get-devicetype = "?devicetype=" + req.http.X-UA-Device;
if (bereq.url ~ "\?") {
set bereq.http.X-get-devicetype = "&devicetype=" + bereq.http.X-UA-Device;
} else {
set bereq.http.X-get-devicetype = "?devicetype=" + bereq.http.X-UA-Device;
}
set req.url = req.url + req.http.X-get-devicetype;
unset req.http.X-get-devicetype;
set bereq.url = bereq.url + bereq.http.X-get-devicetype;
unset bereq.http.X-get-devicetype;
}
}
# do this after vcl_hash, so all Vary-ants can be purged in one go. (avoid ban()ing)
sub vcl_miss { call append_ua; }
sub vcl_pass { call append_ua; }
# Handle redirects, otherwise standard Vary handling code from previous examples.
sub vcl_fetch {
if (req.http.X-UA-Device) {
sub vcl_backend_response {
if (bereq.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
......@@ -39,10 +36,10 @@ varnish v1 -vcl+backend {
# will potentially show the extra address to the client. we don't want that.
# if the backend reorders the get parameters, you may need to be smarter here. (? and & ordering)
if (beresp.status == 301 || beresp.status == 302 || beresp.status == 303) {
set beresp.http.location = regsub(beresp.http.location, "[?&]devicetype=.*$", "");
set beresp.http.Location = regsub(beresp.http.location, "[?&]devicetype=.*$", "");
}
}
set beresp.http.X-UA-Device = req.http.X-UA-Device;
set beresp.http.X-UA-Device = bereq.http.X-UA-Device;
}
sub vcl_deliver {
if ((req.http.X-UA-Device) && (resp.http.Vary)) {
......@@ -53,9 +50,7 @@ varnish v1 -vcl+backend {
} -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"
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 resp.http.X-UA-Device == "mobile-android"
}
client c1 -run
} -run
TESTS=*vtc
all: $(TESTS)
#VARNISHD=/usr/sbin/varnishd
#VARNISHTEST=/usr/bin/varnishtest
VARNISHD=/opt/varnish4/sbin/varnishd
VARNISHTEST=/opt/varnish4/bin/varnishtest
.PHONY: all $(TESTS)
TESTS=*.vtc
099-controlset.vtc: ../controlset.txt
all: controlset $(TESTS)
.PHONY: all controlset $(TESTS)
controlset: ../controlset.txt vtc-from-controlset.py
./vtc-from-controlset.py > 099-controlset.vtc
*.vtc:
/usr/bin/varnishtest -Dvarnishd=/usr/sbin/varnishd -Dprojectdir=$(PWD) $@
$(TESTS): controlset
${VARNISHTEST} -Dvarnishd=${VARNISHD} -Dprojectdir=$(PWD) $@
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