-
Notifications
You must be signed in to change notification settings - Fork 0
/
opts.go
625 lines (584 loc) · 16.5 KB
/
opts.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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
package diskcache
import (
"compress/gzip"
"compress/zlib"
"encoding/base64"
"errors"
"fmt"
"io/fs"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/gobwas/glob"
"github.com/spf13/afero"
"github.com/yookoala/realpath"
)
// Option is a disk cache option.
type Option interface {
apply(interface{}) error
}
// option wraps setting disk cache and simple matcher options.
type option struct {
cache func(*Cache) error
matcher func(*SimpleMatcher) error
}
// apply satisfies the Option interface.
func (opt option) apply(v interface{}) error {
switch z := v.(type) {
case *Cache:
return opt.cache(z)
case *SimpleMatcher:
return opt.matcher(z)
}
return fmt.Errorf("option cannot be used with %T", v)
}
// WithMethod is a disk cache option to set matching request method(s).
func WithMethod(method ...string) Option {
return option{
cache: func(c *Cache) error {
return WithMethod(method...).apply(c.matcher)
},
matcher: func(m *SimpleMatcher) error {
var err error
m.method, err = glob.Compile("{"+strings.Join(method, ",")+"}", ',')
return err
},
}
}
// WithTransport is a disk cache option to set the underlying HTTP transport.
func WithTransport(transport http.RoundTripper) Option {
return option{
cache: func(c *Cache) error {
c.transport = transport
return nil
},
}
}
// WithMode is a disk cache option to set the file mode used when creating
// files and directories on disk.
func WithMode(dirMode, fileMode os.FileMode) Option {
return option{
cache: func(c *Cache) error {
c.dirMode, c.fileMode = dirMode, fileMode
return nil
},
}
}
// WithFs is a disk cache option to set the afero fs used.
//
// See: https://github.com/spf13/afero
func WithFs(fs afero.Fs) Option {
return option{
cache: func(c *Cache) error {
c.fs = fs
return nil
},
}
}
// WithBasePathFs is a disk cache option to set the afero fs used locked to a
// base directory.
//
// See: https://github.com/spf13/afero
func WithBasePathFs(basePath string) Option {
return option{
cache: func(c *Cache) error {
// ensure path exists and is directory
fi, err := os.Stat(basePath)
switch {
case err != nil && errors.Is(err, fs.ErrNotExist):
if err := os.MkdirAll(basePath, c.dirMode); err != nil {
return err
}
case err != nil:
return err
case !fi.IsDir():
return fmt.Errorf("base path %s is not a directory", basePath)
}
// resolve real path
if basePath, err = realpath.Realpath(basePath); err != nil {
return err
}
c.fs = afero.NewBasePathFs(afero.NewOsFs(), basePath)
return nil
},
}
}
// WithAppCacheDir is a disk cache option to set the afero fs used locked to
// the user's cache directory joined with the app name and any passed paths.
//
// The afero base fs directory will typically be $HOME/.cache/<app>/paths...
func WithAppCacheDir(app string, paths ...string) Option {
return option{
cache: func(c *Cache) error {
dir, err := UserCacheDir(append([]string{app}, paths...)...)
if err != nil {
return err
}
return WithBasePathFs(dir).apply(c)
},
}
}
// WithMatchers is a disk cache option to set matchers.
func WithMatchers(matchers ...Matcher) Option {
return option{
cache: func(c *Cache) error {
c.matchers = matchers
return nil
},
}
}
// WithDefaultMatcher is a disk cache option to set the default matcher.
func WithDefaultMatcher(method, host, path, key string, opts ...Option) Option {
return option{
cache: func(c *Cache) error {
m, err := NewSimpleMatcher(method, host, path, key, opts...)
if err != nil {
return err
}
x := &Cache{noDefault: true}
if err := m.apply(x); err != nil {
return err
}
c.matcher = x.matchers[0].(*SimpleMatcher)
return nil
},
}
}
// WithNoDefault is a disk cache option to disable the default matcher.
//
// Prevents propagating settings from default matcher.
func WithNoDefault() Option {
return option{
cache: func(c *Cache) error {
c.noDefault = true
return nil
},
}
}
// WithHeaderTransformers is a disk cache option to set the header
// transformers.
func WithHeaderTransformers(headerTransformers ...HeaderTransformer) Option {
return option{
cache: func(c *Cache) error {
c.matcher.policy.HeaderTransformers = headerTransformers
return nil
},
matcher: func(m *SimpleMatcher) error {
m.policy.HeaderTransformers = headerTransformers
return nil
},
}
}
// WithHeaderBlacklist is a disk cache option to add a header transformer that
// removes any header in the blacklist.
func WithHeaderBlacklist(blacklist ...string) Option {
headerTransformer, err := stripHeaders(blacklist...)
if err != nil {
panic(err)
}
return option{
cache: func(c *Cache) error {
c.matcher.policy.HeaderTransformers = append(c.matcher.policy.HeaderTransformers, headerTransformer)
return nil
},
matcher: func(m *SimpleMatcher) error {
m.policy.HeaderTransformers = append(m.policy.HeaderTransformers, headerTransformer)
return nil
},
}
}
// WithHeaderWhitelist is a disk cache option to add a header transformer that
// removes any header not in the whitelist.
func WithHeaderWhitelist(whitelist ...string) Option {
headerTransformer, err := keepHeaders(whitelist...)
if err != nil {
panic(err)
}
return option{
cache: func(c *Cache) error {
c.matcher.policy.HeaderTransformers = append(c.matcher.policy.HeaderTransformers, headerTransformer)
return nil
},
matcher: func(m *SimpleMatcher) error {
m.policy.HeaderTransformers = append(m.policy.HeaderTransformers, headerTransformer)
return nil
},
}
}
// WithHeaderTransform is a disk cache option to add a header transformer that
// transforms headers matching the provided regexp pairs and replacements.
func WithHeaderTransform(pairs ...string) Option {
headerTransformer, err := NewHeaderTransformer(pairs...)
if err != nil {
panic(err)
}
return option{
cache: func(c *Cache) error {
c.matcher.policy.HeaderTransformers = append(c.matcher.policy.HeaderTransformers, headerTransformer)
return nil
},
matcher: func(m *SimpleMatcher) error {
m.policy.HeaderTransformers = append(m.policy.HeaderTransformers, headerTransformer)
return nil
},
}
}
// WithBodyTransformers is a disk cache option to set the body transformers.
func WithBodyTransformers(bodyTransformers ...BodyTransformer) Option {
return option{
cache: func(c *Cache) error {
c.matcher.policy.BodyTransformers = bodyTransformers
return nil
},
matcher: func(m *SimpleMatcher) error {
m.policy.BodyTransformers = bodyTransformers
return nil
},
}
}
// WithMinifier is a disk cache option to add a body transformer that does
// content minification of HTML, XML, SVG, JavaScript, JSON, and CSS data.
// Useful for reducing disk storage sizes.
//
// See: https://github.com/tdewolff/minify
func WithMinifier() Option {
t := Minifier{
Priority: TransformPriorityMinify,
}
return option{
cache: func(c *Cache) error {
c.matcher.policy.BodyTransformers = append(c.matcher.policy.BodyTransformers, t)
return nil
},
matcher: func(m *SimpleMatcher) error {
m.policy.BodyTransformers = append(m.policy.BodyTransformers, t)
return nil
},
}
}
// WithTruncator is a disk cache option to add a body transformer that
// truncates responses based on match criteria.
func WithTruncator(priority TransformPriority, match func(string, int, string) bool) Option {
t := Truncator{
Priority: priority,
Match: match,
}
return option{
cache: func(c *Cache) error {
c.matcher.policy.BodyTransformers = append(c.matcher.policy.BodyTransformers, t)
return nil
},
matcher: func(m *SimpleMatcher) error {
m.policy.BodyTransformers = append(m.policy.BodyTransformers, t)
return nil
},
}
}
// WithStatusCodeTrunactor is a disk cache option to add a body transformer
// that truncates responses when the status code is not in the provided list.
func WithStatusCodeTruncator(statusCodes ...int) Option {
t := Truncator{
Priority: TransformPriorityFirst,
Match: func(_ string, statusCode int, _ string) bool {
return !containsInt(statusCodes, statusCode)
},
}
return option{
cache: func(c *Cache) error {
c.matcher.policy.BodyTransformers = append(c.matcher.policy.BodyTransformers, t)
return nil
},
matcher: func(m *SimpleMatcher) error {
m.policy.BodyTransformers = append(m.policy.BodyTransformers, t)
return nil
},
}
}
// WithErrorTruncator is a disk cache option to add a body transformer that
// truncates responses when the HTTP status code != OK (200).
func WithErrorTruncator() Option {
t := Truncator{
Priority: TransformPriorityFirst,
Match: func(_ string, code int, _ string) bool {
return code != http.StatusOK
},
}
return option{
cache: func(c *Cache) error {
c.matcher.policy.BodyTransformers = append(c.matcher.policy.BodyTransformers, t)
return nil
},
matcher: func(m *SimpleMatcher) error {
m.policy.BodyTransformers = append(m.policy.BodyTransformers, t)
return nil
},
}
}
// WithBase64Decoder is a disk cache option to add a body transformer that does
// base64 decoding of responses for specific content types.
func WithBase64Decoder(contentTypes ...string) Option {
t := Base64Decoder{
Priority: TransformPriorityDecode,
ContentTypes: contentTypes,
Encoding: base64.StdEncoding,
}
return option{
cache: func(c *Cache) error {
c.matcher.policy.BodyTransformers = append(c.matcher.policy.BodyTransformers, t)
return nil
},
matcher: func(m *SimpleMatcher) error {
m.policy.BodyTransformers = append(m.policy.BodyTransformers, t)
return nil
},
}
}
// WithPrefixStripper is a disk cache option to add a body transformer that
// strips a specific XSS prefix for a specified content type.
//
// Useful for removing XSS prefixes added to JavaScript or JSON content.
func WithPrefixStripper(prefix []byte, contentTypes ...string) Option {
t := PrefixStripper{
Priority: TransformPriorityModify,
ContentTypes: contentTypes,
Prefix: prefix,
}
return option{
cache: func(c *Cache) error {
c.matcher.policy.BodyTransformers = append(c.matcher.policy.BodyTransformers, t)
return nil
},
matcher: func(m *SimpleMatcher) error {
m.policy.BodyTransformers = append(m.policy.BodyTransformers, t)
return nil
},
}
}
// WithMarshalUnmarshaler is a disk cache option to set a marshaler/unmarshaler.
func WithMarshalUnmarshaler(marshalUnmarshaler MarshalUnmarshaler) Option {
return option{
cache: func(c *Cache) error {
c.matcher.policy.MarshalUnmarshaler = marshalUnmarshaler
return nil
},
matcher: func(m *SimpleMatcher) error {
m.policy.MarshalUnmarshaler = marshalUnmarshaler
return nil
},
}
}
// WithGzipCompression is a disk cache option to set a gzip marshaler/unmarshaler.
func WithGzipCompression() Option {
z := GzipMarshalUnmarshaler{
Level: gzip.DefaultCompression,
}
return option{
cache: func(c *Cache) error {
c.matcher.policy.MarshalUnmarshaler = z
return nil
},
matcher: func(m *SimpleMatcher) error {
m.policy.MarshalUnmarshaler = z
return nil
},
}
}
// WithZlibCompression is a disk cache option to set a zlib marshaler/unmarshaler.
func WithZlibCompression() Option {
z := ZlibMarshalUnmarshaler{
Level: zlib.DefaultCompression,
}
return option{
cache: func(c *Cache) error {
c.matcher.policy.MarshalUnmarshaler = z
return nil
},
matcher: func(m *SimpleMatcher) error {
m.policy.MarshalUnmarshaler = z
return nil
},
}
}
// WithFlatStorage is a disk cache option to set a flat marshaler/unmarshaler
// removing headers from responses.
//
// Note: cached responses will not have original headers.
func WithFlatStorage() Option {
z := FlatMarshalUnmarshaler{}
return option{
cache: func(c *Cache) error {
c.matcher.policy.MarshalUnmarshaler = z
return nil
},
matcher: func(m *SimpleMatcher) error {
m.policy.MarshalUnmarshaler = z
return nil
},
}
}
// WithFlatChain is a disk cache option that marshals/unmarshals responses,
// removing headers from responses, and chaining marshaling/unmarshaling to a
// provided marshaler/unmarshaler.
//
// Note: cached responses will not have original headers.
func WithFlatChain(marshalUnmarshaler MarshalUnmarshaler) Option {
z := FlatMarshalUnmarshaler{Chain: marshalUnmarshaler}
return option{
cache: func(c *Cache) error {
c.matcher.policy.MarshalUnmarshaler = z
return nil
},
matcher: func(m *SimpleMatcher) error {
m.policy.MarshalUnmarshaler = z
return nil
},
}
}
// WithFlatGzipCompression is a disk cache option that marshals/unmarshals
// responses, with headers removed from responses, and with gzip compression.
//
// Note: cached responses will not have original headers.
func WithFlatGzipCompression() Option {
return WithFlatChain(GzipMarshalUnmarshaler{
Level: gzip.DefaultCompression,
})
}
// WithFlatZlibCompression is a disk cache option that marshals/unmarshals
// responses, with headers removed from responses, and with zlib compression.
//
// Note: cached responses will not have original headers.
func WithFlatZlibCompression() Option {
return WithFlatChain(ZlibMarshalUnmarshaler{
Level: zlib.DefaultCompression,
})
}
// WithTTL is a disk cache option to set the cache policy TTL.
func WithTTL(ttl time.Duration) Option {
return option{
cache: func(c *Cache) error {
c.matcher.policy.TTL = ttl
return nil
},
matcher: func(m *SimpleMatcher) error {
m.policy.TTL = ttl
return nil
},
}
}
// WithIndexPath is a disk cache option to set the index path name.
func WithIndexPath(indexPath string) Option {
return option{
cache: func(c *Cache) error {
c.matcher.indexPath = indexPath
return nil
},
matcher: func(m *SimpleMatcher) error {
m.indexPath = indexPath
return nil
},
}
}
// WithLongPathHandler is a disk cache option to set a long path handler.
func WithLongPathHandler(longPathHandler func(string) string) Option {
return option{
cache: func(c *Cache) error {
c.matcher.longPathHandler = longPathHandler
return nil
},
matcher: func(m *SimpleMatcher) error {
m.longPathHandler = longPathHandler
return nil
},
}
}
// WithQueryEncoder is a disk cache option to set the query encoder.
func WithQueryEncoder(queryEncoder func(url.Values) string) Option {
return option{
cache: func(c *Cache) error {
c.matcher.queryEncoder = queryEncoder
return nil
},
matcher: func(m *SimpleMatcher) error {
m.queryEncoder = queryEncoder
return nil
},
}
}
// WithQueryPrefix is a disk cache option that sets a query encoder, that adds
// the supplied prefix to non-empty and canonical encoding
//
// The query string encoder can be limited to only the passed fields.
func WithQueryPrefix(prefix string, fields ...string) Option {
f := func(v url.Values) string {
if len(fields) > 0 {
for k := range v {
if !contains(fields, k) {
delete(v, k)
}
}
}
if s := url.QueryEscape(v.Encode()); s != "" {
return prefix + s
}
return ""
}
return option{
cache: func(c *Cache) error {
c.matcher.queryEncoder = f
return nil
},
matcher: func(m *SimpleMatcher) error {
m.queryEncoder = f
return nil
},
}
}
// WithValidator is a disk cache option to set the cache policy validator.
func WithValidator(validator Validator) Option {
return option{
cache: func(c *Cache) error {
c.matcher.policy.Validator = validator
return nil
},
matcher: func(m *SimpleMatcher) error {
m.policy.Validator = validator
return nil
},
}
}
// WithValidatorFunc is a disk cache option to set the cache policy validator.
func WithValidatorFunc(f ValidatorFunc) Option {
validator := NewSimpleValidator(f)
return option{
cache: func(c *Cache) error {
c.matcher.policy.Validator = validator
return nil
},
matcher: func(m *SimpleMatcher) error {
m.policy.Validator = validator
return nil
},
}
}
// WithContentTypeTTL is a disk cache option to set the cache policy TTL for
// matching content types.
func WithContentTypeTTL(ttl time.Duration, contentTypes ...string) Option {
return WithValidatorFunc(func(_ *http.Request, res *http.Response, mod time.Time, _ bool, _ int) (Validity, error) {
if ttl != 0 && time.Now().After(mod.Add(ttl)) && contains(contentTypes, res.Header.Get("Content-Type")) {
return Retry, nil
}
return Valid, nil
})
}
// WithRetryStatusCode is a disk cache option to add a validator to the cache
// policy that retries when the response status is not the expected status.
func WithRetryStatusCode(retries int, expected ...int) Option {
return WithValidatorFunc(func(_ *http.Request, res *http.Response, mod time.Time, _ bool, count int) (Validity, error) {
if retries < count && !containsInt(expected, res.StatusCode) {
return Retry, nil
}
return Valid, nil
})
}