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
@@ -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)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user