forked from security/hakurei
cmd/pkgserver: look up status by name once
This has far less overhead. Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
@@ -3,7 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -37,44 +36,34 @@ func handleInfo(w http.ResponseWriter, _ *http.Request) {
|
|||||||
writeAPIPayload(w, infoPayload)
|
writeAPIPayload(w, infoPayload)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// newStatusHandler returns a [http.HandlerFunc] that offers status files for
|
||||||
|
// viewing or download, if available.
|
||||||
func (index *packageIndex) newStatusHandler(disposition bool) http.HandlerFunc {
|
func (index *packageIndex) newStatusHandler(disposition bool) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
name := path.Base(r.URL.Path)
|
m, ok := index.names[path.Base(r.URL.Path)]
|
||||||
p, ok := rosa.ResolveName(name)
|
if !ok || !m.HasReport {
|
||||||
if !ok {
|
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
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 {
|
|
||||||
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())))
|
|
||||||
}
|
|
||||||
|
|
||||||
|
contentType := "text/plain; charset=utf-8"
|
||||||
|
if disposition {
|
||||||
|
contentType = "application/octet-stream"
|
||||||
|
|
||||||
|
contentDisposition := `attachment; filename="`
|
||||||
|
contentDisposition += m.Name + "-"
|
||||||
|
if m.Version != "" {
|
||||||
|
contentDisposition += m.Version + "-"
|
||||||
|
}
|
||||||
|
contentDisposition += pkg.Encode(m.ident.Value()) + `.log"`
|
||||||
|
w.Header().Set("Content-Disposition", contentDisposition)
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", contentType)
|
||||||
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(m.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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user