This repository has been archived by the owner on Mar 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers.go
121 lines (101 loc) · 2.67 KB
/
helpers.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
package main
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"time"
emoji "gopkg.in/kyokomi/emoji.v1"
"github.com/boltdb/bolt"
)
/* verifyContentType
Reads the original response headers to parse the content type.
====================================================================== */
func verifyContentType(header http.Header) (allowed bool) {
contentTypes := strings.Split(header.Get("Content-Type"), "; ")
allowed = false
allowedFormats := [17]string{
"text/css",
"text/javascript",
"image/vnd.microsoft.icon",
"image/x-icon",
"image/gif",
"image/png",
"image/jpeg",
"image/bmp",
"image/webp",
"audio/midi",
"audio/mpeg",
"audio/webm",
"audio/ogg",
"audio/wav",
"video/webm",
"video/ogg",
}
for _, f := range contentTypes {
for _, a := range allowedFormats {
if a == f {
allowed = true
}
}
}
return allowed
}
/* defineCacheControl
Reads the original response headers to parse Cache-Control rules.
====================================================================== */
func defineCacheControl(header http.Header) (ignore bool, revalidate bool, maxAge int) {
cacheRules := strings.Split(header.Get("Cache-Control"), ", ")
ignore = false
revalidate = false
maxAge = 3600 * 1 // Defaults to 1 hour
for _, rule := range cacheRules {
switch {
case strings.Contains(rule, "private") || strings.Contains(rule, "no-store"):
ignore = true
case strings.Contains(rule, "no-cache") || strings.Contains(rule, "must-revalidate") || strings.Contains(rule, "proxy-revalidate"):
revalidate = true
case strings.Contains(rule, "max-age"):
i, err := strconv.Atoi(rule[8:])
if err == nil {
maxAge = i
} else {
fmt.Println("Error while converting max age string to int", "\n-", err)
}
}
}
return ignore, revalidate, maxAge
}
/* createHash
Creates a hash based on the origin url.
====================================================================== */
func createHash(originURL string) (refHash string) {
h := md5.New()
h.Write([]byte(originURL))
refHash = hex.EncodeToString(h.Sum(nil))
return refHash
}
/* cleanCache
Checks the entire database for expired files.
====================================================================== */
func cleanCache() {
now := time.Now().Unix()
db.Batch(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("Cache"))
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
file := File{}
json.Unmarshal(v, &file)
if now <= (file.Timestamp + file.MaxAge) {
emoji.Println(":fire: Removing expired file:", file.LocalFile)
b.Delete([]byte(k))
os.Remove(file.LocalFile)
}
}
return nil
})
}