edge: a failed precondition on a gzip answer no longer keeps the gzip body headers
ServeContent writes 412 as a bare status without touching headers, so the compressed Content-Length and Content-Encoding framed a body that never came.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2 files changed,
+83
−0
internal/edge/assetserve.go+21 −0internal/edge/assetserve_test.go+62 −0
CI failed — run details
diff --git a/internal/edge/assetserve.go b/internal/edge/assetserve.go| index 554a35b41..28d0972d7 100644 |
| --- a/internal/edge/assetserve.go |
| +++ b/internal/edge/assetserve.go |
| @@ -76,10 +76,31 @@ func serveAssetEntry(w http.ResponseWriter, r *http.Request, e *assetcache.Entry |
| req = r.Clone(r.Context()) |
| req.Header.Del("Range") |
| } |
| + if useGzip { |
| + w = gzipPreconditionGuard{w} |
| + } |
| http.ServeContent(w, req, "", modtime, body) |
| return nil |
| } |
| +// gzipPreconditionGuard clears the gzip body headers when ServeContent answers |
| +// 412. ServeContent writes a failed If-Match or If-Unmodified-Since as a bare |
| +// status and returns without touching headers, so the compressed length and |
| +// Content-Encoding set for the body it was about to send would frame a |
| +// response that has none. 304 needs no help: ServeContent already deletes |
| +// them. Only a 200 carries the gzip representation (a Range never selects it). |
| +type gzipPreconditionGuard struct{ http.ResponseWriter } |
| + |
| +func (g gzipPreconditionGuard) WriteHeader(code int) { |
| + if code == http.StatusPreconditionFailed { |
| + h := g.Header() |
| + h.Del("Content-Length") |
| + h.Del("Content-Encoding") |
| + h.Del("Content-Type") |
| + } |
| + g.ResponseWriter.WriteHeader(code) |
| +} |
| + |
| // varyNamesAcceptEncoding reports whether h's Vary already covers |
| // Accept-Encoding, by name or by "*". |
| func varyNamesAcceptEncoding(h http.Header) bool { |
diff --git a/internal/edge/assetserve_test.go b/internal/edge/assetserve_test.go| index d2ecc5856..b33fd9a5e 100644 |
| --- a/internal/edge/assetserve_test.go |
| +++ b/internal/edge/assetserve_test.go |
| @@ -442,3 +442,65 @@ func TestServeAssetEntryReleasesItsLease(t *testing.T) { |
| t.Errorf("Stats.Retired = %d after replacing an entry that was served from: a reader leaked its lease", st.Retired) |
| } |
| } |
| + |
| +// TestServeAssetEntryPreconditionFailed: a 412 has no body, so it must not |
| +// keep the gzip copy's Content-Length or Content-Encoding. The server leg |
| +// reads the body to its end, as a client does, to prove the framing. |
| +func TestServeAssetEntryPreconditionFailed(t *testing.T) { |
| + h := serveHeader() |
| + h.Set("Last-Modified", "Fri, 23 Oct 2026 08:00:00 GMT") |
| + e := serveEntry(t, serveStore(t), serveKey, serveBody, h, true) |
| + cases := map[string][]string{ |
| + "if-match": {"If-Match", `"other"`}, |
| + "if-unmodified-since": {"If-Unmodified-Since", "Thu, 22 Oct 2026 08:00:00 GMT"}, |
| + "if-none-match (304)": {"If-None-Match", `W/"v1"`}, |
| + "if-modified-since304": {"If-Modified-Since", "Fri, 23 Oct 2026 08:00:00 GMT"}, |
| + } |
| + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + if err := serveAssetEntry(w, r, e, serveAt(), "hit"); err != nil { |
| + t.Errorf("serveAssetEntry: %v", err) |
| + } |
| + })) |
| + defer srv.Close() |
| + for name, kv := range cases { |
| + want := http.StatusPreconditionFailed |
| + if strings.Contains(name, "304") { |
| + want = http.StatusNotModified |
| + } |
| + for _, ae := range []string{"gzip", "identity"} { |
| + rec := doServe(t, e, serveReq("GET", "Accept-Encoding", ae, kv[0], kv[1]), "hit") |
| + if rec.Code != want { |
| + t.Fatalf("%s ae=%q: status %d, want %d", name, ae, rec.Code, want) |
| + } |
| + if rec.Body.Len() != 0 { |
| + t.Errorf("%s ae=%q: %d body bytes", name, ae, rec.Body.Len()) |
| + } |
| + for _, k := range []string{"Content-Encoding", "Content-Length"} { |
| + if v := rec.Header().Get(k); v != "" { |
| + t.Errorf("%s ae=%q: %s = %q on a bodyless answer", name, ae, k, v) |
| + } |
| + } |
| + if rec.Header().Get("Carlos-Cache") != "hit" || rec.Header().Get("Vary") != "Accept-Encoding" { |
| + t.Errorf("%s ae=%q: Carlos-Cache %q, Vary %q", name, ae, rec.Header().Get("Carlos-Cache"), rec.Header().Get("Vary")) |
| + } |
| + |
| + req, _ := http.NewRequest("GET", srv.URL, nil) |
| + req.Header.Set(kv[0], kv[1]) |
| + if ae != "" { |
| + req.Header.Set("Accept-Encoding", ae) // set by hand: the transport then neither adds gzip nor decodes |
| + } |
| + resp, err := srv.Client().Do(req) |
| + if err != nil { |
| + t.Fatalf("%s ae=%q: %v", name, ae, err) |
| + } |
| + b, err := io.ReadAll(resp.Body) |
| + resp.Body.Close() |
| + if err != nil { |
| + t.Errorf("%s ae=%q: reading the body: %v", name, ae, err) |
| + } |
| + if resp.StatusCode != want || len(b) != 0 { |
| + t.Errorf("%s ae=%q: over the wire status %d with %d body bytes, want %d with none", name, ae, resp.StatusCode, len(b), want) |
| + } |
| + } |
| + } |
| +} |