-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
92 lines (77 loc) · 1.8 KB
/
types.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
87
88
89
90
91
92
package zlog
import (
"fmt"
"net/http"
)
type ResponseLog struct {
Request *http.Request
StatusCode int `json:"staus_code"`
Body string `json:"body"`
Header string `json:"header"`
}
func (rl ResponseLog) DumpResponse() string {
res := ""
res += fmt.Sprintf("HTTP/%d.%d %d %s\r\n", rl.Request.ProtoMajor, rl.Request.ProtoMinor,
rl.StatusCode, http.StatusText(rl.StatusCode))
res += rl.Header
res += "\r\n"
res += rl.Body
return res
}
// headerOperation represents an operation on the header
type headerOperation func(http.Header)
type ResponseProxyWriter struct {
writer http.ResponseWriter
Body []byte
Code int
ops []headerOperation
SourceHeader http.Header
wroteHeader bool
}
func (w *ResponseProxyWriter) Header() http.Header {
return w.writer.Header()
}
func (w *ResponseProxyWriter) Write(bytes []byte) (int, error) {
if !w.wroteHeader {
w.WriteHeader(http.StatusOK)
}
w.Body = append(w.Body, bytes[0:len(bytes)]...)
return w.writer.Write(bytes)
}
func cloneHeader(h http.Header) http.Header {
h2 := make(http.Header, len(h))
for k, vv := range h {
vv2 := make([]string, len(vv))
copy(vv2, vv)
h2[k] = vv2
}
return h2
}
func (w *ResponseProxyWriter) WriteHeader(i int) {
if w.wroteHeader {
return
}
w.wroteHeader = true
h := w.writer.Header()
w.SourceHeader = cloneHeader(h)
// perform our revisions
for _, op := range w.ops {
op(h)
}
w.Code = i
w.writer.WriteHeader(i)
}
func (w *ResponseProxyWriter) delHeader(key string) {
// remove the existing one if any
w.writer.Header().Del(key)
// register a future deletion
w.ops = append(w.ops, func(h http.Header) {
h.Del(key)
})
}
func NewRespProxyWriter(w http.ResponseWriter) *ResponseProxyWriter {
return &ResponseProxyWriter{
writer: w,
Body: []byte{},
}
}