This repository has been archived by the owner on Apr 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathvalidator.go
66 lines (54 loc) · 1.52 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
package httpcache
import (
"fmt"
"net/http"
"net/http/httptest"
)
type Validator struct {
Handler http.Handler
}
func (v *Validator) Validate(req *http.Request, res *Resource) bool {
outreq := cloneRequest(req)
resHeaders := res.Header()
if etag := resHeaders.Get("Etag"); etag != "" {
outreq.Header.Set("If-None-Match", etag)
} else if lastMod := resHeaders.Get("Last-Modified"); lastMod != "" {
outreq.Header.Set("If-Modified-Since", lastMod)
}
t := Clock()
resp := httptest.NewRecorder()
v.Handler.ServeHTTP(resp, outreq)
resp.Flush()
if age, err := correctedAge(resp.HeaderMap, t, Clock()); err == nil {
resp.Header().Set("Age", fmt.Sprintf("%.f", age.Seconds()))
}
if headersEqual(resHeaders, resp.HeaderMap) {
res.header = resp.HeaderMap
res.header.Set(ProxyDateHeader, Clock().Format(http.TimeFormat))
return true
}
return false
}
var validationHeaders = []string{"ETag", "Content-MD5", "Last-Modified", "Content-Length"}
func headersEqual(h1, h2 http.Header) bool {
for _, header := range validationHeaders {
if value := h2.Get(header); value != "" {
if h1.Get(header) != value {
debugf("%s changed, %q != %q", header, value, h1.Get(header))
return false
}
}
}
return true
}
// cloneRequest returns a clone of the provided *http.Request.
// The clone is a shallow copy of the struct and its Header map.
func cloneRequest(r *http.Request) *http.Request {
r2 := new(http.Request)
*r2 = *r
r2.Header = make(http.Header)
for k, s := range r.Header {
r2.Header[k] = s
}
return r2
}