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,12 +37,8 @@ func handleInfo(w http.ResponseWriter, _ *http.Request) {
writeAPIPayload(w, infoPayload) writeAPIPayload(w, infoPayload)
} }
func (index *packageIndex) handleStatus(w http.ResponseWriter, r *http.Request) { func (index *packageIndex) newStatusHandler(disposition bool) http.HandlerFunc {
download := path.Dir(r.URL.Path) == "/status" return func(w http.ResponseWriter, r *http.Request) {
if index == nil {
http.Error(w, "index is nil", http.StatusInternalServerError)
return
}
name := path.Base(r.URL.Path) name := path.Base(r.URL.Path)
p, ok := rosa.ResolveName(name) p, ok := rosa.ResolveName(name)
if !ok { if !ok {
@@ -56,12 +52,12 @@ func (index *packageIndex) handleStatus(w http.ResponseWriter, r *http.Request)
return return
} }
if len(pk.status) > 0 { if len(pk.status) > 0 {
if download { if disposition {
w.Header().Set("Content-Type", "application/octet-stream") w.Header().Set("Content-Type", "application/octet-stream")
} else { } else {
w.Header().Set("Content-Type", "text/plain; charset=utf-8") w.Header().Set("Content-Type", "text/plain; charset=utf-8")
} }
if download { if disposition {
var version string var version string
if pk.Version != "\u0000" { if pk.Version != "\u0000" {
version = pk.Version version = pk.Version
@@ -80,6 +76,7 @@ func (index *packageIndex) handleStatus(w http.ResponseWriter, r *http.Request)
http.NotFound(w, r) http.NotFound(w, r)
} }
} }
}
// handleGet writes a slice of metadata with specified order. // handleGet writes a slice of metadata with specified order.
func (index *packageIndex) handleGet(w http.ResponseWriter, r *http.Request) { func (index *packageIndex) handleGet(w http.ResponseWriter, r *http.Request) {
@@ -125,8 +122,8 @@ const apiVersion = "v1"
func (index *packageIndex) registerAPI(mux *http.ServeMux) { func (index *packageIndex) registerAPI(mux *http.ServeMux) {
mux.HandleFunc("GET /api/"+apiVersion+"/info", handleInfo) mux.HandleFunc("GET /api/"+apiVersion+"/info", handleInfo)
mux.HandleFunc("GET /api/"+apiVersion+"/get", index.handleGet) mux.HandleFunc("GET /api/"+apiVersion+"/get", index.handleGet)
mux.HandleFunc("GET /api/"+apiVersion+"/status/", index.handleStatus) mux.HandleFunc("GET /api/"+apiVersion+"/status/", index.newStatusHandler(false))
mux.HandleFunc("GET /status/", index.handleStatus) mux.HandleFunc("GET /status/", index.newStatusHandler(true))
} }
// writeAPIPayload sets headers common to API responses and encodes payload as // writeAPIPayload sets headers common to API responses and encodes payload as