-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathetag.go
86 lines (70 loc) · 1.68 KB
/
etag.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
package etag
import (
"bytes"
"crypto/sha1"
"encoding/hex"
"fmt"
"hash"
"net/http"
"strconv"
"github.com/go-http-utils/fresh"
"github.com/go-http-utils/headers"
)
// Version is this package's version.
const Version = "0.2.1"
type hashWriter struct {
rw http.ResponseWriter
hash hash.Hash
buf *bytes.Buffer
len int
status int
}
func (hw hashWriter) Header() http.Header {
return hw.rw.Header()
}
func (hw *hashWriter) WriteHeader(status int) {
hw.status = status
}
func (hw *hashWriter) Write(b []byte) (int, error) {
if hw.status == 0 {
hw.status = http.StatusOK
}
// bytes.Buffer.Write(b) always return (len(b), nil), so just
// ignore the return values.
hw.buf.Write(b)
l, err := hw.hash.Write(b)
hw.len += l
return l, err
}
func writeRaw(res http.ResponseWriter, hw hashWriter) {
res.WriteHeader(hw.status)
res.Write(hw.buf.Bytes())
}
// Handler wraps the http.Handler h with ETag support.
func Handler(h http.Handler, weak bool) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
hw := hashWriter{rw: res, hash: sha1.New(), buf: bytes.NewBuffer(nil)}
h.ServeHTTP(&hw, req)
resHeader := res.Header()
if hw.hash == nil ||
resHeader.Get(headers.ETag) != "" ||
strconv.Itoa(hw.status)[0] != '2' ||
hw.status == http.StatusNoContent ||
hw.buf.Len() == 0 {
writeRaw(res, hw)
return
}
etag := fmt.Sprintf("%v-%v", strconv.Itoa(hw.len),
hex.EncodeToString(hw.hash.Sum(nil)))
if weak {
etag = "W/" + etag
}
resHeader.Set(headers.ETag, etag)
if fresh.IsFresh(req.Header, resHeader) {
res.WriteHeader(http.StatusNotModified)
res.Write(nil)
} else {
writeRaw(res, hw)
}
})
}