carlos / platform Public

media: a cleanly ended chunked 200 is a whole download

Task 2 fix round 2 (Astra P2). A chunked 200 with no stated length now
finishes on its last chunk: Go reports that as a clean EOF and a stream
cut before it as an error, which resumes. A resumed 206 still needs a
known total (a "*" with none is refused), and a known total must be met
exactly at EOF.

Tests: the chunked download at the cap succeeds again; a chunked stream
cut before its last chunk resumes and completes with a 206 that gives
the total, and fails with a "*" total.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 71dae49460b2953e3a49bb7999d677382b6f3576 parent 796ff7d
2 files changed, +49 −9
  • internal/mediahandler/download.go +10 −5
  • internal/mediahandler/download_test.go +39 −4

CI failed — run details

diff --git a/internal/mediahandler/download.go b/internal/mediahandler/download.go
index b65aed84a..e99b83d61 100644
--- a/internal/mediahandler/download.go
+++ b/internal/mediahandler/download.go
@@ -116,8 +116,10 @@ func newIdleTimer(d time.Duration, f func()) idleTimer {
// Content-Length must be the range's length; else unreadable;
// - every byte is counted as it streams, and one past the cap is
// source_too_large;
-// - a body ending cleanly is the whole download only when a total is
-// known and every byte of it has been written; else unreadable;
+// - a body ending cleanly is the whole download when every byte of a
+// known total has been written, or when it is a chunked 200 that
+// reached its last chunk; a cut chunked 200 resumes like any broken
+// body, and its resume must give the total; else unreadable;
// - 404 source_not_found, 412 source_changed; 403 at or after the
// expiry errGrantExpired, before it source_unreadable; 500, 502, 503
// and 504 are retried as a broken connection; any other status is
@@ -359,9 +361,12 @@ func (d *fetch) stream(ctx context.Context, body io.Reader, cancel func()) (brok
case rerr == nil:
continue
case rerr == io.EOF:
- // Whole only when the object's size is known and every
- // byte of it is here.
- if d.total < 0 || d.written != d.total {
+ // A known size must be met exactly. The size is unknown
+ // only for a chunked 200 (a 206 always resolves its total,
+ // and a resume never accepts a 200): Go reports its last
+ // chunk as this clean EOF and a stream cut before it as an
+ // error, so that body is whole.
+ if d.total >= 0 && d.written != d.total {
return false, jobError{media.CodeUnreadable}
}
return false, nil
diff --git a/internal/mediahandler/download_test.go b/internal/mediahandler/download_test.go
index 30f03389b..fab8cc0be 100644
--- a/internal/mediahandler/download_test.go
+++ b/internal/mediahandler/download_test.go
@@ -288,9 +288,9 @@ func TestDownloadSourceChanged(t *testing.T) {
// TestDownloadCapWhileStreaming: with no Content-Length, a body one byte
// past the cap is source_too_large and the scratch file is removed. A
-// chunked body at the cap whose size no answer gave is unreadable (a
-// download is whole only at a known size); a resume's Content-Range can
-// give it. With a Content-Length, exactly the cap downloads.
+// chunked body at the cap downloads: its last chunk marks it whole. So do
+// one whose size a resume's Content-Range gave, and one with a
+// Content-Length.
func TestDownloadCapWhileStreaming(t *testing.T) {
for _, c := range []struct {
name string
@@ -300,7 +300,7 @@ func TestDownloadCapWhileStreaming(t *testing.T) {
want string
}{
{"one past the cap", mib + 1, true, nil, media.CodeSourceTooLarge},
- {"at the cap, no size", mib, true, nil, media.CodeUnreadable},
+ {"at the cap, chunked", mib, true, nil, "nil"},
{"at the cap, size from the resume", mib, true, []int64{mib / 2}, "nil"},
{"at the cap, Content-Length", mib, false, nil, "nil"},
} {
@@ -1169,3 +1169,38 @@ func TestDownloadStarTotalAfterAKnownSize(t *testing.T) {
t.Fatalf("requests %+v, want a cut GET then a 206", rs)
}
}
+
+// TestDownloadCutChunkedStreamResumes: a chunked 200 cut before its last
+// chunk is a broken connection, never a whole file. It resumes, and the
+// resume must be a 206 with a known total; with a "*" total it fails.
+func TestDownloadCutChunkedStreamResumes(t *testing.T) {
+ for _, c := range []struct {
+ name string
+ noTotal bool
+ want string
+ }{
+ {"206 with a total", false, "nil"},
+ {"206 with *", true, media.CodeUnreadable},
+ } {
+ t.Run(c.name, func(t *testing.T) {
+ h := newHarness(t)
+ data := randomBytes(t, 2*mib)
+ p := h.s.payload(t, media.PresetVideo720p, data, 8*mib)
+ h.s.Chunked = true
+ h.s.Cuts = []int64{mib}
+ h.s.NoTotal = c.noTotal
+ if err := h.download(t, p); codeOf(err) != c.want {
+ t.Fatalf("download = %s, want %s", codeOf(err), c.want)
+ }
+ rs := h.s.Requests()
+ if len(rs) != 2 || rs[1].Header.Get("Range") != fmt.Sprintf("bytes=%d-", mib) {
+ t.Fatalf("requests %+v, want the cut GET then a resume from %d", rs, mib)
+ }
+ if c.want == "nil" {
+ h.wantSource(t, data)
+ } else {
+ h.wantNoSource(t)
+ }
+ })
+ }
+}