forked from bxcodec/httpcache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.go
69 lines (56 loc) · 1.89 KB
/
validator.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
package httpcache
import (
cacheControl "github.com/bxcodec/httpcache/helper/cacheheader"
"net/http"
"time"
)
type Validator interface {
ValidateCacheControl(req *http.Request, resp *http.Response) (validationResult cacheControl.ObjectResults, err error)
}
func NewDefaultValidator() Validator {
return &defaultValidator{}
}
type defaultValidator struct{}
func (d *defaultValidator) ValidateCacheControl(req *http.Request, resp *http.Response) (validationResult cacheControl.ObjectResults, err error) {
reqDir, err := cacheControl.ParseRequestCacheControl(req.Header.Get("Cache-Control"))
if err != nil {
return
}
resDir, err := cacheControl.ParseResponseCacheControl(resp.Header.Get("Cache-Control"))
if err != nil {
return
}
expiry := resp.Header.Get("Expires")
expiresHeader, err := http.ParseTime(expiry)
if err != nil && expiry != "" &&
// https://stackoverflow.com/questions/11357430/http-expires-header-values-0-and-1
expiry != "-1" && expiry != "0" {
return
}
dateHeaderStr := resp.Header.Get("Date")
dateHeader, err := http.ParseTime(dateHeaderStr)
if err != nil && dateHeaderStr != "" {
return
}
lastModifiedStr := resp.Header.Get("Last-Modified")
lastModifiedHeader, err := http.ParseTime(lastModifiedStr)
if err != nil && lastModifiedStr != "" {
return
}
obj := cacheControl.Object{
RespDirectives: resDir,
RespHeaders: resp.Header,
RespStatusCode: resp.StatusCode,
RespExpiresHeader: expiresHeader,
RespDateHeader: dateHeader,
RespLastModifiedHeader: lastModifiedHeader,
ReqDirectives: reqDir,
ReqHeaders: req.Header,
ReqMethod: req.Method,
NowUTC: time.Now().UTC(),
}
validationResult = cacheControl.ObjectResults{}
cacheControl.CachableObject(&obj, &validationResult)
cacheControl.ExpirationObject(&obj, &validationResult)
return validationResult, nil
}