fix(server): answer every chunk request and out-of-range archive.json policies

The chunk endpoint raised the request body limit only before reading a chunk. A
chunk sent again after a lost answer, or to an unknown or finished upload, was left
unread above Kestrel's 1 MiB default, so Kestrel closed the connection instead of
delivering the answer. The limit is now raised before any answer, to the larger of
the upload's and the server's chunk size, and never lowered.

PUT /api/v1/archive/settings answered 500 when archive.json held a thumbnail size or
proxy quality out of range, which the admin API cannot set. It now answers 409
archive_policy with the policy to correct in details (Server.Error.ArchivePolicy).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 15:58:48 +00:00
1 parent e01887ef51
commit 98f301f5fd
7 files changed
+62 -4

No files matched your search

@@ -177,5 +177,21 @@ public sealed class AdminTests(TestContext context)
{
await File.WriteAllBytesAsync(path, saved, context.Cancellation);
}
// A policy this form cannot set, written out of range by another tool, is named so the admin can correct the file.
var current = Titano.Core.Archiving.ManifestStore.ReadArchive(path);
foreach (var (odd, property) in new[]
{
(current with { ThumbnailPolicy = new Titano.Core.Archiving.ThumbnailPolicy { LongEdge = 2048 } }, "thumbnailPolicy"),
(current with { ProxyPolicy = current.ProxyPolicy with { Quality = 0 } }, "proxyPolicy"),
})
{
Titano.Core.Archiving.ManifestStore.WriteArchive(path, odd);
var before = await File.ReadAllBytesAsync(path, context.Cancellation);
using var refused = await admin.PutAsJsonAsync("/api/v1/archive/settings", changed with { ProxyLongEdge = 1280 }, ProtocolJson.Default.ArchiveSettingsDto);
Assert.Equal(property, (await refused.ExpectErrorAsync(HttpStatusCode.Conflict, "archive_policy")).Details);
Assert.SequenceEqual(before, await File.ReadAllBytesAsync(path, context.Cancellation));
}
await File.WriteAllBytesAsync(path, saved, context.Cancellation);
}
}
@@ -170,6 +170,29 @@ public sealed class UploadTests(TestContext context)
await oversized.ExpectErrorAsync(HttpStatusCode.BadRequest, "chunk_size");
}
[Test]
public async Task AnswersThatLeaveALargeChunkUnreadReachTheClient()
{
// Chunks far above the 1 MiB other requests may send: more than the connection buffers while the server reads nothing.
const int large = 32 * 1024 * 1024;
await using var server = await TestServer.StartAsync(context, o => o.ChunkSizeBytes = large);
var (uploader, _, _) = await server.UserAsync("roof-cam", "uploader");
var data = Uploads.Photo(24 * 1024 * 1024, 16);
var id = (await Uploads.StartAsync(uploader, data)).UploadId!.Value;
using (var first = await Uploads.PutChunkAsync(uploader, id, 0, data)) await first.ExpectAsync(HttpStatusCode.OK);
// The client lost the answer and sends the chunk again: the same answer, not a closed connection.
using (var again = await Uploads.PutChunkAsync(uploader, id, 0, data))
{
await again.ExpectAsync(HttpStatusCode.OK);
Assert.SequenceEqual([0], (await again.ReadAsync(ProtocolJson.Default.ChunkReceived)).Received);
}
using (var unknown = await Uploads.PutChunkAsync(uploader, Guid.CreateVersion7(), 0, data)) await unknown.ExpectErrorAsync(HttpStatusCode.NotFound, "not_found");
using (var complete = await uploader.PostAsync($"/api/v1/uploads/{id}/complete", null)) await complete.ExpectAsync(HttpStatusCode.OK);
using var late = await Uploads.PutChunkAsync(uploader, id, 0, data);
await late.ExpectErrorAsync(HttpStatusCode.Conflict, "upload_state");
}
[Test]
public async Task RenderUploadsNeedTheEditPermission()
{