-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdump.go
153 lines (128 loc) · 2.94 KB
/
dump.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
150
151
152
153
package gindump
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"github.com/gin-gonic/gin"
)
type responseWriter struct {
dumpWriter io.Writer
gin.ResponseWriter
}
func (w *responseWriter) Header() http.Header {
return w.ResponseWriter.Header()
}
func (w *responseWriter) WriteHeader(code int) {
w.ResponseWriter.WriteHeader(code)
}
func (w *responseWriter) WriteHeaderNow() {
w.ResponseWriter.WriteHeaderNow()
}
func (w *responseWriter) Write(data []byte) (int, error) {
w.dumpWriter.Write(data)
return w.ResponseWriter.Write(data)
}
func (w *responseWriter) WriteString(s string) (int, error) {
return w.ResponseWriter.WriteString(s)
}
func (w *responseWriter) Status() int {
return w.ResponseWriter.Status()
}
func (w *responseWriter) Size() int {
return w.ResponseWriter.Size()
}
func (w *responseWriter) Written() bool {
return w.ResponseWriter.Written()
}
func (w *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return w.ResponseWriter.Hijack()
}
func (w *responseWriter) CloseNotify() <-chan bool {
return w.ResponseWriter.CloseNotify()
}
func (w *responseWriter) Flush() {
w.ResponseWriter.Flush()
}
const (
keyRequest = "key_dumped_request"
keyResponse = "key_dumped_response"
)
func Dump(maxReqRespSize int) gin.HandlerFunc {
return func(c *gin.Context) {
// request
reqBody := NewLimitedBuffer(maxReqRespSize)
r := io.TeeReader(c.Request.Body, reqBody)
c.Request.Body = ioutil.NopCloser(r)
// response
resBody := NewLimitedBuffer(maxReqRespSize)
w := &responseWriter{
dumpWriter: resBody,
ResponseWriter: c.Writer,
}
c.Writer = w
c.Next()
req := &http.Request{
Method: c.Request.Method,
URL: c.Request.URL,
Proto: c.Request.Proto,
Header: c.Request.Header,
Body: ioutil.NopCloser(bytes.NewBuffer(reqBody.Bytes())),
}
res := &http.Response{
StatusCode: c.Writer.Status(),
Header: c.Writer.Header(),
Body: ioutil.NopCloser(bytes.NewBuffer(resBody.Bytes())),
}
c.Set(keyRequest, req)
c.Set(keyResponse, res)
}
}
type DumpRequest struct {
Method string
URL *url.URL
Proto string
Header http.Header
Body io.ReadCloser
}
type DumpResponse struct {
StatusCode int
Header http.Header
Body io.ReadCloser
}
func GetRequest(c *gin.Context) (*DumpRequest, error) {
v, ok := c.Get(keyRequest)
if !ok {
return nil, fmt.Errorf("request not found")
}
r, ok := v.(*http.Request)
if !ok {
return nil, fmt.Errorf("request not found")
}
return &DumpRequest{
Method: r.Method,
URL: r.URL,
Proto: r.Proto,
Header: r.Header,
Body: r.Body,
}, nil
}
func GetResponse(c *gin.Context) (*DumpResponse, error) {
v, ok := c.Get(keyResponse)
if !ok {
return nil, fmt.Errorf("response not found")
}
r, ok := v.(*http.Response)
if !ok {
return nil, fmt.Errorf("response not found")
}
return &DumpResponse{
StatusCode: r.StatusCode,
Header: r.Header,
Body: r.Body,
}, nil
}