fix(protocol): report Chronos's own info error as that error, not a disposed stream
The identity check reads Chronos's error body to tell it from another service; GetInfoAsync then read it again and threw ObjectDisposedException. The identity now carries the parsed error and GetInfoAsync throws it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0aa2fb8135
commit
2585b5d45f
2 files changed
+33
-4
No files matched your search
@@ -97,7 +97,8 @@ public sealed class ChronosClient : IDisposable
|
|||||||
var answer = await IdentifyAsync(response, ct).ConfigureAwait(false);
|
var answer = await IdentifyAsync(response, ct).ConfigureAwait(false);
|
||||||
if (answer.Info is { } info) return info;
|
if (answer.Info is { } info) return info;
|
||||||
if (answer.Chronos == false) throw new NotChronosServerException(BaseAddress);
|
if (answer.Chronos == false) throw new NotChronosServerException(BaseAddress);
|
||||||
// Chronos's own error, or busy: as any other request reports it.
|
// Chronos's own error, already read (the body cannot be read twice), or busy: as any other request reports it.
|
||||||
|
if (answer.Error is { } error) throw new ChronosApiException(response.StatusCode, error);
|
||||||
await EnsureSuccessAsync(response, ct).ConfigureAwait(false);
|
await EnsureSuccessAsync(response, ct).ConfigureAwait(false);
|
||||||
throw new NotChronosServerException(BaseAddress);
|
throw new NotChronosServerException(BaseAddress);
|
||||||
}
|
}
|
||||||
@@ -112,8 +113,11 @@ public sealed class ChronosClient : IDisposable
|
|||||||
private static bool IsChronos(ServerInfo? info) =>
|
private static bool IsChronos(ServerInfo? info) =>
|
||||||
info is { Name.Length: > 0, Version.Length: > 0, ApiVersion: > 0, Capabilities: not null, Formats: not null, Limits: not null };
|
info is { Name.Length: > 0, Version.Length: > 0, ApiVersion: > 0, Capabilities: not null, Formats: not null, Limits: not null };
|
||||||
|
|
||||||
/// <summary>What a <c>server/info</c> answer is: Chronos's (with its info when it succeeded), something else, or unknown (busy).</summary>
|
/// <summary>
|
||||||
private readonly record struct Identity(bool? Chronos, ServerInfo? Info = null, bool Answered = true);
|
/// What a <c>server/info</c> answer is: Chronos's (with its info when it succeeded, or its error when it failed),
|
||||||
|
/// something else, or unknown (busy).
|
||||||
|
/// </summary>
|
||||||
|
private readonly record struct Identity(bool? Chronos, ServerInfo? Info = null, bool Answered = true, ApiError? Error = null);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Tells a <c>server/info</c> answer of Chronos from any other. Chronos answers it anonymously with its JSON and never
|
/// Tells a <c>server/info</c> answer of Chronos from any other. Chronos answers it anonymously with its JSON and never
|
||||||
@@ -138,7 +142,7 @@ public sealed class ChronosClient : IDisposable
|
|||||||
}
|
}
|
||||||
if (!response.Headers.Contains(ApiVersion.Header)) return new(false);
|
if (!response.Headers.Contains(ApiVersion.Header)) return new(false);
|
||||||
var error = await response.Content.ReadFromJsonAsync(ProtocolJson.Default.ApiError, ct).ConfigureAwait(false);
|
var error = await response.Content.ReadFromJsonAsync(ProtocolJson.Default.ApiError, ct).ConfigureAwait(false);
|
||||||
return new(error is { Code.Length: > 0 } && !error.Code.StartsWith("http_", StringComparison.Ordinal));
|
return error is { Code.Length: > 0 } && !error.Code.StartsWith("http_", StringComparison.Ordinal) ? new(true, Error: error) : new(false);
|
||||||
}
|
}
|
||||||
catch (Exception ex) when (ex is JsonException or NotSupportedException or HttpRequestException)
|
catch (Exception ex) when (ex is JsonException or NotSupportedException or HttpRequestException)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -77,6 +77,24 @@ public sealed class ServerIdentityTests(TestContext context)
|
|||||||
Assert.Equal(0, other.Authorized, "no request carried credentials");
|
Assert.Equal(0, other.Authorized, "no request carried credentials");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task AChronosErrorFromTheInfoIsReportedAsThatError()
|
||||||
|
{
|
||||||
|
// Chronos's own error (its header and code) is read once to tell it from other services: it must still come back
|
||||||
|
// as the server's error, and an unknown certificate still asks for trust.
|
||||||
|
await using var other = await OtherService.StartAsync(context);
|
||||||
|
foreach (var (path, status, code) in new[] { ("chronos500/", HttpStatusCode.InternalServerError, "internal"), ("chronos400/", HttpStatusCode.BadRequest, "bad_request") })
|
||||||
|
{
|
||||||
|
using var plain = new ChronosClient(new Uri(other.Http, path), new CertificatePinning([]), "tests");
|
||||||
|
var error = await Assert.ThrowsAsync<ChronosApiException>(() => plain.GetInfoAsync(context.Cancellation), path);
|
||||||
|
Assert.Equal(status, error.Status, path);
|
||||||
|
Assert.Equal(code, error.Error.Code, path);
|
||||||
|
|
||||||
|
using var untrusted = new ChronosClient(new Uri(other.Https, path), new CertificatePinning([]), "tests");
|
||||||
|
await Assert.ThrowsAsync<UntrustedCertificateException>(() => untrusted.GetInfoAsync(context.Cancellation), path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public async Task AChronosServerBehindAnUnknownCertificateStillAsksForTrust()
|
public async Task AChronosServerBehindAnUnknownCertificateStillAsksForTrust()
|
||||||
{
|
{
|
||||||
@@ -149,6 +167,13 @@ public sealed class ServerIdentityTests(TestContext context)
|
|||||||
// Errors in JSON with a code of their own, as many web services write them.
|
// Errors in JSON with a code of their own, as many web services write them.
|
||||||
foreach (var (path, status) in new[] { ("json401", 401), ("json403", 403), ("json404", 404) })
|
foreach (var (path, status) in new[] { ("json401", 401), ("json403", 403), ("json404", 404) })
|
||||||
app.MapGet($"/{path}/api/v1/server/info", () => Results.Content("""{"code":"denied","message":"Sign in"}""", "application/json", statusCode: status));
|
app.MapGet($"/{path}/api/v1/server/info", () => Results.Content("""{"code":"denied","message":"Sign in"}""", "application/json", statusCode: status));
|
||||||
|
// Errors as Chronos writes them: its header and a code of its own.
|
||||||
|
foreach (var (path, status, code) in new[] { ("chronos500", 500, "internal"), ("chronos400", 400, "bad_request") })
|
||||||
|
app.MapGet($"/{path}/api/v1/server/info", (HttpContext http) =>
|
||||||
|
{
|
||||||
|
http.Response.Headers[ApiVersion.Header] = "1";
|
||||||
|
return Results.Content($$"""{"code":"{{code}}","message":"Failed"}""", "application/json", statusCode: status);
|
||||||
|
});
|
||||||
// More than a server info ever holds.
|
// More than a server info ever holds.
|
||||||
app.MapGet("/huge/api/v1/server/info", () => Results.Content("<!doctype html>" + new string('x', 2 << 20), "text/html"));
|
app.MapGet("/huge/api/v1/server/info", () => Results.Content("<!doctype html>" + new string('x', 2 << 20), "text/html"));
|
||||||
app.MapGet("/hugejson/api/v1/server/info", () => Results.Content("{\"name\":\"" + new string('x', 2 << 20) + "\"}", "application/json"));
|
app.MapGet("/hugejson/api/v1/server/info", () => Results.Content("{\"name\":\"" + new string('x', 2 << 20) + "\"}", "application/json"));
|
||||||
|
|||||||
Reference in new issue
Block a user