-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmwcache.go
323 lines (288 loc) · 7.88 KB
/
mwcache.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
package mwcache
import (
"bytes"
"encoding/gob"
"fmt"
"io"
"net"
"net/http"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"go.uber.org/zap"
)
// backend and config are global to preserve the cache between reloads
var (
backend Backend
config *Config
)
type metadata struct {
Header http.Header
Status int
}
var errStale = fmt.Errorf("stale")
const timeFormat = "Mon, 2 Jan 2006 15:04:05 MST"
func init() {
caddy.RegisterModule(Handler{})
httpcaddyfile.RegisterHandlerDirective("mwcache", parseCaddyfile)
}
type Handler struct {
logger *zap.Logger
config Config
}
type Config struct {
Backend string `json:"backend,omitempty"`
PurgeAcl []string `json:"purge_acl,omitempty"`
RistrettoConfig map[string]string `json:"ristretto_config,omitempty"`
}
// CaddyModule implements caddy.Module
func (Handler) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.mwcache",
New: func() caddy.Module { return new(Handler) },
}
}
func CIDRContainsIP(cidr string, needleStr string) bool {
// Ignore port
if strings.Contains(needleStr, ":") {
needleStr = strings.Split(needleStr, ":")[0]
}
// Return correct value even if the given 'cidr' is a ip address other then a cidr'
haystackIP := net.ParseIP(cidr)
needleIp := net.ParseIP(needleStr)
if haystackIP.Equal(needleIp) {
return true
}
// Cidr check
_, ipNet, err := net.ParseCIDR(cidr)
if err != nil {
return false
}
return ipNet.Contains(needleIp)
}
// ServeHTTP implements caddyhttp.MiddlewareHandler.
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
switch r.Method {
case "PURGE":
// Check Domain against purge acl
// See https://github.com/wikimedia/puppet/blob/120dff45/modules/varnish/templates/wikimedia-frontend.vcl.erb#L501-L513
acl := config.PurgeAcl
found := false
for _, cidr := range acl {
if CIDRContainsIP(cidr, r.RemoteAddr) {
found = true
break
}
}
if !found {
h.logger.Info("purging from " + r.RemoteAddr + " is blocked")
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("Method not allowed"))
return nil
}
key := createKey(r)
backend.delete(key)
h.logger.Info("purged: " + key)
w.WriteHeader(http.StatusNoContent)
w.Write([]byte("Purged"))
return nil
case http.MethodHead:
return h.serveUsingCacheIfAvaliable(w, r, next)
case http.MethodGet:
return h.serveUsingCacheIfAvaliable(w, r, next)
default:
return next.ServeHTTP(w, r)
}
}
func (h Handler) serveUsingCacheIfAvaliable(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
if !requestIsCacheable(r) {
h.logger.Info("request is uncacheable: " + r.URL.RequestURI())
return next.ServeHTTP(w, r)
}
key := createKey(r)
val, err := backend.get(key)
if err != nil {
if err == ErrKeyNotFound {
h.logger.Info("cache miss: " + key)
if err := h.serveAndCache(key, w, r, next); err != nil {
return err
}
return nil
}
return err
}
// Cache hit, response with cache
h.logger.Info("cache hit: " + key)
pool := sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
buf := pool.Get().(*bytes.Buffer)
buf.Reset()
defer pool.Put(buf)
buf.Write([]byte(val))
if err := h.writeResponse(w, buf, true); err != nil {
if err == errStale {
h.logger.Info("staled, drop: " + key)
if err := h.serveAndCache(key, w, r, next); err != nil {
return err
}
} else {
return err
}
}
return nil
}
func (h Handler) serveAndCache(key string, w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
pool := sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
buf := pool.Get().(*bytes.Buffer)
buf.Reset()
defer pool.Put(buf)
rec := caddyhttp.NewResponseRecorder(w, buf, func(status int, header http.Header) bool {
// TODO research cache spec for MediaWiki
if status < 200 || status >= 400 ||
// https://github.com/femiwiki/caddy-mwcache/issues/16
status == 304 {
return false
}
c := header.Get("Cache-Control")
if c == "" {
return false
}
if match, err := regexp.Match(`(private|no-cache|no-store)`, []byte(c)); err == nil && match {
return false
}
if header.Get("Set-Cookie") != "" {
return false
}
if header.Get("Date") == "" {
header.Set("Date", time.Now().UTC().Format(timeFormat))
}
// Recode header to buf
err := gob.NewEncoder(buf).Encode(metadata{
Header: header,
Status: status,
})
if err != nil {
h.logger.Error("", zap.Error(err))
return false
}
// Body is recoded implicitly by the recoder
return true
})
// Fetch upstream response
if err := next.ServeHTTP(rec, r); err != nil {
return err
}
if !rec.Buffered() || buf.Len() == 0 {
h.logger.Info("response is uncacheable: " + key)
} else {
// Cache recoded buf to the backend
response := string(buf.Bytes())
if err := backend.put(key, response); err != nil {
return err
}
h.logger.Info("put cache: " + key)
}
return h.writeResponse(w, buf, false)
}
func (h Handler) writeResponse(w http.ResponseWriter, buf *bytes.Buffer, fromCache bool) error {
header := w.Header()
var meta metadata
if err := gob.NewDecoder(buf).Decode(&meta); err != nil {
return err
}
if fromCache && !h.isFresh(meta.Header) {
return errStale
}
// Write header
for k, v := range meta.Header {
header[k] = v
}
w.WriteHeader(meta.Status)
// Write body
if _, err := io.Copy(w, buf); err != nil {
return err
}
return nil
}
// isFresh investments a request that has the given header is fresh.
// Targets only mediawiki-specific directives defined below files:
// - https://github.com/wikimedia/mediawiki/blob/master/includes/OutputPage.php
// - https://github.com/wikimedia/mediawiki/blob/master/includes/api/ApiMain.php
// - https://github.com/wikimedia/mediawiki/blob/master/includes/AjaxResponse.php
func (h Handler) isFresh(header http.Header) bool {
var maxAgeInt uint64
var err error
var date time.Time
cc := header.Get("Cache-Control")
if cc == "" {
// Cache-Control directive is not provided.
h.logger.Info("stored cache has no Cache-Control header")
return true
}
re := regexp.MustCompile(`s-maxage\s*=\s*(\d+)`)
submatch := re.FindStringSubmatch(cc)
if len(submatch) != 2 {
h.logger.Info("Cache-Control has no s-maxage")
return true
}
maxAgeStr := submatch[1]
if maxAgeInt, err = strconv.ParseUint(maxAgeStr, 10, 32); err != nil {
h.logger.Info("parsing " + maxAgeStr + " failed")
return true
}
dateHeader := header.Get("Date")
if dateHeader == "" {
h.logger.Info("Date header is missing")
return true
}
date, err = time.Parse(timeFormat, dateHeader)
if err != nil {
h.logger.Info("parsing " + dateHeader + " failed")
return true
}
date = date.UTC()
now := time.Now().UTC()
maxAge := time.Duration(maxAgeInt)
return (date.Add(time.Second * maxAge)).After(now)
}
func createKey(r *http.Request) string {
// TODO use hash function?
// Use URL.RequestURI() instead of URL.String() to truncate domain.
return r.URL.RequestURI()
}
// NOTE: requests to RESTBase is not reach this module because of reverse_proxy has higher order
func requestIsCacheable(r *http.Request) bool {
// don't cache authorized requests
if _, _, ok := r.BasicAuth(); ok {
return false
}
// don't cache request with session or token cookie
// https://www.mediawiki.org/wiki/Manual:Varnish_caching#Configuring_Varnish
cookie := r.Header.Get("Cookie")
if match, err := regexp.Match(`([sS]ession|Token)=`, []byte(cookie)); err == nil && match {
return false
}
if key := createKey(r); key == "" {
return false
}
return true
}
// Interface guards
var (
_ caddy.Module = (*Handler)(nil)
_ caddy.Provisioner = (*Handler)(nil)
_ caddy.Validator = (*Handler)(nil)
_ caddyhttp.MiddlewareHandler = (*Handler)(nil)
)