/
caching.go
132 lines (110 loc) · 2.96 KB
/
caching.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
package server
import (
"fmt"
"hash/crc64"
"net/http"
"sort"
"time"
"github.com/readeck/readeck/configs"
)
// Etager must provides a function that returns a list of
// strings used to build an etag header.
type Etager interface {
GetSumStrings() []string
}
// LastModer must provides a function that returns a list
// of times used to build a Last-Modified header.
type LastModer interface {
GetLastModified() []time.Time
}
type checkResult int
const (
checkNone checkResult = iota
checkTrue
checkFalse
)
// WriteEtag adds an Etag header to the response, based on
// the values sent by GetSumStrings. The build date is always
// included.
func (s *Server) WriteEtag(w http.ResponseWriter, i Etager) {
h := crc64.New(crc64.MakeTable(crc64.ISO))
for _, x := range i.GetSumStrings() {
h.Write([]byte(x))
}
h.Write([]byte(configs.BuildTime().String()))
w.Header().Set("Etag", fmt.Sprintf("%x", h.Sum64()))
}
// WriteLastModified adds a Last-Modified headers using the most
// recent date of GetLastModified and the build date.
func (s *Server) WriteLastModified(w http.ResponseWriter, i LastModer) {
mtimes := append(i.GetLastModified(), configs.BuildTime())
sort.Slice(mtimes, func(i, j int) bool {
return mtimes[i].After(mtimes[j])
})
w.Header().Set("Last-Modified", mtimes[0].Format(http.TimeFormat))
}
// WithCaching is a middleware that checks if an Etag and/or a
// Last-Modified headers are sent with the response. If the
// request has the correspondign cache header and theys match
// the request stops with a 304.
func (s *Server) WithCaching(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet && r.Method != http.MethodHead {
next.ServeHTTP(w, r)
return
}
if checkIfMatch(w, r)|checkIfModifiedSince(w, r) == checkTrue {
writeNotModified(w)
return
}
next.ServeHTTP(w, r)
})
}
func writeNotModified(w http.ResponseWriter) {
h := w.Header()
delete(h, "Content-Type")
delete(h, "Content-Length")
if h.Get("Etag") != "" {
delete(h, "Last-Modified")
}
w.WriteHeader(http.StatusNotModified)
}
func checkIfModifiedSince(w http.ResponseWriter, r *http.Request) checkResult {
rh := r.Header.Get("If-Modified-Since")
if rh == "" {
return checkNone
}
wh := w.Header().Get("Last-Modified")
if wh == "" {
return checkNone
}
var err error
var ims time.Time
var modtime time.Time
if ims, err = http.ParseTime(rh); err != nil {
return checkFalse
}
if modtime, err = http.ParseTime(wh); err != nil {
return checkFalse
}
ims = ims.Truncate(time.Second)
modtime = modtime.Truncate(time.Second)
if modtime.Before(ims) || modtime.Equal(ims) {
return checkTrue
}
return checkFalse
}
func checkIfMatch(w http.ResponseWriter, r *http.Request) checkResult {
rh := r.Header.Get("If-None-Match")
if rh == "" {
return checkNone
}
wh := w.Header().Get("Etag")
if wh == "" {
return checkNone
}
if rh == wh {
return checkTrue
}
return checkFalse
}