Skip to content

Commit c96f594

Browse files
committed
Create devicedetect.vcl
1 parent 301cd44 commit c96f594

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

devicedetect.vcl

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Copyright (c) 2012-2014 Varnish Software AS
2+
#
3+
# Redistribution and use in source and binary forms, with or without
4+
# modification, are permitted provided that the following conditions
5+
# are met:
6+
# 1. Redistributions of source code must retain the above copyright
7+
# notice, this list of conditions and the following disclaimer.
8+
# 2. Redistributions in binary form must reproduce the above copyright
9+
# notice, this list of conditions and the following disclaimer in the
10+
# documentation and/or other materials provided with the distribution.
11+
#
12+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
13+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15+
# ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
16+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
18+
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19+
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20+
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21+
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
22+
# SUCH DAMAGE.
23+
#
24+
# detectdevice.vcl - regex based device detection for Varnish
25+
# http://github.com/varnish/varnish-devicedetect/
26+
#
27+
# Author: Lasse Karstensen <[email protected]>
28+
29+
sub devicedetect {
30+
unset req.http.X-UA-Device;
31+
set req.http.X-UA-Device = "pc";
32+
33+
# Handle that a cookie may override the detection alltogether.
34+
if (req.http.Cookie ~ "(?i)X-UA-Device-force") {
35+
/* ;?? means zero or one ;, non-greedy to match the first. */
36+
set req.http.X-UA-Device = regsub(req.http.Cookie, "(?i).*X-UA-Device-force=([^;]+);??.*", "\1");
37+
/* Clean up our mess in the cookie header */
38+
set req.http.Cookie = regsuball(req.http.Cookie, "(^|; ) *X-UA-Device-force=[^;]+;? *", "\1");
39+
/* If the cookie header is now empty, or just whitespace, unset it. */
40+
if (req.http.Cookie ~ "^ *$") { unset req.http.Cookie; }
41+
} else {
42+
if (req.http.User-Agent ~ "(?i)(ads|google|bing|msn|yandex|baidu|ro|career|)bot" ||
43+
req.http.User-Agent ~ "(?i)(baidu|jike|symantec)spider" ||
44+
req.http.User-Agent ~ "(?i)scanner" ||
45+
req.http.User-Agent ~ "(?i)(web)crawler") {
46+
set req.http.X-UA-Device = "bot"; }
47+
# elsif (req.http.User-Agent ~ "(?i)ipad") { set req.http.X-UA-Device = "tablet-ipad"; }
48+
elsif (req.http.User-Agent ~ "(?i)ip(hone|od)") { set req.http.X-UA-Device = "mobile-iphone"; }
49+
/* how do we differ between an android phone and an android tablet?
50+
http://stackoverflow.com/questions/5341637/how-do-detect-android-tablets-in-general-useragent */
51+
elsif (req.http.User-Agent ~ "(?i)android.*(mobile|mini)") { set req.http.X-UA-Device = "mobile-android"; }
52+
// android 3/honeycomb was just about tablet-only, and any phones will probably handle a bigger page layout.
53+
elsif (req.http.User-Agent ~ "(?i)android 3") { set req.http.X-UA-Device = "tablet-android"; }
54+
/* see http://my.opera.com/community/openweb/idopera/ */
55+
elsif (req.http.User-Agent ~ "Opera Mobi") { set req.http.X-UA-Device = "mobile-smartphone"; }
56+
// May very well give false positives towards android tablets. Suggestions welcome.
57+
elsif (req.http.User-Agent ~ "(?i)android") { set req.http.X-UA-Device = "tablet-android"; }
58+
elsif (req.http.User-Agent ~ "PlayBook; U; RIM Tablet") { set req.http.X-UA-Device = "tablet-rim"; }
59+
elsif (req.http.User-Agent ~ "hp-tablet.*TouchPad") { set req.http.X-UA-Device = "tablet-hp"; }
60+
elsif (req.http.User-Agent ~ "Kindle/3") { set req.http.X-UA-Device = "tablet-kindle"; }
61+
elsif (req.http.User-Agent ~ "Mobile.+Firefox") { set req.http.X-UA-Device = "mobile-firefoxos"; }
62+
elsif (req.http.User-Agent ~ "^HTC" ||
63+
req.http.User-Agent ~ "Fennec" ||
64+
req.http.User-Agent ~ "IEMobile" ||
65+
req.http.User-Agent ~ "BlackBerry" ||
66+
req.http.User-Agent ~ "BB10.*Mobile" ||
67+
req.http.User-Agent ~ "GT-.*Build/GINGERBREAD" ||
68+
req.http.User-Agent ~ "SymbianOS.*AppleWebKit") {
69+
set req.http.X-UA-Device = "mobile-smartphone";
70+
}
71+
elsif (req.http.User-Agent ~ "(?i)symbian" ||
72+
req.http.User-Agent ~ "(?i)^sonyericsson" ||
73+
req.http.User-Agent ~ "(?i)^nokia" ||
74+
req.http.User-Agent ~ "(?i)^samsung" ||
75+
req.http.User-Agent ~ "(?i)^lg" ||
76+
req.http.User-Agent ~ "(?i)bada" ||
77+
req.http.User-Agent ~ "(?i)blazer" ||
78+
req.http.User-Agent ~ "(?i)cellphone" ||
79+
req.http.User-Agent ~ "(?i)iemobile" ||
80+
req.http.User-Agent ~ "(?i)midp-2.0" ||
81+
req.http.User-Agent ~ "(?i)u990" ||
82+
req.http.User-Agent ~ "(?i)netfront" ||
83+
req.http.User-Agent ~ "(?i)opera mini" ||
84+
req.http.User-Agent ~ "(?i)palm" ||
85+
req.http.User-Agent ~ "(?i)nintendo wii" ||
86+
req.http.User-Agent ~ "(?i)playstation portable" ||
87+
req.http.User-Agent ~ "(?i)portalmmm" ||
88+
req.http.User-Agent ~ "(?i)proxinet" ||
89+
req.http.User-Agent ~ "(?i)sonyericsson" ||
90+
req.http.User-Agent ~ "(?i)symbian" ||
91+
req.http.User-Agent ~ "(?i)windows\ ?ce" ||
92+
req.http.User-Agent ~ "(?i)winwap" ||
93+
req.http.User-Agent ~ "(?i)eudoraweb" ||
94+
req.http.User-Agent ~ "(?i)htc" ||
95+
req.http.User-Agent ~ "(?i)240x320" ||
96+
req.http.User-Agent ~ "(?i)avantgo") {
97+
set req.http.X-UA-Device = "mobile-generic";
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)