-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexpires.go
161 lines (138 loc) · 3.45 KB
/
expires.go
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package expires
import (
"net/http"
"regexp"
"strconv"
"time"
"github.com/caddyserver/caddy"
"github.com/caddyserver/caddy/caddyhttp/httpserver"
)
type matchDef struct {
re *regexp.Regexp
duration time.Duration
}
func (m *matchDef) Match(header http.Header, request *http.Request) bool {
return m.re.MatchString(request.URL.Path)
}
func (m *matchDef) Duration() time.Duration {
return m.duration
}
func (m *matchDef) Parse(args []string) error {
re, err := regexp.Compile(args[0])
if err != nil {
return err
}
m.re = re
m.duration = parseDuration(args[1])
return nil
}
type headerMatchDef struct {
header string
re *regexp.Regexp
duration time.Duration
}
func (m *headerMatchDef) Match(header http.Header, request *http.Request) bool {
return m.re.MatchString(header.Get(m.header))
}
func (m *headerMatchDef) Duration() time.Duration {
return m.duration
}
func (m *headerMatchDef) Parse(args []string) error {
m.header = args[0]
re, err := regexp.Compile(args[1])
if err != nil {
return err
}
m.re = re
m.duration = parseDuration(args[2])
return nil
}
type matchRule interface {
Duration() time.Duration
Match(http.Header, *http.Request) bool
Parse([]string) error
}
func init() {
caddy.RegisterPlugin("expires", caddy.Plugin{
ServerType: "http",
Action: setup,
})
}
func setup(c *caddy.Controller) error {
rules, err := parseRules(c)
if err != nil {
return err
}
cfg := httpserver.GetConfig(c)
mid := func(next httpserver.Handler) httpserver.Handler {
return expiresHandler{Next: next, Rules: rules}
}
cfg.AddMiddleware(mid)
return nil
}
func parseRules(c *caddy.Controller) ([]matchRule, error) {
rules := []matchRule{}
for c.Next() {
for c.NextBlock() {
switch c.Val() {
case "match":
args := c.RemainingArgs()
if len(args) != 2 {
return nil, c.ArgErr()
}
rule := &matchDef{}
rule.Parse(args)
rules = append(rules, rule)
case "match_header":
args := c.RemainingArgs()
if len(args) != 3 {
return nil, c.ArgErr()
}
rule := &headerMatchDef{}
rule.Parse(args)
rules = append(rules, rule)
default:
return nil, c.SyntaxErr("match")
}
}
}
return rules, nil
}
func parseDuration(str string) time.Duration {
durationRegex := regexp.MustCompile(`(?P<years>\d+y)?(?P<months>\d+m)?(?P<days>\d+d)?T?(?P<hours>\d+h)?(?P<minutes>\d+i)?(?P<seconds>\d+s)?`)
matches := durationRegex.FindStringSubmatch(str)
years := parseInt64(matches[1])
months := parseInt64(matches[2])
days := parseInt64(matches[3])
hours := parseInt64(matches[4])
minutes := parseInt64(matches[5])
seconds := parseInt64(matches[6])
hour := int64(time.Hour)
minute := int64(time.Minute)
second := int64(time.Second)
return time.Duration(years*24*365*hour + months*30*24*hour + days*24*hour + hours*hour + minutes*minute + seconds*second)
}
func parseInt64(value string) int64 {
if len(value) == 0 {
return 0
}
parsed, err := strconv.Atoi(value[:len(value)-1])
if err != nil {
return 0
}
return int64(parsed)
}
type expiresHandler struct {
Next httpserver.Handler
Rules []matchRule
}
func (h expiresHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
for _, rule := range h.Rules {
if rule.Match(w.Header(), r) {
w.Header().Set("Expires", time.Now().Add(rule.Duration()).UTC().Format(time.RFC1123))
w.Header().Set("Cache-Control", "public, max-age="+strconv.Itoa(int(rule.Duration().Seconds())))
break
}
}
return h.Next.ServeHTTP(w, r)
}