forked from bufbuild/buf
/
verbose_transport.go
271 lines (241 loc) · 8.02 KB
/
verbose_transport.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright 2020-2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bufcurl
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"runtime"
"sort"
"strings"
"github.com/bufbuild/buf/private/pkg/verbose"
"github.com/bufbuild/connect-go"
"go.uber.org/atomic"
)
type skipUploadFinishedMessageKey struct{}
func skippingUploadFinishedMessage(ctx context.Context) context.Context {
return context.WithValue(ctx, skipUploadFinishedMessageKey{}, true)
}
type userAgentKey struct{}
func withUserAgent(ctx context.Context, headers http.Header) context.Context {
if userAgentHeaders := headers.Values("user-agent"); len(userAgentHeaders) > 0 {
return context.WithValue(ctx, userAgentKey{}, userAgentHeaders)
}
return ctx // no change
}
// DefaultUserAgent returns the default user agent for the given protocol.
func DefaultUserAgent(protocol string, bufVersion string) string {
// mirror the default user agent for the Connect client library, but
// add "buf/<version>" in front of it.
libUserAgent := "connect-go"
if strings.Contains(protocol, "grpc") {
libUserAgent = "grpc-go-connect"
}
return fmt.Sprintf("buf/%s %s/%s (%s)", bufVersion, libUserAgent, connect.Version, runtime.Version())
}
// NewVerboseHTTPClient creates a new HTTP client with the given transport and
// printing verbose trace information to the given printer.
func NewVerboseHTTPClient(transport http.RoundTripper, printer verbose.Printer) connect.HTTPClient {
return &verboseClient{transport: transport, printer: printer}
}
// TraceTrailersInterceptor returns an interceptor that will print information
// about trailers for streaming calls to the given printer. This is used with
// the Connect and gRPC-web protocols since these protocols include trailers in
// the request body, instead of using actual HTTP trailers. (For the gRPC
// protocol, which uses actual HTTP trailers, the verbose HTTP client suffices
// since it already prints information about the trailers.)
func TraceTrailersInterceptor(printer verbose.Printer) connect.Interceptor {
return traceTrailersInterceptor{printer: printer}
}
type verboseClient struct {
transport http.RoundTripper
printer verbose.Printer
reqNum atomic.Int32
}
type reqNumAddrKey struct{}
func (v *verboseClient) Do(req *http.Request) (*http.Response, error) {
if host := req.Header.Get("Host"); host != "" {
// Set based on host header. This way it is also correctly used as
// the ":authority" meta-header in HTTP/2.
req.Host = host
}
if userAgentHeaders, _ := req.Context().Value(userAgentKey{}).([]string); len(userAgentHeaders) > 0 {
req.Header.Del("user-agent")
for _, val := range userAgentHeaders {
req.Header.Add("user-agent", val)
}
}
reqNum := v.reqNum.Add(1)
if reqNumAddr, _ := req.Context().Value(reqNumAddrKey{}).(*int32); reqNumAddr != nil {
*reqNumAddr = reqNum
}
rawBody := req.Body
if rawBody == nil {
rawBody = io.NopCloser(bytes.NewBuffer(nil))
}
var atEnd func(error)
if skip, _ := req.Context().Value(skipUploadFinishedMessageKey{}).(bool); !skip {
atEnd = func(err error) {
if errors.Is(err, io.EOF) {
v.printer.Printf("* (#%d) Finished upload", reqNum)
}
}
}
req.Body = &verboseReader{
ReadCloser: rawBody,
callback: func(count int) {
v.traceWriteRequestBytes(reqNum, count)
},
whenDone: atEnd,
whenStart: func() {
// we defer this until body is read so that our HTTP client's dialer and TLS
// config can potentially log useful things about connection setup *before*
// we print the request info.
v.traceRequest(req, reqNum)
},
}
resp, err := v.transport.RoundTrip(req)
if resp != nil {
v.traceResponse(resp, reqNum)
if resp.Body != nil {
resp.Body = &verboseReader{
ReadCloser: resp.Body,
callback: func(count int) {
v.traceReadResponseBytes(reqNum, count)
},
whenDone: func(err error) {
traceTrailers(v.printer, resp.Trailer, false, reqNum)
v.printer.Printf("* (#%d) Call complete", reqNum)
},
}
}
}
return resp, err
}
func (v *verboseClient) traceRequest(r *http.Request, reqNum int32) {
// we look at the *raw* http headers, in case any get added by the
// Connect client impl or an interceptor after we could otherwise
// inspect them from an interceptor
var queryString string
if r.URL.RawQuery != "" {
queryString = "?" + r.URL.RawQuery
} else if r.URL.ForceQuery {
queryString = "?"
}
v.printer.Printf("> (#%d) %s %s%s\n", reqNum, r.Method, r.URL.Path, queryString)
traceMetadata(v.printer, r.Header, fmt.Sprintf("> (#%d) ", reqNum))
v.printer.Printf("> (#%d)\n", reqNum)
}
func (v *verboseClient) traceWriteRequestBytes(reqNum int32, count int) {
v.printer.Printf("} (#%d) [%d bytes data]", reqNum, count)
}
func (v *verboseClient) traceResponse(r *http.Response, reqNum int32) {
v.printer.Printf("< (#%d) %s %s\n", reqNum, r.Proto, r.Status)
traceMetadata(v.printer, r.Header, fmt.Sprintf("< (#%d) ", reqNum))
v.printer.Printf("< (#%d)\n", reqNum)
}
func (v *verboseClient) traceReadResponseBytes(reqNum int32, count int) {
v.printer.Printf("{ (#%d) [%d bytes data]", reqNum, count)
}
func traceMetadata(printer verbose.Printer, meta http.Header, prefix string) {
keys := make([]string, 0, len(meta))
for key := range meta {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
vals := meta[key]
for _, val := range vals {
printer.Printf("%s%s: %s\n", prefix, key, val)
}
}
}
func traceTrailers(printer verbose.Printer, trailers http.Header, synthetic bool, reqNum int32) {
if len(trailers) == 0 {
return
}
printer.Printf("< (#%d)\n", reqNum)
prefix := fmt.Sprintf("< (#%d) ", reqNum)
if synthetic {
// mark synthetic trailers with an asterisk
prefix += "[*] "
}
traceMetadata(printer, trailers, prefix)
}
type verboseReader struct {
io.ReadCloser
callback func(int)
whenStart func()
whenDone func(error)
started atomic.Bool
done atomic.Bool
}
func (v *verboseReader) Read(dest []byte) (n int, err error) {
if v.started.CompareAndSwap(false, true) && v.whenStart != nil {
v.whenStart()
}
n, err = v.ReadCloser.Read(dest)
if n > 0 && v.callback != nil {
v.callback(n)
}
if err != nil {
if v.done.CompareAndSwap(false, true) && v.whenDone != nil {
v.whenDone(err)
}
}
return n, err
}
func (v *verboseReader) Close() error {
err := v.ReadCloser.Close()
if v.done.CompareAndSwap(false, true) && v.whenDone != nil {
reportError := err
if reportError == nil {
reportError = io.EOF
}
v.whenDone(reportError)
}
return err
}
type traceTrailersInterceptor struct {
printer verbose.Printer
}
func (t traceTrailersInterceptor) WrapUnary(unaryFunc connect.UnaryFunc) connect.UnaryFunc {
return unaryFunc
}
func (t traceTrailersInterceptor) WrapStreamingClient(clientFunc connect.StreamingClientFunc) connect.StreamingClientFunc {
return func(ctx context.Context, spec connect.Spec) connect.StreamingClientConn {
var reqNum int32
ctx = context.WithValue(ctx, reqNumAddrKey{}, &reqNum)
return &traceTrailersStream{StreamingClientConn: clientFunc(ctx, spec), reqNum: &reqNum, printer: t.printer}
}
}
func (t traceTrailersInterceptor) WrapStreamingHandler(handlerFunc connect.StreamingHandlerFunc) connect.StreamingHandlerFunc {
return handlerFunc
}
type traceTrailersStream struct {
connect.StreamingClientConn
reqNum *int32
printer verbose.Printer
done atomic.Bool
}
func (s *traceTrailersStream) Receive(msg any) error {
err := s.StreamingClientConn.Receive(msg)
if err != nil && s.done.CompareAndSwap(false, true) {
traceTrailers(s.printer, s.ResponseTrailer(), true, *s.reqNum)
}
return err
}