forked from goproxy/goproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sumdb_client_ops.go
119 lines (105 loc) · 3.18 KB
/
sumdb_client_ops.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
package goproxy
import (
"bytes"
"context"
"errors"
"fmt"
"io/fs"
"net/http"
"net/url"
"strings"
"sync"
"sync/atomic"
"time"
)
// sumdbClientOps implements [golang.org/x/mod/sumdb.ClientOps].
type sumdbClientOps struct {
name string
key string
directURL *url.URL
urlValue atomic.Value
urlDetermineMutex sync.Mutex
urlDeterminedAt time.Time
urlDetermineErr error
envGOPROXY string
httpClient *http.Client
}
// newSumdbClientOps creates a new [sumdbClientOps].
func newSumdbClientOps(envGOPROXY, envGOSUMDB string, httpClient *http.Client) (*sumdbClientOps, error) {
var (
sco = &sumdbClientOps{envGOPROXY: envGOPROXY, httpClient: httpClient}
u *url.URL
isDirectURL bool
err error
)
sco.name, sco.key, u, isDirectURL, err = parseEnvGOSUMDB(envGOSUMDB)
if err != nil {
return nil, err
}
if isDirectURL {
sco.directURL = u
} else {
sco.urlValue.Store(u)
}
return sco, nil
}
// url returns the URL for connecting to the checksum database.
func (sco *sumdbClientOps) url() (*url.URL, error) {
if v := sco.urlValue.Load(); v != nil {
return v.(*url.URL), nil
}
sco.urlDetermineMutex.Lock()
defer sco.urlDetermineMutex.Unlock()
if time.Since(sco.urlDeterminedAt) < 10*time.Second && sco.urlDetermineErr != nil {
return nil, sco.urlDetermineErr
}
u := sco.directURL
err := walkEnvGOPROXY(sco.envGOPROXY, func(proxy *url.URL) error {
pu := appendURL(proxy, "sumdb", sco.name)
if err := httpGet(context.Background(), sco.httpClient, appendURL(pu, "/supported").String(), nil); err != nil {
return err
}
u = pu
return nil
}, func() error { return nil })
sco.urlDeterminedAt = time.Now()
if err != nil && !errors.Is(err, fs.ErrNotExist) {
sco.urlDetermineErr = err
return nil, err
}
sco.urlDetermineErr = nil
sco.urlValue.Store(u)
return u, nil
}
// ReadRemote implements [golang.org/x/mod/sumdb.ClientOps].
func (sco *sumdbClientOps) ReadRemote(path string) ([]byte, error) {
u, err := sco.url()
if err != nil {
return nil, err
}
var buf bytes.Buffer
if err := httpGet(context.Background(), sco.httpClient, appendURL(u, path).String(), &buf); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// ReadConfig implements [golang.org/x/mod/sumdb.ClientOps].
func (sco *sumdbClientOps) ReadConfig(file string) ([]byte, error) {
if file == "key" {
return []byte(sco.key), nil
}
if strings.HasSuffix(file, "/latest") {
return []byte{}, nil // Empty result means empty tree.
}
return nil, fmt.Errorf("unknown config %s", file)
}
// WriteConfig implements [golang.org/x/mod/sumdb.ClientOps].
func (*sumdbClientOps) WriteConfig(file string, old, new []byte) error { return nil }
// ReadCache implements [golang.org/x/mod/sumdb.ClientOps].
func (*sumdbClientOps) ReadCache(file string) ([]byte, error) { return nil, fs.ErrNotExist }
// WriteCache implements [golang.org/x/mod/sumdb.ClientOps].
func (*sumdbClientOps) WriteCache(file string, data []byte) {}
// Log implements [golang.org/x/mod/sumdb.ClientOps].
func (*sumdbClientOps) Log(msg string) {}
// SecurityError implements [golang.org/x/mod/sumdb.ClientOps].
func (*sumdbClientOps) SecurityError(msg string) {}