Debian Codegraph @

sumdb/internal/sumweb/server.go

raw symbols SCIP
1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package sumweb implements the HTTP protocols for serving or accessing a go.sum database.
6 package sumweb
7
8 import (
9 "context"
10 "net/http"
11 "os"
12 "regexp"
13 "strings"
14
15 "golang.org/x/exp/sumdb/internal/tlog"
16 )
17
18 // A Server provides the external operations
19 // (underlying database access and so on)
20 // needed to implement the HTTP server Handler.
21 type Server interface {
22 // NewContext returns the context to use for the request r.
23 NewContext(r *http.Request) (context.Context, error)
24
25 // Signed returns the signed hash of the latest tree.
26 Signed(ctx context.Context) ([]byte, error)
27
28 // ReadRecords returns the content for the n records id through id+n-1.
29 ReadRecords(ctx context.Context, id, n int64) ([][]byte, error)
30
31 // Lookup looks up a record by its associated key ("module@version"),
32 // returning the record ID.
33 Lookup(ctx context.Context, key string) (int64, error)
34
35 // ReadTileData reads the content of tile t.
36 // It is only invoked for hash tiles (t.L ≥ 0).
37 ReadTileData(ctx context.Context, t tlog.Tile) ([]byte, error)
38 }
39
40 // A Handler is the go.sum database server handler,
41 // which should be invoked to serve the paths listed in Paths.
42 // The calling code is responsible for initializing Server.
43 type Handler struct {
44 Server Server
45 }
46
47 // Paths are the URL paths for which Handler should be invoked.
48 //
49 // Typically a server will do:
50 //
51 // handler := &sumweb.Handler{Server: srv}
52 // for _, path := range sumweb.Paths {
53 // http.HandleFunc(path, handler)
54 // }
55 var Paths = []string{
56 "/lookup/",
57 "/latest",
58 "/tile/",
59 }
60
61 var modVerRE = regexp.MustCompile(`^[^@]+@v[0-9]+\.[0-9]+\.[0-9]+(-[^@]*)?(\+incompatible)?$`)
62
63 func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
64 ctx, err := h.Server.NewContext(r)
65 if err != nil {
66 http.Error(w, err.Error(), 500)
67 return
68 }
69
70 switch {
71 default:
72 http.NotFound(w, r)
73
74 case strings.HasPrefix(r.URL.Path, "/lookup/"):
75 mod := strings.TrimPrefix(r.URL.Path, "/lookup/")
76 if !modVerRE.MatchString(mod) {
77 http.Error(w, "invalid module@version syntax", http.StatusBadRequest)
78 return
79 }
80 i := strings.Index(mod, "@")
81 encPath, encVers := mod[:i], mod[i+1:]
82 path, err := decodePath(encPath)
83 if err != nil {
84 reportError(w, r, err)
85 return
86 }
87 vers, err := decodeVersion(encVers)
88 if err != nil {
89 reportError(w, r, err)
90 return
91 }
92 id, err := h.Server.Lookup(ctx, path+"@"+vers)
93 if err != nil {
94 reportError(w, r, err)
95 return
96 }
97 records, err := h.Server.ReadRecords(ctx, id, 1)
98 if err != nil {
99 // This should never happen - the lookup says the record exists.
100 http.Error(w, err.Error(), http.StatusInternalServerError)
101 return
102 }
103 if len(records) != 1 {
104 http.Error(w, "invalid record count returned by ReadRecords", http.StatusInternalServerError)
105 return
106 }
107 msg, err := tlog.FormatRecord(id, records[0])
108 if err != nil {
109 http.Error(w, err.Error(), http.StatusInternalServerError)
110 return
111 }
112 signed, err := h.Server.Signed(ctx)
113 if err != nil {
114 http.Error(w, err.Error(), http.StatusInternalServerError)
115 return
116 }
117 w.Header().Set("Content-Type", "text/plain; charset=UTF-8")
118 w.Write(msg)
119 w.Write(signed)
120
121 case r.URL.Path == "/latest":
122 data, err := h.Server.Signed(ctx)
123 if err != nil {
124 http.Error(w, err.Error(), http.StatusInternalServerError)
125 return
126 }
127 w.Header().Set("Content-Type", "text/plain; charset=UTF-8")
128 w.Write(data)
129
130 case strings.HasPrefix(r.URL.Path, "/tile/"):
131 t, err := tlog.ParseTilePath(r.URL.Path[1:])
132 if err != nil {
133 http.Error(w, "invalid tile syntax", http.StatusBadRequest)
134 return
135 }
136 if t.L == -1 {
137 // Record data.
138 start := t.N << uint(t.H)
139 records, err := h.Server.ReadRecords(ctx, start, int64(t.W))
140 if err != nil {
141 reportError(w, r, err)
142 return
143 }
144 if len(records) != t.W {
145 http.Error(w, "invalid record count returned by ReadRecords", http.StatusInternalServerError)
146 return
147 }
148 var data []byte
149 for i, text := range records {
150 msg, err := tlog.FormatRecord(start+int64(i), text)
151 if err != nil {
152 http.Error(w, err.Error(), http.StatusInternalServerError)
153 }
154 data = append(data, msg...)
155 }
156 w.Header().Set("Content-Type", "text/plain; charset=UTF-8")
157 w.Write(data)
158 return
159 }
160
161 data, err := h.Server.ReadTileData(ctx, t)
162 if err != nil {
163 reportError(w, r, err)
164 return
165 }
166 w.Header().Set("Content-Type", "application/octet-stream")
167 w.Write(data)
168 }
169 }
170
171 // reportError reports err to w.
172 // If it's a not-found, the reported error is 404.
173 // Otherwise it is an internal server error.
174 // The caller must only call reportError in contexts where
175 // a not-found err should be reported as 404.
176 func reportError(w http.ResponseWriter, r *http.Request, err error) {
177 if os.IsNotExist(err) {
178 http.Error(w, err.Error(), http.StatusNotFound)
179 return
180 }
181 http.Error(w, err.Error(), http.StatusInternalServerError)
182 }