forked from security/hakurei
cmd/pkgserver: satisfy handler signature in method
This is somewhat cleaner. Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
@@ -27,8 +27,8 @@ var (
|
|||||||
infoPayloadOnce sync.Once
|
infoPayloadOnce sync.Once
|
||||||
)
|
)
|
||||||
|
|
||||||
// serveInfo returns constant system information.
|
// handleInfo writes constant system information.
|
||||||
func serveInfo(w http.ResponseWriter, _ *http.Request) {
|
func handleInfo(w http.ResponseWriter, _ *http.Request) {
|
||||||
infoPayloadOnce.Do(func() {
|
infoPayloadOnce.Do(func() {
|
||||||
infoPayload.Count = int(rosa.PresetUnexportedStart)
|
infoPayload.Count = int(rosa.PresetUnexportedStart)
|
||||||
infoPayload.HakureiVersion = info.Version()
|
infoPayload.HakureiVersion = info.Version()
|
||||||
@@ -37,86 +37,82 @@ func serveInfo(w http.ResponseWriter, _ *http.Request) {
|
|||||||
writeAPIPayload(w, infoPayload)
|
writeAPIPayload(w, infoPayload)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (index *packageIndex) serveStatus() http.HandlerFunc {
|
func (index *packageIndex) handleStatus(w http.ResponseWriter, r *http.Request) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
download := path.Dir(r.URL.Path) == "/status"
|
||||||
download := path.Dir(r.URL.Path) == "/status"
|
if index == nil {
|
||||||
if index == nil {
|
http.Error(w, "index is nil", http.StatusInternalServerError)
|
||||||
http.Error(w, "index is nil", http.StatusInternalServerError)
|
return
|
||||||
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 {
|
http.NotFound(w, r)
|
||||||
http.NotFound(w, r)
|
return
|
||||||
return
|
}
|
||||||
}
|
m := rosa.GetMetadata(p)
|
||||||
m := rosa.GetMetadata(p)
|
pk, ok := index.names[m.Name]
|
||||||
pk, ok := index.names[m.Name]
|
if !ok {
|
||||||
if !ok {
|
http.NotFound(w, r)
|
||||||
http.NotFound(w, r)
|
return
|
||||||
return
|
}
|
||||||
}
|
if len(pk.status) > 0 {
|
||||||
if len(pk.status) > 0 {
|
if download {
|
||||||
if download {
|
w.Header().Set("Content-Type", "application/octet-stream")
|
||||||
w.Header().Set("Content-Type", "application/octet-stream")
|
|
||||||
} else {
|
|
||||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
||||||
}
|
|
||||||
if download {
|
|
||||||
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("Cache-Control", "no-cache, no-store, must-revalidate")
|
|
||||||
_, err := io.Copy(w, bytes.NewReader(pk.status))
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
http.NotFound(w, r)
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||||
}
|
}
|
||||||
|
if download {
|
||||||
|
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("Cache-Control", "no-cache, no-store, must-revalidate")
|
||||||
|
_, err := io.Copy(w, bytes.NewReader(pk.status))
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (index *packageIndex) serveGet() http.HandlerFunc {
|
func (index *packageIndex) handleGet(w http.ResponseWriter, r *http.Request) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
q := r.URL.Query()
|
||||||
q := r.URL.Query()
|
limit, err := strconv.Atoi(q.Get("limit"))
|
||||||
limit, err := strconv.Atoi(q.Get("limit"))
|
if err != nil || limit > 100 || limit < 1 {
|
||||||
if err != nil || limit > 100 || limit < 1 {
|
http.Error(w, fmt.Sprintf("limit must be an integer between 1 and 100"), http.StatusBadRequest)
|
||||||
http.Error(w, fmt.Sprintf("limit must be an integer between 1 and 100"), http.StatusBadRequest)
|
return
|
||||||
return
|
|
||||||
}
|
|
||||||
i, err := strconv.Atoi(q.Get("index"))
|
|
||||||
if err != nil || i >= len(index.sorts[0]) || i < 0 {
|
|
||||||
http.Error(w, fmt.Sprintf("index must be an integer between 0 and %d", len(index.sorts[0])-1), http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
sort, err := strconv.Atoi(q.Get("sort"))
|
|
||||||
if err != nil || sort >= len(index.sorts) || sort < 0 {
|
|
||||||
http.Error(w, fmt.Sprintf("sort must be an integer between 0 and %d", len(index.sorts)-1), http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
values := index.sorts[sort][i:min(i+limit, len(index.sorts[sort]))]
|
|
||||||
// TODO(mae): remove count field
|
|
||||||
writeAPIPayload(w, &struct {
|
|
||||||
Count int `json:"count"`
|
|
||||||
Values []*metadata `json:"values"`
|
|
||||||
}{len(values), values})
|
|
||||||
}
|
}
|
||||||
|
i, err := strconv.Atoi(q.Get("index"))
|
||||||
|
if err != nil || i >= len(index.sorts[0]) || i < 0 {
|
||||||
|
http.Error(w, fmt.Sprintf("index must be an integer between 0 and %d", len(index.sorts[0])-1), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
sort, err := strconv.Atoi(q.Get("sort"))
|
||||||
|
if err != nil || sort >= len(index.sorts) || sort < 0 {
|
||||||
|
http.Error(w, fmt.Sprintf("sort must be an integer between 0 and %d", len(index.sorts)-1), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
values := index.sorts[sort][i:min(i+limit, len(index.sorts[sort]))]
|
||||||
|
// TODO(mae): remove count field
|
||||||
|
writeAPIPayload(w, &struct {
|
||||||
|
Count int `json:"count"`
|
||||||
|
Values []*metadata `json:"values"`
|
||||||
|
}{len(values), values})
|
||||||
}
|
}
|
||||||
|
|
||||||
const ApiVersion = "v1"
|
const ApiVersion = "v1"
|
||||||
|
|
||||||
func apiRoutes(mux *http.ServeMux, index *packageIndex) {
|
func apiRoutes(mux *http.ServeMux, index *packageIndex) {
|
||||||
mux.HandleFunc(fmt.Sprintf("GET /api/%s/info", ApiVersion), serveInfo)
|
mux.HandleFunc(fmt.Sprintf("GET /api/%s/info", ApiVersion), handleInfo)
|
||||||
mux.HandleFunc(fmt.Sprintf("GET /api/%s/get", ApiVersion), index.serveGet())
|
mux.HandleFunc(fmt.Sprintf("GET /api/%s/get", ApiVersion), index.handleGet)
|
||||||
mux.HandleFunc(fmt.Sprintf("GET /api/%s/status/", ApiVersion), index.serveStatus())
|
mux.HandleFunc(fmt.Sprintf("GET /api/%s/status/", ApiVersion), index.handleStatus)
|
||||||
mux.HandleFunc("GET /status/", index.serveStatus())
|
mux.HandleFunc("GET /status/", index.handleStatus)
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeAPIPayload sets headers common to API responses and encodes payload as
|
// writeAPIPayload sets headers common to API responses and encodes payload as
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ func TestAPIInfo(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
serveInfo(w, httptest.NewRequestWithContext(
|
handleInfo(w, httptest.NewRequestWithContext(
|
||||||
t.Context(),
|
t.Context(),
|
||||||
http.MethodGet,
|
http.MethodGet,
|
||||||
prefix+"info",
|
prefix+"info",
|
||||||
@@ -42,7 +42,7 @@ func TestAPIGet(t *testing.T) {
|
|||||||
index := newIndex(t)
|
index := newIndex(t)
|
||||||
newRequest := func(suffix string) *httptest.ResponseRecorder {
|
newRequest := func(suffix string) *httptest.ResponseRecorder {
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
index.serveGet()(w, httptest.NewRequestWithContext(
|
index.handleGet(w, httptest.NewRequestWithContext(
|
||||||
t.Context(),
|
t.Context(),
|
||||||
http.MethodGet,
|
http.MethodGet,
|
||||||
target+suffix,
|
target+suffix,
|
||||||
|
|||||||
Reference in New Issue
Block a user