-
Notifications
You must be signed in to change notification settings - Fork 5
/
response.go
71 lines (62 loc) · 1.34 KB
/
response.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
package microcache
import (
"net/http"
"strings"
"time"
)
// Response is used both as a cache object for the response
// and to wrap http.ResponseWriter for downstream requests.
type Response struct {
found bool
date time.Time
expires time.Time
status int
headerWritten bool
header http.Header
body []byte
}
func (res *Response) Write(b []byte) (int, error) {
res.body = append(res.body, b...)
return len(b), nil
}
func (res *Response) Header() http.Header {
return res.header
}
func (res *Response) WriteHeader(code int) {
res.status = code
res.headerWritten = true
}
func (res *Response) sendResponse(w http.ResponseWriter) {
for header, values := range res.header {
// Do not forward microcache headers to client
if strings.HasPrefix(header, "Microcache-") {
continue
}
for _, val := range values {
w.Header().Add(header, val)
}
}
if res.headerWritten {
w.WriteHeader(res.status)
}
w.Write(res.body)
return
}
func (res *Response) clone() Response {
return Response{
found: res.found,
date: res.date,
expires: res.expires,
status: res.status,
header: res.header,
body: res.body,
}
}
type passthroughWriter struct {
http.ResponseWriter
status int
}
func (w *passthroughWriter) WriteHeader(code int) {
w.status = code
w.ResponseWriter.WriteHeader(code)
}