forked from goproxy/goproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsumdb_client_ops.go
195 lines (162 loc) · 3.9 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
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
package goproxy
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"sync"
)
// sumdbClientOps implements the `sumdb.ClientOps`.
type sumdbClientOps struct {
endpointURL *url.URL
envGOPROXY string
envGOSUMDB string
httpClient *http.Client
errorLogger *log.Logger
loadOnce sync.Once
loadError error
}
// load loads the stuff of the sco up.
func (sco *sumdbClientOps) load() {
sumdbName := sco.envGOSUMDB
if i := strings.Index(sumdbName, "+"); i >= 0 {
sumdbName = sumdbName[:i]
}
for _, proxy := range strings.Split(sco.envGOPROXY, ",") {
if proxy == "direct" || proxy == "off" {
break
}
var proxyURL *url.URL
proxyURL, sco.loadError = parseRawURL(proxy)
if sco.loadError != nil {
return
}
endpointURL := appendURL(proxyURL, "sumdb", sumdbName)
operationURL := appendURL(endpointURL, "/supported")
var req *http.Request
req, sco.loadError = http.NewRequest(
http.MethodGet,
operationURL.String(),
nil,
)
if sco.loadError != nil {
return
}
var res *http.Response
res, sco.loadError = httpDo(sco.httpClient, req)
if sco.loadError != nil {
return
}
defer res.Body.Close()
var b []byte
b, sco.loadError = ioutil.ReadAll(res.Body)
if sco.loadError != nil {
return
}
switch res.StatusCode {
case http.StatusOK:
case http.StatusNotFound, http.StatusGone:
continue
default:
sco.loadError = fmt.Errorf(
"GET %s: %s: %s",
redactedURL(operationURL),
res.Status,
b,
)
return
}
sco.endpointURL = endpointURL
return
}
sumdbURL := sco.envGOSUMDB
if i := strings.Index(sumdbURL, " "); i > 0 {
sumdbURL = sumdbURL[i+1:]
} else {
sumdbURL = sumdbName
}
var endpointURL *url.URL
endpointURL, sco.loadError = parseRawURL(sumdbURL)
if sco.loadError != nil {
return
}
sco.endpointURL = endpointURL
}
// ReadRemote implements the `sumdb.ClientOps`.
func (sco *sumdbClientOps) ReadRemote(path string) ([]byte, error) {
if sco.loadOnce.Do(sco.load); sco.loadError != nil {
return nil, sco.loadError
}
operationURL := appendURL(sco.endpointURL, path)
req, err := http.NewRequest(http.MethodGet, operationURL.String(), nil)
if err != nil {
return nil, err
}
res, err := httpDo(sco.httpClient, req)
if err != nil {
return nil, err
}
defer res.Body.Close()
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
switch res.StatusCode {
case http.StatusOK:
case http.StatusNotFound, http.StatusGone:
return nil, notFoundError(fmt.Errorf("%s", b))
default:
return nil, fmt.Errorf(
"GET %s: %s: %s",
redactedURL(operationURL),
res.Status,
b,
)
}
return b, nil
}
// ReadConfig implements the `sumdb.ClientOps`.
func (sco *sumdbClientOps) ReadConfig(file string) ([]byte, error) {
if sco.loadOnce.Do(sco.load); sco.loadError != nil {
return nil, sco.loadError
}
if file == "key" {
return []byte(sco.envGOSUMDB), nil
}
if strings.HasSuffix(file, "/latest") {
// Empty result means empty tree.
return []byte{}, nil
}
return nil, fmt.Errorf("unknown config %s", file)
}
// WriteConfig implements the `sumdb.ClientOps`.
func (sco *sumdbClientOps) WriteConfig(file string, old, new []byte) error {
sco.loadOnce.Do(sco.load)
return sco.loadError
}
// ReadCache implements the `sumdb.ClientOps`.
func (sco *sumdbClientOps) ReadCache(file string) ([]byte, error) {
if sco.loadOnce.Do(sco.load); sco.loadError != nil {
return nil, sco.loadError
}
return nil, ErrCacheNotFound
}
// WriteCache implements the `sumdb.ClientOps`.
func (sco *sumdbClientOps) WriteCache(file string, data []byte) {
sco.loadOnce.Do(sco.load)
}
// Log implements the `sumdb.ClientOps`.
func (sco *sumdbClientOps) Log(msg string) {
sco.loadOnce.Do(sco.load)
}
// SecurityError implements the `sumdb.ClientOps`.
func (sco *sumdbClientOps) SecurityError(msg string) {
sco.loadOnce.Do(sco.load)
if sco.errorLogger != nil {
sco.errorLogger.Print(msg)
} else {
log.Print(msg)
}
}