2022-02-21 17:47:41 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
// cacheRecorder is an implementation of http.ResponseWriter
|
|
|
|
type cacheRecorder struct {
|
2022-02-21 21:02:33 +00:00
|
|
|
item cacheItem
|
|
|
|
done bool
|
2022-02-21 17:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newCacheRecorder() *cacheRecorder {
|
|
|
|
return &cacheRecorder{
|
2022-02-21 21:02:33 +00:00
|
|
|
item: cacheItem{
|
2022-02-21 17:47:41 +00:00
|
|
|
code: http.StatusOK,
|
2022-02-21 21:02:33 +00:00
|
|
|
header: http.Header{},
|
2022-02-21 17:47:41 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 21:02:33 +00:00
|
|
|
func (c *cacheRecorder) finish() *cacheItem {
|
|
|
|
c.done = true
|
|
|
|
return &c.item
|
2022-02-21 17:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Header implements http.ResponseWriter.
|
2022-02-21 21:02:33 +00:00
|
|
|
func (c *cacheRecorder) Header() http.Header {
|
|
|
|
if c.done {
|
2022-02-21 17:47:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-02-21 21:02:33 +00:00
|
|
|
return c.item.header
|
2022-02-21 17:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write implements http.ResponseWriter.
|
2022-02-21 21:02:33 +00:00
|
|
|
func (c *cacheRecorder) Write(buf []byte) (int, error) {
|
|
|
|
if c.done {
|
2022-02-21 17:47:41 +00:00
|
|
|
return 0, nil
|
|
|
|
}
|
2022-02-21 21:02:33 +00:00
|
|
|
c.item.body = append(c.item.body, buf...)
|
2022-02-21 17:47:41 +00:00
|
|
|
return len(buf), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteString implements io.StringWriter.
|
2022-02-21 21:02:33 +00:00
|
|
|
func (c *cacheRecorder) WriteString(str string) (int, error) {
|
|
|
|
if c.done {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
return c.Write([]byte(str))
|
2022-02-21 17:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteHeader implements http.ResponseWriter.
|
2022-02-21 21:02:33 +00:00
|
|
|
func (c *cacheRecorder) WriteHeader(code int) {
|
|
|
|
if c.done {
|
2022-02-21 17:47:41 +00:00
|
|
|
return
|
|
|
|
}
|
2022-02-21 21:02:33 +00:00
|
|
|
c.item.code = code
|
2022-02-21 17:47:41 +00:00
|
|
|
}
|