generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathtransport.go
79 lines (71 loc) · 1.94 KB
/
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
package client
import (
"fmt"
"io"
"net/http"
"reflect"
"runtime/debug"
"strings"
)
type ResponseBodyLimitedTransport struct {
http.RoundTripper
LimitBytes int64
UserAgent string
}
func (r *ResponseBodyLimitedTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if r.UserAgent != "" {
req.Header.Set("User-Agent", r.UserAgent)
}
resp, err := r.RoundTripper.RoundTrip(req)
if resp != nil && resp.Body != nil {
resp.Body = &limitReadCloser{
limit: r.LimitBytes,
ReadCloser: resp.Body,
}
}
return resp, err
}
type limitReadCloser struct {
limit int64
bytesRead int64
io.ReadCloser
}
func (l *limitReadCloser) Read(p []byte) (int, error) {
n, err := l.ReadCloser.Read(p)
l.bytesRead += int64(n)
if l.bytesRead > l.limit {
return 0, fmt.Errorf("reached read limit of %d bytes after reading %d bytes", l.limit, l.bytesRead)
}
return n, err
}
// ImportPath is the canonical import path that allows us to identify
// official client builds vs modified forks, and use that info in User-Agent header.
var ImportPath = importPath()
// importPath returns the path that library consumers would have in go.mod
func importPath() string {
p := reflect.ValueOf(ResponseBodyLimitedTransport{}).Type().PkgPath()
// we have monorepo, so stripping the remainder
return strings.TrimSuffix(p, "/routing/http/client")
}
// moduleVersion returns a useful user agent version string allowing us to
// identify requests coming from official releases of this module vs forks.
func moduleVersion() (ua string) {
ua = ImportPath
var module *debug.Module
if bi, ok := debug.ReadBuildInfo(); ok {
// If debug.ReadBuildInfo was successful, we can read Version by finding
// this client in the dependency list of the app that has it in go.mod
for _, dep := range bi.Deps {
if dep.Path == ImportPath {
module = dep
break
}
}
if module != nil {
ua += "@" + module.Version
return
}
ua += "@unknown"
}
return
}