| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | "net/url" |
| 9 | "sort" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | "unicode" |
| 13 | "unicode/utf8" |
| 14 | |
| 15 | "the-valley/webui/export" |
| 16 | ) |
| 17 | |
| 18 | // treeURL is the directory at path in commit. It ends in a slash. |
| 19 | func treeURL(project, commit string, path []string) string { |
| 20 | u := "/source/" + url.PathEscape(project) + "/tree/" + commit + "/" |
| 21 | if len(path) > 0 { |
| 22 | u += escapePath(path) + "/" |
| 23 | } |
| 24 | return u |
| 25 | } |
| 26 | |
| 27 | // blobURL is the file at path in commit. |
| 28 | func blobURL(project, commit string, path []string) string { |
| 29 | return "/source/" + url.PathEscape(project) + "/blob/" + commit + "/" + escapePath(path) |
| 30 | } |
| 31 | |
| 32 | // logURL is the history that ends at commit. |
| 33 | func logURL(project, commit string) string { |
| 34 | return "/source/" + url.PathEscape(project) + "/log/" + commit |
| 35 | } |
| 36 | |
| 37 | func escapePath(path []string) string { |
| 38 | parts := make([]string, len(path)) |
| 39 | for i, s := range path { |
| 40 | parts[i] = url.PathEscape(s) |
| 41 | } |
| 42 | return strings.Join(parts, "/") |
| 43 | } |
| 44 | |
| 45 | // source serves the routes under /source/<project>/. The project is |
| 46 | // checked first, so everything under a project the export does not hold |
| 47 | // as public answers as a missing project does. |
| 48 | func (q *request) source(project string, rest []string) { |
| 49 | p, ok := q.project(project) |
| 50 | if !ok { |
| 51 | q.notFound() |
| 52 | return |
| 53 | } |
| 54 | repo, ok := q.g.Repo(p.Name) |
| 55 | if !ok { |
| 56 | q.notFound() |
| 57 | return |
| 58 | } |
| 59 | var view func(ctx context.Context) error |
| 60 | switch { |
| 61 | case len(rest) == 0 || len(rest) == 1 && rest[0] == "": |
| 62 | http.Redirect(q.w, q.r, treeURL(p.Name, p.SourceTip, nil), http.StatusFound) |
| 63 | return |
| 64 | case len(rest) >= 2 && rest[0] == "tree": |
| 65 | path := rest[2:] |
| 66 | if n := len(path); n > 0 && path[n-1] == "" { |
| 67 | path = path[:n-1] |
| 68 | } |
| 69 | if !validPath(path) { |
| 70 | q.notFound() |
| 71 | return |
| 72 | } |
| 73 | view = func(ctx context.Context) error { return q.dir(ctx, p, repo, rest[1], path) } |
| 74 | case len(rest) >= 3 && rest[0] == "blob": |
| 75 | path := rest[2:] |
| 76 | if !validPath(path) { |
| 77 | q.notFound() |
| 78 | return |
| 79 | } |
| 80 | view = func(ctx context.Context) error { return q.file(ctx, p, repo, rest[1], path) } |
| 81 | case len(rest) == 2 && rest[0] == "log": |
| 82 | view = func(ctx context.Context) error { return q.log(ctx, p, repo, rest[1]) } |
| 83 | default: |
| 84 | q.notFound() |
| 85 | return |
| 86 | } |
| 87 | ctx, done, err := q.gitContext() |
| 88 | if err != nil { |
| 89 | return // the reader went away |
| 90 | } |
| 91 | defer done() |
| 92 | if err := view(ctx); err != nil { |
| 93 | q.fail(err) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // validPath is a path a source route may name: at most MaxDepth |
| 98 | // segments, none of them empty, a dot segment, or holding a slash or a |
| 99 | // NUL. |
| 100 | func validPath(path []string) bool { |
| 101 | if len(path) > MaxDepth { |
| 102 | return false |
| 103 | } |
| 104 | for _, s := range path { |
| 105 | if s == "" || s == "." || s == ".." || strings.ContainsAny(s, "/\x00") { |
| 106 | return false |
| 107 | } |
| 108 | } |
| 109 | return true |
| 110 | } |
| 111 | |
| 112 | // commitTree is the tree of commit, when commit is a commit the |
| 113 | // project's published source tip reaches. ok is false for anything else: |
| 114 | // an unreachable commit, another kind of object, or text that is no full |
| 115 | // object ID. |
| 116 | func commitTree(ctx context.Context, p *export.Project, repo, commit string) (tree string, ok bool, err error) { |
| 117 | reachable, err := export.Reachable(ctx, repo, p.SourceTip, commit) |
| 118 | if err != nil || !reachable { |
| 119 | return "", false, err |
| 120 | } |
| 121 | out, err := export.Git(ctx, repo, "rev-parse", "--verify", commit+"^{tree}") |
| 122 | if err != nil { |
| 123 | return "", false, err |
| 124 | } |
| 125 | tree = strings.TrimSpace(string(out)) |
| 126 | if !export.ValidObjectID(tree) { |
| 127 | return "", false, fmt.Errorf("the tree of %s is %q", commit, tree) |
| 128 | } |
| 129 | return tree, true, nil |
| 130 | } |
| 131 | |
| 132 | // entry is one entry of a tree, as git ls-tree --long gives it. Size is |
| 133 | // -1 for an entry that is not a blob. |
| 134 | type entry struct { |
| 135 | Mode string |
| 136 | Type string |
| 137 | OID string |
| 138 | Size int64 |
| 139 | Name string |
| 140 | } |
| 141 | |
| 142 | func listTree(ctx context.Context, repo, tree string) ([]entry, error) { |
| 143 | out, err := export.Git(ctx, repo, "ls-tree", "-z", "--long", tree) |
| 144 | if err != nil { |
| 145 | return nil, err |
| 146 | } |
| 147 | var entries []entry |
| 148 | for _, rec := range bytes.Split(out, []byte{0}) { |
| 149 | if len(rec) == 0 { |
| 150 | continue |
| 151 | } |
| 152 | meta, name, ok := bytes.Cut(rec, []byte{'\t'}) |
| 153 | f := strings.Fields(string(meta)) |
| 154 | if !ok || len(f) != 4 || !export.ValidObjectID(f[2]) { |
| 155 | return nil, fmt.Errorf("git ls-tree gave %q", rec) |
| 156 | } |
| 157 | e := entry{Mode: f[0], Type: f[1], OID: f[2], Size: -1, Name: string(name)} |
| 158 | if f[3] != "-" { |
| 159 | n, err := strconv.ParseInt(f[3], 10, 64) |
| 160 | if err != nil || n < 0 { |
| 161 | return nil, fmt.Errorf("git ls-tree gave %q", rec) |
| 162 | } |
| 163 | e.Size = n |
| 164 | } |
| 165 | entries = append(entries, e) |
| 166 | } |
| 167 | return entries, nil |
| 168 | } |
| 169 | |
| 170 | // resolve walks path down from the tree root, one tree entry at a time, |
| 171 | // and gives the entry it ends at. ok is false when a segment names no |
| 172 | // entry, or a segment other than the last names something that is not a |
| 173 | // tree. |
| 174 | func resolve(ctx context.Context, repo, root string, path []string) (e entry, ok bool, err error) { |
| 175 | e = entry{Mode: "040000", Type: "tree", OID: root, Size: -1} |
| 176 | for _, name := range path { |
| 177 | if e.Type != "tree" { |
| 178 | return entry{}, false, nil |
| 179 | } |
| 180 | entries, err := listTree(ctx, repo, e.OID) |
| 181 | if err != nil { |
| 182 | return entry{}, false, err |
| 183 | } |
| 184 | found := false |
| 185 | for _, c := range entries { |
| 186 | if c.Name == name { |
| 187 | e, found = c, true |
| 188 | break |
| 189 | } |
| 190 | } |
| 191 | if !found { |
| 192 | return entry{}, false, nil |
| 193 | } |
| 194 | } |
| 195 | return e, true, nil |
| 196 | } |
| 197 | |
| 198 | // Entry modes git gives for a symbolic link and a submodule. |
| 199 | const ( |
| 200 | modeSymlink = "120000" |
| 201 | modeSubmodule = "160000" |
| 202 | ) |
| 203 | |
| 204 | // shown is how a page shows a blob: its text, or a notice that says why |
| 205 | // it is not shown. A symbolic link is shown as its target and is never |
| 206 | // followed. |
| 207 | func shown(ctx context.Context, repo string, e entry) (text, notice string, err error) { |
| 208 | if e.Size > MaxFile { |
| 209 | return "", fmt.Sprintf("This file is %s, larger than the %s a page shows.", size(e.Size), size(MaxFile)), nil |
| 210 | } |
| 211 | data, err := export.Git(ctx, repo, "cat-file", "blob", e.OID) |
| 212 | if err != nil { |
| 213 | return "", "", err |
| 214 | } |
| 215 | if e.Mode == modeSymlink { |
| 216 | return "", "A symbolic link to " + oneLine(string(data)) + ". It is not followed.", nil |
| 217 | } |
| 218 | if bytes.IndexByte(data, 0) >= 0 || !utf8.Valid(data) { |
| 219 | return "", fmt.Sprintf("This file is binary, %s, and is not shown.", size(e.Size)), nil |
| 220 | } |
| 221 | return string(data), "", nil |
| 222 | } |
| 223 | |
| 224 | // dirPage is one directory of a commit. |
| 225 | type dirPage struct { |
| 226 | Project string |
| 227 | Commit string |
| 228 | Short string |
| 229 | AtTip bool |
| 230 | TipURL string |
| 231 | LogURL string |
| 232 | Crumbs []link |
| 233 | Entries []dirEntry |
| 234 | Omitted int |
| 235 | Readme *readme |
| 236 | } |
| 237 | |
| 238 | type dirEntry struct { |
| 239 | Name string |
| 240 | URL string |
| 241 | Kind string |
| 242 | Size string |
| 243 | } |
| 244 | |
| 245 | type readme struct { |
| 246 | Name string |
| 247 | URL string |
| 248 | Text string |
| 249 | Notice string |
| 250 | } |
| 251 | |
| 252 | func (q *request) dir(ctx context.Context, p *export.Project, repo, commit string, path []string) error { |
| 253 | root, ok, err := commitTree(ctx, p, repo, commit) |
| 254 | if err != nil || !ok { |
| 255 | q.missing(err) |
| 256 | return nil |
| 257 | } |
| 258 | e, ok, err := resolve(ctx, repo, root, path) |
| 259 | if err != nil || !ok || e.Type != "tree" { |
| 260 | q.missing(err) |
| 261 | return nil |
| 262 | } |
| 263 | entries, err := listTree(ctx, repo, e.OID) |
| 264 | if err != nil { |
| 265 | return err |
| 266 | } |
| 267 | page := dirPage{Project: p.Name, Commit: commit, Short: commit[:12], AtTip: commit == p.SourceTip, |
| 268 | TipURL: treeURL(p.Name, p.SourceTip, nil), LogURL: logURL(p.Name, commit), |
| 269 | Crumbs: crumbs(p.Name, commit, path)} |
| 270 | // Directories first, then everything else, each in git's order. |
| 271 | sort.SliceStable(entries, func(i, j int) bool { return entries[i].Type == "tree" && entries[j].Type != "tree" }) |
| 272 | for i, c := range entries { |
| 273 | if i == MaxEntries { |
| 274 | page.Omitted = len(entries) - MaxEntries |
| 275 | break |
| 276 | } |
| 277 | at := append(append([]string{}, path...), c.Name) |
| 278 | d := dirEntry{Name: oneLine(c.Name)} |
| 279 | switch { |
| 280 | case c.Type == "tree": |
| 281 | d.Kind, d.URL = "directory", treeURL(p.Name, commit, at) |
| 282 | case c.Mode == modeSubmodule || c.Type == "commit": |
| 283 | d.Kind = "submodule at " + c.OID[:12] |
| 284 | case c.Mode == modeSymlink: |
| 285 | d.Kind, d.URL = "symbolic link", blobURL(p.Name, commit, at) |
| 286 | default: |
| 287 | d.Kind, d.URL, d.Size = "file", blobURL(p.Name, commit, at), size(c.Size) |
| 288 | } |
| 289 | if !validPath(at) { |
| 290 | d.URL = "" // a name no route takes is listed and not linked |
| 291 | } |
| 292 | page.Entries = append(page.Entries, d) |
| 293 | } |
| 294 | if r, ok := readmeOf(entries); ok { |
| 295 | text, notice, err := shown(ctx, repo, r) |
| 296 | if err != nil { |
| 297 | return err |
| 298 | } |
| 299 | at := append(append([]string{}, path...), r.Name) |
| 300 | page.Readme = &readme{Name: oneLine(r.Name), URL: blobURL(p.Name, commit, at), Text: trimEnd(text), Notice: notice} |
| 301 | } |
| 302 | title := p.Name + " at " + commit[:12] |
| 303 | if len(path) > 0 { |
| 304 | title = strings.Join(path, "/") + " · " + title |
| 305 | } |
| 306 | q.page(http.StatusOK, "tree", title, page) |
| 307 | return nil |
| 308 | } |
| 309 | |
| 310 | // readmeOf is the directory's README: README.md, README or README.txt |
| 311 | // when there is one, and otherwise the first file whose name is README |
| 312 | // in any case, with any extension. |
| 313 | func readmeOf(entries []entry) (entry, bool) { |
| 314 | for _, name := range []string{"README.md", "README", "README.txt"} { |
| 315 | for _, e := range entries { |
| 316 | if e.Name == name && e.Type == "blob" && e.Mode != modeSymlink { |
| 317 | return e, true |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | for _, e := range entries { |
| 322 | base, _, _ := strings.Cut(strings.ToLower(e.Name), ".") |
| 323 | if base == "readme" && e.Type == "blob" && e.Mode != modeSymlink { |
| 324 | return e, true |
| 325 | } |
| 326 | } |
| 327 | return entry{}, false |
| 328 | } |
| 329 | |
| 330 | // filePage is one file of a commit. |
| 331 | type filePage struct { |
| 332 | Project string |
| 333 | Commit string |
| 334 | Short string |
| 335 | AtTip bool |
| 336 | TipURL string |
| 337 | LogURL string |
| 338 | Crumbs []link |
| 339 | Name string |
| 340 | Size string |
| 341 | Lines []string |
| 342 | Notice string |
| 343 | } |
| 344 | |
| 345 | func (q *request) file(ctx context.Context, p *export.Project, repo, commit string, path []string) error { |
| 346 | root, ok, err := commitTree(ctx, p, repo, commit) |
| 347 | if err != nil || !ok { |
| 348 | q.missing(err) |
| 349 | return nil |
| 350 | } |
| 351 | e, ok, err := resolve(ctx, repo, root, path) |
| 352 | if err != nil || !ok || e.Type != "blob" { |
| 353 | q.missing(err) |
| 354 | return nil |
| 355 | } |
| 356 | text, notice, err := shown(ctx, repo, e) |
| 357 | if err != nil { |
| 358 | return err |
| 359 | } |
| 360 | page := filePage{Project: p.Name, Commit: commit, Short: commit[:12], AtTip: commit == p.SourceTip, |
| 361 | TipURL: treeURL(p.Name, p.SourceTip, nil), LogURL: logURL(p.Name, commit), |
| 362 | Crumbs: crumbs(p.Name, commit, path), Name: oneLine(path[len(path)-1]), |
| 363 | Size: size(e.Size), Notice: notice} |
| 364 | if notice == "" { |
| 365 | page.Lines = strings.Split(strings.TrimSuffix(text, "\n"), "\n") |
| 366 | } |
| 367 | q.page(http.StatusOK, "blob", strings.Join(path, "/")+" · "+p.Name+" at "+commit[:12], page) |
| 368 | return nil |
| 369 | } |
| 370 | |
| 371 | // crumbs are the links from the project's root down to path. The last is |
| 372 | // the page itself and has no link. |
| 373 | func crumbs(project, commit string, path []string) []link { |
| 374 | out := []link{{project, treeURL(project, commit, nil)}} |
| 375 | for i, name := range path { |
| 376 | out = append(out, link{oneLine(name), treeURL(project, commit, path[:i+1])}) |
| 377 | } |
| 378 | out[len(out)-1].URL = "" |
| 379 | return out |
| 380 | } |
| 381 | |
| 382 | // logPage is one page of the history that ends at a commit. |
| 383 | type logPage struct { |
| 384 | Project string |
| 385 | Commit string |
| 386 | Short string |
| 387 | AtTip bool |
| 388 | Commits []logEntry |
| 389 | Newer string |
| 390 | Older string |
| 391 | } |
| 392 | |
| 393 | type logEntry struct { |
| 394 | Short string |
| 395 | URL string |
| 396 | Subject string |
| 397 | Author string |
| 398 | Date string |
| 399 | } |
| 400 | |
| 401 | func (q *request) log(ctx context.Context, p *export.Project, repo, commit string) error { |
| 402 | skip := 0 |
| 403 | if s := q.r.URL.Query().Get("skip"); s != "" { |
| 404 | n, err := strconv.Atoi(s) |
| 405 | if err != nil || n < 0 || n > 1_000_000 || strconv.Itoa(n) != s { |
| 406 | q.notFound() |
| 407 | return nil |
| 408 | } |
| 409 | skip = n |
| 410 | } |
| 411 | if _, ok, err := commitTree(ctx, p, repo, commit); err != nil || !ok { |
| 412 | q.missing(err) |
| 413 | return nil |
| 414 | } |
| 415 | out, err := export.Git(ctx, repo, "log", "--no-mailmap", "--no-notes", "--no-show-signature", |
| 416 | "--no-decorate", "--no-color", "-z", "--format=%H%n%an%n%aI%n%s", |
| 417 | "--max-count="+strconv.Itoa(LogPage+1), "--skip="+strconv.Itoa(skip), commit, "--") |
| 418 | if err != nil { |
| 419 | return err |
| 420 | } |
| 421 | page := logPage{Project: p.Name, Commit: commit, Short: commit[:12], AtTip: commit == p.SourceTip} |
| 422 | for _, rec := range strings.Split(string(out), "\x00") { |
| 423 | f := strings.SplitN(strings.TrimPrefix(rec, "\n"), "\n", 4) |
| 424 | if len(f) != 4 || !export.ValidObjectID(f[0]) { |
| 425 | continue |
| 426 | } |
| 427 | if len(page.Commits) == LogPage { |
| 428 | page.Older = logURL(p.Name, commit) + "?skip=" + strconv.Itoa(skip+LogPage) |
| 429 | break |
| 430 | } |
| 431 | page.Commits = append(page.Commits, logEntry{Short: f[0][:12], URL: treeURL(p.Name, f[0], nil), |
| 432 | Author: oneLine(f[1]), Date: stampOf(f[2]), Subject: oneLine(f[3])}) |
| 433 | } |
| 434 | if skip > 0 { |
| 435 | page.Newer = logURL(p.Name, commit) |
| 436 | if skip > LogPage { |
| 437 | page.Newer += "?skip=" + strconv.Itoa(skip-LogPage) |
| 438 | } |
| 439 | } |
| 440 | q.page(http.StatusOK, "log", "History of "+p.Name+" at "+commit[:12], page) |
| 441 | return nil |
| 442 | } |
| 443 | |
| 444 | // missing answers a source route whose commit or path names nothing it |
| 445 | // serves: the one 404, or the one 500 when git failed. |
| 446 | func (q *request) missing(err error) { |
| 447 | if err != nil { |
| 448 | q.fail(err) |
| 449 | return |
| 450 | } |
| 451 | q.notFound() |
| 452 | } |
| 453 | |
| 454 | // oneLine is text from a repository as a page shows it on one line: |
| 455 | // valid UTF-8, with control and bidirectional formatting characters |
| 456 | // replaced, so a name cannot reorder or hide the text around it. |
| 457 | func oneLine(s string) string { |
| 458 | return strings.Map(func(r rune) rune { |
| 459 | if unicode.IsControl(r) || unicode.Is(unicode.Bidi_Control, r) { |
| 460 | return '�' |
| 461 | } |
| 462 | return r |
| 463 | }, strings.ToValidUTF8(s, "�")) |
| 464 | } |
| 465 | |
| 466 | // size is a byte count in words. |
| 467 | func size(n int64) string { |
| 468 | switch { |
| 469 | case n < 0: |
| 470 | return "" |
| 471 | case n < 1<<10: |
| 472 | return count(int(n), "byte") |
| 473 | case n < 1<<20: |
| 474 | return fmt.Sprintf("%.1f KiB", float64(n)/(1<<10)) |
| 475 | case n < 1<<30: |
| 476 | return fmt.Sprintf("%.1f MiB", float64(n)/(1<<20)) |
| 477 | } |
| 478 | return fmt.Sprintf("%.1f GiB", float64(n)/(1<<30)) |
| 479 | } |