From 3dd4acd233318967d7d87b1e29d34157cec994a2 Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Sat, 17 Oct 2026 12:40:33 +1100 Subject: [PATCH] Add HEAD route for remotes matching GET headers --- e2e/head_test.go | 100 +++++++++++++++++++++++++++++++++ internal/api/v1/proxy.go | 66 +++++++++++++--------- internal/api/v1/routes_test.go | 17 ++++++ internal/proxy/engine.go | 2 +- internal/proxy/engine_test.go | 14 +++++ 5 files changed, 173 insertions(+), 26 deletions(-) create mode 100644 e2e/head_test.go create mode 100644 internal/api/v1/routes_test.go diff --git a/e2e/head_test.go b/e2e/head_test.go new file mode 100644 index 0000000..f7b9ac7 --- /dev/null +++ b/e2e/head_test.go @@ -0,0 +1,100 @@ +//go:build e2e + +package e2e + +import ( + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "strconv" + "testing" + "time" + + "git.unkin.net/unkin/artifactapi/internal/testsupport" +) + +// assertHeadMatchesGet checks HEAD returns GET's status, type and length with no body. +func assertHeadMatchesGet(t *testing.T, url string) { + t.Helper() + get, err := http.Get(url) + if err != nil { + t.Fatalf("GET: %v", err) + } + body, _ := io.ReadAll(get.Body) + get.Body.Close() + if get.StatusCode != http.StatusOK { + t.Fatalf("GET %s: %d %s", url, get.StatusCode, body) + } + + head, err := http.Head(url) + if err != nil { + t.Fatalf("HEAD: %v", err) + } + hb, _ := io.ReadAll(head.Body) + head.Body.Close() + if head.StatusCode != http.StatusOK { + t.Fatalf("HEAD %s: got %d, want 200", url, head.StatusCode) + } + if len(hb) != 0 { + t.Errorf("HEAD returned a %d-byte body", len(hb)) + } + if head.Header.Get("Content-Type") != get.Header.Get("Content-Type") { + t.Errorf("HEAD Content-Type %q, GET %q", head.Header.Get("Content-Type"), get.Header.Get("Content-Type")) + } + if head.ContentLength != int64(len(body)) { + t.Errorf("HEAD Content-Length %d, GET body %d bytes", head.ContentLength, len(body)) + } +} + +func TestProxyHeadNuGetRewritten(t *testing.T) { + var upstream *httptest.Server + upstream = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]any{"version": "3.0.0", "resources": []map[string]string{ + {"@id": upstream.URL + "/v3-flatcontainer/", "@type": "PackageBaseAddress/3.0.0"}, + }}) + })) + defer upstream.Close() + + createRemote(t, mustFormat(`{"name": "head-nuget", "package_type": "nuget", "base_url": %q}`, upstream.URL+"/v3/index.json")) + defer deleteRemote(t, "head-nuget") + + assertHeadMatchesGet(t, apiURL("/api/v1/remote/head-nuget/index.json")) +} + +func TestProxyHeadGitHubRPMRepodata(t *testing.T) { + rpm := testsupport.MinimalRPM("demo", "1.2", "3", "x86_64") + var gh *httptest.Server + mux := http.NewServeMux() + mux.HandleFunc("/repos/acme/tools/releases", func(w http.ResponseWriter, r *http.Request) { + if p := r.URL.Query().Get("page"); p != "" && p != "1" { + w.Write([]byte("[]")) + return + } + json.NewEncoder(w).Encode([]map[string]any{{"tag_name": "v1.2-3", "assets": []map[string]any{{ + "name": "demo-1.2-3.x86_64.rpm", + "size": len(rpm), + "browser_download_url": gh.URL + "/acme/tools/releases/download/v1.2-3/demo-1.2-3.x86_64.rpm", + }}}}) + }) + mux.HandleFunc("/acme/tools/releases/download/", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Length", strconv.Itoa(len(rpm))) + w.Write(rpm) + }) + gh = httptest.NewServer(mux) + defer gh.Close() + + createRemote(t, mustFormat(`{"name": "head-ghrpm", "package_type": "github_rpm", "base_url": %q, "releases_remote": "github"}`, gh.URL+"/repos/acme/tools")) + defer deleteRemote(t, "head-ghrpm") + + url := apiURL("/api/v1/remote/head-ghrpm/repodata/repomd.xml") + deadline := time.Now().Add(30 * time.Second) + for { + if _, status := getBody(t, url); status == http.StatusOK || time.Now().After(deadline) { + break + } + time.Sleep(500 * time.Millisecond) + } + assertHeadMatchesGet(t, url) +} diff --git a/internal/api/v1/proxy.go b/internal/api/v1/proxy.go index 7429007..43ba060 100644 --- a/internal/api/v1/proxy.go +++ b/internal/api/v1/proxy.go @@ -16,6 +16,7 @@ import ( "git.unkin.net/unkin/artifactapi/internal/proxy" "git.unkin.net/unkin/artifactapi/internal/storage" "git.unkin.net/unkin/artifactapi/internal/virtual" + "git.unkin.net/unkin/artifactapi/pkg/models" ) type ProxyHandler struct { @@ -41,6 +42,7 @@ func NewProxyHandler(engine *proxy.Engine, virtualEngine *virtual.Engine, db *da func (h *ProxyHandler) Routes() chi.Router { r := chi.NewRouter() r.Get("/remote/{remoteName}/*", h.handleProxy) + r.Head("/remote/{remoteName}/*", h.handleProxyHead) r.Get("/local/{localName}/*", h.handleLocal) r.Get("/virtual/{virtualName}/*", h.handleVirtual) return r @@ -68,31 +70,43 @@ func (h *ProxyHandler) handleDockerPing(w http.ResponseWriter, r *http.Request) w.WriteHeader(http.StatusOK) } -func (h *ProxyHandler) handleProxy(w http.ResponseWriter, r *http.Request) { +// remoteFor resolves the remote and provider named in the request, writing the +// error response itself when either is missing. +func (h *ProxyHandler) remoteFor(w http.ResponseWriter, r *http.Request) (*models.Remote, provider.Provider, bool) { remoteName := chi.URLParam(r, "remoteName") - path := chi.URLParam(r, "*") - remote, err := h.db.GetRemote(r.Context(), remoteName) if err != nil { http.Error(w, fmt.Sprintf("remote %q not found", remoteName), http.StatusNotFound) - return + return nil, nil, false } - prov, err := provider.Get(remote.PackageType) if err != nil { http.Error(w, fmt.Sprintf("no provider for %q", remote.PackageType), http.StatusInternalServerError) + return nil, nil, false + } + return remote, prov, true +} + +// serveRemote lets metadata-only remotes (e.g. github_rpm) synthesize their own +// responses and redirect package downloads instead of proxying bytes. +func (h *ProxyHandler) serveRemote(w http.ResponseWriter, r *http.Request, remote *models.Remote, prov provider.Provider, path string) bool { + rs, ok := prov.(provider.RemoteServer) + return ok && rs.ServeRemote(w, r, *remote, path, fmt.Sprintf("%s://%s", scheme(r), r.Host), h.db) +} + +func (h *ProxyHandler) handleProxy(w http.ResponseWriter, r *http.Request) { + remote, prov, ok := h.remoteFor(w, r) + if !ok { return } - - // Metadata-only remotes (e.g. github_rpm) synthesize their own responses and - // redirect package downloads to a backend remote instead of proxying bytes. - if rs, ok := prov.(provider.RemoteServer); ok { - proxyBaseURL := fmt.Sprintf("%s://%s", scheme(r), r.Host) - if rs.ServeRemote(w, r, *remote, path, proxyBaseURL, h.db) { - return - } + path := chi.URLParam(r, "*") + if h.serveRemote(w, r, remote, prov, path) { + return } + h.serveFetch(w, r, remote, prov, path) +} +func (h *ProxyHandler) serveFetch(w http.ResponseWriter, r *http.Request, remote *models.Remote, prov provider.Provider, path string) { result, err := h.engine.Fetch(r.Context(), *remote, path, prov, r.Header) if err != nil { var proxyErr *proxy.ProxyError @@ -100,7 +114,7 @@ func (h *ProxyHandler) handleProxy(w http.ResponseWriter, r *http.Request) { http.Error(w, proxyErr.Message, proxyErr.Status) return } - slog.Error("proxy fetch failed", "remote", remoteName, "path", path, "error", err) + slog.Error("proxy fetch failed", "remote", remote.Name, "path", path, "error", err) http.Error(w, "bad gateway", http.StatusBadGateway) return } @@ -120,25 +134,27 @@ func (h *ProxyHandler) handleProxy(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", result.ContentType) w.Header().Set("X-Artifact-Source", result.Source) if result.Size > 0 { + w.Header().Set("Content-Length", fmt.Sprintf("%d", result.Size)) w.Header().Set("X-Artifact-Size", fmt.Sprintf("%d", result.Size)) } w.WriteHeader(http.StatusOK) io.Copy(w, body) } +// handleProxyHead answers HEAD like GET without a body. Responses a provider +// synthesizes or rewrites per request go through the GET path so their headers +// match; everything else is answered from store metadata or an upstream HEAD. func (h *ProxyHandler) handleProxyHead(w http.ResponseWriter, r *http.Request) { - remoteName := chi.URLParam(r, "remoteName") - path := chi.URLParam(r, "*") - - remote, err := h.db.GetRemote(r.Context(), remoteName) - if err != nil { - http.Error(w, fmt.Sprintf("remote %q not found", remoteName), http.StatusNotFound) + remote, prov, ok := h.remoteFor(w, r) + if !ok { return } - - prov, err := provider.Get(remote.PackageType) - if err != nil { - http.Error(w, fmt.Sprintf("no provider for %q", remote.PackageType), http.StatusInternalServerError) + path := chi.URLParam(r, "*") + if h.serveRemote(w, r, remote, prov, path) { + return + } + if _, ok := prov.(provider.ServeRewriter); ok && prov.Classify(path) == provider.Mutable { + h.serveFetch(w, r, remote, prov, path) return } @@ -149,7 +165,7 @@ func (h *ProxyHandler) handleProxyHead(w http.ResponseWriter, r *http.Request) { http.Error(w, proxyErr.Message, proxyErr.Status) return } - slog.Error("proxy head failed", "remote", remoteName, "path", path, "error", err) + slog.Error("proxy head failed", "remote", remote.Name, "path", path, "error", err) http.Error(w, "bad gateway", http.StatusBadGateway) return } diff --git a/internal/api/v1/routes_test.go b/internal/api/v1/routes_test.go new file mode 100644 index 0000000..4493a5c --- /dev/null +++ b/internal/api/v1/routes_test.go @@ -0,0 +1,17 @@ +package v1 + +import ( + "net/http" + "testing" + + "github.com/go-chi/chi/v5" +) + +func TestRoutesRemoteHead(t *testing.T) { + r := (&ProxyHandler{}).Routes() + for _, m := range []string{http.MethodGet, http.MethodHead} { + if !r.Match(chi.NewRouteContext(), m, "/remote/myremote/some/path") { + t.Errorf("%s /remote/myremote/some/path not routed", m) + } + } +} diff --git a/internal/proxy/engine.go b/internal/proxy/engine.go index 45a1b87..cc267cc 100644 --- a/internal/proxy/engine.go +++ b/internal/proxy/engine.go @@ -353,7 +353,7 @@ func (e *Engine) headUpstreamOnce(ctx context.Context, remote models.Remote, pat req.Header.Set(k, v) } } - return http.DefaultClient.Do(req) + return clientForRemote(remote).Do(req) } resp, err := doHead(nil) diff --git a/internal/proxy/engine_test.go b/internal/proxy/engine_test.go index 87ce690..2374aec 100644 --- a/internal/proxy/engine_test.go +++ b/internal/proxy/engine_test.go @@ -216,6 +216,20 @@ func TestHead(t *testing.T) { } } +func TestHeadUpstreamUsesRemoteTimeout(t *testing.T) { + slow := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + time.Sleep(3 * time.Second) + })) + defer slow.Close() + r := models.Remote{Name: "slow-head", PackageType: models.PackageGeneric, BaseURL: slow.URL, UpstreamResponseHeaderTimeout: 1} + + start := time.Now() + _, err := (&Engine{}).headUpstreamOnce(context.Background(), r, "blob.bin", prov(t, models.PackageGeneric)) + if err == nil || time.Since(start) > 2*time.Second { + t.Fatalf("HEAD ignored the remote's response header timeout: err=%v after %s", err, time.Since(start)) + } +} + func TestStaleOnError(t *testing.T) { requireStack(t) ctx := context.Background()