This repository has been archived by the owner on Apr 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
handler.go
590 lines (493 loc) · 13.8 KB
/
handler.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
package httpcache
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"math"
"net/http"
"strconv"
"sync"
"time"
"gopkg.in/djherbis/stream.v1"
)
const (
CacheHeader = "X-Cache"
ProxyDateHeader = "Proxy-Date"
)
var Writes sync.WaitGroup
var storeable = map[int]bool{
http.StatusOK: true,
http.StatusFound: true,
http.StatusNonAuthoritativeInfo: true,
http.StatusMultipleChoices: true,
http.StatusMovedPermanently: true,
http.StatusGone: true,
http.StatusNotFound: true,
}
var cacheableByDefault = map[int]bool{
http.StatusOK: true,
http.StatusFound: true,
http.StatusNotModified: true,
http.StatusNonAuthoritativeInfo: true,
http.StatusMultipleChoices: true,
http.StatusMovedPermanently: true,
http.StatusGone: true,
http.StatusPartialContent: true,
}
type Handler struct {
Shared bool
upstream http.Handler
validator *Validator
cache Cache
}
func NewHandler(cache Cache, upstream http.Handler) *Handler {
return &Handler{
upstream: upstream,
cache: cache,
validator: &Validator{upstream},
Shared: false,
}
}
func (h *Handler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
cReq, err := newCacheRequest(r)
if err != nil {
http.Error(rw, "invalid request: "+err.Error(),
http.StatusBadRequest)
return
}
if !cReq.isCacheable() {
debugf("request not cacheable")
rw.Header().Set(CacheHeader, "SKIP")
h.pipeUpstream(rw, cReq)
return
}
res, err := h.lookup(cReq)
if err != nil && err != ErrNotFoundInCache {
http.Error(rw, "lookup error: "+err.Error(),
http.StatusInternalServerError)
return
}
cacheType := "private"
if h.Shared {
cacheType = "shared"
}
if err == ErrNotFoundInCache {
if cReq.CacheControl.Has("only-if-cached") {
http.Error(rw, "key not in cache",
http.StatusGatewayTimeout)
return
}
debugf("%s %s not in %s cache", r.Method, r.URL.String(), cacheType)
h.passUpstream(rw, cReq)
return
} else {
debugf("%s %s found in %s cache", r.Method, r.URL.String(), cacheType)
}
if h.needsValidation(res, cReq) {
if cReq.CacheControl.Has("only-if-cached") {
http.Error(rw, "key was in cache, but required validation",
http.StatusGatewayTimeout)
return
}
debugf("validating cached response")
if h.validator.Validate(r, res) {
debugf("response is valid")
h.cache.Freshen(res, cReq.Key.String())
} else {
debugf("response is changed")
h.passUpstream(rw, cReq)
return
}
}
debugf("serving from cache")
res.Header().Set(CacheHeader, "HIT")
h.serveResource(res, rw, cReq)
if err := res.Close(); err != nil {
errorf("Error closing resource: %s", err.Error())
}
}
// freshness returns the duration that a requested resource will be fresh for
func (h *Handler) freshness(res *Resource, r *cacheRequest) (time.Duration, error) {
maxAge, err := res.MaxAge(h.Shared)
if err != nil {
return time.Duration(0), err
}
if r.CacheControl.Has("max-age") {
reqMaxAge, err := r.CacheControl.Duration("max-age")
if err != nil {
return time.Duration(0), err
}
if reqMaxAge < maxAge {
debugf("using request max-age of %s", reqMaxAge.String())
maxAge = reqMaxAge
}
}
age, err := res.Age()
if err != nil {
return time.Duration(0), err
}
if res.IsStale() {
return time.Duration(0), nil
}
if hFresh := res.HeuristicFreshness(); hFresh > maxAge {
debugf("using heuristic freshness of %q", hFresh)
maxAge = hFresh
}
return maxAge - age, nil
}
func (h *Handler) needsValidation(res *Resource, r *cacheRequest) bool {
if res.MustValidate(h.Shared) {
return true
}
freshness, err := h.freshness(res, r)
if err != nil {
debugf("error calculating freshness: %s", err.Error())
return true
}
if r.CacheControl.Has("min-fresh") {
reqMinFresh, err := r.CacheControl.Duration("min-fresh")
if err != nil {
debugf("error parsing request min-fresh: %s", err.Error())
return true
}
if freshness < reqMinFresh {
debugf("resource is fresh, but won't satisfy min-fresh of %s", reqMinFresh)
return true
}
}
debugf("resource has a freshness of %s", freshness)
if freshness <= 0 && r.CacheControl.Has("max-stale") {
if len(r.CacheControl["max-stale"]) == 0 {
debugf("resource is stale, but client sent max-stale")
return false
} else if maxStale, _ := r.CacheControl.Duration("max-stale"); maxStale >= (freshness * -1) {
log.Printf("resource is stale, but within allowed max-stale period of %s", maxStale)
return false
}
}
return freshness <= 0
}
// pipeUpstream makes the request via the upstream handler, the response is not stored or modified
func (h *Handler) pipeUpstream(w http.ResponseWriter, r *cacheRequest) {
rw := newResponseStreamer(w)
rdr, err := rw.Stream.NextReader()
if err != nil {
debugf("error creating next stream reader: %v", err)
w.Header().Set(CacheHeader, "SKIP")
h.upstream.ServeHTTP(w, r.Request)
return
}
defer rdr.Close()
debugf("piping request upstream")
go func() {
h.upstream.ServeHTTP(rw, r.Request)
rw.Stream.Close()
}()
rw.WaitHeaders()
if r.Method != "HEAD" && !r.isStateChanging() {
return
}
res := rw.Resource()
defer res.Close()
if r.Method == "HEAD" {
h.cache.Freshen(res, r.Key.ForMethod("GET").String())
} else if res.IsNonErrorStatus() {
h.invalidateResource(res, r)
}
}
// passUpstream makes the request via the upstream handler and stores the result
func (h *Handler) passUpstream(w http.ResponseWriter, r *cacheRequest) {
rw := newResponseStreamer(w)
rdr, err := rw.Stream.NextReader()
if err != nil {
debugf("error creating next stream reader: %v", err)
w.Header().Set(CacheHeader, "SKIP")
h.upstream.ServeHTTP(w, r.Request)
return
}
t := Clock()
debugf("passing request upstream")
rw.Header().Set(CacheHeader, "MISS")
go func() {
h.upstream.ServeHTTP(rw, r.Request)
rw.Stream.Close()
}()
rw.WaitHeaders()
debugf("upstream responded headers in %s", Clock().Sub(t).String())
// just the headers!
res := NewResourceBytes(rw.StatusCode, nil, rw.Header())
if !h.isCacheable(res, r) {
rdr.Close()
debugf("resource is uncacheable")
rw.Header().Set(CacheHeader, "SKIP")
return
}
b, err := ioutil.ReadAll(rdr)
rdr.Close()
if err != nil {
debugf("error reading stream: %v", err)
rw.Header().Set(CacheHeader, "SKIP")
return
}
debugf("full upstream response took %s", Clock().Sub(t).String())
res.ReadSeekCloser = &byteReadSeekCloser{bytes.NewReader(b)}
if age, err := correctedAge(res.Header(), t, Clock()); err == nil {
res.Header().Set("Age", strconv.Itoa(int(math.Ceil(age.Seconds()))))
} else {
debugf("error calculating corrected age: %s", err.Error())
}
rw.Header().Set(ProxyDateHeader, Clock().Format(http.TimeFormat))
h.storeResource(res, r)
}
// correctedAge adjusts the age of a resource for clock skew and travel time
// https://httpwg.github.io/specs/rfc7234.html#rfc.section.4.2.3
func correctedAge(h http.Header, reqTime, respTime time.Time) (time.Duration, error) {
date, err := timeHeader("Date", h)
if err != nil {
return time.Duration(0), err
}
apparentAge := respTime.Sub(date)
if apparentAge < 0 {
apparentAge = 0
}
respDelay := respTime.Sub(reqTime)
ageSeconds, err := intHeader("Age", h)
age := time.Second * time.Duration(ageSeconds)
correctedAge := age + respDelay
if apparentAge > correctedAge {
correctedAge = apparentAge
}
residentTime := Clock().Sub(respTime)
currentAge := correctedAge + residentTime
return currentAge, nil
}
func (h *Handler) isCacheable(res *Resource, r *cacheRequest) bool {
cc, err := res.cacheControl()
if err != nil {
errorf("Error parsing cache-control: %s", err.Error())
return false
}
if cc.Has("no-cache") || cc.Has("no-store") {
return false
}
if cc.Has("private") && len(cc["private"]) == 0 && h.Shared {
return false
}
if _, ok := storeable[res.Status()]; !ok {
return false
}
if r.Header.Get("Authorization") != "" && h.Shared {
return false
}
if res.Header().Get("Authorization") != "" && h.Shared &&
!cc.Has("must-revalidate") && !cc.Has("s-maxage") {
return false
}
if res.HasExplicitExpiration() {
return true
}
if _, ok := cacheableByDefault[res.Status()]; !ok && !cc.Has("public") {
return false
}
if res.HasValidators() {
return true
} else if res.HeuristicFreshness() > 0 {
return true
}
return false
}
func (h *Handler) serveResource(res *Resource, w http.ResponseWriter, req *cacheRequest) {
for key, headers := range res.Header() {
for _, header := range headers {
w.Header().Add(key, header)
}
}
age, err := res.Age()
if err != nil {
http.Error(w, "Error calculating age: "+err.Error(),
http.StatusInternalServerError)
return
}
// http://httpwg.github.io/specs/rfc7234.html#warn.113
if age > (time.Hour*24) && res.HeuristicFreshness() > (time.Hour*24) {
w.Header().Add("Warning", `113 - "Heuristic Expiration"`)
}
// http://httpwg.github.io/specs/rfc7234.html#warn.110
freshness, err := h.freshness(res, req)
if err != nil || freshness <= 0 {
w.Header().Add("Warning", `110 - "Response is Stale"`)
}
debugf("resource is %s old, updating age from %s",
age.String(), w.Header().Get("Age"))
w.Header().Set("Age", fmt.Sprintf("%.f", math.Floor(age.Seconds())))
w.Header().Set("Via", res.Via())
// hacky handler for non-ok statuses
if res.Status() != http.StatusOK {
w.WriteHeader(res.Status())
io.Copy(w, res)
} else {
http.ServeContent(w, req.Request, "", res.LastModified(), res)
}
}
func (h *Handler) invalidateResource(res *Resource, r *cacheRequest) {
Writes.Add(1)
go func() {
defer Writes.Done()
debugf("invalidating resource %+v", res)
}()
}
func (h *Handler) storeResource(res *Resource, r *cacheRequest) {
Writes.Add(1)
go func() {
defer Writes.Done()
t := Clock()
keys := []string{r.Key.String()}
headers := res.Header()
if h.Shared {
res.RemovePrivateHeaders()
}
// store a secondary vary version
if vary := headers.Get("Vary"); vary != "" {
keys = append(keys, r.Key.Vary(vary, r.Request).String())
}
if err := h.cache.Store(res, keys...); err != nil {
errorf("storing resources %#v failed with error: %s", keys, err.Error())
}
debugf("stored resources %+v in %s", keys, Clock().Sub(t))
}()
}
// lookupResource finds the best matching Resource for the
// request, or nil and ErrNotFoundInCache if none is found
func (h *Handler) lookup(req *cacheRequest) (*Resource, error) {
res, err := h.cache.Retrieve(req.Key.String())
// HEAD requests can possibly be served from GET
if err == ErrNotFoundInCache && req.Method == "HEAD" {
res, err = h.cache.Retrieve(req.Key.ForMethod("GET").String())
if err != nil {
return nil, err
}
if res.HasExplicitExpiration() && req.isCacheable() {
debugf("using cached GET request for serving HEAD")
return res, nil
} else {
return nil, ErrNotFoundInCache
}
} else if err != nil {
return res, err
}
// Secondary lookup for Vary
if vary := res.Header().Get("Vary"); vary != "" {
res, err = h.cache.Retrieve(req.Key.Vary(vary, req.Request).String())
if err != nil {
return res, err
}
}
return res, nil
}
type cacheRequest struct {
*http.Request
Key Key
Time time.Time
CacheControl CacheControl
}
func newCacheRequest(r *http.Request) (*cacheRequest, error) {
cc, err := ParseCacheControl(r.Header.Get("Cache-Control"))
if err != nil {
return nil, err
}
if r.Proto == "HTTP/1.1" && r.Host == "" {
return nil, errors.New("Host header can't be empty")
}
return &cacheRequest{
Request: r,
Key: NewRequestKey(r),
Time: Clock(),
CacheControl: cc,
}, nil
}
func (r *cacheRequest) isStateChanging() bool {
if !(r.Method == "POST" || r.Method == "PUT" || r.Method == "DELETE") {
return true
}
return false
}
func (r *cacheRequest) isCacheable() bool {
if !(r.Method == "GET" || r.Method == "HEAD") {
return false
}
if r.Header.Get("If-Match") != "" ||
r.Header.Get("If-Unmodified-Since") != "" ||
r.Header.Get("If-Range") != "" {
return false
}
if maxAge, ok := r.CacheControl.Get("max-age"); ok && maxAge == "0" {
return false
}
if r.CacheControl.Has("no-store") || r.CacheControl.Has("no-cache") {
return false
}
return true
}
func newResponseStreamer(w http.ResponseWriter) *responseStreamer {
strm, err := stream.NewStream("responseBuffer", stream.NewMemFS())
if err != nil {
panic(err)
}
return &responseStreamer{
ResponseWriter: w,
Stream: strm,
C: make(chan struct{}),
}
}
type responseStreamer struct {
StatusCode int
http.ResponseWriter
*stream.Stream
// C will be closed by WriteHeader to signal the headers' writing.
C chan struct{}
}
// WaitHeaders returns iff and when WriteHeader has been called.
func (rw *responseStreamer) WaitHeaders() {
for range rw.C {
}
}
func (rw *responseStreamer) WriteHeader(status int) {
defer close(rw.C)
rw.StatusCode = status
rw.ResponseWriter.WriteHeader(status)
}
func (rw *responseStreamer) Write(b []byte) (int, error) {
rw.Stream.Write(b)
return rw.ResponseWriter.Write(b)
}
func (rw *responseStreamer) Close() error {
return rw.Stream.Close()
}
// Resource returns a copy of the responseStreamer as a Resource object
func (rw *responseStreamer) Resource() *Resource {
r, err := rw.Stream.NextReader()
if err == nil {
b, err := ioutil.ReadAll(r)
r.Close()
if err == nil {
return NewResourceBytes(rw.StatusCode, b, rw.Header())
}
}
return &Resource{
header: rw.Header(),
statusCode: rw.StatusCode,
ReadSeekCloser: errReadSeekCloser{err},
}
}
type errReadSeekCloser struct {
err error
}
func (e errReadSeekCloser) Error() string {
return e.err.Error()
}
func (e errReadSeekCloser) Close() error { return e.err }
func (e errReadSeekCloser) Read(_ []byte) (int, error) { return 0, e.err }
func (e errReadSeekCloser) Seek(_ int64, _ int) (int64, error) { return 0, e.err }