ServeDownload handles v1beta1 DownloadService/Download.
(w http.ResponseWriter, r *http.Request)
| 456 | |
| 457 | // ServeDownload handles v1beta1 DownloadService/Download. |
| 458 | func (h *commitServiceHandler) ServeDownload(w http.ResponseWriter, r *http.Request) { |
| 459 | if r.Method != http.MethodPost { |
| 460 | h.logHandlerError(r, w, "method not allowed", http.StatusMethodNotAllowed) |
| 461 | return |
| 462 | } |
| 463 | |
| 464 | body, err := io.ReadAll(r.Body) |
| 465 | if err != nil { |
| 466 | h.badRequest(r, w, "reading body", slog.String("read_error", err.Error())) |
| 467 | return |
| 468 | } |
| 469 | |
| 470 | var ref *moduleRef |
| 471 | commitID := parseResourceRefID(body) |
| 472 | if commitID != "" { |
| 473 | h.commitMu.RLock() |
| 474 | if mapped, ok := h.commitMap[commitID]; ok { |
| 475 | r := mapped |
| 476 | ref = &r |
| 477 | } |
| 478 | h.commitMu.RUnlock() |
| 479 | } |
| 480 | // Build lookup-attrs in one place: when ref is found, include owner/module/commit |
| 481 | // so the line is independently useful for correlation with prior GetCommits traffic. |
| 482 | // When ref is NOT found, we still want commit_id visible to operators. |
| 483 | lookupAttrs := []slog.Attr{ |
| 484 | slog.String("handler", "ServeDownload"), |
| 485 | slog.String("procedure", "DownloadService/Download"), |
| 486 | slog.String("branch", "commit_id_lookup"), |
| 487 | slog.String("commit_id", commitID), |
| 488 | slog.Bool("ref_found", ref != nil), |
| 489 | slog.Int("body_bytes", len(body)), |
| 490 | } |
| 491 | if ref != nil { |
| 492 | lookupAttrs = append(lookupAttrs, |
| 493 | slog.String("owner", ref.owner), |
| 494 | slog.String("module", ref.module), |
| 495 | slog.String("repo", ref.module), |
| 496 | ) |
| 497 | // Resolve the git commit this commit_id was minted from. infoCache is the |
| 498 | // only place we keep it; commitMap only stores the ref. |
| 499 | h.commitMu.RLock() |
| 500 | if info, ok := h.infoCache[ref.owner+"/"+ref.module]; ok && info.commitID == commitID { |
| 501 | lookupAttrs = append(lookupAttrs, slog.String("commit", info.commit)) |
| 502 | } |
| 503 | h.commitMu.RUnlock() |
| 504 | } |
| 505 | h.hlog(r).LogAttrs(r.Context(), slog.LevelInfo, "handler decision", lookupAttrs...) |
| 506 | if ref == nil { |
| 507 | // Foreign / cached commit_id fallback. |
| 508 | // |
| 509 | // commitMap is keyed only by ids this proxy mints, but a buf CLI client |
| 510 | // may send a commit_id it cached from a different registry (e.g. real |
| 511 | // buf.build, pinned in buf.lock). That id will never match commitMap, |
| 512 | // yet the module the client wants is one this proxy serves. Before |
| 513 | // rejecting, try to resolve the module identity: |
| 514 | // |
| 515 | // 1. If infoCache has exactly one entry, the proxy is serving a single |
nothing calls this directly
no test coverage detected