-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcapturewriter.go
149 lines (121 loc) · 2.84 KB
/
capturewriter.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package speakeasy
import (
"bytes"
"io"
"net/http"
)
type captureWriter struct {
reqW *requestWriter
origResW http.ResponseWriter
resW *responseWriter
reqBuf *bytes.Buffer
resBuf *bytes.Buffer
reqValid bool
resValid bool
status int
statusWritten bool
responseSize int
maxBuffer int
}
func NewCaptureWriter(origResW http.ResponseWriter, maxBuffer int) *captureWriter {
cw := &captureWriter{
origResW: origResW,
reqBuf: bytes.NewBuffer([]byte{}),
resBuf: bytes.NewBuffer([]byte{}),
reqValid: true,
resValid: true,
status: http.StatusOK,
responseSize: 0,
maxBuffer: maxBuffer,
}
cw.reqW = &requestWriter{
cw: cw,
}
cw.resW = &responseWriter{
cw: cw,
}
return cw
}
func (c *captureWriter) GetRequestWriter() *requestWriter {
return c.reqW
}
func (c *captureWriter) GetResponseWriter() *responseWriter {
return c.resW
}
func (c *captureWriter) IsReqValid() bool {
return c.reqValid
}
func (c *captureWriter) IsResValid() bool {
return c.resValid
}
func (c *captureWriter) GetReqBuffer() *bytes.Buffer {
return c.reqBuf
}
func (c *captureWriter) GetResBuffer() *bytes.Buffer {
return c.resBuf
}
func (c *captureWriter) GetStatus() int {
return c.status
}
func (c *captureWriter) GetResponseSize() int {
return c.responseSize
}
func (c *captureWriter) writeReq(p []byte) (int, error) {
// Check if we have exceeded the buffer size and if so drop rest of request
if (c.reqBuf.Len() + c.resBuf.Len() + len(p)) > c.maxBuffer {
c.reqValid = false
} else if c.reqValid {
_, err := c.reqBuf.Write(p)
if err != nil {
c.reqValid = false
}
}
return len(p), nil
}
func (c *captureWriter) writeRes(p []byte) (int, error) {
if c.resBuf.Len() == 0 {
c.writeHeader(c.status)
}
// Check if we have exceeded the buffer size and if so drop rest of response
if (c.reqBuf.Len() + c.resBuf.Len() + len(p)) > c.maxBuffer {
c.resValid = false
} else if c.resValid {
_, err := c.resBuf.Write(p)
if err != nil {
c.resValid = false
}
}
n, err := c.origResW.Write(p)
if err != nil {
c.resValid = false
}
c.responseSize += n
return n, err
}
func (c *captureWriter) writeHeader(statusCode int) {
if !c.statusWritten {
c.status = statusCode
c.statusWritten = true
c.origResW.WriteHeader(statusCode)
}
}
type requestWriter struct {
cw *captureWriter
}
var _ io.Writer = &requestWriter{}
func (r *requestWriter) Write(p []byte) (int, error) {
return r.cw.writeReq(p)
}
type responseWriter struct {
cw *captureWriter
}
var _ http.ResponseWriter = &responseWriter{}
func (r *responseWriter) Write(data []byte) (int, error) {
return r.cw.writeRes(data)
}
func (r *responseWriter) WriteHeader(statusCode int) {
r.cw.writeHeader(statusCode)
}
func (r *responseWriter) Header() http.Header {
return r.cw.origResW.Header()
}