-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache_control.go
165 lines (138 loc) · 3.09 KB
/
cache_control.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
162
163
164
165
package embed
import (
"fmt"
"net/http"
"strconv"
"strings"
"time"
)
// CacheControl represents a response's cache policy.
type CacheControl struct {
MustRevalidate bool
NoCache bool
NoStore bool
Public bool
Private bool
Immutable bool
MaxAge *time.Duration
}
const (
expires = "Expires"
date = "Date"
cacheControl = "Cache-Control"
mustRevalidate = "must-revalidate"
noCache = "no-cache"
noStore = "no-store"
public = "public"
private = "private"
immutable = "immutable"
maxAge = "max-age"
)
// NewCacheControl creates a new instance of CacheControl from related headers in the given HTTP response.
func NewCacheControl(resp *http.Response) *CacheControl {
c := newCacheControlExpires(resp)
for _, d := range directives(resp) {
ts := strings.Split(d, "=")
switch ts[0] {
case mustRevalidate:
c.MustRevalidate = true
case noCache:
c.NoCache = true
case noStore:
c.NoStore = true
case public:
c.Public = true
case private:
c.Private = true
case immutable:
c.Immutable = true
case maxAge:
n, err := strconv.Atoi(ts[1])
if err != nil {
continue
}
a := time.Duration(n) * time.Second
c.MaxAge = &a
}
}
return c
}
// Convert Expires to Cache-Control: max-age
func newCacheControlExpires(resp *http.Response) *CacheControl {
var c CacheControl
e, ok := resp.Header[expires]
if !ok {
return &c
}
t, err := time.Parse(time.RFC1123, e[0])
if err != nil {
// Treat invalid Expires as expired.
a := time.Duration(0)
c.MaxAge = &a
return &c
}
d, ok := resp.Header[date]
if !ok {
return &c
}
s, err := time.Parse(time.RFC1123, d[0])
if err != nil {
return &c
}
a := t.Sub(s)
c.MaxAge = &a
return &c
}
// TODO: Proper directive parsing.
// Cache-Control directives can be something like `private="foo,bar,baz"`.
func directives(resp *http.Response) []string {
ds := []string{}
for _, v := range resp.Header[cacheControl] {
for _, d := range strings.Split(v, ",") {
d = strings.Trim(d, " ")
ds = append(ds, d)
}
}
return ds
}
// Merge merges 2 CacheControls.
func (c *CacheControl) Merge(o *CacheControl) *CacheControl {
var n CacheControl
n.MustRevalidate = c.MustRevalidate || o.MustRevalidate
n.NoCache = c.NoCache || o.NoCache
n.NoStore = c.NoStore || o.NoStore
n.Public = c.Public && o.Public
n.Private = c.Private || o.Private
n.Immutable = c.Immutable && o.Immutable
n.MaxAge = c.MaxAge
if n.MaxAge != nil && o.MaxAge != nil && *n.MaxAge > *o.MaxAge {
n.MaxAge = o.MaxAge
}
return &n
}
// String generates CacheControl header value.
func (c *CacheControl) String() string {
var ds []string
if c.MustRevalidate {
ds = append(ds, mustRevalidate)
}
if c.NoCache {
ds = append(ds, noCache)
}
if c.NoStore {
ds = append(ds, noStore)
}
if c.Public {
ds = append(ds, public)
}
if c.Private {
ds = append(ds, private)
}
if c.Immutable {
ds = append(ds, immutable)
}
if c.MaxAge != nil {
ds = append(ds, fmt.Sprintf("%s=%d", maxAge, *c.MaxAge/time.Second))
}
return strings.Join(ds, ",")
}