File: devicedetect.vcl.fold

package info (click to toggle)
kf6-syntax-highlighting 6.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 47,568 kB
  • sloc: xml: 197,750; cpp: 12,850; python: 3,023; sh: 955; perl: 546; ruby: 488; pascal: 393; javascript: 161; php: 150; jsp: 132; lisp: 131; haskell: 124; ada: 119; ansic: 107; makefile: 96; f90: 94; ml: 85; cobol: 81; yacc: 71; csh: 62; erlang: 54; sql: 51; java: 47; objc: 37; awk: 31; asm: 30; tcl: 29; fortran: 18; cs: 10
file content (113 lines) | stat: -rw-r--r-- 9,305 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#
# Copyright (c) 2016-2018 Varnish Cache project
# Copyright (c) 2012-2016 Varnish Software AS
#
# SPDX-License-Identifier: BSD-2-Clause
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# detectdevice.vcl - regex based device detection for Varnish
# https://github.com/varnishcache/varnish-devicedetect/
#
# Original author: Lasse Karstensen <lkarsten@varnish-software.com>

sub devicedetect <beginfold id='1'>{</beginfold id='1'>
	unset req.http.X-UA-Device;
	set req.http.X-UA-Device = "pc";

	# Handle that a cookie may override the detection alltogether.
	if <beginfold id='2'>(</beginfold id='2'>req.http.Cookie ~ "(?i)X-UA-Device-force"<endfold id='2'>)</endfold id='2'> <beginfold id='3'>{</beginfold id='3'>
		<beginfold id='4'>/*</beginfold id='4'> ;?? means zero or one ;, non-greedy to match the first. <endfold id='4'>*/</endfold id='4'>
		set req.http.X-UA-Device = regsub(req.http.Cookie, "(?i).*X-UA-Device-force=([^;]+);??.*", "\1");
		<beginfold id='4'>/*</beginfold id='4'> Clean up our mess in the cookie header <endfold id='4'>*/</endfold id='4'>
		set req.http.Cookie = regsuball(req.http.Cookie, "(^|; ) *X-UA-Device-force=[^;]+;? *", "\1");
		<beginfold id='4'>/*</beginfold id='4'> If the cookie header is now empty, or just whitespace, unset it. <endfold id='4'>*/</endfold id='4'>
		if <beginfold id='2'>(</beginfold id='2'>req.http.Cookie ~ "^ *$"<endfold id='2'>)</endfold id='2'> <beginfold id='3'>{</beginfold id='3'> unset req.http.Cookie; <endfold id='3'>}</endfold id='3'>
	<endfold id='3'>}</endfold id='3'> else <beginfold id='3'>{</beginfold id='3'>
        if <beginfold id='2'>(</beginfold id='2'>req.http.User-Agent ~ "\(compatible; Googlebot-Mobile/2.1; \+http://www.google.com/bot.html\)" ||
            <beginfold id='2'>(</beginfold id='2'>req.http.User-Agent ~ "(Android|iPhone)" && req.http.User-Agent ~ "\(compatible.?; Googlebot/2.1.?; \+http://www.google.com/bot.html"<endfold id='2'>)</endfold id='2'> ||
			<beginfold id='2'>(</beginfold id='2'>req.http.User-Agent ~ "(iPhone|Windows Phone)" && req.http.User-Agent ~ "\(compatible; bingbot/2.0; \+http://www.bing.com/bingbot.htm"<endfold id='2'>)</endfold id='2'><endfold id='2'>)</endfold id='2'> <beginfold id='3'>{</beginfold id='3'>
            set req.http.X-UA-Device = "mobile-bot"; <endfold id='3'>}</endfold id='3'>
		elsif <beginfold id='2'>(</beginfold id='2'>req.http.User-Agent ~ "(?i)(ads|google|bing|msn|yandex|baidu|ro|career|seznam|)bot" ||
		    req.http.User-Agent ~ "(?i)(baidu|jike|symantec)spider" ||
		    req.http.User-Agent ~ "(?i)pingdom" ||
		    req.http.User-Agent ~ "(?i)facebookexternalhit" ||
		    req.http.User-Agent ~ "(?i)scanner" ||
		    req.http.User-Agent ~ "(?i)slurp" ||
		    req.http.User-Agent ~ "(?i)(web)crawler"<endfold id='2'>)</endfold id='2'> <beginfold id='3'>{</beginfold id='3'>
			set req.http.X-UA-Device = "bot"; <endfold id='3'>}</endfold id='3'>
		elsif <beginfold id='2'>(</beginfold id='2'>req.http.User-Agent ~ "(?i)ipad"<endfold id='2'>)</endfold id='2'>        <beginfold id='3'>{</beginfold id='3'> set req.http.X-UA-Device = "tablet-ipad"; <endfold id='3'>}</endfold id='3'>
		elsif <beginfold id='2'>(</beginfold id='2'>req.http.User-Agent ~ "(?i)ip(hone|od)"<endfold id='2'>)</endfold id='2'> <beginfold id='3'>{</beginfold id='3'> set req.http.X-UA-Device = "mobile-iphone"; <endfold id='3'>}</endfold id='3'>
		<beginfold id='4'>/*</beginfold id='4'> how do we differ between an android phone and an android tablet?
		   http://stackoverflow.com/questions/5341637/how-do-detect-android-tablets-in-general-useragent <endfold id='4'>*/</endfold id='4'>
		elsif <beginfold id='2'>(</beginfold id='2'>req.http.User-Agent ~ "(?i)android.*(mobile|mini)"<endfold id='2'>)</endfold id='2'> <beginfold id='3'>{</beginfold id='3'> set req.http.X-UA-Device = "mobile-android"; <endfold id='3'>}</endfold id='3'>
		// android 3/honeycomb was just about tablet-only, and any phones will probably handle a bigger page layout.
		elsif <beginfold id='2'>(</beginfold id='2'>req.http.User-Agent ~ "(?i)android 3"<endfold id='2'>)</endfold id='2'>              <beginfold id='3'>{</beginfold id='3'> set req.http.X-UA-Device = "tablet-android"; <endfold id='3'>}</endfold id='3'>
		<beginfold id='4'>/*</beginfold id='4'> Opera Mobile <endfold id='4'>*/</endfold id='4'>
		elsif <beginfold id='2'>(</beginfold id='2'>req.http.User-Agent ~ "Opera Mobi"<endfold id='2'>)</endfold id='2'>                  <beginfold id='3'>{</beginfold id='3'> set req.http.X-UA-Device = "mobile-smartphone"; <endfold id='3'>}</endfold id='3'>
		// May very well give false positives towards android tablets. Suggestions welcome.
		elsif <beginfold id='2'>(</beginfold id='2'>req.http.User-Agent ~ "(?i)android"<endfold id='2'>)</endfold id='2'>         <beginfold id='3'>{</beginfold id='3'> set req.http.X-UA-Device = "tablet-android"; <endfold id='3'>}</endfold id='3'>
		elsif <beginfold id='2'>(</beginfold id='2'>req.http.User-Agent ~ "PlayBook; U; RIM Tablet"<endfold id='2'>)</endfold id='2'>         <beginfold id='3'>{</beginfold id='3'> set req.http.X-UA-Device = "tablet-rim"; <endfold id='3'>}</endfold id='3'>
		elsif <beginfold id='2'>(</beginfold id='2'>req.http.User-Agent ~ "hp-tablet.*TouchPad"<endfold id='2'>)</endfold id='2'>         <beginfold id='3'>{</beginfold id='3'> set req.http.X-UA-Device = "tablet-hp"; <endfold id='3'>}</endfold id='3'>
		elsif <beginfold id='2'>(</beginfold id='2'>req.http.User-Agent ~ "Kindle/3"<endfold id='2'>)</endfold id='2'>         <beginfold id='3'>{</beginfold id='3'> set req.http.X-UA-Device = "tablet-kindle"; <endfold id='3'>}</endfold id='3'>
		elsif <beginfold id='2'>(</beginfold id='2'>req.http.User-Agent ~ "Touch.+Tablet PC" ||
		    req.http.User-Agent ~ "Windows NT [0-9.]+; ARM;" <endfold id='2'>)</endfold id='2'> <beginfold id='3'>{</beginfold id='3'>
		        set req.http.X-UA-Device = "tablet-microsoft";
		<endfold id='3'>}</endfold id='3'>
		elsif <beginfold id='2'>(</beginfold id='2'>req.http.User-Agent ~ "Mobile.+Firefox"<endfold id='2'>)</endfold id='2'>     <beginfold id='3'>{</beginfold id='3'> set req.http.X-UA-Device = "mobile-firefoxos"; <endfold id='3'>}</endfold id='3'>
		elsif <beginfold id='2'>(</beginfold id='2'>req.http.User-Agent ~ "^HTC" ||
		    req.http.User-Agent ~ "Fennec" ||
		    req.http.User-Agent ~ "IEMobile" ||
		    req.http.User-Agent ~ "BlackBerry" ||
		    req.http.User-Agent ~ "BB10.*Mobile" ||
		    req.http.User-Agent ~ "GT-.*Build/GINGERBREAD" ||
		    req.http.User-Agent ~ "SymbianOS.*AppleWebKit"<endfold id='2'>)</endfold id='2'> <beginfold id='3'>{</beginfold id='3'>
			set req.http.X-UA-Device = "mobile-smartphone";
		<endfold id='3'>}</endfold id='3'>
		elsif <beginfold id='2'>(</beginfold id='2'>req.http.User-Agent ~ "(?i)symbian" ||
		    req.http.User-Agent ~ "(?i)^sonyericsson" ||
		    req.http.User-Agent ~ "(?i)^nokia" ||
		    req.http.User-Agent ~ "(?i)^samsung" ||
		    req.http.User-Agent ~ "(?i)^lg" ||
		    req.http.User-Agent ~ "(?i)bada" ||
		    req.http.User-Agent ~ "(?i)blazer" ||
		    req.http.User-Agent ~ "(?i)cellphone" ||
		    req.http.User-Agent ~ "(?i)iemobile" ||
		    req.http.User-Agent ~ "(?i)midp-2.0" ||
		    req.http.User-Agent ~ "(?i)u990" ||
		    req.http.User-Agent ~ "(?i)netfront" ||
		    req.http.User-Agent ~ "(?i)opera mini" ||
		    req.http.User-Agent ~ "(?i)palm" ||
		    req.http.User-Agent ~ "(?i)nintendo wii" ||
		    req.http.User-Agent ~ "(?i)playstation portable" ||
		    req.http.User-Agent ~ "(?i)portalmmm" ||
		    req.http.User-Agent ~ "(?i)proxinet" ||
		    req.http.User-Agent ~ "(?i)windows\ ?ce" ||
		    req.http.User-Agent ~ "(?i)winwap" ||
		    req.http.User-Agent ~ "(?i)eudoraweb" ||
		    req.http.User-Agent ~ "(?i)htc" ||
		    req.http.User-Agent ~ "(?i)240x320" ||
		    req.http.User-Agent ~ "(?i)avantgo"<endfold id='2'>)</endfold id='2'> <beginfold id='3'>{</beginfold id='3'>
			set req.http.X-UA-Device = "mobile-generic";
		<endfold id='3'>}</endfold id='3'>
	<endfold id='3'>}</endfold id='3'>
<endfold id='1'>}</endfold id='1'>