Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
V
varnish-devicedetect
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
varnishcache
varnish-devicedetect
Commits
43148113
Commit
43148113
authored
Dec 19, 2014
by
Lasse Karstensen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Convert VCL to Varnish 4.0 format.
No changes to the ruleset itself.
parent
277c0161
Changes
12
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
96 additions
and
110 deletions
+96
-110
INSTALL.rst
INSTALL.rst
+30
-36
README.rst
README.rst
+2
-2
controlset.txt
controlset.txt
+2
-2
devicedetect-dev.vcl
devicedetect-dev.vcl
+12
-11
devicedetect.vcl
devicedetect.vcl
+1
-1
001-load-ruleset.vtc
tests/001-load-ruleset.vtc
+1
-2
020-mobile-android.vtc
tests/020-mobile-android.vtc
+1
-2
065-redir-mobile.vtc
tests/065-redir-mobile.vtc
+6
-6
071-example1.vtc
tests/071-example1.vtc
+8
-11
072-example2.vtc
tests/072-example2.vtc
+6
-11
073-example3.vtc
tests/073-example3.vtc
+15
-20
Makefile
tests/Makefile
+12
-6
No files found.
INSTALL.rst
View file @
43148113
...
...
@@ -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 (
be
req.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 (
be
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";
}
}
set beresp.http.X-UA-Device = req.http.X-UA-Device;
set beresp.http.X-UA-Device =
be
req.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 (
be
req.url ~ "\?") {
set
bereq.http.X-get-devicetype = "&devicetype=" + be
req.http.X-UA-Device;
} else {
set
bereq.http.X-get-devicetype = "?devicetype=" + be
req.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 + be
req.http.X-get-devicetype;
unset
be
req.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 (
be
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
...
...
@@ -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.
l
ocation = regsub(beresp.http.location, "[?&]devicetype=.*$", "");
set beresp.http.
L
ocation = regsub(beresp.http.location, "[?&]devicetype=.*$", "");
}
}
set beresp.http.X-UA-Device = req.http.X-UA-Device;
set beresp.http.X-UA-Device =
be
req.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);
}
}
...
...
README.rst
View file @
43148113
...
...
@@ -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 <l
asse
@varnish-software.com>.
This shiny piece of code is authored by Lasse Karstensen <l
karsten
@varnish-software.com>.
controlset.txt
View file @
43148113
#
#
# This file contains some manually classified user-agents that can be used
# to test for false positives.
#
#
# Format: <classification>\t<UA-string>\n
#
...
...
devicedetect-dev.vcl
View file @
43148113
...
...
@@ -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);
}
}
devicedetect.vcl
View file @
43148113
...
...
@@ -24,7 +24,7 @@
# detectdevice.vcl - regex based device detection for Varnish
# http://github.com/varnish/varnish-devicedetect/
#
# Author: Lasse Karstensen <l
asse
@varnish-software.com>
# Author: Lasse Karstensen <l
karsten
@varnish-software.com>
sub devicedetect {
unset req.http.X-UA-Device;
...
...
tests/001-load-ruleset.vtc
View file @
43148113
...
...
@@ -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
tests/020-mobile-android.vtc
View file @
43148113
...
...
@@ -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
tests/065-redir-mobile.vtc
View file @
43148113
...
...
@@ -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);
}
}
...
...
tests/071-example1.vtc
View file @
43148113
...
...
@@ -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 (
be
req.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 =
be
req.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
tests/072-example2.vtc
View file @
43148113
...
...
@@ -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 (
be
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";
}
}
set beresp.http.X-UA-Device = req.http.X-UA-Device;
set beresp.http.X-UA-Device =
be
req.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
tests/073-example3.vtc
View file @
43148113
...
...
@@ -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 (
be
req.url ~ "\?") {
set
bereq.http.X-get-devicetype = "&devicetype=" + be
req.http.X-UA-Device;
} else {
set
bereq.http.X-get-devicetype = "?devicetype=" + be
req.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 + be
req.http.X-get-devicetype;
unset
be
req.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 (
be
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
...
...
@@ -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.
l
ocation = regsub(beresp.http.location, "[?&]devicetype=.*$", "");
set beresp.http.
L
ocation = regsub(beresp.http.location, "[?&]devicetype=.*$", "");
}
}
set beresp.http.X-UA-Device = req.http.X-UA-Device;
set beresp.http.X-UA-Device =
be
req.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/Makefile
View file @
43148113
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)
$@
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment