| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 19 |
|
| 20 |
|
| 21 |
type Server interface { |
| 22 |
|
| 23 |
NewContext(r *http.Request) (context.Context, error) |
| 24 |
|
| 25 |
|
| 26 |
Signed(ctx context.Context) ([]byte, error) |
| 27 |
|
| 28 |
|
| 29 |
ReadRecords(ctx context.Context, id, n int64) ([][]byte, error) |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
Lookup(ctx context.Context, key string) (int64, error) |
| 34 |
|
| 35 |
|
| 36 |
). |
| 37 |
ReadTileData(ctx context.Context, t tlog.Tile) ([]byte, error) |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
type Handler struct { |
| 44 |
Server Server |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 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 |
} |