-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
67 lines (54 loc) · 1.26 KB
/
handler.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
package main
import (
"log"
"net/http"
"time"
)
var epoch = time.Unix(0, 0).Format(time.RFC1123)
var noCacheHeaders = map[string]string{
"Expires": epoch,
"Cache-Control": "no-cache, private, max-age=0",
"Pragma": "no-cache",
"X-Accel-Expires": "0",
}
var etagHeaders = []string{
"ETag",
"If-Modified-Since",
"If-Match",
"If-None-Match",
"If-Range",
"If-Unmodified-Since",
}
func noCache(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Delete any ETag headers that may have been set
for _, v := range etagHeaders {
if r.Header.Get(v) != "" {
r.Header.Del(v)
}
}
rw := &responseWriter{ResponseWriter: w}
// Set our NoCache headers
for k, v := range noCacheHeaders {
rw.Header().Set(k, v)
}
start := time.Now()
h.ServeHTTP(rw, r)
dur := time.Since(start)
log.Printf(`"%s %s %s" %d %d %s`, r.Method, r.RequestURI, r.Proto, rw.status, rw.written, dur)
}
}
type responseWriter struct {
http.ResponseWriter
status int
written int
}
func (rw *responseWriter) WriteHeader(code int) {
rw.status = code
rw.ResponseWriter.WriteHeader(code)
}
func (rw *responseWriter) Write(b []byte) (int, error) {
n, err := rw.ResponseWriter.Write(b)
rw.written = n
return n, err
}