From 50649fdbf4c63f97efa8f88ac57dbfdc2300c72d Mon Sep 17 00:00:00 2001 From: Ophestra Date: Wed, 11 Mar 2026 02:33:34 +0900 Subject: [PATCH] cmd/pkgserver: determine disposition route in mux This removes duplicate checks and uses the more sound check in mux. Signed-off-by: Ophestra --- cmd/pkgserver/api.go | 73 +++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 38 deletions(-) diff --git a/cmd/pkgserver/api.go b/cmd/pkgserver/api.go index 6549e4a..8864626 100644 --- a/cmd/pkgserver/api.go +++ b/cmd/pkgserver/api.go @@ -37,47 +37,44 @@ func handleInfo(w http.ResponseWriter, _ *http.Request) { writeAPIPayload(w, infoPayload) } -func (index *packageIndex) handleStatus(w http.ResponseWriter, r *http.Request) { - download := path.Dir(r.URL.Path) == "/status" - if index == nil { - http.Error(w, "index is nil", http.StatusInternalServerError) - return - } - name := path.Base(r.URL.Path) - p, ok := rosa.ResolveName(name) - if !ok { - 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 download { - w.Header().Set("Content-Type", "application/octet-stream") - } else { - w.Header().Set("Content-Type", "text/plain; charset=utf-8") +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 { + http.NotFound(w, r) + return } - if download { - var version string - if pk.Version != "\u0000" { - version = pk.Version + 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 { - version = "unknown" + 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("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) + 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) + } + } else { + http.NotFound(w, r) } - } else { - http.NotFound(w, r) } } @@ -125,8 +122,8 @@ const apiVersion = "v1" func (index *packageIndex) registerAPI(mux *http.ServeMux) { mux.HandleFunc("GET /api/"+apiVersion+"/info", handleInfo) mux.HandleFunc("GET /api/"+apiVersion+"/get", index.handleGet) - mux.HandleFunc("GET /api/"+apiVersion+"/status/", index.handleStatus) - mux.HandleFunc("GET /status/", index.handleStatus) + mux.HandleFunc("GET /api/"+apiVersion+"/status/", index.newStatusHandler(false)) + mux.HandleFunc("GET /status/", index.newStatusHandler(true)) } // writeAPIPayload sets headers common to API responses and encodes payload as