From fb36c54025eeb4b0aeff63043ab6f5757b830984 Mon Sep 17 00:00:00 2001 From: Ophestra Date: Wed, 11 Mar 2026 03:17:56 +0900 Subject: [PATCH] cmd/pkgserver: look up status by name once This has far less overhead. Signed-off-by: Ophestra --- cmd/pkgserver/api.go | 51 +++++++++++++++++--------------------------- 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/cmd/pkgserver/api.go b/cmd/pkgserver/api.go index 8864626..fe70800 100644 --- a/cmd/pkgserver/api.go +++ b/cmd/pkgserver/api.go @@ -3,7 +3,6 @@ package main import ( "bytes" "encoding/json" - "fmt" "io" "log" "net/http" @@ -37,43 +36,33 @@ func handleInfo(w http.ResponseWriter, _ *http.Request) { writeAPIPayload(w, infoPayload) } +// newStatusHandler returns a [http.HandlerFunc] that offers status files for +// viewing or download, if available. func (index *packageIndex) newStatusHandler(disposition bool) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - name := path.Base(r.URL.Path) - p, ok := rosa.ResolveName(name) - if !ok { + m, ok := index.names[path.Base(r.URL.Path)] + if !ok || !m.HasReport { http.NotFound(w, r) return } - m := rosa.GetMetadata(p) - pk, ok := index.names[m.Name] - if !ok { - http.NotFound(w, r) - return - } - if len(pk.status) > 0 { - if disposition { - w.Header().Set("Content-Type", "application/octet-stream") - } else { - w.Header().Set("Content-Type", "text/plain; charset=utf-8") - } - if disposition { - var version string - if pk.Version != "\u0000" { - version = pk.Version - } else { - version = "unknown" - } - w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s-%s-%s.log\"", pk.Name, version, pkg.Encode(pk.ident.Value()))) - } - w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") - _, err := io.Copy(w, bytes.NewReader(pk.status)) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + contentType := "text/plain; charset=utf-8" + if disposition { + contentType = "application/octet-stream" + + contentDisposition := `attachment; filename="` + contentDisposition += m.Name + "-" + if m.Version != "" { + contentDisposition += m.Version + "-" } - } else { - http.NotFound(w, r) + contentDisposition += pkg.Encode(m.ident.Value()) + `.log"` + w.Header().Set("Content-Disposition", contentDisposition) + } + w.Header().Set("Content-Type", contentType) + w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") + _, err := io.Copy(w, bytes.NewReader(m.status)) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) } } }