forked from jlelse/GoBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cacheRecorder.go
58 lines (50 loc) · 1.01 KB
/
cacheRecorder.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
package main
import (
"net/http"
)
// cacheRecorder is an implementation of http.ResponseWriter
type cacheRecorder struct {
item cacheItem
done bool
}
func newCacheRecorder() *cacheRecorder {
return &cacheRecorder{
item: cacheItem{
code: http.StatusOK,
header: http.Header{},
},
}
}
func (c *cacheRecorder) finish() *cacheItem {
c.done = true
return &c.item
}
// Header implements http.ResponseWriter.
func (c *cacheRecorder) Header() http.Header {
if c.done {
return nil
}
return c.item.header
}
// Write implements http.ResponseWriter.
func (c *cacheRecorder) Write(buf []byte) (int, error) {
if c.done {
return 0, nil
}
c.item.body = append(c.item.body, buf...)
return len(buf), nil
}
// WriteString implements io.StringWriter.
func (c *cacheRecorder) WriteString(str string) (int, error) {
if c.done {
return 0, nil
}
return c.Write([]byte(str))
}
// WriteHeader implements http.ResponseWriter.
func (c *cacheRecorder) WriteHeader(code int) {
if c.done {
return
}
c.item.code = code
}