forked from security/hakurei
cmd/pkgserver: expose size and store pre-encoded ident
This change also handles SIGSEGV correctly in newStatusHandler, and makes serving status fully zero copy. Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
@@ -1,9 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
@@ -11,7 +9,6 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"hakurei.app/internal/info"
|
"hakurei.app/internal/info"
|
||||||
"hakurei.app/internal/pkg"
|
|
||||||
"hakurei.app/internal/rosa"
|
"hakurei.app/internal/rosa"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -50,19 +47,27 @@ func (index *packageIndex) newStatusHandler(disposition bool) http.HandlerFunc {
|
|||||||
if disposition {
|
if disposition {
|
||||||
contentType = "application/octet-stream"
|
contentType = "application/octet-stream"
|
||||||
|
|
||||||
|
// quoting like this is unsound, but okay, because metadata is hardcoded
|
||||||
contentDisposition := `attachment; filename="`
|
contentDisposition := `attachment; filename="`
|
||||||
contentDisposition += m.Name + "-"
|
contentDisposition += m.Name + "-"
|
||||||
if m.Version != "" {
|
if m.Version != "" {
|
||||||
contentDisposition += m.Version + "-"
|
contentDisposition += m.Version + "-"
|
||||||
}
|
}
|
||||||
contentDisposition += pkg.Encode(m.ident.Value()) + `.log"`
|
contentDisposition += m.ids + `.log"`
|
||||||
w.Header().Set("Content-Disposition", contentDisposition)
|
w.Header().Set("Content-Disposition", contentDisposition)
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", contentType)
|
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(m.status))
|
if err := func() (err error) {
|
||||||
if err != nil {
|
defer index.handleAccess(&err)()
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
_, err = w.Write(m.status)
|
||||||
|
return
|
||||||
|
}(); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(
|
||||||
|
w, "cannot deliver status, contact maintainers",
|
||||||
|
http.StatusInternalServerError,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"unique"
|
|
||||||
|
|
||||||
"hakurei.app/internal/pkg"
|
"hakurei.app/internal/pkg"
|
||||||
"hakurei.app/internal/rosa"
|
"hakurei.app/internal/rosa"
|
||||||
@@ -23,6 +22,9 @@ const (
|
|||||||
type packageIndex struct {
|
type packageIndex struct {
|
||||||
sorts [sortOrderEnd + 1][rosa.PresetUnexportedStart]*metadata
|
sorts [sortOrderEnd + 1][rosa.PresetUnexportedStart]*metadata
|
||||||
names map[string]*metadata
|
names map[string]*metadata
|
||||||
|
|
||||||
|
// Taken from [rosa.Report] if available.
|
||||||
|
handleAccess func(*error) func()
|
||||||
}
|
}
|
||||||
|
|
||||||
// metadata holds [rosa.Metadata] extended with additional information.
|
// metadata holds [rosa.Metadata] extended with additional information.
|
||||||
@@ -33,11 +35,13 @@ type metadata struct {
|
|||||||
// Populated via [rosa.Toolchain.Version], [rosa.Unversioned] is equivalent
|
// Populated via [rosa.Toolchain.Version], [rosa.Unversioned] is equivalent
|
||||||
// to the zero value. Otherwise, the zero value is invalid.
|
// to the zero value. Otherwise, the zero value is invalid.
|
||||||
Version string `json:"version,omitempty"`
|
Version string `json:"version,omitempty"`
|
||||||
|
// Output data size, available if present in report.
|
||||||
|
Size int64 `json:"size,omitempty"`
|
||||||
// Whether the underlying [pkg.Artifact] is present in the report.
|
// Whether the underlying [pkg.Artifact] is present in the report.
|
||||||
HasReport bool `json:"report"`
|
HasReport bool `json:"report"`
|
||||||
|
|
||||||
// Ident resolved from underlying [pkg.Artifact].
|
// Ident string encoded ahead of time.
|
||||||
ident unique.Handle[pkg.ID]
|
ids string
|
||||||
// Backed by [rosa.Report], access must be prepared by HandleAccess.
|
// Backed by [rosa.Report], access must be prepared by HandleAccess.
|
||||||
status []byte
|
status []byte
|
||||||
}
|
}
|
||||||
@@ -46,6 +50,7 @@ type metadata struct {
|
|||||||
func (index *packageIndex) populate(cache *pkg.Cache, report *rosa.Report) (err error) {
|
func (index *packageIndex) populate(cache *pkg.Cache, report *rosa.Report) (err error) {
|
||||||
if report != nil {
|
if report != nil {
|
||||||
defer report.HandleAccess(&err)()
|
defer report.HandleAccess(&err)()
|
||||||
|
index.handleAccess = report.HandleAccess
|
||||||
}
|
}
|
||||||
|
|
||||||
var work [rosa.PresetUnexportedStart]*metadata
|
var work [rosa.PresetUnexportedStart]*metadata
|
||||||
@@ -65,12 +70,10 @@ func (index *packageIndex) populate(cache *pkg.Cache, report *rosa.Report) (err
|
|||||||
}
|
}
|
||||||
|
|
||||||
if cache != nil && report != nil {
|
if cache != nil && report != nil {
|
||||||
m.ident = cache.Ident(rosa.Std.Load(p))
|
id := cache.Ident(rosa.Std.Load(p))
|
||||||
status, n := report.ArtifactOf(m.ident)
|
m.ids = pkg.Encode(id.Value())
|
||||||
if n >= 0 {
|
m.status, m.Size = report.ArtifactOf(id)
|
||||||
m.HasReport = true
|
m.HasReport = m.Size >= 0
|
||||||
m.status = status
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
work[p] = &m
|
work[p] = &m
|
||||||
|
|||||||
Reference in New Issue
Block a user