-
Notifications
You must be signed in to change notification settings - Fork 0
/
cacheable.vcl
111 lines (90 loc) · 3.05 KB
/
cacheable.vcl
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
#
# Varnish VCL for cacheable
#
# Author Karto Martin <source@karto.net>
# Copyright (c) 2015 Karto Martin. All Right Reserved.
# License The MIT License
#
#######################################################################
# Client side
sub cacheable_recv {
# Only deal with "normal" types
if ("GET" != req.method && "HEAD" != req.method && "POST" != req.method && "PUT" != req.method &&
"TRACE" != req.method && "OPTIONS" != req.method && "DELETE" != req.method) {
/* Non-RFC2616 or CONNECT which is weird. */
return (pipe);
}
# 5.2.1.6. no-transform http://tools.ietf.org/html/rfc7234#section-5.2.1.6
# The "no-transform" request directive indicates that an intermediary
# (whether or not it implements a cache) MUST NOT transform the
# payload, as defined in Section 5.7.2 of [RFC7230].
if (req.http.Cache-Control ~ "(?i)(^|,)\s*no-transform\s*(,|$)") {
set req.esi = false;
}
# Only cache GET or HEAD requests. This makes sure the POST requests are always passed.
# POST could be cached, but varnish doesn't support it
if ("GET" != req.method && "HEAD" != req.method) {
/* We only deal with GET and HEAD by default */
return (pass);
}
}
#sub cacheable_pipe {
#}
#sub cacheable_pass {
#}
#sub cacheable_hash {
#}
#sub cacheable_purge {
#}
sub cacheable_hit {
call cacheable_hit_set_cacheable;
# 5.2.1.7. only-if-cached
if (req.http.Cache-Control ~ "(?i)(^|,)\s*only-if-cached\s*(,|$)" && req.http.X-Cacheable !~ "^deliver;") {
return (synth (504, "hit/only-if-cached"));
}
# pass cacheable
if (req.http.X-Cacheable ~ "^pass;") {
return (pass);
}
# Fetch cacheable
if (req.http.X-Cacheable ~ "^fetch;") {
call cacheable_hit_revalidate;
}
# Unknown not deliverable cacheable
if (req.http.X-Cacheable !~ "^deliver;") {
std.log("cacheable_hit_cacheable: Unknown cacheable directive: " + req.http.X-Cacheable);
std.syslog(13, "cacheable_hit_cacheable: Unknown cacheable directive: " + req.http.X-Cacheable);
header.append(req.http.VAR-Log, "cacheable_hit_cacheable: Unknown cacheable directive: " + req.http.X-Cacheable);
return (pass);
}
}
sub cacheable_miss {
# 5.2.1.7. only-if-cached
if (req.http.Cache-Control ~ "(?i)(^|,)\s*only-if-cached\s*(,|$)") {
return (synth (504, "miss/only-if-cached"));
}
}
sub cacheable_deliver {
if (obj.uncacheable) {
set resp.http.X-Cache = "PASS";
} elsif (obj.hits == 0) {
set resp.http.X-Cache = "MISS";
} else {
set resp.http.X-Cache = "HIT";
}
}
#######################################################################
# Backend Fetch
#sub cacheable_backend_fetch {
#}
sub cacheable_backend_response {
call cacheable_backend_response_cacheable;
}
#sub cacheable_backend_error {
#}
#######################################################################
# Housekeeping
#sub cacheable_init {
#}
#sub cacheable_fini {
#}