feat(tvix/nar-bridge): support HTTP HEAD for narinfo

Change-Id: I4ec8e9108f52d0a84bc1af01a27bd51de4ab493f
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11456
Reviewed-by: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
This commit is contained in:
Edwin Mackenzie-Owen 2024-04-14 21:17:11 +02:00
parent d84fd49169
commit 465370c11f

View file

@ -96,11 +96,12 @@ func renderNarinfo(
} }
func registerNarinfoGet(s *Server) { func registerNarinfoGet(s *Server) {
// GET $outHash.narinfo looks up the PathInfo from the tvix-store, // GET/HEAD $outHash.narinfo looks up the PathInfo from the tvix-store,
// and then render a .narinfo file to the client. // and, if it's a GET request, render a .narinfo file to the client.
// It will keep the PathInfo in the lookup map, // In both cases it will keep the PathInfo in the lookup map,
// so a subsequent GET /nar/ $narhash.nar request can find it. // so a subsequent GET/HEAD /nar/ $narhash.nar request can find it.
s.handler.Get("/{outputhash:^["+nixbase32.Alphabet+"]{32}}.narinfo", func(w http.ResponseWriter, r *http.Request) { genNarinfoHandler := func(isHead bool) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close() defer r.Body.Close()
ctx := r.Context() ctx := r.Context()
@ -119,7 +120,7 @@ func registerNarinfoGet(s *Server) {
return return
} }
err = renderNarinfo(ctx, log, s.pathInfoServiceClient, &s.narDbMu, s.narDb, outputHash, w, false) err = renderNarinfo(ctx, log, s.pathInfoServiceClient, &s.narDbMu, s.narDb, outputHash, w, isHead)
if err != nil { if err != nil {
if errors.Is(err, fs.ErrNotExist) { if errors.Is(err, fs.ErrNotExist) {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
@ -128,5 +129,9 @@ func registerNarinfoGet(s *Server) {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
} }
} }
}) }
}
s.handler.Get("/{outputhash:^["+nixbase32.Alphabet+"]{32}}.narinfo", genNarinfoHandler(false))
s.handler.Head("/{outputhash:^["+nixbase32.Alphabet+"]{32}}.narinfo", genNarinfoHandler(true))
} }