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:
7 files changed
+62
-4
No files matched your search
@@ -29,6 +29,7 @@
|
||||
<data name="Server.Error.ArchiveBusy" xml:space="preserve"><value>Another program is changing the archive's settings; try again shortly.</value><comment>long</comment></data>
|
||||
<data name="Server.Error.ArchiveManifest" xml:space="preserve"><value>The archive's archive.json is damaged, from a newer Titano or of another archive; check the archive folder.</value><comment>long</comment></data>
|
||||
<data name="Server.Error.ArchiveOffline" xml:space="preserve"><value>The archive is offline or read-only; try again when it is back.</value><comment>long</comment></data>
|
||||
<data name="Server.Error.ArchivePolicy" xml:space="preserve"><value>The archive's archive.json has a thumbnail or proxy setting Titano cannot use; correct it in the archive folder.</value><comment>long</comment></data>
|
||||
<data name="Server.Error.ArchiveRoot" xml:space="preserve"><value>Its photos are the archive's root folder, which never moves to the trash.</value><comment>long</comment></data>
|
||||
<data name="Server.Error.BadCredentials" xml:space="preserve"><value>Wrong user name or password.</value><comment>long</comment></data>
|
||||
<data name="Server.Error.BadRequest" xml:space="preserve"><value>The request is not valid.</value><comment>long</comment></data>
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
<data name="Server.Error.ArchiveBusy" xml:space="preserve"><value>Un altro programma sta modificando le impostazioni dell'archivio; riprova tra poco.</value></data>
|
||||
<data name="Server.Error.ArchiveManifest" xml:space="preserve"><value>Il file archive.json dell'archivio è danneggiato, di un Titano più recente o di un altro archivio; controlla la cartella dell'archivio.</value></data>
|
||||
<data name="Server.Error.ArchiveOffline" xml:space="preserve"><value>L'archivio non è raggiungibile o è di sola lettura; riprova quando torna disponibile.</value></data>
|
||||
<data name="Server.Error.ArchivePolicy" xml:space="preserve"><value>Il file archive.json dell'archivio ha un'impostazione delle miniature o dei proxy che Titano non può usare; correggila nella cartella dell'archivio.</value></data>
|
||||
<data name="Server.Error.ArchiveRoot" xml:space="preserve"><value>Le sue foto sono la cartella principale dell'archivio, che non va mai nel cestino.</value></data>
|
||||
<data name="Server.Error.BadCredentials" xml:space="preserve"><value>Nome utente o password errati.</value></data>
|
||||
<data name="Server.Error.BadRequest" xml:space="preserve"><value>La richiesta non è valida.</value></data>
|
||||
|
||||
@@ -67,7 +67,9 @@ public static class ArchiveEndpoints
|
||||
/// Writes the name, templates and proxy policy into the archive's <c>archive.json</c> through the archive engine (so a
|
||||
/// restart and the proxy jobs see them), then into the index. 503 <c>archive_offline</c> while the archive cannot be
|
||||
/// written; 409 <c>archive_busy</c> while another process holds its settings; 409 <c>archive_manifest</c> when its
|
||||
/// archive.json is damaged, written by a newer Titano or of another archive (nothing is written).
|
||||
/// archive.json is damaged, written by a newer Titano or of another archive; 409 <c>archive_policy</c> when it holds a
|
||||
/// thumbnail or proxy setting this API cannot set and Titano cannot use (details: <c>thumbnailPolicy</c> or
|
||||
/// <c>proxyPolicy</c>, the property to correct in the file). Nothing is written.
|
||||
/// </summary>
|
||||
private static async Task<IResult> UpdateAsync(HttpContext context, Caller caller, ArchiveSettingsDto request, TitanoDatabase db, ServerArchive archive,
|
||||
[FromServices] ArchiveService? archives, AuditLog audit)
|
||||
@@ -99,6 +101,17 @@ public static class ArchiveEndpoints
|
||||
{
|
||||
return ApiErrors.Conflict(context, "archive_manifest");
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
{
|
||||
// The request's fields were checked above: what fails is a value read from archive.json (a thumbnail size, or a
|
||||
// proxy quality, written by hand or by another tool), which the admin corrects in the file, not in this form.
|
||||
return ApiErrors.Conflict(context, "archive_policy", ex.ParamName switch
|
||||
{
|
||||
"thumbnailLongEdge" => "thumbnailPolicy",
|
||||
"proxyLongEdge" => "proxyPolicy",
|
||||
var other => other,
|
||||
});
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
// ArchiveOfflineException included: nothing was written.
|
||||
|
||||
@@ -54,7 +54,8 @@ public static class UploadEndpoints
|
||||
|
||||
/// <summary>
|
||||
/// Chunks are the only large request bodies; every other endpoint keeps Kestrel's small default limit. The service
|
||||
/// raises it to the chunk size of this upload (fixed when it started) before it reads the body.
|
||||
/// raises it before any answer to at least the chunk size of this upload (fixed when it started) and of this server,
|
||||
/// so a body it leaves unread can still be drained.
|
||||
/// </summary>
|
||||
private static async Task<IResult> ChunkAsync(HttpContext context, Caller caller, Guid id, int index, UploadService service)
|
||||
{
|
||||
|
||||
@@ -101,14 +101,17 @@ public sealed class UploadService(
|
||||
public async Task<Outcome<ChunkReceived>> ReceiveChunkAsync(Caller caller, Guid id, int index, HttpRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var upload = await db.Uploads.GetAsync(id, cancellationToken);
|
||||
// A body left unread by an answer below is drained after it: one of a chunk's size must not count as too large, or
|
||||
// Kestrel would close the connection while the client still sends it and the client would never see the answer. The
|
||||
// upload keeps the chunk size it started with: a smaller setting since a restart must not refuse its chunks (413).
|
||||
if (request.HttpContext.Features.Get<IHttpMaxRequestBodySizeFeature>() is { IsReadOnly: false, MaxRequestBodySize: { } current } limit)
|
||||
limit.MaxRequestBodySize = Math.Max(current, Math.Max(upload?.ChunkSize ?? 0, options.ChunkSizeBytes));
|
||||
if (upload is null || upload.UserId != caller.Id) return Fail<ChunkReceived>(404, "not_found", "upload");
|
||||
if (upload.State != UploadState.Receiving) return Fail<ChunkReceived>(409, "upload_state");
|
||||
if (index < 0 || index >= upload.ChunkCount) return Fail<ChunkReceived>(400, "chunk_out_of_range");
|
||||
var expected = Math.Min(upload.ChunkSize, upload.Size - (long)index * upload.ChunkSize);
|
||||
if (request.ContentLength is { } announced && announced != expected) return Fail<ChunkReceived>(400, "chunk_size", expected.ToString(CultureInfo.InvariantCulture));
|
||||
if (upload.HasChunk(index)) return new ChunkReceived(index, UploadRules.ReceivedChunks(upload));
|
||||
// The upload keeps the chunk size it started with: a smaller setting since a restart must not refuse its chunks (413).
|
||||
if (request.HttpContext.Features.Get<IHttpMaxRequestBodySizeFeature>() is { IsReadOnly: false } limit) limit.MaxRequestBodySize = upload.ChunkSize;
|
||||
|
||||
if (_chunksInFlight.AddOrUpdate(caller.Id, 1, (_, n) => n + 1) > options.ConcurrentChunksPerUser)
|
||||
{
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user