forked from rogpeppe/go-internal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.go
349 lines (325 loc) · 8.8 KB
/
proxy.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
Package goproxytest serves Go modules from a proxy server designed to run on
localhost during tests, both to make tests avoid requiring specific network
servers and also to make them significantly faster.
Each module archive is either a file named path_vers.txtar or path_vers.txt, or
a directory named path_vers, where slashes in path have been replaced with underscores.
The archive or directory must contain two files ".info" and ".mod", to be served as
the info and mod files in the proxy protocol (see
https://research.swtch.com/vgo-module). The remaining files are served as the
content of the module zip file. The path@vers prefix required of files in the
zip file is added automatically by the proxy: the files in the archive have
names without the prefix, like plain "go.mod", "x.go", and so on.
See ../cmd/txtar-addmod and ../cmd/txtar-c for tools generate txtar
files, although it's fine to write them by hand.
*/
package goproxytest
import (
"archive/zip"
"bytes"
"cmp"
"encoding/json"
"fmt"
"io/fs"
"log"
"net"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
"golang.org/x/mod/module"
"golang.org/x/mod/semver"
"golang.org/x/tools/txtar"
"github.com/cli/go-internal/par"
)
type Server struct {
server *http.Server
URL string
dir string
logf func(string, ...any)
modList []module.Version
zipCache par.Cache
archiveCache par.Cache
}
// NewTestServer is a wrapper around [NewServer] for use in Go tests.
// Failure to start the server stops the test via [testing.TB.Fatalf],
// all server logs go through [testing.TB.Logf],
// and the server is closed when the test finishes via [testing.TB.Cleanup].
func NewTestServer(tb testing.TB, dir, addr string) *Server {
srv, err := newServer(dir, addr, tb.Logf)
if err != nil {
tb.Fatalf("cannot start Go proxy: %v", err)
}
tb.Cleanup(srv.Close)
return srv
}
// NewServer starts the Go module proxy listening on the given
// network address. It serves modules taken from the given directory
// name. If addr is empty, it will listen on an arbitrary
// localhost port. If dir is empty, "testmod" will be used.
//
// The returned Server should be closed after use.
func NewServer(dir, addr string) (*Server, error) {
return newServer(dir, addr, log.Printf)
}
func newServer(dir, addr string, logf func(string, ...any)) (*Server, error) {
addr = cmp.Or(addr, "localhost:0")
dir = cmp.Or(dir, "testmod")
srv := Server{dir: dir, logf: logf}
if err := srv.readModList(); err != nil {
return nil, fmt.Errorf("cannot read modules: %v", err)
}
l, err := net.Listen("tcp", addr)
if err != nil {
return nil, fmt.Errorf("cannot listen on %q: %v", addr, err)
}
srv.server = &http.Server{
Handler: http.HandlerFunc(srv.handler),
}
addr = l.Addr().String()
srv.URL = "http://" + addr + "/mod"
go func() {
if err := srv.server.Serve(l); err != nil && err != http.ErrServerClosed {
srv.logf("go proxy: http.Serve: %v", err)
}
}()
return &srv, nil
}
// Close shuts down the proxy.
func (srv *Server) Close() {
srv.server.Close()
}
func (srv *Server) readModList() error {
entries, err := os.ReadDir(srv.dir)
if err != nil {
return err
}
for _, entry := range entries {
name := entry.Name()
switch {
case strings.HasSuffix(name, ".txt"):
name = strings.TrimSuffix(name, ".txt")
case strings.HasSuffix(name, ".txtar"):
name = strings.TrimSuffix(name, ".txtar")
case entry.IsDir():
default:
continue
}
i := strings.LastIndex(name, "_v")
if i < 0 {
continue
}
encPath := strings.ReplaceAll(name[:i], "_", "/")
path, err := module.UnescapePath(encPath)
if err != nil {
return fmt.Errorf("cannot decode module path in %q: %v", name, err)
}
encVers := name[i+1:]
vers, err := module.UnescapeVersion(encVers)
if err != nil {
return fmt.Errorf("cannot decode module version in %q: %v", name, err)
}
srv.modList = append(srv.modList, module.Version{Path: path, Version: vers})
}
return nil
}
// handler serves the Go module proxy protocol.
// See the proxy section of https://research.swtch.com/vgo-module.
func (srv *Server) handler(w http.ResponseWriter, r *http.Request) {
if !strings.HasPrefix(r.URL.Path, "/mod/") {
http.NotFound(w, r)
return
}
path := strings.TrimPrefix(r.URL.Path, "/mod/")
i := strings.Index(path, "/@v/")
if i < 0 {
http.NotFound(w, r)
return
}
enc, file := path[:i], path[i+len("/@v/"):]
path, err := module.UnescapePath(enc)
if err != nil {
srv.logf("go proxy_test: %v\n", err)
http.NotFound(w, r)
return
}
if file == "list" {
n := 0
for _, m := range srv.modList {
if m.Path == path && !isPseudoVersion(m.Version) {
if err := module.Check(m.Path, m.Version); err == nil {
fmt.Fprintf(w, "%s\n", m.Version)
n++
}
}
}
if n == 0 {
http.NotFound(w, r)
}
return
}
i = strings.LastIndex(file, ".")
if i < 0 {
http.NotFound(w, r)
return
}
encVers, ext := file[:i], file[i+1:]
vers, err := module.UnescapeVersion(encVers)
if err != nil {
srv.logf("go proxy_test: %v\n", err)
http.NotFound(w, r)
return
}
if allHex(vers) {
var best string
// Convert commit hash (only) to known version.
// Use latest version in semver priority, to match similar logic
// in the repo-based module server (see modfetch.(*codeRepo).convert).
for _, m := range srv.modList {
if m.Path == path && semver.Compare(best, m.Version) < 0 {
var hash string
if isPseudoVersion(m.Version) {
hash = m.Version[strings.LastIndex(m.Version, "-")+1:]
} else {
hash = srv.findHash(m)
}
if strings.HasPrefix(hash, vers) || strings.HasPrefix(vers, hash) {
best = m.Version
}
}
}
if best != "" {
vers = best
}
}
a := srv.readArchive(path, vers)
if a == nil {
// As of https://go-review.googlesource.com/c/go/+/189517, cmd/go
// resolves all paths. i.e. for github.com/hello/world, cmd/go attempts
// to resolve github.com, github.com/hello and github.com/hello/world.
// cmd/go expects a 404/410 response if there is nothing there. Hence we
// cannot return with a 500.
srv.logf("go proxy: no archive %s %s\n", path, vers)
http.NotFound(w, r)
return
}
switch ext {
case "info", "mod":
want := "." + ext
for _, f := range a.Files {
if f.Name == want {
w.Write(f.Data)
return
}
}
case "zip":
type cached struct {
zip []byte
err error
}
c := srv.zipCache.Do(a, func() interface{} {
var buf bytes.Buffer
z := zip.NewWriter(&buf)
for _, f := range a.Files {
if strings.HasPrefix(f.Name, ".") {
continue
}
zf, err := z.Create(path + "@" + vers + "/" + f.Name)
if err != nil {
return cached{nil, err}
}
if _, err := zf.Write(f.Data); err != nil {
return cached{nil, err}
}
}
if err := z.Close(); err != nil {
return cached{nil, err}
}
return cached{buf.Bytes(), nil}
}).(cached)
if c.err != nil {
srv.logf("go proxy: %v\n", c.err)
http.Error(w, c.err.Error(), 500)
return
}
w.Write(c.zip)
return
}
http.NotFound(w, r)
}
func (srv *Server) findHash(m module.Version) string {
a := srv.readArchive(m.Path, m.Version)
if a == nil {
return ""
}
var data []byte
for _, f := range a.Files {
if f.Name == ".info" {
data = f.Data
break
}
}
var info struct{ Short string }
json.Unmarshal(data, &info)
return info.Short
}
func (srv *Server) readArchive(path, vers string) *txtar.Archive {
enc, err := module.EscapePath(path)
if err != nil {
srv.logf("go proxy: %v\n", err)
return nil
}
encVers, err := module.EscapeVersion(vers)
if err != nil {
srv.logf("go proxy: %v\n", err)
return nil
}
prefix := strings.ReplaceAll(enc, "/", "_")
name := filepath.Join(srv.dir, prefix+"_"+encVers)
txtName := name + ".txt"
txtarName := name + ".txtar"
a := srv.archiveCache.Do(name, func() interface{} {
a, err := txtar.ParseFile(txtarName)
if os.IsNotExist(err) {
// fall back to trying with the .txt extension
a, err = txtar.ParseFile(txtName)
}
if os.IsNotExist(err) {
// fall back to trying a directory
a = new(txtar.Archive)
err = filepath.WalkDir(name, func(path string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
if path == name && !entry.IsDir() {
return fmt.Errorf("expected a directory root")
}
if entry.IsDir() {
return nil
}
arpath := filepath.ToSlash(strings.TrimPrefix(path, name+string(os.PathSeparator)))
data, err := os.ReadFile(path)
if err != nil {
return err
}
a.Files = append(a.Files, txtar.File{
Name: arpath,
Data: data,
})
return nil
})
}
if err != nil {
if !os.IsNotExist(err) {
srv.logf("go proxy: %v\n", err)
}
a = nil
}
return a
}).(*txtar.Archive)
return a
}