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,47 +37,44 @@ 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 {
|
name := path.Base(r.URL.Path)
|
||||||
http.Error(w, "index is nil", http.StatusInternalServerError)
|
p, ok := rosa.ResolveName(name)
|
||||||
return
|
if !ok {
|
||||||
}
|
http.NotFound(w, r)
|
||||||
name := path.Base(r.URL.Path)
|
return
|
||||||
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")
|
|
||||||
}
|
}
|
||||||
if download {
|
m := rosa.GetMetadata(p)
|
||||||
var version string
|
pk, ok := index.names[m.Name]
|
||||||
if pk.Version != "\u0000" {
|
if !ok {
|
||||||
version = pk.Version
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(pk.status) > 0 {
|
||||||
|
if disposition {
|
||||||
|
w.Header().Set("Content-Type", "application/octet-stream")
|
||||||
} else {
|
} 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")
|
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
||||||
_, err := io.Copy(w, bytes.NewReader(pk.status))
|
_, err := io.Copy(w, bytes.NewReader(pk.status))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
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) {
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user