forked from lox/httpcache
/
resource.go
249 lines (200 loc) · 4.91 KB
/
resource.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package httpcache
import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
)
const (
lastModDivisor = 10
viaPseudonym = "httpcache"
)
var Clock = func() time.Time {
return time.Now().UTC()
}
type ReadSeekCloser interface {
io.Reader
io.Seeker
io.Closer
}
type byteReadSeekCloser struct {
*bytes.Reader
}
func (brsc *byteReadSeekCloser) Close() error { return nil }
type Resource struct {
ReadSeekCloser
RequestTime, ResponseTime time.Time
header http.Header
statusCode int
cc CacheControl
stale bool
}
func NewResource(statusCode int, body ReadSeekCloser, hdrs http.Header) *Resource {
return &Resource{
header: hdrs,
ReadSeekCloser: body,
statusCode: statusCode,
}
}
func NewResourceBytes(statusCode int, b []byte, hdrs http.Header) *Resource {
return &Resource{
header: hdrs,
statusCode: statusCode,
ReadSeekCloser: &byteReadSeekCloser{bytes.NewReader(b)},
}
}
func (r *Resource) IsNonErrorStatus() bool {
return r.statusCode >= 200 && r.statusCode < 400
}
func (r *Resource) Status() int {
return r.statusCode
}
func (r *Resource) Header() http.Header {
return r.header
}
func (r *Resource) IsStale() bool {
return r.stale
}
func (r *Resource) MarkStale() {
r.stale = true
}
func (r *Resource) cacheControl() (CacheControl, error) {
if r.cc != nil {
return r.cc, nil
}
cc, err := ParseCacheControlHeaders(r.header)
if err != nil {
return cc, err
}
r.cc = cc
return cc, nil
}
func (r *Resource) LastModified() time.Time {
var modTime time.Time
if lastModHeader := r.header.Get("Last-Modified"); lastModHeader != "" {
if t, err := http.ParseTime(lastModHeader); err == nil {
modTime = t
}
}
return modTime
}
func (r *Resource) Expires() (time.Time, error) {
if expires := r.header.Get("Expires"); expires != "" {
return http.ParseTime(expires)
}
return time.Time{}, nil
}
func (r *Resource) MustValidate(shared bool) bool {
cc, err := r.cacheControl()
if err != nil {
debugf("Error parsing Cache-Control: ", err.Error())
return true
}
// The s-maxage directive also implies the semantics of proxy-revalidate
if cc.Has("s-maxage") && shared {
return true
}
if cc.Has("must-revalidate") || (cc.Has("proxy-revalidate") && shared) {
return true
}
return false
}
func (r *Resource) DateAfter(d time.Time) bool {
if dateHeader := r.header.Get("Date"); dateHeader != "" {
if t, err := http.ParseTime(dateHeader); err != nil {
return false
} else {
return t.After(d)
}
}
return false
}
// Calculate the age of the resource
func (r *Resource) Age() (time.Duration, error) {
var age time.Duration
if ageInt, err := intHeader("Age", r.header); err == nil {
age = time.Second * time.Duration(ageInt)
}
if proxyDate, err := timeHeader(ProxyDateHeader, r.header); err == nil {
return Clock().Sub(proxyDate) + age, nil
}
if date, err := timeHeader("Date", r.header); err == nil {
return Clock().Sub(date) + age, nil
}
return time.Duration(0), errors.New("Unable to calculate age")
}
func (r *Resource) MaxAge(shared bool) (time.Duration, error) {
cc, err := r.cacheControl()
if err != nil {
return time.Duration(0), err
}
if cc.Has("s-maxage") && shared {
if maxAge, err := cc.Duration("s-maxage"); err != nil {
return time.Duration(0), err
} else if maxAge > 0 {
return maxAge, nil
}
}
if cc.Has("max-age") {
if maxAge, err := cc.Duration("max-age"); err != nil {
return time.Duration(0), err
} else if maxAge > 0 {
return maxAge, nil
}
}
if expiresVal := r.header.Get("Expires"); expiresVal != "" {
expires, err := http.ParseTime(expiresVal)
if err != nil {
return time.Duration(0), err
}
return expires.Sub(Clock()), nil
}
return time.Duration(0), nil
}
func (r *Resource) RemovePrivateHeaders() {
cc, err := r.cacheControl()
if err != nil {
debugf("Error parsing Cache-Control: %s", err.Error())
}
for _, p := range cc["private"] {
debugf("removing private header %q", p)
r.header.Del(p)
}
}
func (r *Resource) HasValidators() bool {
if r.header.Get("Last-Modified") != "" || r.header.Get("Etag") != "" {
return true
}
return false
}
func (r *Resource) HasExplicitExpiration() bool {
cc, err := r.cacheControl()
if err != nil {
debugf("Error parsing Cache-Control: %s", err.Error())
return false
}
if d, _ := cc.Duration("max-age"); d > time.Duration(0) {
return true
}
if d, _ := cc.Duration("s-maxage"); d > time.Duration(0) {
return true
}
if exp, _ := r.Expires(); !exp.IsZero() {
return true
}
return false
}
func (r *Resource) HeuristicFreshness() time.Duration {
if !r.HasExplicitExpiration() && r.header.Get("Last-Modified") != "" {
return Clock().Sub(r.LastModified()) / time.Duration(lastModDivisor)
}
return time.Duration(0)
}
func (r *Resource) Via() string {
via := []string{}
via = append(via, fmt.Sprintf("1.1 %s", viaPseudonym))
return strings.Join(via, ",")
}