edge: a cold asset request with If-Match or If-Unmodified-Since is answered from the entry (asset cache plan 3 final F5)
assetBuffered now buffers on every precondition http.ServeContent evaluates, so a cold local or far GET carrying a failing If-Match or If-Unmodified-Since gets the 412 its warm repeat gets, not the body streamed as 200.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+70
−3
internal/edge/asset_far_test.go+26 −0internal/edge/asset_local_test.go+39 −0internal/edge/assetcatch.go+5 −3
diff --git a/internal/edge/asset_far_test.go b/internal/edge/asset_far_test.go| index 238b68210..0278766bf 100644 |
| --- a/internal/edge/asset_far_test.go |
| +++ b/internal/edge/asset_far_test.go |
| @@ -1210,3 +1210,29 @@ func TestFarHitsMeetTheGuard(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +// ---- preconditions on a cold far request (final review F5) --------------- |
| + |
| +// A cold far request carrying a failing If-Match or If-Unmodified-Since is |
| +// answered as its warm repeat is: 412 from the entry the fill stored. The |
| +// fill hop carries neither header. |
| +func TestFarColdMissAnswersFailingPreconditionsFromTheEntry(t *testing.T) { |
| + for name, h := range assetPreconditions { |
| + t.Run(name, func(t *testing.T) { |
| + s := newFarScene(t, assetValidated()) |
| + cold := s.get(t, "/assets/app.js", h[0], h[1]) |
| + wantCache(t, cold, "miss") |
| + if got := s.rec.get().header.Get(h[0]); got != "" { |
| + t.Fatalf("the fill hop carried %s %q", h[0], got) |
| + } |
| + warm := s.get(t, "/assets/app.js", h[0], h[1]) |
| + wantCache(t, warm, "hit") |
| + if cold.Code != http.StatusPreconditionFailed || warm.Code != cold.Code || cold.Body.Len() != 0 { |
| + t.Fatalf("cold %d (%d body bytes), warm %d; want 412 for both", cold.Code, cold.Body.Len(), warm.Code) |
| + } |
| + if n := s.rec.get().n; n != 1 { |
| + t.Fatalf("the home saw %d requests, want 1", n) |
| + } |
| + }) |
| + } |
| +} |
diff --git a/internal/edge/asset_local_test.go b/internal/edge/asset_local_test.go| index e55840047..7b82aef7a 100644 |
| --- a/internal/edge/asset_local_test.go |
| +++ b/internal/edge/asset_local_test.go |
| @@ -1286,3 +1286,42 @@ func TestHomeFillMeetsTheLapse(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +// ---- preconditions on a cold request (final review F5) ------------------- |
| + |
| +// assetPreconditions are a failing If-Match and a failing |
| +// If-Unmodified-Since against an entry stored from assetValidated. |
| +var assetPreconditions = map[string][2]string{ |
| + "if-match": {"If-Match", `"other"`}, |
| + "if-unmodified-since": {"If-Unmodified-Since", "Thu, 22 Oct 2026 08:00:00 GMT"}, |
| +} |
| + |
| +// assetValidated answers laBody with a strong ETag and a Last-Modified after |
| +// the If-Unmodified-Since date above. |
| +func assetValidated() http.HandlerFunc { |
| + return assetOK(laBody, "ETag", `"e1"`, "Last-Modified", "Fri, 23 Oct 2026 08:00:00 GMT") |
| +} |
| + |
| +// A cold local request carrying a failing If-Match or If-Unmodified-Since is |
| +// answered as its warm repeat is: 412 from the entry, not the body streamed |
| +// as 200. The fill itself sends neither header. |
| +func TestColdMissAnswersFailingPreconditionsFromTheEntry(t *testing.T) { |
| + for name, h := range assetPreconditions { |
| + t.Run(name, func(t *testing.T) { |
| + s := newLAScene(t, assetValidated()) |
| + cold := s.get(t, "/assets/app.js", h[0], h[1]) |
| + wantCache(t, cold, "miss") |
| + if got := s.b.lastReq().Header.Get(h[0]); got != "" { |
| + t.Fatalf("the fill forwarded %s %q", h[0], got) |
| + } |
| + warm := s.get(t, "/assets/app.js", h[0], h[1]) |
| + wantCache(t, warm, "hit") |
| + if cold.Code != http.StatusPreconditionFailed || warm.Code != cold.Code || cold.Body.Len() != 0 { |
| + t.Fatalf("cold %d (%d body bytes), warm %d; want 412 for both", cold.Code, cold.Body.Len(), warm.Code) |
| + } |
| + if n := s.b.n.Load(); n != 1 { |
| + t.Fatalf("the app saw %d requests, want 1", n) |
| + } |
| + }) |
| + } |
| +} |
diff --git a/internal/edge/assetcatch.go b/internal/edge/assetcatch.go| index bed6f12f6..7074e04f4 100644 |
| --- a/internal/edge/assetcatch.go |
| +++ b/internal/edge/assetcatch.go |
| @@ -204,13 +204,15 @@ var ( |
| // assetBuffered reports whether an initiating request waits for the commit |
| // and is answered from the entry (astra P3-5) rather than being teed the |
| -// body as it fills: a HEAD, or a request with a Range or a validator, whose |
| -// answer the entry has to compute. |
| +// body as it fills: a HEAD, or a request with a Range or any precondition |
| +// http.ServeContent evaluates (If-Match, If-None-Match, If-Modified-Since, |
| +// If-Unmodified-Since, If-Range), whose answer the entry has to compute. A |
| +// cold request then gets the answer its warm repeat would, 412 included. |
| func assetBuffered(r *http.Request) bool { |
| if r.Method == http.MethodHead { |
| return true |
| } |
| - for _, h := range []string{"Range", "If-None-Match", "If-Modified-Since", "If-Range"} { |
| + for _, h := range []string{"Range", "If-Match", "If-None-Match", "If-Modified-Since", "If-Unmodified-Since", "If-Range"} { |
| if _, ok := r.Header[h]; ok { |
| return true |
| } |