forked from kevburnsjr/microcache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
165 lines (147 loc) · 4.55 KB
/
request.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 microcache
import (
"crypto/sha1"
"encoding/base64"
"io"
"net/http"
"strconv"
"strings"
"time"
)
func getRequestHash(m *microcache, r *http.Request) string {
reqHash := r.URL.Path
if m.HashQuery {
if m.QueryIgnore != nil {
for key, values := range r.URL.Query() {
if _, ok := m.QueryIgnore[key]; ok {
continue
}
for _, value := range values {
reqHash = reqHash + "&" + key + "=" + value
}
}
} else {
reqHash = reqHash + r.URL.RawQuery
}
}
for _, header := range m.Vary {
reqHash = reqHash + header + r.Header.Get(header)
}
h := sha1.New()
io.WriteString(h, reqHash)
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
// RequestOpts stores per-request cache options. This is necessary to allow
// custom response headers to be evaluated, cached and applied prior to
// response object retrieval (ie. microcache-vary, microcache-nocache, etc)
type RequestOpts struct {
found bool
ttl time.Duration
staleIfError time.Duration
staleRecache bool
staleWhileRevalidate time.Duration
collapsedForwarding bool
vary []string
varyQuery []string
nocache bool
}
func (req *RequestOpts) getObjectHash(reqHash string, r *http.Request) string {
objHash := reqHash
for _, header := range req.vary {
objHash = objHash + "&" + header + ": " + r.Header.Get(header)
}
if len(req.varyQuery) > 0 {
queryParams := r.URL.Query()
for _, param := range req.varyQuery {
if vals, ok := queryParams[param]; ok {
for _, val := range vals {
objHash = objHash + "&" + param + "=" + val
}
}
}
}
return objHash
}
func buildRequestOpts(m *microcache, res Response, r *http.Request) RequestOpts {
headers := res.header
req := RequestOpts{
found: true,
nocache: m.Nocache,
ttl: m.TTL,
staleIfError: m.StaleIfError,
staleRecache: m.StaleRecache,
staleWhileRevalidate: m.StaleWhileRevalidate,
collapsedForwarding: m.CollapsedForwarding,
vary: m.Vary,
}
// w.Header().Set("microcache-cache", "1")
if headers.Get("microcache-cache") != "" {
req.nocache = false
}
// w.Header().Set("microcache-nocache", "1")
if headers.Get("microcache-nocache") != "" {
req.nocache = true
}
// w.Header().Set("microcache-ttl", "10") // 10 seconds
ttlHdr, _ := strconv.Atoi(headers.Get("microcache-ttl"))
if ttlHdr > 0 {
req.ttl = time.Duration(ttlHdr) * time.Second
}
// w.Header().Set("microcache-stale-if-error", "20") // 20 seconds
staleIfErrorHdr, _ := strconv.Atoi(headers.Get("microcache-stale-if-error"))
if staleIfErrorHdr > 0 {
req.staleIfError = time.Duration(staleIfErrorHdr) * time.Second
}
// w.Header().Set("microcache-stale-while-revalidate", "20") // 20 seconds
staleWhileRevalidateHdr, _ := strconv.Atoi(headers.Get("microcache-stale-while-revalidate"))
if staleWhileRevalidateHdr > 0 {
req.staleWhileRevalidate = time.Duration(staleWhileRevalidateHdr) * time.Second
}
// w.Header().Set("microcache-collapsed-fowarding", "1")
if headers.Get("microcache-collapsed-fowarding") != "" {
req.collapsedForwarding = true
}
// w.Header().Set("microcache-no-collapsed-fowarding", "1")
if headers.Get("microcache-no-collapsed-fowarding") != "" {
req.collapsedForwarding = false
}
// w.Header().Set("microcache-stale-recache", "1")
if headers.Get("microcache-stale-recache") != "" {
req.staleRecache = true
}
// w.Header().Set("microcache-no-stale-recache", "1")
if headers.Get("microcache-no-stale-recache") != "" {
req.staleRecache = false
}
// w.Header().Add("microcache-vary-query", "q, page, limit")
if varyQueries, ok := headers["Microcache-Vary-Query"]; ok {
for _, hdr := range varyQueries {
varyQueryParams := strings.Split(hdr, ",")
for i, v := range varyQueryParams {
varyQueryParams[i] = strings.Trim(v, " ")
}
req.varyQuery = append(req.varyQuery, varyQueryParams...)
}
}
// w.Header().Add("microcache-vary", "accept-language, accept-encoding")
if varyHdr, ok := headers["Microcache-Vary"]; ok {
for _, hdr := range varyHdr {
varyHdrs := strings.Split(hdr, ",")
for i, v := range varyHdrs {
varyHdrs[i] = strings.Trim(v, " ")
}
req.vary = append(req.vary, varyHdrs...)
}
}
// w.Header().Add("Vary", "accept-language, accept-encoding")
if varyHdr, ok := headers["Vary"]; ok {
for _, hdr := range varyHdr {
varyHdrs := strings.Split(hdr, ",")
for i, v := range varyHdrs {
varyHdrs[i] = strings.Trim(v, " ")
}
req.vary = append(req.vary, varyHdrs...)
}
}
return req
}