/
99_bottles.vcl
75 lines (62 loc) · 1.62 KB
/
99_bottles.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
backend F_fake_backend {
.host = "example.com";
.port = "80";
.dynamic = true;
.connect_timeout = 5s;
.first_byte_timeout = 15s;
.probe = {
.dummy = true;
}
}
sub vcl_recv {
#FASTLY recv
set req.backend = F_fake_backend;
set req.http.X-Bottle = if(req.url ~ "(?i)bottle=([^&]*)", urldecode(re.group.1), "99");
call set_next_bottle;
set req.http.X-Verse = if(req.http.X-Verse,req.http.X-Verse,"") req.http.X-Bottle {" bottles of beer on the wall, "} req.http.X-Bottle {" bottles of beer.
Take one down and pass it around, "} req.http.X-Next-Bottle {" bottles of beer on the wall.
"};
if(req.http.X-Bottle == "0") {
error 996;
}
return(lookup);
}
sub vcl_fetch {
#FASTLY fetch
if(req.restarts > 0) {
set beresp.http.Fastly-Restarts = req.restarts;
}
return(deliver);
}
sub vcl_deliver {
#FASTLY deliver
set resp.http.X-Verse = req.http.X-Verse;
set resp.http.X-Bottle = req.http.X-Bottle;
set resp.http.X-Next-Bottle = req.http.X-Next-Bottle;
if(req.restarts == 2) {
# For now, limit to 3 bottles as we can only restart 3 times
set req.http.X-Next-Bottle = "0";
}
set req.url = "/?bottle=" req.http.X-Next-Bottle;
if(req.restarts < 3) {
restart;
}
return(deliver);
}
sub set_next_bottle {
set req.grace = 1d;
set req.http.X-Original-Grace = req.grace;
set req.grace = std.atoi(req.http.X-Bottle);
set req.grace -= 1s;
set req.http.X-Next-Bottle = regsub(req.grace, "\..*", "");
set req.grace = std.atoi(req.http.X-Original-Grace);
}
sub vcl_error {
#FASTLY error
if(obj.status == 996) {
set obj.status = 200;
set obj.response = "Cheers!";
synthetic req.http.X-Verse;
return (deliver);
}
}