forked from security/hakurei
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:
@@ -37,12 +37,8 @@ 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
|
||||
}
|
||||
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 {
|
||||
@@ -56,12 +52,12 @@ func (index *packageIndex) handleStatus(w http.ResponseWriter, r *http.Request)
|
||||
return
|
||||
}
|
||||
if len(pk.status) > 0 {
|
||||
if download {
|
||||
if disposition {
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
}
|
||||
if download {
|
||||
if disposition {
|
||||
var version string
|
||||
if pk.Version != "\u0000" {
|
||||
version = pk.Version
|
||||
@@ -79,6 +75,7 @@ func (index *packageIndex) handleStatus(w http.ResponseWriter, r *http.Request)
|
||||
} else {
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// handleGet writes a slice of metadata with specified order.
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user