-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfresh.go
121 lines (94 loc) · 2.83 KB
/
fresh.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 fresh
import (
"net/http"
"strings"
"time"
"github.com/go-http-utils/headers"
)
// Version is this package's verison
const Version = "0.4.0"
// IsFresh check whether cache can be used in this HTTP request
func IsFresh(reqHeader http.Header, resHeader http.Header) bool {
isEtagMatched, isModifiedMatched := false, false
ifModifiedSince := reqHeader.Get(headers.IfModifiedSince)
ifUnmodifiedSince := reqHeader.Get(headers.IfUnmodifiedSince)
ifNoneMatch := reqHeader.Get(headers.IfNoneMatch)
ifMatch := reqHeader.Get(headers.IfMatch)
cacheControl := reqHeader.Get(headers.CacheControl)
etag := resHeader.Get(headers.ETag)
lastModified := resHeader.Get(headers.LastModified)
if ifModifiedSince == "" &&
ifUnmodifiedSince == "" &&
ifNoneMatch == "" &&
ifMatch == "" {
return false
}
if strings.Contains(cacheControl, "no-cache") {
return false
}
if etag != "" && ifNoneMatch != "" {
isEtagMatched = checkEtagNoneMatch(trimTags(strings.Split(ifNoneMatch, ",")), etag)
}
if etag != "" && ifMatch != "" && isEtagMatched != true {
isEtagMatched = checkEtagMatch(trimTags(strings.Split(ifMatch, ",")), etag)
}
if lastModified != "" && ifModifiedSince != "" {
isModifiedMatched = checkModifedMatch(lastModified, ifModifiedSince)
}
if lastModified != "" && ifUnmodifiedSince != "" && isModifiedMatched != true {
isModifiedMatched = checkUnmodifedMatch(lastModified, ifUnmodifiedSince)
}
return isEtagMatched || isModifiedMatched
}
func trimTags(tags []string) []string {
trimedTags := make([]string, len(tags))
for i, tag := range tags {
trimedTags[i] = strings.TrimSpace(tag)
}
return trimedTags
}
func checkEtagNoneMatch(etagsToNoneMatch []string, etag string) bool {
for _, etagToNoneMatch := range etagsToNoneMatch {
if etagToNoneMatch == "*" || etagToNoneMatch == etag || etagToNoneMatch == "W/"+etag {
return true
}
}
return false
}
func checkEtagMatch(etagsToMatch []string, etag string) bool {
for _, etagToMatch := range etagsToMatch {
if etagToMatch == "*" {
return false
}
if strings.HasPrefix(etagToMatch, "W/") {
if etagToMatch == "W/"+etag {
return false
}
} else {
if etagToMatch == etag {
return false
}
}
}
return true
}
func checkModifedMatch(lastModified, ifModifiedSince string) bool {
if lm, ims, ok := parseTimePairs(lastModified, ifModifiedSince); ok == true {
return lm.Before(ims)
}
return false
}
func checkUnmodifedMatch(lastModified, ifUnmodifiedSince string) bool {
if lm, ius, ok := parseTimePairs(lastModified, ifUnmodifiedSince); ok == true {
return lm.After(ius)
}
return false
}
func parseTimePairs(s1, s2 string) (t1 time.Time, t2 time.Time, ok bool) {
if t1, err := time.Parse(http.TimeFormat, s1); err == nil {
if t2, err := time.Parse(http.TimeFormat, s2); err == nil {
return t1, t2, true
}
}
return t1, t2, false
}