-
Notifications
You must be signed in to change notification settings - Fork 63
/
interceptor_debug.go
210 lines (176 loc) · 5.03 KB
/
interceptor_debug.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package clientv2
import (
"crypto/tls"
"fmt"
"net/http"
"net/http/httptrace"
"net/http/httputil"
clientV1 "github.com/qiniu/go-sdk/v7/client"
"github.com/qiniu/go-sdk/v7/internal/log"
)
type DebugLevel int
const (
DebugLevelPrintNone DebugLevel = 0
DebugLevelPrintNormal DebugLevel = 1
DebugLevelPrintDetail DebugLevel = 2
)
var (
printRequestTrace = false
printRequestLevel *DebugLevel = nil
printResponseLevel *DebugLevel = nil
)
func PrintRequestTrace(isPrint bool) {
printRequestTrace = isPrint
}
func IsPrintRequestTrace() bool {
return printRequestTrace
}
func PrintRequest(level DebugLevel) {
printRequestLevel = &level
}
func IsPrintRequest() bool {
if printRequestLevel != nil {
return *printRequestLevel == DebugLevelPrintNormal || *printRequestLevel == DebugLevelPrintDetail
}
return clientV1.DebugMode
}
func IsPrintRequestBody() bool {
if printRequestLevel != nil {
return *printRequestLevel == DebugLevelPrintDetail
}
return clientV1.DeepDebugInfo
}
func PrintResponse(level DebugLevel) {
printResponseLevel = &level
}
func IsPrintResponse() bool {
if printResponseLevel != nil {
return *printResponseLevel == DebugLevelPrintNormal || *printResponseLevel == DebugLevelPrintDetail
}
return clientV1.DebugMode
}
func IsPrintResponseBody() bool {
if printResponseLevel != nil {
return *printResponseLevel == DebugLevelPrintDetail
}
return clientV1.DeepDebugInfo
}
type debugInterceptor struct {
}
func newDebugInterceptor() Interceptor {
return &debugInterceptor{}
}
func (interceptor *debugInterceptor) Priority() InterceptorPriority {
return InterceptorPriorityDebug
}
func (interceptor *debugInterceptor) Intercept(req *http.Request, handler Handler) (*http.Response, error) {
if interceptor == nil {
return handler(req)
}
label := interceptor.requestLabel(req)
if e := interceptor.printRequest(label, req); e != nil {
return nil, e
}
req = interceptor.printRequestTrace(label, req)
resp, err := handler(req)
if e := interceptor.printResponse(label, resp); e != nil {
return nil, e
}
return resp, err
}
func (interceptor *debugInterceptor) requestLabel(req *http.Request) string {
if req == nil || req.URL == nil {
return ""
}
return fmt.Sprintf("Url:%s", req.URL.String())
}
func (interceptor *debugInterceptor) printRequest(label string, req *http.Request) error {
if req == nil {
return nil
}
printReq := IsPrintRequest()
if !printReq {
return nil
}
info := label + " request:\n"
d, dErr := httputil.DumpRequest(req, IsPrintRequestBody())
if dErr != nil {
return dErr
}
info += string(d) + "\n"
log.Debug(info)
return nil
}
func (interceptor *debugInterceptor) printRequestTrace(label string, req *http.Request) *http.Request {
if !IsPrintRequestTrace() || req == nil {
return req
}
label += "\n"
trace := &httptrace.ClientTrace{
GetConn: func(hostPort string) {
log.Debug(label + fmt.Sprintf("GetConn, %s \n", hostPort))
},
GotConn: func(connInfo httptrace.GotConnInfo) {
remoteAddr := connInfo.Conn.RemoteAddr()
log.Debug(label + fmt.Sprintf("GotConn, Network:%s RemoteAddr:%s \n", remoteAddr.Network(), remoteAddr.String()))
},
PutIdleConn: func(err error) {
log.Debug(label + fmt.Sprintf("PutIdleConn, err:%v \n", err))
},
GotFirstResponseByte: func() {
log.Debug(label + "GotFirstResponseByte \n")
},
Got100Continue: func() {
log.Debug(label + "Got100Continue \n")
},
DNSStart: func(info httptrace.DNSStartInfo) {
log.Debug(label + fmt.Sprintf("DNSStart, host:%s \n", info.Host))
},
DNSDone: func(info httptrace.DNSDoneInfo) {
log.Debug(label + fmt.Sprintf("DNSDone, addr:%+v \n", info.Addrs))
},
ConnectStart: func(network, addr string) {
log.Debug(label + fmt.Sprintf("ConnectStart, network:%+v ip:%s \n", network, addr))
},
ConnectDone: func(network, addr string, err error) {
log.Debug(label + fmt.Sprintf("ConnectDone, network:%s ip:%s err:%v \n", network, addr, err))
},
TLSHandshakeStart: func() {
log.Debug(label + "TLSHandshakeStart \n")
},
TLSHandshakeDone: func(state tls.ConnectionState, err error) {
log.Debug(label + fmt.Sprintf("TLSHandshakeDone, state:%+v err:%s \n", state, err))
},
// go1.10 不支持
//WroteHeaderField: func(key string, value []string) {
// log.Debug(label + fmt.Sprintf("WroteHeaderField, key:%s value:%s \n", key, value))
//},
WroteHeaders: func() {
log.Debug(label + "WroteHeaders \n")
},
Wait100Continue: func() {
log.Debug(label + "Wait100Continue \n")
},
WroteRequest: func(info httptrace.WroteRequestInfo) {
log.Debug(label + fmt.Sprintf("WroteRequest, err:%v \n", info.Err))
},
}
return req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
}
func (interceptor *debugInterceptor) printResponse(label string, resp *http.Response) error {
if resp == nil {
return nil
}
printResp := IsPrintResponse()
if !printResp {
return nil
}
info := label + " response:\n"
d, dErr := httputil.DumpResponse(resp, IsPrintResponseBody())
if dErr != nil {
return dErr
}
info += string(d) + "\n"
log.Debug(info)
return nil
}