cmd/pkgserver: determine disposition route in mux

This removes duplicate checks and uses the more sound check in mux.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
2026-03-11 02:33:34 +09:00
parent 91aa21d92d
commit 50649fdbf4

View File

@@ -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